From b109ae78fb51c8f288e18c28cfd711f0192b20c8 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 7 Sep 2024 02:59:28 +0800 Subject: config(nvim): refactor a lot. --- configs/nvim/init.lua | 152 +------------------ configs/nvim/lazy-lock.json | 20 ++- configs/nvim/lua/crupest/constants.lua | 32 ---- configs/nvim/lua/crupest/nvim.lua | 114 -------------- configs/nvim/lua/crupest/nvim/fs.lua | 167 --------------------- configs/nvim/lua/crupest/nvim/keymap.lua | 9 ++ configs/nvim/lua/crupest/nvim/lsp/c.lua | 7 +- configs/nvim/lua/crupest/nvim/lsp/csharp.lua | 14 -- configs/nvim/lua/crupest/nvim/lsp/deno.lua | 13 -- configs/nvim/lua/crupest/nvim/lsp/frontend.lua | 28 ---- configs/nvim/lua/crupest/nvim/lsp/init.lua | 34 +++++ configs/nvim/lua/crupest/nvim/lsp/lua.lua | 4 +- configs/nvim/lua/crupest/nvim/lsp/rust.lua | 13 -- configs/nvim/lua/crupest/nvim/plugins/cmp.lua | 31 ++++ configs/nvim/lua/crupest/nvim/plugins/format.lua | 76 ---------- .../nvim/lua/crupest/nvim/plugins/formatter.lua | 114 -------------- configs/nvim/lua/crupest/nvim/plugins/gitsign.lua | 51 +++++++ configs/nvim/lua/crupest/nvim/plugins/init.lua | 11 ++ configs/nvim/lua/crupest/nvim/plugins/lint.lua | 39 +++-- configs/nvim/lua/crupest/nvim/plugins/others.lua | 20 +++ configs/nvim/lua/crupest/nvim/plugins/snip.lua | 5 +- .../nvim/lua/crupest/nvim/plugins/telescope.lua | 11 ++ configs/nvim/lua/crupest/system.lua | 7 - configs/nvim/lua/crupest/system/find.lua | 153 ------------------- configs/nvim/lua/crupest/system/fs.lua | 101 ------------- configs/nvim/lua/crupest/table.lua | 42 ------ configs/nvim/lua/crupest/utils/find.lua | 152 +++++++++++++++++++ configs/nvim/lua/crupest/utils/fs.lua | 101 +++++++++++++ configs/nvim/lua/crupest/utils/nvim.lua | 44 ++++++ configs/nvim/lua/crupest/utils/table.lua | 42 ++++++ configs/nvim/lua/plugins.lua | 15 +- 31 files changed, 560 insertions(+), 1062 deletions(-) delete mode 100644 configs/nvim/lua/crupest/constants.lua delete mode 100644 configs/nvim/lua/crupest/nvim.lua delete mode 100644 configs/nvim/lua/crupest/nvim/fs.lua create mode 100644 configs/nvim/lua/crupest/nvim/keymap.lua delete mode 100644 configs/nvim/lua/crupest/nvim/lsp/csharp.lua delete mode 100644 configs/nvim/lua/crupest/nvim/lsp/deno.lua delete mode 100644 configs/nvim/lua/crupest/nvim/lsp/frontend.lua create mode 100644 configs/nvim/lua/crupest/nvim/lsp/init.lua delete mode 100644 configs/nvim/lua/crupest/nvim/lsp/rust.lua create mode 100644 configs/nvim/lua/crupest/nvim/plugins/cmp.lua delete mode 100644 configs/nvim/lua/crupest/nvim/plugins/format.lua delete mode 100644 configs/nvim/lua/crupest/nvim/plugins/formatter.lua create mode 100644 configs/nvim/lua/crupest/nvim/plugins/gitsign.lua create mode 100644 configs/nvim/lua/crupest/nvim/plugins/init.lua create mode 100644 configs/nvim/lua/crupest/nvim/plugins/others.lua create mode 100644 configs/nvim/lua/crupest/nvim/plugins/telescope.lua delete mode 100644 configs/nvim/lua/crupest/system.lua delete mode 100644 configs/nvim/lua/crupest/system/find.lua delete mode 100644 configs/nvim/lua/crupest/system/fs.lua delete mode 100644 configs/nvim/lua/crupest/table.lua create mode 100644 configs/nvim/lua/crupest/utils/find.lua create mode 100644 configs/nvim/lua/crupest/utils/fs.lua create mode 100644 configs/nvim/lua/crupest/utils/nvim.lua create mode 100644 configs/nvim/lua/crupest/utils/table.lua diff --git a/configs/nvim/init.lua b/configs/nvim/init.lua index 92cfedb..a2b84aa 100644 --- a/configs/nvim/init.lua +++ b/configs/nvim/init.lua @@ -19,10 +19,7 @@ if is_win then let &shellpipe = '2>&1 | %%{ "$_" } | tee %s; exit $LastExitCode' set shellquote= shellxquote= ]]) -else - vim.cmd([[ - let &shell='bash --login' - ]]) + vim.opt.completeslash = 'slash' end -- spellchecker: enable @@ -36,11 +33,6 @@ vim.opt.wrap = false; vim.opt.number = true; -- spellchecker: enable -if is_win then - -- spellchecker: disable-next-line - vim.opt.completeslash = 'slash' -end - vim.g.load_doxygen_syntax = true; vim.g.doxygen_javadoc_autobrief = false; @@ -63,142 +55,6 @@ require("lazy").setup("plugins") vim.cmd("colorscheme catppuccin-macchiato") --- setup neo-tree -require("neo-tree").setup({ - filesystem = { - filtered_items = { - hide_dotfiles = false, - hide_gitignored = false, - hide_hidden = false, -- only works on Windows for hidden files/directories - }, - use_libuv_file_watcher = true - } -}) - --- setup lualine -require('lualine').setup({ - options = { - theme = "auto", -- Can also be "auto" to detect automatically. - } -}) - --- setup toggleterm -require("toggleterm").setup { - open_mapping = "", - start_in_insert = false, -} - --- setup autopairs -require("nvim-autopairs").setup {} - --- setup gitsigns -require('gitsigns').setup() - --- setup format -local format = require("crupest.nvim.plugins.format") -format.setup_format() - --- setup lint -local lint = require("crupest.nvim.plugins.lint") -lint.setup_lint() - --- setup nvim-cmp -local snip = require("crupest.nvim.plugins.snip") -local luasnip = snip.luasnip -snip.setup_snip() - -local cmp = require("cmp") - -cmp.setup({ - snippet = { - expand = function(args) - luasnip.lsp_expand(args.body) - end, - }, - window = { - }, - mapping = cmp.mapping.preset.insert({ - [''] = cmp.mapping.select_next_item(), - [''] = cmp.mapping.select_prev_item(), - [''] = cmp.mapping.scroll_docs(-4), - [''] = cmp.mapping.scroll_docs(4), - [''] = cmp.mapping.complete(), - [''] = cmp.mapping.abort(), - [''] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. - [''] = cmp.mapping.confirm({ select = true }) - }), - sources = cmp.config.sources({ - { name = 'nvim_lsp' }, - { name = 'luasnip' }, - }, { - { name = 'buffer' }, - }) -}) - - -require("crupest.nvim.lsp.c").setup_lsp_c() -require("crupest.nvim.lsp.lua").setup_lsp_lua() -require("crupest.nvim.lsp.deno").setup_lsp_deno() -require("crupest.nvim.lsp.frontend").setup_lsp_frontend() -require("crupest.nvim.lsp.csharp").setup_lsp_csharp() --- There is some problem of rust analyzer. --- require("crupest.nvim.lsp.rust").setup_lsp_rust() - --- Use LspAttach autocommand to only map the following keys --- after the language server attaches to the current buffer -vim.api.nvim_create_autocmd('LspAttach', { - group = vim.api.nvim_create_augroup('UserLspConfig', {}), - callback = function(ev) - -- Enable completion triggered by - vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc' - - -- Buffer local mappings. - -- See `:help vim.lsp.*` for documentation on any of the below functions - local opts = { buffer = ev.buf } - vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts) - vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts) - vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts) - vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts) - vim.keymap.set('n', '', vim.lsp.buf.signature_help, opts) - vim.keymap.set('n', 'wa', vim.lsp.buf.add_workspace_folder, opts) - vim.keymap.set('n', 'wr', vim.lsp.buf.remove_workspace_folder, opts) - vim.keymap.set('n', 'wl', function() - print(vim.inspect(vim.lsp.buf.list_workspace_folders())) - end, opts) - vim.keymap.set('n', 'D', vim.lsp.buf.type_definition, opts) - vim.keymap.set('n', 'rn', vim.lsp.buf.rename, opts) - vim.keymap.set({ 'n', 'v' }, 'ca', vim.lsp.buf.code_action, opts) - vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts) - - vim.keymap.set('n', 'f', format.do_format, opts) - end, -}) - - --- For terminal emulator -vim.keymap.set('t', '', [[]]) - --- setup keymap for telescope -local builtin = require('telescope.builtin') -vim.keymap.set('n', 'f', builtin.find_files, {}) -vim.keymap.set('n', 'g', builtin.live_grep, {}) -vim.keymap.set('n', 'b', builtin.buffers, {}) -vim.keymap.set('n', 'h', builtin.help_tags, {}) - --- setup keymap for tree -vim.keymap.set('n', 't', "Neotree toggle", {}) - --- See `:help vim.diagnostic.*` for documentation on any of the below functions -vim.keymap.set('n', 'le', vim.diagnostic.open_float) -vim.keymap.set('n', 'l[', vim.diagnostic.goto_prev) -vim.keymap.set('n', 'l]', vim.diagnostic.goto_next) -vim.keymap.set('n', 'll', vim.diagnostic.setloclist) -vim.keymap.set('n', 'lr', lint.run_lint) - -vim.keymap.set("n", "", "bnext") -vim.keymap.set("n", "", "bNext") -vim.keymap.set("n", "", "") -vim.keymap.set("n", "", require("crupest.nvim").win_close_buf) -vim.keymap.set("n", "", require("crupest.nvim").close_float) - -require("crupest.nvim.fs").setup_filesystem_user_commands() +require("crupest.nvim.lsp").setup() +require("crupest.nvim.plugins").setup() +require("crupest.nvim.keymap").setup() diff --git a/configs/nvim/lazy-lock.json b/configs/nvim/lazy-lock.json index b16a6e0..59f5473 100644 --- a/configs/nvim/lazy-lock.json +++ b/configs/nvim/lazy-lock.json @@ -1,24 +1,22 @@ { - "LuaSnip": { "branch": "master", "commit": "7ad2eaeaca56d6ed63acacbfc114b99f1f67b982" }, + "LuaSnip": { "branch": "master", "commit": "45db5addf8d0a201e1cf247cae4cdce605ad3768" }, "catppuccin": { "branch": "main", "commit": "4fd72a9ab64b393c2c22b168508fd244877fec96" }, "cmp-buffer": { "branch": "main", "commit": "3022dbc9166796b644a841a02de8dd1cc1d311fa" }, "cmp-cmdline": { "branch": "main", "commit": "d250c63aa13ead745e3a40f61fdd3470efde3923" }, "cmp-nvim-lsp": { "branch": "main", "commit": "39e2eda76828d88b773cc27a3f61d2ad782c922d" }, "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" }, "cmp_luasnip": { "branch": "master", "commit": "05a9ab28b53f71d1aece421ef32fee2cb857a843" }, - "formatter.nvim": { "branch": "master", "commit": "ad246d34ce7a32f752071ed81b09b94e6b127fad" }, - "gitsigns.nvim": { "branch": "main", "commit": "562dc47189ad3c8696dbf460d38603a74d544849" }, - "lazy.nvim": { "branch": "main", "commit": "077102c5bfc578693f12377846d427f49bc50076" }, + "gitsigns.nvim": { "branch": "main", "commit": "1ef74b546732f185d0f806860fa5404df7614f28" }, + "lazy.nvim": { "branch": "main", "commit": "48b52b5cfcf8f88ed0aff8fde573a5cc20b1306d" }, "lualine.nvim": { "branch": "master", "commit": "b431d228b7bbcdaea818bdc3e25b8cdbe861f056" }, - "neo-tree.nvim": { "branch": "main", "commit": "206241e451c12f78969ff5ae53af45616ffc9b72" }, + "neo-tree.nvim": { "branch": "main", "commit": "0774fa2085c62a147fcc7b56f0ac37053cc80217" }, "nui.nvim": { "branch": "main", "commit": "61574ce6e60c815b0a0c4b5655b8486ba58089a1" }, - "nvim-autopairs": { "branch": "master", "commit": "19606af7c039271d5aa96bceff101e7523af3136" }, + "nvim-autopairs": { "branch": "master", "commit": "fd2badc24e675f947162a16c124d395bde80dbd6" }, "nvim-cmp": { "branch": "main", "commit": "ae644feb7b67bf1ce4260c231d1d4300b19c6f30" }, - "nvim-lint": { "branch": "master", "commit": "debabca63c0905b59ce596a55a8e33eafdf66342" }, - "nvim-lspconfig": { "branch": "master", "commit": "911167921d49cd5c1c9b2436031d0da3945e787f" }, - "nvim-web-devicons": { "branch": "master", "commit": "3722e3d1fb5fe1896a104eb489e8f8651260b520" }, - "omnisharp-extended-lsp.nvim": { "branch": "main", "commit": "aad7bf06b4ca0de816b919d475a75b30f5f62b61" }, + "nvim-lint": { "branch": "master", "commit": "a7ce9c78a7c710c28bee56dfab10a5c0c80b7fb5" }, + "nvim-lspconfig": { "branch": "master", "commit": "bdbc65aadc708ce528efb22bca5f82a7cca6b54d" }, + "nvim-web-devicons": { "branch": "master", "commit": "9793801f974bba70e4ac5d7eae6c4f5659993d8e" }, "plenary.nvim": { "branch": "master", "commit": "ec289423a1693aeae6cd0d503bac2856af74edaa" }, "telescope.nvim": { "branch": "master", "commit": "5972437de807c3bc101565175da66a1aa4f8707a" }, "toggleterm.nvim": { "branch": "main", "commit": "137d06fb103952a0fb567882bb8527e2f92d327d" } -} \ No newline at end of file +} diff --git a/configs/nvim/lua/crupest/constants.lua b/configs/nvim/lua/crupest/constants.lua deleted file mode 100644 index 89ae968..0000000 --- a/configs/nvim/lua/crupest/constants.lua +++ /dev/null @@ -1,32 +0,0 @@ -local config_patterns = { - cspell = { - ".cspell.json", - "cspell.json", - ".cSpell.json", - "cSpell.json", - "cspell.config.js", - "cspell.config.cjs", - "cspell.config.json", - "cspell.config.yaml", - "cspell.config.yml", - "cspell.yaml", - "cspell.yml", - }, - nodejs = { - "package.json" - }, - deno = { - "deno.json", "deno.jsonc" - } -} - -local filetype_collections = { - js_ts = { 'javascript', 'javascriptreact', 'typescript', 'typescriptreact' }, - html_css = { 'html', 'css' }, - frontend = { 'javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'html', 'css' }, -} - -return { - config_patterns = config_patterns, - filetype_collections = filetype_collections, -} diff --git a/configs/nvim/lua/crupest/nvim.lua b/configs/nvim/lua/crupest/nvim.lua deleted file mode 100644 index 505f06c..0000000 --- a/configs/nvim/lua/crupest/nvim.lua +++ /dev/null @@ -1,114 +0,0 @@ -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 - --- 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 = {} - 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 buf_is_normal(buf) - return vim.fn.bufexists(buf) ~= 0 and vim.fn.buflisted(buf) ~= 0 -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 buf = vim.api.nvim_get_current_buf() - - if not buf_is_normal(buf) then - return - end - - local jumps_info = vim.fn.getjumplist() - - 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_jumps, #old_jumps) - end - - 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(buf) - 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 - - 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, - buf_is_normal = buf_is_normal, - get_previous_buffer = get_previous_buffer, - list_wins_editing_buf = list_wins_editing_buf, - win_close_buf = win_close_buf, - close_float = close_float, -} - diff --git a/configs/nvim/lua/crupest/nvim/fs.lua b/configs/nvim/lua/crupest/nvim/fs.lua deleted file mode 100644 index 2410535..0000000 --- a/configs/nvim/lua/crupest/nvim/fs.lua +++ /dev/null @@ -1,167 +0,0 @@ -local crupest_nvim = require("crupest.nvim"); -local fs = require("crupest.system.fs"); -local full_path = require("crupest.system").full_path; - -local list_listed_bufs = crupest_nvim.list_listed_bufs; -local buf_is_normal = crupest_nvim.buf_is_normal; - --- 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/keymap.lua b/configs/nvim/lua/crupest/nvim/keymap.lua new file mode 100644 index 0000000..624c04c --- /dev/null +++ b/configs/nvim/lua/crupest/nvim/keymap.lua @@ -0,0 +1,9 @@ +local function setup() + vim.keymap.set("n", "", "bnext") + vim.keymap.set("n", "", "bNext") + vim.keymap.set("n", "", require("crupest.utils.nvim").close_float) +end + +return { + setup = setup +} diff --git a/configs/nvim/lua/crupest/nvim/lsp/c.lua b/configs/nvim/lua/crupest/nvim/lsp/c.lua index 28edae0..e0466b1 100644 --- a/configs/nvim/lua/crupest/nvim/lsp/c.lua +++ b/configs/nvim/lua/crupest/nvim/lsp/c.lua @@ -1,10 +1,11 @@ local lspconfig = require("lspconfig") local capabilities = require("cmp_nvim_lsp").default_capabilities() -local get_exe = require("crupest.system.find").get_exe + +local get_exe = require("crupest.utils.find").get_exe local brew_clangd_path = "/usr/local/opt/llvm/bin/clangd" -local function setup_lsp_c() +local function setup() local clangd = "clangd" if get_exe(brew_clangd_path) then @@ -24,5 +25,5 @@ local function setup_lsp_c() end return { - setup_lsp_c = setup_lsp_c + setup = setup } diff --git a/configs/nvim/lua/crupest/nvim/lsp/csharp.lua b/configs/nvim/lua/crupest/nvim/lsp/csharp.lua deleted file mode 100644 index f5cd29b..0000000 --- a/configs/nvim/lua/crupest/nvim/lsp/csharp.lua +++ /dev/null @@ -1,14 +0,0 @@ -local lspconfig = require("lspconfig"); -local capabilities = require("cmp_nvim_lsp").default_capabilities() - - -local function setup_lsp_csharp() - lspconfig.csharp_ls.setup { - capabilities = capabilities, - root_dir = lspconfig.util.root_pattern("*.csproj"), - } -end - -return { - setup_lsp_csharp = setup_lsp_csharp -} diff --git a/configs/nvim/lua/crupest/nvim/lsp/deno.lua b/configs/nvim/lua/crupest/nvim/lsp/deno.lua deleted file mode 100644 index d553a35..0000000 --- a/configs/nvim/lua/crupest/nvim/lsp/deno.lua +++ /dev/null @@ -1,13 +0,0 @@ -local lspconfig = require("lspconfig"); -local capabilities = require("cmp_nvim_lsp").default_capabilities() - -local function setup_lsp_deno() - lspconfig.denols.setup { - capabilities = capabilities, - root_dir = lspconfig.util.root_pattern("deno.json", "deno.jsonc"), - } -end - -return { - setup_lsp_deno = setup_lsp_deno -} diff --git a/configs/nvim/lua/crupest/nvim/lsp/frontend.lua b/configs/nvim/lua/crupest/nvim/lsp/frontend.lua deleted file mode 100644 index 2573cf1..0000000 --- a/configs/nvim/lua/crupest/nvim/lsp/frontend.lua +++ /dev/null @@ -1,28 +0,0 @@ -local lspconfig = require("lspconfig"); -local capabilities = require("cmp_nvim_lsp").default_capabilities() - -local function setup_lsp_frontend() - lspconfig.cssls.setup { - capabilities = capabilities - } - - lspconfig.html.setup { - capabilities = capabilities - } - - lspconfig.tsserver.setup { - capabilities = capabilities, - on_new_config = function(new_config, new_root_dir) - local local_tsserver = require("crupest.system.find").find_npm_exe(new_root_dir, "typescript-language-server"); - if local_tsserver then - new_config.cmd = { local_tsserver, "--stdio" } - end - end, - root_dir = lspconfig.util.root_pattern("package.json"), - single_file_support = false, - } -end - -return { - setup_lsp_frontend = setup_lsp_frontend -} diff --git a/configs/nvim/lua/crupest/nvim/lsp/init.lua b/configs/nvim/lua/crupest/nvim/lsp/init.lua new file mode 100644 index 0000000..464d7ef --- /dev/null +++ b/configs/nvim/lua/crupest/nvim/lsp/init.lua @@ -0,0 +1,34 @@ +local function setup() + require("crupest.nvim.lsp.c").setup() + require("crupest.nvim.lsp.lua").setup() + + -- Use LspAttach autocommand to only map the following keys + -- after the language server attaches to the current buffer + vim.api.nvim_create_autocmd('LspAttach', { + group = vim.api.nvim_create_augroup('UserLspConfig', {}), + callback = function(ev) + -- Buffer local mappings. + -- See `:help vim.lsp.*` for documentation on any of the below functions + local opts = { buffer = ev.buf } + vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts) + vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts) + vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts) + vim.keymap.set('n', '', vim.lsp.buf.signature_help, opts) + vim.keymap.set('n', 'wa', vim.lsp.buf.add_workspace_folder, opts) + vim.keymap.set('n', 'wr', vim.lsp.buf.remove_workspace_folder, opts) + vim.keymap.set('n', 'wl', function() + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + end, opts) + vim.keymap.set('n', 'D', vim.lsp.buf.type_definition, opts) + vim.keymap.set('n', 'rn', vim.lsp.buf.rename, opts) + vim.keymap.set({ 'n', 'v' }, 'ca', vim.lsp.buf.code_action, opts) + vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts) + vim.keymap.set('n', 'f', vim.lsp.buf.format, opts) + end, + }) +end + + +return { + setup = setup +} diff --git a/configs/nvim/lua/crupest/nvim/lsp/lua.lua b/configs/nvim/lua/crupest/nvim/lsp/lua.lua index 4df0f49..1785515 100644 --- a/configs/nvim/lua/crupest/nvim/lsp/lua.lua +++ b/configs/nvim/lua/crupest/nvim/lsp/lua.lua @@ -1,7 +1,7 @@ local lspconfig = require("lspconfig") local capabilities = require("cmp_nvim_lsp").default_capabilities() -local function setup_lsp_lua() +local function setup() lspconfig.lua_ls.setup { capabilities = capabilities, settings = { @@ -27,5 +27,5 @@ local function setup_lsp_lua() end return { - setup_lsp_lua = setup_lsp_lua + setup = setup } diff --git a/configs/nvim/lua/crupest/nvim/lsp/rust.lua b/configs/nvim/lua/crupest/nvim/lsp/rust.lua deleted file mode 100644 index 219fc12..0000000 --- a/configs/nvim/lua/crupest/nvim/lsp/rust.lua +++ /dev/null @@ -1,13 +0,0 @@ -local lspconfig = require("lspconfig") -local capabilities = require("cmp_nvim_lsp").default_capabilities() - - -local function setup_lsp_rust() - lspconfig.rust_analyzer.setup { - capabilities = capabilities, - } -end - -return { - setup_lsp_rust = setup_lsp_rust -} diff --git a/configs/nvim/lua/crupest/nvim/plugins/cmp.lua b/configs/nvim/lua/crupest/nvim/plugins/cmp.lua new file mode 100644 index 0000000..9b1d876 --- /dev/null +++ b/configs/nvim/lua/crupest/nvim/plugins/cmp.lua @@ -0,0 +1,31 @@ +local function setup() + local cmp = require("cmp") + local luasnip = require("luasnip") + + cmp.setup { + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + window = { + }, + mapping = cmp.mapping.preset.insert({ + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.abort(), + [''] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. + }), + sources = cmp.config.sources({ + { name = 'nvim_lsp' }, + { name = 'luasnip' }, + }, { + { name = 'buffer' }, + }) + } +end + +return { + setup = setup +} diff --git a/configs/nvim/lua/crupest/nvim/plugins/format.lua b/configs/nvim/lua/crupest/nvim/plugins/format.lua deleted file mode 100644 index 0e3320a..0000000 --- a/configs/nvim/lua/crupest/nvim/plugins/format.lua +++ /dev/null @@ -1,76 +0,0 @@ -local find = require("crupest.system.find") -local constants = require("crupest.constants") - -local function wrap_formatter_with_exe(name, exe) - local formatter = require('formatter.defaults.' .. name) - formatter = formatter() - formatter.try_node_modules = false - formatter.exe = exe - return formatter -end - -local function set_formatters_for_filetype(filetype, formatters) - require('formatter.config').values.filetype[filetype] = formatters -end - -local my_formatters = { - { - name = "prettier", - exe_places = { "npm" }, - filetypes = constants.filetype_collections.frontend, - config_files = constants.config_patterns.nodejs, - }, -} - -local function find_custom_formatter(opts) - if opts == nil then opts = {} end - if opts.buf == nil then opts.buf = 0 end - - for _, f in ipairs(my_formatters) do - local r = find.find_exe_for_buf(opts.buf, f) - if r ~= nil then - local formatter = wrap_formatter_with_exe(r.name, r.exe_path) - set_formatters_for_filetype(r.filetype, { formatter }) - return r.name - end - end - - return nil -end - - -local function do_format(opt) - if not opt then - opt = {} - end - - if not opt.buf then - opt.buf = 0 - end - - local custom_formatter = find_custom_formatter(opt) - - if custom_formatter then - print("Use custom formatters: " .. custom_formatter .. ".") - vim.cmd("Format") - return - end - - local has_lsp = vim.lsp.get_active_clients({ bufnr = 0 }) - if has_lsp then - print("Use lsp formatter.") - vim.lsp.buf.format { async = true } - return - end - - vim.notify("No formatters found.", vim.log.levels.ERROR); -end - -local function setup_format() - require("formatter").setup {} -end - -return { - setup_format = setup_format, - do_format = do_format, -} diff --git a/configs/nvim/lua/crupest/nvim/plugins/formatter.lua b/configs/nvim/lua/crupest/nvim/plugins/formatter.lua deleted file mode 100644 index 9f6138f..0000000 --- a/configs/nvim/lua/crupest/nvim/plugins/formatter.lua +++ /dev/null @@ -1,114 +0,0 @@ -local fs = require("crupest.system.fs") -local find_npm_exe = require("crupest.system.find").find_npm_exe; - -local prettier_formatter = function() - local current_buffer = vim.api.nvim_buf_get_name(0) - local prettier_exe = find_npm_exe(current_buffer, "prettier") or "prettier" - - if vim.fn.has("win32") ~= 0 then - local escape = fs.escape_space - current_buffer = escape(current_buffer) - prettier_exe = escape(prettier_exe) - end - - return { - exe = prettier_exe, - args = { - "--stdin-filepath", - current_buffer - }, - stdin = true, - } -end - -local formatters_for_filetype = { - html = { - prettier_formatter - }, - css = { - prettier_formatter - }, - javascript = { - prettier_formatter - }, - javascriptreact = { - prettier_formatter - }, - typescript = { - prettier_formatter - }, - typescriptreact = { - prettier_formatter - } -} - -local function get_formatter_name(formatter) - if formatter == prettier_formatter then return "prettier" end - return nil -end - -local function get_formatter_name_list(formatters) - local result = {} - for _, formatter in ipairs(formatters) do - table.insert(result, get_formatter_name(formatter)) - end - return result -end - -local function setup_formatter() - require("formatter").setup { - filetype = formatters_for_filetype - } -end - - -local function get_custom_formatters(bufnr) - local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype") - for ft, formatters in pairs(formatters_for_filetype) do - if filetype == ft then - return true, get_formatter_name_list(formatters) - end - end - return false, {} -end - -local function run_formatter(opt) - if not opt then - opt = {} - end - - if not opt.buf then - opt.buf = 0 - end - - local has_custom_formatter, formatter_names = get_custom_formatters(opt.buf) - - local formatter_name_str = "" - for i, name in ipairs(formatter_names) do - if i == 1 then - formatter_name_str = name - else - formatter_name_str = formatter_name_str .. " " .. name - end - end - - if has_custom_formatter then - print("Use custom formatters: " .. formatter_name_str .. ".") - vim.cmd("Format") - return - end - - local has_lsp = vim.lsp.get_active_clients({ bufnr = 0 }) - if has_lsp then - print("Use lsp formatter.") - vim.lsp.buf.format { async = true } - return - end - - vim.notify("No formatters found.", vim.log.levels.ERROR); -end - -return { - setup_formatter = setup_formatter, - run_formatter = run_formatter -} diff --git a/configs/nvim/lua/crupest/nvim/plugins/gitsign.lua b/configs/nvim/lua/crupest/nvim/plugins/gitsign.lua new file mode 100644 index 0000000..220c91a --- /dev/null +++ b/configs/nvim/lua/crupest/nvim/plugins/gitsign.lua @@ -0,0 +1,51 @@ +local function setup() + local gitsigns = require('gitsigns') + gitsigns.setup { + on_attach = function(bufnr) + local function map(mode, l, r, opts) + opts = opts or {} + opts.buffer = bufnr + vim.keymap.set(mode, l, r, opts) + end + + -- Navigation + map('n', ']c', function() + if vim.wo.diff then + vim.cmd.normal({ ']c', bang = true }) + else + gitsigns.nav_hunk('next') + end + end) + + map('n', '[c', function() + if vim.wo.diff then + vim.cmd.normal({ '[c', bang = true }) + else + gitsigns.nav_hunk('prev') + end + end) + + -- Actions + map('n', 'hs', gitsigns.stage_hunk) + map('n', 'hr', gitsigns.reset_hunk) + map('v', 'hs', function() gitsigns.stage_hunk { vim.fn.line('.'), vim.fn.line('v') } end) + map('v', 'hr', function() gitsigns.reset_hunk { vim.fn.line('.'), vim.fn.line('v') } end) + map('n', 'hS', gitsigns.stage_buffer) + map('n', 'hu', gitsigns.undo_stage_hunk) + map('n', 'hR', gitsigns.reset_buffer) + map('n', 'hp', gitsigns.preview_hunk) + map('n', 'hb', function() gitsigns.blame_line { full = true } end) + map('n', 'tb', gitsigns.toggle_current_line_blame) + map('n', 'hd', gitsigns.diffthis) + map('n', 'hD', function() gitsigns.diffthis('~') end) + map('n', 'td', gitsigns.toggle_deleted) + + -- Text object + map({ 'o', 'x' }, 'ih', ':Gitsigns select_hunk') + end + } +end + +return { + setup = setup +} diff --git a/configs/nvim/lua/crupest/nvim/plugins/init.lua b/configs/nvim/lua/crupest/nvim/plugins/init.lua new file mode 100644 index 0000000..637c8e5 --- /dev/null +++ b/configs/nvim/lua/crupest/nvim/plugins/init.lua @@ -0,0 +1,11 @@ +local function setup() + require("crupest.nvim.plugins.lint").setup() + require("crupest.nvim.plugins.snip").setup() + require("crupest.nvim.plugins.cmp").setup() + require("crupest.nvim.plugins.telescope").setup() + require("crupest.nvim.plugins.gitsign").setup() +end + +return { + setup = setup +} diff --git a/configs/nvim/lua/crupest/nvim/plugins/lint.lua b/configs/nvim/lua/crupest/nvim/plugins/lint.lua index 2ba3e36..6649e74 100644 --- a/configs/nvim/lua/crupest/nvim/plugins/lint.lua +++ b/configs/nvim/lua/crupest/nvim/plugins/lint.lua @@ -1,23 +1,30 @@ local lint = require("lint") -local find = require('crupest.system.find') -local constants = require("crupest.constants") +local find = require('crupest.utils.find') +local is_win = vim.fn.has("win32") ~= 0 +local cspell_config_patterns = { + ".cspell.json", + "cspell.json", + ".cSpell.json", + "cSpell.json", + "cspell.config.js", + "cspell.config.cjs", + "cspell.config.json", + "cspell.config.yaml", + "cspell.config.yml", + "cspell.yaml", + "cspell.yml", +} local my_linters = { { name = "cspell", exe_places = { "npm", "global" }, - config_files = constants.config_patterns.cspell, - }, - { - name = "eslint", - exe_places = { "npm" }, - filetypes = constants.filetype_collections.js_ts, - config_files = constants.config_patterns.nodejs, + config_files = cspell_config_patterns, }, } -local function run_lint(opt) +local function run(opt) if not opt then opt = {} end @@ -44,8 +51,8 @@ local function run_lint(opt) lint.try_lint(linter_names) end -local function setup_lint() - if require('crupest.system').is_win then +local function setup() + if is_win then for _, l in ipairs(my_linters) do local name = l.name local linter = require('lint.linters.' .. name) @@ -59,14 +66,16 @@ local function setup_lint() vim.api.nvim_create_autocmd({ "BufWritePost" }, { callback = function(opt) - run_lint({ + run({ buf = opt.buffer }) end, }) + + vim.keymap.set('n', 'lr', run) end return { - setup_lint = setup_lint, - run_lint = run_lint + setup = setup, + run = run } diff --git a/configs/nvim/lua/crupest/nvim/plugins/others.lua b/configs/nvim/lua/crupest/nvim/plugins/others.lua new file mode 100644 index 0000000..2d728ae --- /dev/null +++ b/configs/nvim/lua/crupest/nvim/plugins/others.lua @@ -0,0 +1,20 @@ +local function setup() + require("neo-tree").setup { + filesystem = { + filtered_items = { + hide_dotfiles = false, + hide_gitignored = false, + hide_hidden = false, -- only works on Windows for hidden files/directories + }, + use_libuv_file_watcher = true + } + } + + require('lualine').setup {} + + require("nvim-autopairs").setup {} +end + +return { + setup = setup +} diff --git a/configs/nvim/lua/crupest/nvim/plugins/snip.lua b/configs/nvim/lua/crupest/nvim/plugins/snip.lua index 1cf1b7c..1cf9800 100644 --- a/configs/nvim/lua/crupest/nvim/plugins/snip.lua +++ b/configs/nvim/lua/crupest/nvim/plugins/snip.lua @@ -26,7 +26,7 @@ local function copy(args) return args[1] end -local function setup_snip() +local function setup() vim.keymap.set({ "i", "s" }, "", function() luasnip.jump(1) end, { silent = true }) vim.keymap.set({ "i", "s" }, "", function() luasnip.jump(-1) end, { silent = true }) @@ -69,6 +69,5 @@ local function setup_snip() end return { - setup_snip = setup_snip, - luasnip = luasnip + setup = setup, } diff --git a/configs/nvim/lua/crupest/nvim/plugins/telescope.lua b/configs/nvim/lua/crupest/nvim/plugins/telescope.lua new file mode 100644 index 0000000..d68b7f2 --- /dev/null +++ b/configs/nvim/lua/crupest/nvim/plugins/telescope.lua @@ -0,0 +1,11 @@ +local function setup() + local builtin = require('telescope.builtin') + vim.keymap.set('n', 'ff', builtin.find_files, {}) + vim.keymap.set('n', 'fg', builtin.live_grep, {}) + vim.keymap.set('n', 'fb', builtin.buffers, {}) + vim.keymap.set('n', 'fh', builtin.help_tags, {}) +end + +return { + setup = setup +} diff --git a/configs/nvim/lua/crupest/system.lua b/configs/nvim/lua/crupest/system.lua deleted file mode 100644 index 982aaa4..0000000 --- a/configs/nvim/lua/crupest/system.lua +++ /dev/null @@ -1,7 +0,0 @@ -local is_win = vim.fn.has("win32") ~= 0 -local is_mac = vim.fn.has("mac") ~= 0 - -return { - is_win = is_win, - is_mac = is_mac -} diff --git a/configs/nvim/lua/crupest/system/find.lua b/configs/nvim/lua/crupest/system/find.lua deleted file mode 100644 index 0876622..0000000 --- a/configs/nvim/lua/crupest/system/find.lua +++ /dev/null @@ -1,153 +0,0 @@ -local system = require("crupest.system") -local fs = require("crupest.system.fs"); - -local win_exe_exts = { "exe", "CMD", "cmd", "ps1" } - - -local function get_exe(path) - if system.is_win then - for _, ext in ipairs(win_exe_exts) do - if string.find(path, "%." .. ext .. "$") and fs.isfile(path) then - return path - end - end - for _, ext in ipairs(win_exe_exts) do - local p = path .. "." .. ext - if fs.isfile(p) then return p end - end - return nil - end - - if vim.fn.executable(path) ~= 0 then - return path - end - - return nil -end - -local function find_global_exe(name) - if vim.fn.executable(name) ~= 0 then - return name - end - - return nil -end - -local function first_exe(paths) - for _, v in ipairs(paths) do - local exe = get_exe(v) - if exe then return exe end - end - - return nil -end - -local function find_file_or_directory(path, name) - return fs.walk_up(path, function(current_path) - local p = current_path .. "/" .. name - if fs.isdir(p) then - return p, "directory" - elseif fs.isfile(p) then - return p, "file" - end - return nil - end) -end - -local function find_file(path, name) - return fs.walk_up(path, function(current_path) - local p = current_path .. "/" .. name - if fs.isfile(p) then - return p - end - return nil - end) -end - -local function find_files_or_directories(path, names) - for _, name in ipairs(names) do - local p, type = find_file_or_directory(path, name) - if p then return p, type end - end - return nil -end - -local function find_files(path, names) - for _, name in ipairs(names) do - local p = find_file(path, name) - if p then return p end - end - return nil -end - -local function find_node_modules(path) - return fs.walk_up(path, function(current_path) - local node_modules_path = current_path .. "/node_modules" - if fs.isdir(node_modules_path) 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 exe_path end - return nil -end - -local function find_exe(path, exe, places) - for _, place in ipairs(places) do - if place == "npm" then - local r = find_npm_exe(path, exe) - if r then return r end - end - if place == "global" then - local r = find_global_exe(exe) - if r then return r end - end - end - return nil -end - -local function find_exe_for_buf(buf, opts) - local r = {} - r.name = opts.name - r.file = vim.api.nvim_buf_get_name(buf) - r.filetype = vim.api.nvim_buf_get_option(buf, "filetype") - r.exe_name = opts.name - r.exe_places = opts.exe_places or { "global" } - - if opts.config_files then - r.config_file = find_files(r.file, opts.config_files) - if r.config_file == nil then return nil end - end - - if opts.filetypes then - if not require("crupest.table").includes(opts.filetypes, r.filetype) then - return nil - end - end - - r.exe_path = find_exe(r.file, r.exe_name, r.exe_places) - if r.exe_path == nil then return nil end - - return r -end - -return { - get_exe = get_exe, - find_global_exe = find_global_exe, - first_exe = first_exe, - find_file_or_directory = find_file_or_directory, - find_files_or_directories = find_files_or_directories, - find_file = find_file, - find_files = find_files, - find_node_modules = find_node_modules, - find_npm_exe = find_npm_exe, - find_exe = find_exe, - find_exe_for_buf = find_exe_for_buf, -} diff --git a/configs/nvim/lua/crupest/system/fs.lua b/configs/nvim/lua/crupest/system/fs.lua deleted file mode 100644 index d34c2f6..0000000 --- a/configs/nvim/lua/crupest/system/fs.lua +++ /dev/null @@ -1,101 +0,0 @@ -local function clean_path(path) - if path == "/" then return path end - path = string.gsub(path, "[/\\]+", "/") - if string.sub(path, string.len(path)) == '/' then - path = string.sub(path, 1, string.len(path) - 1) - end - return path -end - -local function full_path(name) - local path = vim.fn.fnamemodify(name, ":p") - return clean_path(path) -end - -local function escape_space(str) - return (string.gsub(str, " ", "\\ ")) -end - -local function path_get_dir(path) - return full_path(vim.fn.fnamemodify(clean_path(path), ":h")) -end - -local function walk_up(path, func) - local current_path = full_path(path) - while true do - local result = func(current_path) - if result ~= nil then - return result - end - local new_path = path_get_dir(current_path) - if new_path == current_path then - break - end - current_path = new_path - end - return nil -end - -local function exist(path) - return vim.uv.fs_stat(path) -end - -local function isfile(path) - local s = vim.uv.fs_stat(path) - if not s then return false end - return s.type == "file" -end - -local function isdir(path) - local s = vim.uv.fs_stat(path) - if not s then return false end - return s.type == "directory" -end - -local function mkdir(dir) - local parents = {} - - walk_up(dir, function(p) - table.insert(parents, 1, p) - end) - - for _, v in ipairs(parents) do - if exist(v) and not isdir(v) then - vim.notify(v .. " is not a dir. Can't make dir " .. dir, vim.log.levels.ERROR) - return - end - if not exist(v) then - vim.notify("Creating dir " .. v) - assert(vim.uv.fs_mkdir(v, 504)) -- mode = 0770 - end - end -end - -local function copy(old, new) - mkdir(path_get_dir(new)) - assert(vim.uv.fs_copyfile(old, new)) -end - -local function remove(path) - assert(vim.uv.fs_unlink(path)) -end - -local function move(old, new) - mkdir(path_get_dir(new)) - assert(vim.uv.fs_rename(old, new)) -end - -return { - clean_path = clean_path, - full_path = full_path, - escape_space = escape_space, - path_get_dir = path_get_dir, - walk_up = walk_up, - exist = exist, - isfile = isfile, - isdir = isdir, - mkdir = mkdir, - copy = copy, - remove = remove, - move = move -} diff --git a/configs/nvim/lua/crupest/table.lua b/configs/nvim/lua/crupest/table.lua deleted file mode 100644 index 22419b0..0000000 --- a/configs/nvim/lua/crupest/table.lua +++ /dev/null @@ -1,42 +0,0 @@ -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 - -local function includes(tbl, element) - for _, v in ipairs(tbl) do - if v == element then return true end - end - return false -end - -local function string_start_with(str, prefix) - return string.find(str, prefix, 0, true) == 1 -end - -return { - remove_element = remove_element, - element_at = element_at, - includes = includes, - string_start_with = string_start_with, -} diff --git a/configs/nvim/lua/crupest/utils/find.lua b/configs/nvim/lua/crupest/utils/find.lua new file mode 100644 index 0000000..83968d2 --- /dev/null +++ b/configs/nvim/lua/crupest/utils/find.lua @@ -0,0 +1,152 @@ +local fs = require("crupest.utils.fs"); +local is_win = vim.fn.has("win32") ~= 0 + +local win_exe_exts = { "exe", "CMD", "cmd", "ps1" } + +local function get_exe(path) + if is_win then + for _, ext in ipairs(win_exe_exts) do + if string.find(path, "%." .. ext .. "$") and fs.isfile(path) then + return path + end + end + for _, ext in ipairs(win_exe_exts) do + local p = path .. "." .. ext + if fs.isfile(p) then return p end + end + return nil + end + + if vim.fn.executable(path) ~= 0 then + return path + end + + return nil +end + +local function find_global_exe(name) + if vim.fn.executable(name) ~= 0 then + return name + end + + return nil +end + +local function first_exe(paths) + for _, v in ipairs(paths) do + local exe = get_exe(v) + if exe then return exe end + end + + return nil +end + +local function find_file_or_directory(path, name) + return fs.walk_up(path, function(current_path) + local p = current_path .. "/" .. name + if fs.isdir(p) then + return p, "directory" + elseif fs.isfile(p) then + return p, "file" + end + return nil + end) +end + +local function find_file(path, name) + return fs.walk_up(path, function(current_path) + local p = current_path .. "/" .. name + if fs.isfile(p) then + return p + end + return nil + end) +end + +local function find_files_or_directories(path, names) + for _, name in ipairs(names) do + local p, type = find_file_or_directory(path, name) + if p then return p, type end + end + return nil +end + +local function find_files(path, names) + for _, name in ipairs(names) do + local p = find_file(path, name) + if p then return p end + end + return nil +end + +local function find_node_modules(path) + return fs.walk_up(path, function(current_path) + local node_modules_path = current_path .. "/node_modules" + if fs.isdir(node_modules_path) 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 exe_path end + return nil +end + +local function find_exe(path, exe, places) + for _, place in ipairs(places) do + if place == "npm" then + local r = find_npm_exe(path, exe) + if r then return r end + end + if place == "global" then + local r = find_global_exe(exe) + if r then return r end + end + end + return nil +end + +local function find_exe_for_buf(buf, opts) + local r = {} + r.name = opts.name + r.file = vim.api.nvim_buf_get_name(buf) + r.filetype = vim.api.nvim_buf_get_option(buf, "filetype") + r.exe_name = opts.name + r.exe_places = opts.exe_places or { "global" } + + if opts.config_files then + r.config_file = find_files(r.file, opts.config_files) + if r.config_file == nil then return nil end + end + + if opts.filetypes then + if not require("crupest.table").includes(opts.filetypes, r.filetype) then + return nil + end + end + + r.exe_path = find_exe(r.file, r.exe_name, r.exe_places) + if r.exe_path == nil then return nil end + + return r +end + +return { + get_exe = get_exe, + find_global_exe = find_global_exe, + first_exe = first_exe, + find_file_or_directory = find_file_or_directory, + find_files_or_directories = find_files_or_directories, + find_file = find_file, + find_files = find_files, + find_node_modules = find_node_modules, + find_npm_exe = find_npm_exe, + find_exe = find_exe, + find_exe_for_buf = find_exe_for_buf, +} diff --git a/configs/nvim/lua/crupest/utils/fs.lua b/configs/nvim/lua/crupest/utils/fs.lua new file mode 100644 index 0000000..d34c2f6 --- /dev/null +++ b/configs/nvim/lua/crupest/utils/fs.lua @@ -0,0 +1,101 @@ +local function clean_path(path) + if path == "/" then return path end + path = string.gsub(path, "[/\\]+", "/") + if string.sub(path, string.len(path)) == '/' then + path = string.sub(path, 1, string.len(path) - 1) + end + return path +end + +local function full_path(name) + local path = vim.fn.fnamemodify(name, ":p") + return clean_path(path) +end + +local function escape_space(str) + return (string.gsub(str, " ", "\\ ")) +end + +local function path_get_dir(path) + return full_path(vim.fn.fnamemodify(clean_path(path), ":h")) +end + +local function walk_up(path, func) + local current_path = full_path(path) + while true do + local result = func(current_path) + if result ~= nil then + return result + end + local new_path = path_get_dir(current_path) + if new_path == current_path then + break + end + current_path = new_path + end + return nil +end + +local function exist(path) + return vim.uv.fs_stat(path) +end + +local function isfile(path) + local s = vim.uv.fs_stat(path) + if not s then return false end + return s.type == "file" +end + +local function isdir(path) + local s = vim.uv.fs_stat(path) + if not s then return false end + return s.type == "directory" +end + +local function mkdir(dir) + local parents = {} + + walk_up(dir, function(p) + table.insert(parents, 1, p) + end) + + for _, v in ipairs(parents) do + if exist(v) and not isdir(v) then + vim.notify(v .. " is not a dir. Can't make dir " .. dir, vim.log.levels.ERROR) + return + end + if not exist(v) then + vim.notify("Creating dir " .. v) + assert(vim.uv.fs_mkdir(v, 504)) -- mode = 0770 + end + end +end + +local function copy(old, new) + mkdir(path_get_dir(new)) + assert(vim.uv.fs_copyfile(old, new)) +end + +local function remove(path) + assert(vim.uv.fs_unlink(path)) +end + +local function move(old, new) + mkdir(path_get_dir(new)) + assert(vim.uv.fs_rename(old, new)) +end + +return { + clean_path = clean_path, + full_path = full_path, + escape_space = escape_space, + path_get_dir = path_get_dir, + walk_up = walk_up, + exist = exist, + isfile = isfile, + isdir = isdir, + mkdir = mkdir, + copy = copy, + remove = remove, + move = move +} diff --git a/configs/nvim/lua/crupest/utils/nvim.lua b/configs/nvim/lua/crupest/utils/nvim.lua new file mode 100644 index 0000000..ac732fd --- /dev/null +++ b/configs/nvim/lua/crupest/utils/nvim.lua @@ -0,0 +1,44 @@ +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 + +-- 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 = {} + 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 buf_is_normal(buf) + return vim.fn.bufexists(buf) ~= 0 and vim.fn.buflisted(buf) ~= 0 +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, + buf_is_normal = buf_is_normal, + list_wins_editing_buf = list_wins_editing_buf, + close_float = close_float, +} + diff --git a/configs/nvim/lua/crupest/utils/table.lua b/configs/nvim/lua/crupest/utils/table.lua new file mode 100644 index 0000000..22419b0 --- /dev/null +++ b/configs/nvim/lua/crupest/utils/table.lua @@ -0,0 +1,42 @@ +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 + +local function includes(tbl, element) + for _, v in ipairs(tbl) do + if v == element then return true end + end + return false +end + +local function string_start_with(str, prefix) + return string.find(str, prefix, 0, true) == 1 +end + +return { + remove_element = remove_element, + element_at = element_at, + includes = includes, + string_start_with = string_start_with, +} diff --git a/configs/nvim/lua/plugins.lua b/configs/nvim/lua/plugins.lua index 1e4ded9..d7d0247 100644 --- a/configs/nvim/lua/plugins.lua +++ b/configs/nvim/lua/plugins.lua @@ -2,7 +2,6 @@ return { { "catppuccin/nvim", name = "catppuccin", priority = 1000 }, "neovim/nvim-lspconfig", - "Hoffs/omnisharp-extended-lsp.nvim", "hrsh7th/nvim-cmp", "hrsh7th/cmp-nvim-lsp", "hrsh7th/cmp-buffer", @@ -10,12 +9,15 @@ return { "hrsh7th/cmp-cmdline", "L3MON4D3/LuaSnip", "saadparwaiz1/cmp_luasnip", - "nvim-tree/nvim-web-devicons", - "nvim-lua/plenary.nvim", - "nvim-lualine/lualine.nvim", - "nvim-telescope/telescope.nvim", + { + "nvim-lualine/lualine.nvim", + dependencies = { 'nvim-tree/nvim-web-devicons' } + }, + { + "nvim-telescope/telescope.nvim", + dependencies = { 'nvim-lua/plenary.nvim' } + }, "windwp/nvim-autopairs", - "mhartington/formatter.nvim", "mfussenegger/nvim-lint", "akinsho/toggleterm.nvim", "lewis6991/gitsigns.nvim", @@ -23,6 +25,7 @@ return { "nvim-neo-tree/neo-tree.nvim", dependencies = { "nvim-lua/plenary.nvim", + "nvim-tree/nvim-web-devicons", "MunifTanjim/nui.nvim", }, }, -- cgit v1.2.3