diff --git a/nvim/.config/nvim/init.lua b/nvim/.config/nvim/init.lua index edf424c..fbe2daa 100644 --- a/nvim/.config/nvim/init.lua +++ b/nvim/.config/nvim/init.lua @@ -1,16 +1,19 @@ require("set") require("remap") +require("lsp") + +-- Set up lazy local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim" if not vim.loop.fs_stat(lazypath) then - vim.fn.system({ - "git", - "clone", - "--filter=blob:none", - "https://github.com/folke/lazy.nvim.git", - "--branch=stable", -- latest stable release - lazypath, - }) + vim.fn.system({ + "git", + "clone", + "--filter=blob:none", + "https://github.com/folke/lazy.nvim.git", + "--branch=stable", -- latest stable release + lazypath, + }) end vim.opt.rtp:prepend(lazypath) diff --git a/nvim/.config/nvim/lsp/clangd.lua b/nvim/.config/nvim/lsp/clangd.lua new file mode 100644 index 0000000..998ecb5 --- /dev/null +++ b/nvim/.config/nvim/lsp/clangd.lua @@ -0,0 +1,13 @@ +return { + cmd = { + 'clangd' + }, + root_markers = { + '.clangd', + 'compile_commands.json' + }, + filetypes = { + 'c', + 'cpp' + } +} diff --git a/nvim/.config/nvim/lsp/luals.lua b/nvim/.config/nvim/lsp/luals.lua new file mode 100644 index 0000000..4ccfb89 --- /dev/null +++ b/nvim/.config/nvim/lsp/luals.lua @@ -0,0 +1,20 @@ +return { + cmd = { + 'lua-language-server' + }, + filetypes = { + 'lua' + }, + root_markers = { + '.luarc.json', + '.luarc.jsonc', + '.git' + }, + settings = { + Lua = { + runtime = { + version = 'LuaJIT' + } + } + } +} diff --git a/nvim/.config/nvim/lsp/pyright.lua b/nvim/.config/nvim/lsp/pyright.lua new file mode 100644 index 0000000..c77078f --- /dev/null +++ b/nvim/.config/nvim/lsp/pyright.lua @@ -0,0 +1,20 @@ +return { + cmd = { + 'pyright-langserver', '--stdio' + }, + settings = { + python = { + analysis = { + typeCheckingMode = "basic", -- or "strict", "off" + autoSearchPaths = true, + useLibraryCodeForTypes = true, + }, + }, + }, + root_markers = { + 'pyproject.toml', 'setup.py', 'setup.cfg', 'requirements.txt', '.git' + }, + filetypes = { + 'python' + }, +} diff --git a/nvim/.config/nvim/lua/lsp.lua b/nvim/.config/nvim/lua/lsp.lua new file mode 100644 index 0000000..c0a7aa7 --- /dev/null +++ b/nvim/.config/nvim/lua/lsp.lua @@ -0,0 +1,56 @@ +-- Set up LSP + +local lsp_configs = {} + +for _, f in pairs(vim.api.nvim_get_runtime_file('lsp/*.lua', true)) do + local server_name = vim.fn.fnamemodify(f, ':t:r') + table.insert(lsp_configs, server_name) +end + +vim.lsp.enable(lsp_configs) + + +vim.o.completeopt = "menu,menuone,noinsert" +vim.api.nvim_set_keymap("i", "", "", { noremap = true }) + + +-- TODO: Put this somewhere sensible +vim.api.nvim_create_autocmd('LspAttach', { + group = vim.api.nvim_create_augroup('my.lsp', {}), + callback = function(args) + local client = assert(vim.lsp.get_client_by_id(args.data.client_id)) + + -- Enable auto-completion. Note: Use CTRL-Y to select an item. |complete_CTRL-Y| + if client:supports_method('textDocument/completion') then + -- Trigger autocompletion on EVERY keypress. May be slow! + local chars = {}; for i = 32, 126 do table.insert(chars, string.char(i)) end + client.server_capabilities.completionProvider.triggerCharacters = chars + + vim.lsp.completion.enable(true, client.id, args.buf, { autotrigger = true }) + end + + vim.keymap.set('n', '', + function() vim.lsp.buf.format({ bufnr = args.buf, id = client.id, timeout_ms = 1000 }) end, + { desc = "Format" }) + vim.keymap.set('i', '', + function() vim.lsp.buf.format({ bufnr = args.buf, id = client.id, timeout_ms = 1000 }) end, + { desc = "Format" }) + + -- if client:supports_method('textDocument/implementation') then + -- -- Create a keymap for vim.lsp.buf.implementation ... + -- end + + -- -- Auto-format ("lint") on save. + -- -- Usually not needed if server supports "textDocument/willSaveWaitUntil". + -- if not client:supports_method('textDocument/willSaveWaitUntil') + -- and client:supports_method('textDocument/formatting') then + -- vim.api.nvim_create_autocmd('BufWritePre', { + -- group = vim.api.nvim_create_augroup('my.lsp', { clear = false }), + -- buffer = args.buf, + -- callback = function() + -- vim.lsp.buf.format({ bufnr = args.buf, id = client.id, timeout_ms = 1000 }) + -- end, + -- }) + -- end + end, +}) diff --git a/nvim/.config/nvim/lua/plugins/lsp.lua b/nvim/.config/nvim/lua/plugins/lsp.lua deleted file mode 100644 index 76556d2..0000000 --- a/nvim/.config/nvim/lua/plugins/lsp.lua +++ /dev/null @@ -1,193 +0,0 @@ -return { - { - { - 'VonHeikemen/lsp-zero.nvim', - branch = 'v3.x', - lazy = true, - config = false, - init = function() - -- Disable automatic setup, we are doing it manually - vim.g.lsp_zero_extend_cmp = 0 - vim.g.lsp_zero_extend_lspconfig = 0 - end - }, - { - 'williamboman/mason.nvim', - lazy = false, - opts = { - registries = { - "github:antsouchlos/mason-registry", - "github:mason-org/mason-registry" - } - } - }, - - -- Autocompletion - { - 'hrsh7th/nvim-cmp', - event = 'InsertEnter', - dependencies = { - 'L3MON4D3/LuaSnip', - 'hrsh7th/cmp-buffer', - 'hrsh7th/cmp-path', - 'hrsh7th/cmp-cmdline', - 'hrsh7th/cmp-nvim-lsp-signature-help', - 'saadparwaiz1/cmp_luasnip' - }, - config = function() - local lsp_zero = require('lsp-zero') - lsp_zero.extend_cmp() - - local cmp = require('cmp') - - cmp.setup({ - sources = { - -- LuaFormatter off - { name = 'path' }, - { name = 'nvim_lsp' }, - { name = 'nvim_lua' }, - { name = 'luasnip', keyword_length = 2 }, - { name = 'buffer', keyword_length = 3 }, - { name = 'nvim_lsp_signature_help' } - -- LuaFormatter on - }, - formatting = lsp_zero.cmp_format(), - mapping = cmp.mapping.preset.insert({ - [''] = cmp.mapping.select_prev_item(cmp_select), - [''] = cmp.mapping.select_next_item(cmp_select), - [''] = cmp.mapping.confirm({ - select = true - }), - [''] = cmp.mapping.complete() - }), - snippet = { - expand = function(args) require('luasnip').lsp_expand(args.body) end - } - }) - - cmp.setup.filetype('gitcommit', { - -- LuaFormatter off - sources = cmp.config.sources( - { { name = 'git' } }, - { { name = 'buffer' } } - ) - -- LuaFormatter on - }) - - cmp.setup.cmdline({ - '/', - '?' - }, { - mapping = cmp.mapping.preset.cmdline(), - sources = { - { - name = 'buffer' - } - } - }) - - cmp.setup.cmdline(':', { - mapping = cmp.mapping.preset.cmdline(), - sources = cmp.config.sources({ - { - name = 'path' - } - }, { - { - name = 'cmdline' - } - }) - }) - end - }, - - -- LSP - { - 'neovim/nvim-lspconfig', - cmd = { - 'LspInfo', - 'LspInstall', - 'LspStart' - }, - event = { - 'BufReadPre', - 'BufNewFile' - }, - dependencies = { - 'hrsh7th/cmp-nvim-lsp', - 'williamboman/mason-lspconfig.nvim', - "folke/trouble.nvim" - }, - config = function() - local lsp_zero = require('lsp-zero') - lsp_zero.extend_lspconfig() - - lsp_zero.on_attach(function(client, bufnr) - vim.keymap.set("n", "ld", function() vim.lsp.buf.definition() end, { - desc = "Go to definition" - }) - vim.keymap.set("n", "lh", function() vim.lsp.buf.hover() end, { - desc = "Hover" - }) - vim.keymap.set("n", "lr", ":Trouble lsp_references", { - desc = "Show references" - }) - vim.keymap.set("n", "ln", function() vim.lsp.buf.rename() end, { - desc = "Rename" - }) - vim.keymap.set("n", "ls", function() vim.lsp.buf.signature_help() end, { - desc = "Signature help" - }) - vim.keymap.set("n", "la", function() vim.lsp.buf.code_action() end, { - desc = "Code actions" - }) - end) - - require('mason-lspconfig').setup({ - ensure_installed = { - 'clangd', - 'pyright', - 'cmake', - 'texlab' - }, - handlers = { - lsp_zero.default_setup, - lua_ls = function() - local lua_opts = lsp_zero.nvim_lua_ls() - require('lspconfig').lua_ls.setup(lua_opts) - end - } - }) - - require('lspconfig')['clangd'].setup { - cmd = { - "clangd", - "--background-index", - "--clang-tidy", - "--completion-style=bundled", - "--cross-file-rename", - "--header-insertion=iwyu", - -- Required for embedded system compilers - "--query-driver=/**/*g++", - "--offset-encoding=utf-16" - -- "-j=8", - -- "--malloc-trim", - -- "--pch-storage=memory" - } - } - - -- require('lspconfig').matlab_ls.setup({ - -- settings = { - -- filetypes = { - -- "matlab" - -- }, - -- matlab = { - -- installPath = "/opt/matlab/R2023a/" - -- } - -- }, - -- single_file_support = true - -- }) - end - } - } -} diff --git a/nvim/.config/nvim/lua/plugins/mason.lua b/nvim/.config/nvim/lua/plugins/mason.lua new file mode 100644 index 0000000..44313d6 --- /dev/null +++ b/nvim/.config/nvim/lua/plugins/mason.lua @@ -0,0 +1,10 @@ +return { + 'williamboman/mason.nvim', + lazy = false, + opts = { + registries = { + "github:antsouchlos/mason-registry", + "github:mason-org/mason-registry" + } + } +} diff --git a/nvim/.config/nvim/lua/plugins/neoformat.lua b/nvim/.config/nvim/lua/plugins/neoformat.lua deleted file mode 100644 index f8cb95f..0000000 --- a/nvim/.config/nvim/lua/plugins/neoformat.lua +++ /dev/null @@ -1,124 +0,0 @@ -return { - { - 'sbdchd/neoformat', - event = { - "BufReadPost", - "BufNewFile" - }, - init = function() - ---------------- - -- Formatters - ---------------- - - vim.api.nvim_create_autocmd("FileType", { - pattern = "python", - callback = function() - vim.g.neoformat_python_autopep8 = { - exe = 'autopep8', - args = {'--max-line-length', '79', '--experimental'}, - -- replace = 1 - } - vim.g.neoformat_enabled_python = { - 'autopep8' - } - end - }) - - vim.api.nvim_create_autocmd("FileType", { - pattern = { - "cpp", - "c" - }, - callback = function() - vim.g.neoformat_enabled_cpp = { - 'clangformat' - } - vim.g.neoformat_enabled_c = { - 'clangformat' - } - end - }) - - vim.api.nvim_create_autocmd("FileType", { - pattern = "lua", - callback = function() - vim.g.neoformat_enable_lua = { - 'luaformatter' - } - end - }) - - vim.api.nvim_create_autocmd("FileType", { - pattern = "cmake", - callback = function() - vim.g.neoformat_enabled_cmake = { - 'cmake-format' - } - end - }) - - -- Commented out rust formatter - -- vim.api.nvim_create_autocmd("FileType", { - -- pattern = "rust", - -- callback = function() - -- vim.g.neoformat_enabled_rust = {'rustfmt'} - -- end - -- }) - - vim.api.nvim_create_autocmd("FileType", { - pattern = "markdown", - callback = function() - vim.g.neoformat_markdown_mdformat = { - exe = 'mdformat', - args = { - '--wrap=79', - '--number' - }, - replace = 1 - } - vim.g.neoformat_enabled_markdown = { - 'mdformat' - } - end - }) - - vim.api.nvim_create_autocmd("FileType", { - pattern = "tex", - callback = function() - vim.g.neoformat_tex_texfmt = { - exe = "tex-fmt", - args = { - "--stdin", - "--tabsize", - "4" - }, - stdin = 1 - } - vim.g.neoformat_enabled_tex = { - "texfmt" - } - end - }) - - ---------------- - -- Other - ---------------- - - -- vim.cmd([[ - -- augroup Neoformat - -- autocmd! - -- autocmd BufWritePre * undojoin | Neoformat - -- augroup END - -- ]]) - - vim.api.nvim_set_keymap('n', '', 'Neoformat', { - noremap = true, - silent = true - }) - vim.api.nvim_set_keymap('i', '', 'Neoformat', { - noremap = true, - silent = true - }) - end - } -}