1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
require("utility.misc")
function table.filter(list, predicate)
local filtered_list = {}
for _, item in ipairs(list) do
if predicate(item) then
table.insert(filtered_list, item)
end
end
return filtered_list
end
local function set_terminal_height(height)
if not vim.api.nvim_win_is_valid(vim.g.term_win) then
return
end
vim.api.nvim_win_set_height(vim.g.term_win, height)
end
local function get_nerdtree_win_id()
local nerdtree_win_num = vim.api.nvim_eval("g:NERDTree.GetWinNum()")
return vim.fn.win_getid(nerdtree_win_num)
end
function _G.check_back_space()
local col = vim.api.nvim_win_get_cursor(0)[2]
if col == 0 then
return true
end
return vim.api.nvim_get_current_line():sub(col, col):find("%s") and true
end
function _G.show_documentation()
if vim.fn.index({"vim", "help"}, vim.bo.filetype) >= 0 then
vim.cmd("help " .. vim.fn.expand("<cword>"))
elseif vim.fn["coc#rpc#ready()"] then
vim.fn.CocActionAsync("doHover")
else
vim.cmd(vim.go.keywordprg .. " " .. vim.fn.expand("<cword>"))
end
end
function _G.toggle_terminal()
if vim.fn.win_gotoid(vim.g.term_win) == 1 then
vim.cmd("quit!")
else
vim.cmd("botright new")
vim.api.nvim_win_set_height(vim.fn.win_getid(), vim.g.terminal_height)
vim.fn.termopen(vim.env.SHELL, {detach=0})
vim.g.term_buf = vim.fn.bufnr("")
vim.o.number = false
vim.o.relativenumber = false
vim.o.signcolumn = "no"
vim.cmd("startinsert!")
vim.g.term_win = vim.fn.win_getid()
if vim.fn.exists("g:NERDTree") and vim.api.nvim_eval("g:NERDTree.IsOpen()") then
local nerdtree_win_id = get_nerdtree_win_id()
vim.api.nvim_win_call(
nerdtree_win_id,
function()
vim.cmd("wincmd H")
end
)
vim.api.nvim_win_set_width(nerdtree_win_id, vim.g.NERDTreeWinSize)
end
end
end
function _G.toggle_nerdtree()
vim.cmd("NERDTreeToggle")
vim.api.nvim_win_set_option(
vim.fn.win_getid(),
"winhighlight",
"Normal:NERDTreeWindow"
)
end
function _G.get_current_file()
return vim.fn.expand("%")
end
function _G.remove_hidden_windowless_buffers()
local all_buffers = vim.fn.getbufinfo()
local buffers = table.filter(all_buffers, function (item)
local windows = item.windows
return #windows == 0 and item.hidden
end)
for _, buffer in ipairs(buffers) do
vim.api.nvim_buf_delete(buffer.bufnr, { force = true })
end
end
function _G.close_current_buffer()
local curr_win_id = vim.fn.win_getid()
if curr_win_id == vim.g.term_win or curr_win_id == get_nerdtree_win_id() then
return
end
local current_buf_nr = vim.fn.bufnr()
local all_buffers = vim.fn.getbufinfo()
local file_buffers = table.filter(
all_buffers,
function(buf_info)
return buf_info.loaded == 1
and buf_info.listed == 1
and buf_info.bufnr ~= current_buf_nr
and not string.find(buf_info.name, "NERD_tree_")
and not string.find(buf_info.name, "term://")
end
)
if #file_buffers == 0 then
vim.cmd("new")
else
vim.cmd("buffer " .. file_buffers[1].bufnr)
end
vim.api.nvim_buf_delete(current_buf_nr, { force = true })
set_terminal_height(vim.g.terminal_height)
end
|