aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--configs/nvim/lua/crupest/nvim.lua87
-rw-r--r--configs/nvim/lua/crupest/system.lua69
-rw-r--r--configs/nvim/lua/crupest/table.lua30
3 files changed, 186 insertions, 0 deletions
diff --git a/configs/nvim/lua/crupest/nvim.lua b/configs/nvim/lua/crupest/nvim.lua
new file mode 100644
index 0000000..2eea4f1
--- /dev/null
+++ b/configs/nvim/lua/crupest/nvim.lua
@@ -0,0 +1,87 @@
+local function list_listed_bufs()
+ local bufs = vim.api.nvim_list_bufs()
+ local result = {}
+ for _, v in ipairs(bufs) do
+ if vim.fn.buflisted(v) ~= 0 then
+ table.insert(result, v)
+ end
+ end
+ return result
+end
+
+local function get_previous_buffer(buf)
+ local bufs = list_listed_bufs()
+
+ -- no buffers at all
+ if #bufs == 0 then return nil end
+
+ -- find the buf in bufs
+ local index = 0
+ for i, v in ipairs(bufs) do
+ if buf == v then
+ index = i
+ break
+ end
+ end
+
+ -- it's the only one
+ if #bufs == 1 and index == 1 then
+ return nil
+ end
+
+ -- it's the first one
+ if index == 1 then
+ return bufs[2]
+ end
+
+ return bufs[index - 1]
+end
+
+local function list_wins_editing_buf(buf)
+ local wins = vim.api.nvim_list_wins()
+ local result = {}
+ for _, win in ipairs(wins) do
+ if vim.api.nvim_win_get_buf(win) == buf then
+ table.insert(result, win)
+ end
+ end
+ return result
+end
+
+local function win_close_buf()
+ local current_buffer = vim.api.nvim_get_current_buf()
+ local jumps_info = vim.fn.getjumplist()
+
+ local old_jump_list = { unpack(jumps_info[1], 1, jumps_info[2]) }
+ while #old_jump_list ~= 0 do
+ local last_jump = old_jump_list[#old_jump_list]
+ if last_jump.bufnr ~= current_buffer and vim.fn.bufexists(last_jump.bufnr) ~= 0 and vim.fn.buflisted(last_jump.bufnr) ~= 0 then
+ break
+ end
+ table.remove(old_jump_list, #old_jump_list)
+ end
+
+ if #old_jump_list ~= 0 then
+ local last_jump = old_jump_list[#old_jump_list]
+ vim.api.nvim_win_set_buf(0, last_jump.bufnr)
+ vim.api.nvim_win_set_cursor(0, {last_jump.lnum, last_jump.col})
+ else
+ local previous_buf = get_previous_buffer(current_buffer)
+ if previous_buf then
+ vim.api.nvim_win_set_buf(0, previous_buf)
+ else
+ local new_buf = vim.api.nvim_create_buf(true, false)
+ vim.api.nvim_win_set_buf(0, new_buf)
+ end
+ end
+
+ vim.api.nvim_buf_delete(current_buffer, {})
+end
+
+return {
+ list_listed_bufs = list_listed_bufs,
+ get_previous_buffer = get_previous_buffer,
+ list_wins_editing_buf = list_wins_editing_buf,
+ win_close_buf = win_close_buf
+}
+
diff --git a/configs/nvim/lua/crupest/system.lua b/configs/nvim/lua/crupest/system.lua
new file mode 100644
index 0000000..74f8670
--- /dev/null
+++ b/configs/nvim/lua/crupest/system.lua
@@ -0,0 +1,69 @@
+local function clean_path(path)
+ return path and (string.gsub(path, "[/\\]+", "/"))
+end
+
+local function get_exe(path)
+ if vim.fn.has("win32") ~= 0 then
+ local suffixes = { ".exe", ".CMD", ".cmd", ".ps1" }
+ for _, v in ipairs(suffixes) do
+ if string.find(path, v.."$") and vim.uv.fs_stat(path) then
+ return path
+ end
+ end
+ for _, v in ipairs(suffixes) do
+ local p = path..v
+ if vim.uv.fs_stat(p) then return p end
+ end
+ return nil
+ end
+
+ if vim.fn.executable(path) ~= 0 then
+ return path
+ end
+
+ return nil
+end
+
+local function walk_up(path, func)
+ local current_path = vim.fn.fnamemodify(path, ":p")
+ while true do
+ local result = func(current_path)
+ if result then
+ return result
+ end
+ local new_path = vim.fn.fnamemodify(current_path, ":h")
+ if new_path == current_path then
+ break
+ end
+ current_path = new_path
+ end
+ return nil
+end
+
+local function find_node_modules(path)
+ return walk_up(path, function (current_path)
+ local node_modules_path = current_path.."/node_modules"
+ if vim.fn.isdirectory(node_modules_path) ~= 0 then
+ return node_modules_path
+ end
+ return nil
+ end)
+end
+
+local function find_npm_exe(path, exe)
+ local node_modules_path = find_node_modules(path)
+ if not node_modules_path then return nil end
+ local try_exe_path = node_modules_path.."/.bin/"..exe
+ local exe_path = get_exe(try_exe_path)
+ if exe_path then return clean_path(exe_path) end
+ return nil
+end
+
+return {
+ clean_path = clean_path,
+ get_exe = get_exe,
+ walk_up = walk_up,
+ find_node_modules = find_node_modules,
+ find_npm_exe = find_npm_exe
+}
+
diff --git a/configs/nvim/lua/crupest/table.lua b/configs/nvim/lua/crupest/table.lua
new file mode 100644
index 0000000..6807151
--- /dev/null
+++ b/configs/nvim/lua/crupest/table.lua
@@ -0,0 +1,30 @@
+local function remove_element(tbl, element)
+ local index = nil
+ for i, v in ipairs(tbl) do
+ if element == v then
+ index = i
+ break
+ end
+ end
+ if index then
+ table.remove(tbl, index)
+ end
+ return tbl
+end
+
+local function element_at(tbl, element)
+ local at = nil
+ for i, v in ipairs(tbl) do
+ if element == v then
+ at = i
+ break
+ end
+ end
+ return at
+end
+
+return {
+ remove_element = remove_element,
+ element_at = element_at
+}
+