diff options
Diffstat (limited to 'lua/functions.lua')
-rw-r--r-- | lua/functions.lua | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/lua/functions.lua b/lua/functions.lua new file mode 100644 index 0000000..12eca3a --- /dev/null +++ b/lua/functions.lua @@ -0,0 +1,124 @@ +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 + +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(height) + if vim.fn.win_gotoid(vim.g.term_win) == 1 then + vim.g.is_term_open = 0 + vim.cmd("quit!") + else + vim.cmd("botright new") + + vim.cmd("resize " .. height) + + if not pcall(function() vim.cmd("buffer " .. vim.g.term_buf) end) then + 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" + end + + vim.cmd("startinsert!") + + vim.g.term_win = vim.fn.win_getid() + vim.g.is_term_open = 1 + + if vim.fn.exists("g:NERDTree") and vim.api.nvim_eval("g:NERDTree.IsOpen()") then + vim.cmd("NERDTreeFocus") + + vim.cmd("wincmd H") + + local width_diff = vim.fn.winwidth(0) - vim.g.NERDTreeWinSize + + vim.cmd(width_diff .. "wincmd <") + + vim.cmd("wincmd p") + + vim.fn.feedkeys("A") + end + end +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 current_buf_nr = vim.fn.bufnr() + + local all_buffers = vim.fn.getbufinfo() + + local 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 #buffers ~= 0 then + vim.cmd("buffer " .. buffers[1].bufnr) + else + vim.cmd("new") + end + + vim.api.nvim_buf_delete(current_buf_nr, { force = true }) + + -- Resize terminal + if vim.g.is_term_open == 1 then + vim.cmd("wincmd j") + vim.cmd("resize " .. vim.g.terminal_height) + vim.cmd("wincmd k") + end +end + |