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 split(inputstr, sep) if sep == nil then sep = "%s" end local t={} for str in string.gmatch(inputstr, "([^"..sep.."]+)") do table.insert(t, str) end return t end local function file_exists(name) local f=io.open(name,"r") if f~=nil then io.close(f) return true else return false end 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("")) elseif vim.fn["coc#rpc#ready()"] then vim.fn.CocActionAsync("doHover") else vim.cmd(vim.go.keywordprg .. " " .. vim.fn.expand("")) 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 num = vim.fn.system("od -A n -t d -N 3 /dev/random") num = num:gsub("%s+", "") num = num:gsub("%s+", "") function _G.prepare_pr_review_curr_buf() local buf_name = vim.fn.bufname() local buf_name_parts = split(buf_name, "/") local buf_file = buf_name_parts[#buf_name_parts] local side = buf_name_parts[#buf_name_parts - 1] local tmp_file = "/tmp/nvim-pr-review-" .. num .. "-" .. side .. "-" .. buf_file if file_exists(tmp_file) then return end local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true) local file = io.open(tmp_file, "a") if file == nil then error("Temporary file not created") return end for _, line in next, lines, nil do file:write(line) end file:close() vim.api.nvim_buf_set_name(0, tmp_file) end function _G.unprepare_pr_review_curr_buf() local buf_name = vim.fn.bufname() local buf_name_parts = split(buf_name, "/") local buf_file = buf_name_parts[#buf_name_parts] local side = buf_name_parts[#buf_name_parts - 1] local tmp_file = "/tmp/nvim-pr-review-" .. num .. "-" .. side .. "-" .. buf_file if file_exists(tmp_file) == false then return end os.remove(tmp_file) end