diff options
Diffstat (limited to 'configs/nvim/config-root/lua')
16 files changed, 752 insertions, 0 deletions
diff --git a/configs/nvim/config-root/lua/crupest/nvim/keymap.lua b/configs/nvim/config-root/lua/crupest/nvim/keymap.lua new file mode 100644 index 0000000..624c04c --- /dev/null +++ b/configs/nvim/config-root/lua/crupest/nvim/keymap.lua @@ -0,0 +1,9 @@ +local function setup() + vim.keymap.set("n", "<c-tab>", "<cmd>bnext<cr>") + vim.keymap.set("n", "<c-s-tab>", "<cmd>bNext<cr>") + vim.keymap.set("n", "<esc>", require("crupest.utils.nvim").close_float) +end + +return { + setup = setup +} diff --git a/configs/nvim/config-root/lua/crupest/nvim/lsp/c.lua b/configs/nvim/config-root/lua/crupest/nvim/lsp/c.lua new file mode 100644 index 0000000..e0466b1 --- /dev/null +++ b/configs/nvim/config-root/lua/crupest/nvim/lsp/c.lua @@ -0,0 +1,29 @@ +local lspconfig = require("lspconfig") +local capabilities = require("cmp_nvim_lsp").default_capabilities() + +local get_exe = require("crupest.utils.find").get_exe + +local brew_clangd_path = "/usr/local/opt/llvm/bin/clangd" + +local function setup() + local clangd = "clangd" + + if get_exe(brew_clangd_path) then + clangd = brew_clangd_path + end + + -- setup lsp clangd + lspconfig.clangd.setup { + cmd = { clangd }, + capabilities = capabilities, + on_attach = function(_, bufnr) + vim.keymap.set('n', '<space>s', "<cmd>ClangdSwitchSourceHeader<cr>", { + buffer = bufnr + }) + end + } +end + +return { + setup = setup +} diff --git a/configs/nvim/config-root/lua/crupest/nvim/lsp/init.lua b/configs/nvim/config-root/lua/crupest/nvim/lsp/init.lua new file mode 100644 index 0000000..464d7ef --- /dev/null +++ b/configs/nvim/config-root/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', '<C-k>', vim.lsp.buf.signature_help, opts) + vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, opts) + vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, opts) + vim.keymap.set('n', '<space>wl', function() + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + end, opts) + vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, opts) + vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts) + vim.keymap.set({ 'n', 'v' }, '<space>ca', vim.lsp.buf.code_action, opts) + vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts) + vim.keymap.set('n', '<space>f', vim.lsp.buf.format, opts) + end, + }) +end + + +return { + setup = setup +} diff --git a/configs/nvim/config-root/lua/crupest/nvim/lsp/lua.lua b/configs/nvim/config-root/lua/crupest/nvim/lsp/lua.lua new file mode 100644 index 0000000..1785515 --- /dev/null +++ b/configs/nvim/config-root/lua/crupest/nvim/lsp/lua.lua @@ -0,0 +1,31 @@ +local lspconfig = require("lspconfig") +local capabilities = require("cmp_nvim_lsp").default_capabilities() + +local function setup() + lspconfig.lua_ls.setup { + capabilities = capabilities, + settings = { + Lua = { + runtime = { + version = "LuaJIT" + }, + diagnostics = { + globals = { "vim" }, + }, + workspace = { + library = { + [vim.fn.expand "$VIMRUNTIME/lua"] = true, + [vim.fn.expand "$VIMRUNTIME/lua/vim/lsp"] = true, + [vim.fn.stdpath "data" .. "/lazy/lazy.nvim/lua/lazy"] = true, + }, + maxPreload = 100000, + preloadFileSize = 10000, + }, + }, + }, + } +end + +return { + setup = setup +} diff --git a/configs/nvim/config-root/lua/crupest/nvim/plugins/cmp.lua b/configs/nvim/config-root/lua/crupest/nvim/plugins/cmp.lua new file mode 100644 index 0000000..9b1d876 --- /dev/null +++ b/configs/nvim/config-root/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({ + ['<C-b>'] = cmp.mapping.scroll_docs(-4), + ['<C-f>'] = cmp.mapping.scroll_docs(4), + ['<C-Space>'] = cmp.mapping.complete(), + ['<C-e>'] = cmp.mapping.abort(), + ['<CR>'] = 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/config-root/lua/crupest/nvim/plugins/gitsign.lua b/configs/nvim/config-root/lua/crupest/nvim/plugins/gitsign.lua new file mode 100644 index 0000000..220c91a --- /dev/null +++ b/configs/nvim/config-root/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', '<leader>hs', gitsigns.stage_hunk) + map('n', '<leader>hr', gitsigns.reset_hunk) + map('v', '<leader>hs', function() gitsigns.stage_hunk { vim.fn.line('.'), vim.fn.line('v') } end) + map('v', '<leader>hr', function() gitsigns.reset_hunk { vim.fn.line('.'), vim.fn.line('v') } end) + map('n', '<leader>hS', gitsigns.stage_buffer) + map('n', '<leader>hu', gitsigns.undo_stage_hunk) + map('n', '<leader>hR', gitsigns.reset_buffer) + map('n', '<leader>hp', gitsigns.preview_hunk) + map('n', '<leader>hb', function() gitsigns.blame_line { full = true } end) + map('n', '<leader>tb', gitsigns.toggle_current_line_blame) + map('n', '<leader>hd', gitsigns.diffthis) + map('n', '<leader>hD', function() gitsigns.diffthis('~') end) + map('n', '<leader>td', gitsigns.toggle_deleted) + + -- Text object + map({ 'o', 'x' }, 'ih', ':<C-U>Gitsigns select_hunk<CR>') + end + } +end + +return { + setup = setup +} diff --git a/configs/nvim/config-root/lua/crupest/nvim/plugins/init.lua b/configs/nvim/config-root/lua/crupest/nvim/plugins/init.lua new file mode 100644 index 0000000..637c8e5 --- /dev/null +++ b/configs/nvim/config-root/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/config-root/lua/crupest/nvim/plugins/lint.lua b/configs/nvim/config-root/lua/crupest/nvim/plugins/lint.lua new file mode 100644 index 0000000..6649e74 --- /dev/null +++ b/configs/nvim/config-root/lua/crupest/nvim/plugins/lint.lua @@ -0,0 +1,81 @@ +local lint = require("lint") +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 = cspell_config_patterns, + }, +} + +local function run(opt) + if not opt then + opt = {} + end + + if not opt.buf then + opt.buf = 0 + end + + local linters = {} + + for _, l in ipairs(my_linters) do + local linter = find.find_exe_for_buf(opt.buf, l) + if linter then table.insert(linters, linter) end + end + + + local linter_names = {} + + for _, linter in ipairs(linters) do + table.insert(linter_names, linter.name) + require('lint.linters.' .. linter.name).cmd = linter.exe_path + end + + lint.try_lint(linter_names) +end + +local function setup() + if is_win then + for _, l in ipairs(my_linters) do + local name = l.name + local linter = require('lint.linters.' .. name) + if linter.cmd == 'cmd.exe' then + linter.cmd = linter.args[2] + end + table.remove(linter.args, 1) + table.remove(linter.args, 1) + end + end + + vim.api.nvim_create_autocmd({ "BufWritePost" }, { + callback = function(opt) + run({ + buf = opt.buffer + }) + end, + }) + + vim.keymap.set('n', '<leader>lr', run) +end + +return { + setup = setup, + run = run +} diff --git a/configs/nvim/config-root/lua/crupest/nvim/plugins/others.lua b/configs/nvim/config-root/lua/crupest/nvim/plugins/others.lua new file mode 100644 index 0000000..2d728ae --- /dev/null +++ b/configs/nvim/config-root/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/config-root/lua/crupest/nvim/plugins/snip.lua b/configs/nvim/config-root/lua/crupest/nvim/plugins/snip.lua new file mode 100644 index 0000000..1cf9800 --- /dev/null +++ b/configs/nvim/config-root/lua/crupest/nvim/plugins/snip.lua @@ -0,0 +1,73 @@ +local luasnip = require("luasnip") + +local ls = luasnip +-- some shorthands... +local s = ls.snippet +local sn = ls.snippet_node +local t = ls.text_node +local i = ls.insert_node +local f = ls.function_node +local c = ls.choice_node +local d = ls.dynamic_node +local r = ls.restore_node +local l = require("luasnip.extras").lambda +local rep = require("luasnip.extras").rep +local p = require("luasnip.extras").partial +local m = require("luasnip.extras").match +local n = require("luasnip.extras").nonempty +local dl = require("luasnip.extras").dynamic_lambda +local fmt = require("luasnip.extras.fmt").fmt +local fmta = require("luasnip.extras.fmt").fmta +local types = require("luasnip.util.types") +local conds = require("luasnip.extras.conditions") +local conds_expand = require("luasnip.extras.conditions.expand") + +local function copy(args) + return args[1] +end + +local function setup() + vim.keymap.set({ "i", "s" }, "<C-L>", function() luasnip.jump(1) end, { silent = true }) + vim.keymap.set({ "i", "s" }, "<C-J>", function() luasnip.jump(-1) end, { silent = true }) + + vim.keymap.set({ "i", "s" }, "<C-E>", function() + if luasnip.choice_active() then + luasnip.change_choice(1) + end + end, { silent = true }) + + luasnip.add_snippets("cpp", { + s("cs", { + i(1, "classname"), + t("::"), + f(copy, 1), + t("("), + i(0), + t(") { }") + }), + + s("ds", { + i(1, "classname"), + t("::~"), + f(copy, 1), + t("() { }") + }), + + s("csds", { + i(1, "classname"), + t("::"), + f(copy, 1), + t("("), + i(0), + t({ ") { }", "", "" }), + f(copy, 1), + t("::~"), + f(copy, 1), + t("() { }") + }), + }) +end + +return { + setup = setup, +} diff --git a/configs/nvim/config-root/lua/crupest/nvim/plugins/telescope.lua b/configs/nvim/config-root/lua/crupest/nvim/plugins/telescope.lua new file mode 100644 index 0000000..d68b7f2 --- /dev/null +++ b/configs/nvim/config-root/lua/crupest/nvim/plugins/telescope.lua @@ -0,0 +1,11 @@ +local function setup() + local builtin = require('telescope.builtin') + vim.keymap.set('n', '<leader>ff', builtin.find_files, {}) + vim.keymap.set('n', '<leader>fg', builtin.live_grep, {}) + vim.keymap.set('n', '<leader>fb', builtin.buffers, {}) + vim.keymap.set('n', '<leader>fh', builtin.help_tags, {}) +end + +return { + setup = setup +} diff --git a/configs/nvim/config-root/lua/crupest/utils/find.lua b/configs/nvim/config-root/lua/crupest/utils/find.lua new file mode 100644 index 0000000..83968d2 --- /dev/null +++ b/configs/nvim/config-root/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/config-root/lua/crupest/utils/fs.lua b/configs/nvim/config-root/lua/crupest/utils/fs.lua new file mode 100644 index 0000000..d34c2f6 --- /dev/null +++ b/configs/nvim/config-root/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/config-root/lua/crupest/utils/nvim.lua b/configs/nvim/config-root/lua/crupest/utils/nvim.lua new file mode 100644 index 0000000..ac732fd --- /dev/null +++ b/configs/nvim/config-root/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/config-root/lua/crupest/utils/table.lua b/configs/nvim/config-root/lua/crupest/utils/table.lua new file mode 100644 index 0000000..22419b0 --- /dev/null +++ b/configs/nvim/config-root/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/config-root/lua/plugins.lua b/configs/nvim/config-root/lua/plugins.lua new file mode 100644 index 0000000..d7d0247 --- /dev/null +++ b/configs/nvim/config-root/lua/plugins.lua @@ -0,0 +1,32 @@ +-- spellchecker: disable +return { + { "catppuccin/nvim", name = "catppuccin", priority = 1000 }, + "neovim/nvim-lspconfig", + "hrsh7th/nvim-cmp", + "hrsh7th/cmp-nvim-lsp", + "hrsh7th/cmp-buffer", + "hrsh7th/cmp-path", + "hrsh7th/cmp-cmdline", + "L3MON4D3/LuaSnip", + "saadparwaiz1/cmp_luasnip", + { + "nvim-lualine/lualine.nvim", + dependencies = { 'nvim-tree/nvim-web-devicons' } + }, + { + "nvim-telescope/telescope.nvim", + dependencies = { 'nvim-lua/plenary.nvim' } + }, + "windwp/nvim-autopairs", + "mfussenegger/nvim-lint", + "akinsho/toggleterm.nvim", + "lewis6991/gitsigns.nvim", + { + "nvim-neo-tree/neo-tree.nvim", + dependencies = { + "nvim-lua/plenary.nvim", + "nvim-tree/nvim-web-devicons", + "MunifTanjim/nui.nvim", + }, + }, +} |