aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2023-09-08 17:57:37 +0800
committercrupest <crupest@outlook.com>2023-09-13 21:12:21 +0800
commit51020952fc2f99ace8873514f3223ce4b84580b8 (patch)
tree9614208be1450c2f57cdaf18facd5d9b1d0178fa
parentf6872f1b55c73d6382819cb531162a4b18ca621b (diff)
downloadcrupest-51020952fc2f99ace8873514f3223ce4b84580b8.tar.gz
crupest-51020952fc2f99ace8873514f3223ce4b84580b8.tar.bz2
crupest-51020952fc2f99ace8873514f3223ce4b84580b8.zip
Update nvim config.
-rw-r--r--configs/nvim/init.lua97
-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
5 files changed, 42 insertions, 185 deletions
diff --git a/configs/nvim/init.lua b/configs/nvim/init.lua
index 1b8f0f2..e5d8d35 100644
--- a/configs/nvim/init.lua
+++ b/configs/nvim/init.lua
@@ -89,13 +89,10 @@ require("nvim-autopairs").setup {}
-- setup formatter
local prettier_formatter = function ()
local current_buffer = vim.api.nvim_buf_get_name(0)
- local prettier_exe = require("crupest-util").find_npm_exe(current_buffer, "prettier") or "prettier"
+ local prettier_exe = require("crupest.system").find_npm_exe(current_buffer, "prettier") or "prettier"
if vim.fn.has("win32") ~= 0 then
- local escape = function (str)
- return ({ string.gsub(str, " ", "\\ " )})[1]
- end
-
+ local escape = require("crupest.system").escape_space
current_buffer = escape(current_buffer)
prettier_exe = escape(prettier_exe)
end
@@ -139,9 +136,7 @@ local lint = require("lint")
local linter_eslint = require("lint.linters.eslint")
linter_eslint.cmd = function ()
local current_buffer = vim.api.nvim_buf_get_name(0)
- local local_eslint = require("crupest-util").find_npm_exe(current_buffer, "eslint")
- if local_eslint then return local_eslint end
- return "eslint"
+ return require("crupest.system").find_npm_exe(current_buffer, "eslint") or "eslint"
end
-- lint library use 'cmd /C' to run exe, but we don't need this, so explicitly
-- set args to empty.
@@ -201,6 +196,9 @@ lspconfig.lua_ls.setup {
capabilities = capabilities,
settings = {
Lua = {
+ runtime = {
+ version = "LuaJIT"
+ },
diagnostics = {
globals = { "vim" },
},
@@ -291,85 +289,6 @@ vim.cmd.colorscheme "catppuccin"
vim.keymap.set("n", "<c-tab>", "<cmd>bnext<cr>")
vim.keymap.set("n", "<c-s-tab>", "<cmd>bNext<cr>")
vim.keymap.set("n", "<s-tab>", "<c-o>")
-
-local list_listed_bufs = function ()
- 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 get_previous_buffer = function (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
-
--- 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.)
-vim.keymap.set("n", "<c-q>", function ()
- 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)
-
-vim.keymap.set("n", "<esc>", function ()
- 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)
+vim.keymap.set("n", "<c-q>", require("crupest.nvim").win_close_buf)
+vim.keymap.set("n", "<esc>", require("crupest.nvim").close_float)
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
}