diff options
author | crupest <crupest@outlook.com> | 2023-10-02 00:02:38 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2023-10-02 00:02:38 +0800 |
commit | 2acf3a9ee8656c679f1eb230070d4e2bc963717c (patch) | |
tree | 91bb5fff77d125919e5c3a7d5523837fc14005f4 | |
parent | add2f396a2b677a23963bd63c627a516e0c92835 (diff) | |
download | crupest-2acf3a9ee8656c679f1eb230070d4e2bc963717c.tar.gz crupest-2acf3a9ee8656c679f1eb230070d4e2bc963717c.tar.bz2 crupest-2acf3a9ee8656c679f1eb230070d4e2bc963717c.zip |
Update nvim configs.
-rw-r--r-- | configs/nvim/init.lua | 37 | ||||
-rw-r--r-- | configs/nvim/lazy-lock.json | 1 | ||||
-rw-r--r-- | configs/nvim/lua/crupest/filesystem-cmd.lua | 49 | ||||
-rw-r--r-- | configs/nvim/lua/crupest/nvim.lua | 114 | ||||
-rw-r--r-- | configs/nvim/lua/crupest/nvim/fs.lua | 168 | ||||
-rw-r--r-- | configs/nvim/lua/crupest/nvim/plugins/lint.lua | 37 | ||||
-rw-r--r-- | configs/nvim/lua/plugins.lua | 1 |
7 files changed, 211 insertions, 196 deletions
diff --git a/configs/nvim/init.lua b/configs/nvim/init.lua index 22b4aa6..499093c 100644 --- a/configs/nvim/init.lua +++ b/configs/nvim/init.lua @@ -61,7 +61,8 @@ require("neo-tree").setup({ hide_dotfiles = true, hide_gitignored = true, hide_hidden = true, -- only works on Windows for hidden files/directories - } + }, + use_libuv_file_watcher = true } }) @@ -72,9 +73,6 @@ require('lualine').setup({ } }) --- setup gitsigns -require('gitsigns').setup() - -- setup toggleterm require("toggleterm").setup { open_mapping = "<C-`>", @@ -129,31 +127,8 @@ require("formatter").setup { } -- setup lint -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) - 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. -linter_eslint.args = {} -linter_eslint.append_fname = true - -lint.linters_by_ft = { - javascript = { "eslint", "cspell" }, - javascriptreact = { "eslint", "cspell" }, - typescript = { "eslint", "cspell" }, - typescriptreact = { "eslint", "cspell" }, - cs = { "cspell" } -} - -vim.api.nvim_create_autocmd({ "BufWritePost" }, { - callback = function() - lint.try_lint() - end, -}) +local lint = require("crupest.nvim.plugins.lint") +lint.setup_lint() -- setup nvim-cmp local cmp = require("cmp") @@ -339,7 +314,7 @@ vim.keymap.set('n', '<leader>le', vim.diagnostic.open_float) vim.keymap.set('n', '<leader>l[', vim.diagnostic.goto_prev) vim.keymap.set('n', '<leader>l]', vim.diagnostic.goto_next) vim.keymap.set('n', '<leader>lt', vim.diagnostic.setloclist) -vim.keymap.set('n', '<leader>ll', lint.try_lint) +vim.keymap.set('n', '<leader>ll', lint.run_lint) vim.keymap.set("n", "<c-tab>", "<cmd>bnext<cr>") @@ -348,5 +323,5 @@ vim.keymap.set("n", "<s-tab>", "<c-o>") vim.keymap.set("n", "<c-q>", require("crupest.nvim").win_close_buf) vim.keymap.set("n", "<esc>", require("crupest.nvim").close_float) -require("crupest.filesystem-cmd").setup_filesystem_user_commands() +require("crupest.nvim.fs").setup_filesystem_user_commands() diff --git a/configs/nvim/lazy-lock.json b/configs/nvim/lazy-lock.json index b1a40a4..a7ee4b6 100644 --- a/configs/nvim/lazy-lock.json +++ b/configs/nvim/lazy-lock.json @@ -7,7 +7,6 @@ "cmp_luasnip": { "branch": "master", "commit": "18095520391186d634a0045dacaa346291096566" },
"everforest": { "branch": "master", "commit": "83b666410d7ae0eccf96dbbe3b4b6ac5b8172d38" },
"formatter.nvim": { "branch": "master", "commit": "34dcdfa0c75df667743b2a50dd99c84a557376f0" },
- "gitsigns.nvim": { "branch": "main", "commit": "bdeba1cec3faddd89146690c10b9a87949c0ee66" },
"lazy.nvim": { "branch": "main", "commit": "59335c5b9d116f5d3948f833288a89e2a829a005" },
"lualine.nvim": { "branch": "master", "commit": "45e27ca739c7be6c49e5496d14fcf45a303c3a63" },
"neo-tree.nvim": { "branch": "main", "commit": "7e2a3caf999e2028abb643eb0472f351b2777591" },
diff --git a/configs/nvim/lua/crupest/filesystem-cmd.lua b/configs/nvim/lua/crupest/filesystem-cmd.lua deleted file mode 100644 index 3148cef..0000000 --- a/configs/nvim/lua/crupest/filesystem-cmd.lua +++ /dev/null @@ -1,49 +0,0 @@ -local function setup_filesystem_user_commands() - vim.api.nvim_create_user_command("Mv", function(opts) - require("crupest.nvim").mv_buf_file(vim.api.nvim_get_current_buf(), opts.fargs[1]) - end, { - nargs = 1, - complete = "file" - }) - - vim.api.nvim_create_user_command("MvFile", function(opts) - if (#opts.fargs ~= 2) then - vim.notify("MvFile accepts exactly two arguments, old file and new file.") - end - require("crupest.nvim").mv_file(opts.fargs[1], opts.fargs[2]) - end, { - nargs = "+", - complete = "file" - }) - - vim.api.nvim_create_user_command("MvDir", function(opts) - if (#opts.fargs ~= 2) then - vim.notify("MvDir accepts exactly two arguments, old dir and new dir.") - end - require("crupest.nvim").mv_dir(opts.fargs[1], opts.fargs[2]) - end, { - nargs = "+", - complete = "file" - }) - - vim.api.nvim_create_user_command("Rename", function(opts) - require("crupest.nvim").rename_buf_file(vim.api.nvim_get_current_buf(), opts.fargs[1]) - end, { - nargs = 1, - complete = "file" - }) - - vim.api.nvim_create_user_command("RenameFile", function(opts) - if (#opts.fargs ~= 2) then - vim.notify("RenameFile accepts exactly two arguments, old file and new file.") - end - require("crupest.nvim").rename_file(opts.fargs[1], opts.fargs[2]) - end, { - nargs = "+", - complete = "file" - }) -end - -return { - setup_filesystem_user_commands = setup_filesystem_user_commands -} diff --git a/configs/nvim/lua/crupest/nvim.lua b/configs/nvim/lua/crupest/nvim.lua index 44c9b5f..1c252fd 100644 --- a/configs/nvim/lua/crupest/nvim.lua +++ b/configs/nvim/lua/crupest/nvim.lua @@ -1,5 +1,3 @@ -local fs = require("crupest.system.fs") - local function list_listed_bufs() local bufs = vim.api.nvim_list_bufs() local result = {} @@ -105,123 +103,11 @@ local function close_float() end end -local function full_path(name) - return vim.fn.fnamemodify(name, ":p:gs?\\?/?") -end - -local function coerce_path_for_dir(old, new) - if fs.isdir(new) then - return new .. "/" .. vim.fn.fnamemodify(old, ":t") - end - return new -end - -local function do_mv_file(old, new, overwrite) - new = coerce_path_for_dir(old, new) - - if full_path(old) == full_path(new) then - vim.notify("Paths are identical. Do nothing.", vim.log.levels.WARN) - return false - end - - if not fs.isfile(old) then - vim.notify("Not exists or not a file. Can't move.", vim.log.levels.ERROR) - return false - end - - if not overwrite and fs.exist(new) then - vim.notify("Target path exists.", vim.log.levels.ERROR) - return false - end - - fs.move(old, new) - vim.notify("File moved.") - - return new -end - -local function mv_file(old, new, overwrite) - new = do_mv_file(old, new, overwrite) - if not new then return end - - local bufs = list_listed_bufs() - for _, b in ipairs(bufs) do - if full_path(vim.api.nvim_buf_get_name(b)) == full_path(old) then - vim.api.nvim_buf_set_name(b, new) - end - end -end - -local function mv_buf_file(buf, new, overwrite) - if not buf_is_normal(buf) then - vim.notify("Buf is not a normal buffer, can't move it.", vim.log.levels.ERROR) - return - end - - local name = vim.api.nvim_buf_get_name(buf) - - new = do_mv_file(name, new, overwrite) - if not new then return end - - vim.api.nvim_buf_set_name(buf, new) -end - -local function mv_dir(old_dir, new_dir, overwrite) - new_dir = coerce_path_for_dir(old_dir, new_dir) - - if full_path(old_dir) == full_path(new_dir) then - vim.notify("Paths are identical. Do nothing.", vim.log.levels.WARN) - return - end - - if not fs.isdir(old_dir) then - vim.notify("Not exist or not a dir. Can't move.", vim.log.levels.ERROR) - end - - if not overwrite and fs.exist(new_dir) then - vim.notify("Target path exists.", vim.log.levels.ERROR) - return - end - - if fs.isdir(old_dir) then - fs.move(old_dir, new_dir) - vim.notify("Dir moved.") - end - - local bufs = list_listed_bufs() - - for _, buf in ipairs(bufs) do - local name = vim.api.nvim_buf_get_name(buf) - local full_name = full_path(name) - local old_dir_full = full_path(old_dir) - if string.find(full_name, old_dir_full, 1, true) == 1 then - local new_name = new_dir .. string.sub(full_name, #old_dir_full + 1) - vim.api.nvim_buf_set_name(buf, new_name) - end - end -end - -local function rename_file(old, new, overwrite) - local dir = vim.fn.fnamemodify(old, ":h") - mv_file(old, dir .. "/" .. new, overwrite) -end - -local function rename_buf_file(buf, new_name, overwrite) - local old_path = vim.api.nvim_buf_get_name(buf) - local dir = vim.fn.fnamemodify(old_path, ":h") - mv_buf_file(buf, dir .. "/" .. new_name, overwrite) -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, close_float = close_float, - mv_file = mv_file, - mv_buf_file = mv_buf_file, - mv_dir = mv_dir, - rename_file = rename_file, - rename_buf_file = rename_buf_file } diff --git a/configs/nvim/lua/crupest/nvim/fs.lua b/configs/nvim/lua/crupest/nvim/fs.lua new file mode 100644 index 0000000..94eb51e --- /dev/null +++ b/configs/nvim/lua/crupest/nvim/fs.lua @@ -0,0 +1,168 @@ +local list_listed_bufs = require("crupest.nvim").list_listed_bufs; +local fs = require("crupest.system.fs"); + +local function full_path(name) + return vim.fn.fnamemodify(name, ":p:gs?\\?/?") +end + +-- There are two situations. +-- 1. the new path is not a dir, then it is used +-- 2. the new path is a dir, then it is appended with the last name of old path, to create a new valid file path +local function coerce_path_for_dir(old, new) + if fs.isdir(new) then + return new .. "/" .. vim.fn.fnamemodify(old, ":t") + end + return new +end + +local function do_mv_file(old, new, overwrite) + new = coerce_path_for_dir(old, new) + + if full_path(old) == full_path(new) then + vim.notify("Paths are identical. Do nothing.", vim.log.levels.WARN) + return false + end + + if not fs.isfile(old) then + vim.notify("Not exists or not a file. Can't move.", vim.log.levels.ERROR) + return false + end + + if not overwrite and fs.exist(new) then + vim.notify("Target path exists.", vim.log.levels.ERROR) + return false + end + + fs.move(old, new) + vim.notify("File moved.") + + return new +end + +local function mv_file(old, new, overwrite) + new = do_mv_file(old, new, overwrite) + if not new then return end + + local bufs = list_listed_bufs() + for _, b in ipairs(bufs) do + if full_path(vim.api.nvim_buf_get_name(b)) == full_path(old) then + vim.api.nvim_buf_set_name(b, new) + end + end +end + +local function mv_buf_file(buf, new, overwrite) + if not buf_is_normal(buf) then + vim.notify("Buf is not a normal buffer, can't move it.", vim.log.levels.ERROR) + return + end + + local name = vim.api.nvim_buf_get_name(buf) + + new = do_mv_file(name, new, overwrite) + if not new then return end + + vim.api.nvim_buf_set_name(buf, new) +end + +local function mv_dir(old_dir, new_dir, overwrite) + new_dir = coerce_path_for_dir(old_dir, new_dir) + + if full_path(old_dir) == full_path(new_dir) then + vim.notify("Paths are identical. Do nothing.", vim.log.levels.WARN) + return + end + + if not fs.isdir(old_dir) then + vim.notify("Not exist or not a dir. Can't move.", vim.log.levels.ERROR) + end + + if not overwrite and fs.exist(new_dir) then + vim.notify("Target path exists.", vim.log.levels.ERROR) + return + end + + if fs.isdir(old_dir) then + fs.move(old_dir, new_dir) + vim.notify("Dir moved.") + end + + local bufs = list_listed_bufs() + + for _, buf in ipairs(bufs) do + local name = vim.api.nvim_buf_get_name(buf) + local full_name = full_path(name) + local old_dir_full = full_path(old_dir) + if string.find(full_name, old_dir_full, 1, true) == 1 then + local new_name = new_dir .. string.sub(full_name, #old_dir_full + 1) + vim.api.nvim_buf_set_name(buf, new_name) + end + end +end + +local function rename_file(old, new, overwrite) + local dir = vim.fn.fnamemodify(old, ":h") + mv_file(old, dir .. "/" .. new, overwrite) +end + +local function rename_buf_file(buf, new_name, overwrite) + local old_path = vim.api.nvim_buf_get_name(buf) + local dir = vim.fn.fnamemodify(old_path, ":h") + mv_buf_file(buf, dir .. "/" .. new_name, overwrite) +end + +local function setup_filesystem_user_commands() + vim.api.nvim_create_user_command("Mv", function(opts) + mv_buf_file(vim.api.nvim_get_current_buf(), opts.fargs[1]) + end, { + nargs = 1, + complete = "file" + }) + + vim.api.nvim_create_user_command("MvFile", function(opts) + if (#opts.fargs ~= 2) then + vim.notify("MvFile accepts exactly two arguments, old file and new file.") + end + mv_file(opts.fargs[1], opts.fargs[2]) + end, { + nargs = "+", + complete = "file" + }) + + vim.api.nvim_create_user_command("MvDir", function(opts) + if (#opts.fargs ~= 2) then + vim.notify("MvDir accepts exactly two arguments, old dir and new dir.") + end + mv_dir(opts.fargs[1], opts.fargs[2]) + end, { + nargs = "+", + complete = "file" + }) + + vim.api.nvim_create_user_command("Rename", function(opts) + rename_buf_file(vim.api.nvim_get_current_buf(), opts.fargs[1]) + end, { + nargs = 1, + complete = "file" + }) + + vim.api.nvim_create_user_command("RenameFile", function(opts) + if (#opts.fargs ~= 2) then + vim.notify("RenameFile accepts exactly two arguments, old file and new file.") + end + rename_file(opts.fargs[1], opts.fargs[2]) + end, { + nargs = "+", + complete = "file" + }) +end + +return { + mv_file = mv_file, + mv_buf_file = mv_buf_file, + mv_dir = mv_dir, + rename_file = rename_file, + rename_buf_file = rename_buf_file, + setup_filesystem_user_commands = setup_filesystem_user_commands +} + diff --git a/configs/nvim/lua/crupest/nvim/plugins/lint.lua b/configs/nvim/lua/crupest/nvim/plugins/lint.lua new file mode 100644 index 0000000..abab018 --- /dev/null +++ b/configs/nvim/lua/crupest/nvim/plugins/lint.lua @@ -0,0 +1,37 @@ +local lint = require("lint") + +local function run_lint() + lint.try_lint() + lint.try_lint("cspell") +end + +local function setup_lint() + local linter_eslint = require("lint.linters.eslint") + + linter_eslint.cmd = function() + local current_buffer = vim.api.nvim_buf_get_name(0) + 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. + linter_eslint.args = {} + linter_eslint.append_fname = true + + lint.linters_by_ft = { + javascript = { "eslint" }, + javascriptreact = { "eslint" }, + typescript = { "eslint" }, + typescriptreact = { "eslint" }, + } + + vim.api.nvim_create_autocmd({ "BufWritePost" }, { + callback = run_lint, + }) +end + +return { + setup_lint = setup_lint, + run_lint = run_lint +} + diff --git a/configs/nvim/lua/plugins.lua b/configs/nvim/lua/plugins.lua index 5ba7e41..42038a7 100644 --- a/configs/nvim/lua/plugins.lua +++ b/configs/nvim/lua/plugins.lua @@ -18,7 +18,6 @@ return { "nvim-lualine/lualine.nvim", "nvim-telescope/telescope.nvim", "windwp/nvim-autopairs", - "lewis6991/gitsigns.nvim", "mhartington/formatter.nvim", "mfussenegger/nvim-lint", "akinsho/toggleterm.nvim", |