aboutsummaryrefslogtreecommitdiff
path: root/configs/nvim/lua
diff options
context:
space:
mode:
Diffstat (limited to 'configs/nvim/lua')
-rw-r--r--configs/nvim/lua/crupest-util-test.lua6
-rw-r--r--configs/nvim/lua/crupest-util.lua78
-rw-r--r--configs/nvim/lua/crupest/nvim.lua39
-rw-r--r--configs/nvim/lua/crupest/system.lua7
4 files changed, 34 insertions, 96 deletions
diff --git a/configs/nvim/lua/crupest-util-test.lua b/configs/nvim/lua/crupest-util-test.lua
deleted file mode 100644
index 1f12aeb..0000000
--- a/configs/nvim/lua/crupest-util-test.lua
+++ /dev/null
@@ -1,6 +0,0 @@
-local test_tsx_path = "~/codes/Timeline/FrontEnd/src/index.tsx"
-
-local util = loadfile("./lua/crupest-util.lua")()
-
-print(util.find_npm_exe(test_tsx_path, "eslint"))
-
diff --git a/configs/nvim/lua/crupest-util.lua b/configs/nvim/lua/crupest-util.lua
deleted file mode 100644
index 3d1e9b2..0000000
--- a/configs/nvim/lua/crupest-util.lua
+++ /dev/null
@@ -1,78 +0,0 @@
-local M = {}
-
-M.clean_path = function (path)
- return path and (string.gsub(path, "[/\\]+", "/"))
-end
-
-M.get_exe = function (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
-
-M.walk_up = function (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
-
-M.find_node_modules = function (path)
- return M.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
-
-M.find_npm_exe = function (path, exe)
- local node_modules_path = M.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 = M.get_exe(try_exe_path)
- if exe_path then return M.clean_path(exe_path) end
- return nil
-end
-
-function M.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
-
-return M
diff --git a/configs/nvim/lua/crupest/nvim.lua b/configs/nvim/lua/crupest/nvim.lua
index 2eea4f1..c47b7d5 100644
--- a/configs/nvim/lua/crupest/nvim.lua
+++ b/configs/nvim/lua/crupest/nvim.lua
@@ -37,6 +37,7 @@ local function get_previous_buffer(buf)
return bufs[index - 1]
end
+-- list the windows that are currently editing the given buffer
local function list_wins_editing_buf(buf)
local wins = vim.api.nvim_list_wins()
local result = {}
@@ -48,25 +49,28 @@ local function list_wins_editing_buf(buf)
return result
end
+-- Delete current buffer and jump back.
+-- If no previous jump, switch to previous buffer.
+-- If no previous buffer (no other buffers), create a unnamed one. (So the window does not quit.)
local function win_close_buf()
- local current_buffer = vim.api.nvim_get_current_buf()
+ local buf = 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
+ local old_jumps = { unpack(jumps_info[1], 1, jumps_info[2]) }
+ while #old_jumps ~= 0 do
+ local last_jump = old_jumps[#old_jumps]
+ if last_jump.bufnr ~= buf 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)
+ table.remove(old_jumps, #old_jumps)
end
- if #old_jump_list ~= 0 then
- local last_jump = old_jump_list[#old_jump_list]
+ if #old_jumps ~= 0 then
+ local last_jump = old_jumps[#old_jumps]
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)
+ local previous_buf = get_previous_buffer(buf)
if previous_buf then
vim.api.nvim_win_set_buf(0, previous_buf)
else
@@ -75,13 +79,26 @@ local function win_close_buf()
end
end
- vim.api.nvim_buf_delete(current_buffer, {})
+ local wins = list_wins_editing_buf(buf)
+ if #wins == 0 then
+ vim.api.nvim_buf_delete(buf, {})
+ end
+end
+
+local function close_float()
+ local wins = vim.api.nvim_list_wins()
+ for _, v in ipairs(wins) do
+ if vim.api.nvim_win_get_config(v).relative ~= '' then
+ vim.api.nvim_win_close(v, false)
+ end
+ end
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
+ win_close_buf = win_close_buf,
+ close_float = close_float,
}
diff --git a/configs/nvim/lua/crupest/system.lua b/configs/nvim/lua/crupest/system.lua
index 74f8670..a5a90a6 100644
--- a/configs/nvim/lua/crupest/system.lua
+++ b/configs/nvim/lua/crupest/system.lua
@@ -59,11 +59,16 @@ local function find_npm_exe(path, exe)
return nil
end
+local function escape_space(str)
+ return (string.gsub(str, " ", "\\ " ))
+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
+ find_npm_exe = find_npm_exe,
+ escape_space = escape_space
}