LSP finally working a decent amount

This commit is contained in:
RingOfStorms (Joshua Bell) 2023-06-30 17:53:02 -05:00
parent 518951ec2f
commit 9c884fb841
15 changed files with 489 additions and 112 deletions

View file

@ -1,6 +1,7 @@
-- Remap space as leader key
vim.keymap.set("", "<Space>", "<Nop>", { silent = true })
vim.g.mapleader = " "
vim.g.maplocalleader = " "
-- Modes
-- normal_mode = "n",
@ -32,15 +33,11 @@ require('util').keymaps({
["<C-j>"] = { "<C-w>j", desc = "Move window below current" },
["<C-k>"] = { "<C-w>k", desc = "Move window above current" },
["<C-l>"] = { "<C-w>l", desc = "Move window right current" },
-- reformat outside of LSP attachment
["<leader>lf"] = { function() vim.lsp.buf.format() end, desc = "Reformat file" }
-- ["<leader>,j"] = { name = " Jest Tests" },
-- ["<leader>,jr"] = { function() require("jester").run() end, desc = "Run test under cursor" },
-- ["<leader>,jf"] = { function() require("jester").run_file() end, desc = "Run tests for file" },
-- ["<leader>,jl"] = { function() require("jester").run_last() end, desc = "Run last ran test" },
-- ["<leader>lz"] = { ":LspRestart<CR>", desc = "Restart LSP Server" },
-- reformat LSP
["<leader>lf"] = { function() vim.lsp.buf.format() end, desc = "Reformat file" },
["<leader>ld"] = { function() vim.diagnostic.open_float() end, desc = "Show diagnostic message"},
["<leader>ll"] = { function() vim.diagnostic.setloclist() end, desc = "Show diagnostic list"},
["<leader>lz"] = { ":LspRestart<CR>", desc = "Restart LSP Server" },
},
v = {
["y"] = { '"*y', desc = "Copy to system clipboard" },

View file

@ -2,7 +2,7 @@
-- vim.opt.clipboard = "unnamedplus"
-- allow use of mouse
vim.opt.mouse = 'a'
vim.opt.mouse = "a"
-- line numbering, relative
vim.opt.number = true
@ -22,15 +22,15 @@ vim.opt.breakindent = true
vim.opt.smartindent = true
-- set tab length
vim.opt.tabstop = 2;
vim.opt.softtabstop = 2;
vim.opt.shiftwidth = 2;
vim.opt.tabstop = 2
vim.opt.softtabstop = 2
vim.opt.shiftwidth = 2
vim.opt.expandtab = true
-- Dont use swap files, use undotree
vim.opt.swapfile = false
vim.opt.backup = false
vim.opt.undodir = os.getenv('HOME') .. '/.vim/undodir'
vim.opt.undodir = os.getenv("HOME") .. "/.vim/undodir"
vim.opt.undofile = true
-- Search settings
@ -41,3 +41,8 @@ vim.opt.incsearch = true
vim.opt.splitbelow = true
vim.opt.splitright = true
-- Set completeopt to have a better completion experience
vim.o.completeopt = "menuone,noselect"
vim.diagnostic.config{
float={border="single"}
}

10
lua/plugins/comments.lua Normal file
View file

@ -0,0 +1,10 @@
vim.g.NERDCreateDefaultMappings = 0
vim.cmd('filetype plugin on')
return {
"preservim/nerdcommenter",
keys = {
{ "<leader>/", ':call nerdcommenter#Comment(0, "toggle")<CR>', desc = "Toggle comments on selection" },
{ "<leader>/", ':call nerdcommenter#Comment(0, "toggle")<CR>', desc = "Toggle comments on selection", mode = "v" },
},
}

View file

@ -0,0 +1,30 @@
vim.cmd([[highlight IndentBlanklineIndent1 guifg=#E06C75 gui=nocombine]])
vim.cmd([[highlight IndentBlanklineIndent2 guifg=#E5C07B gui=nocombine]])
vim.cmd([[highlight IndentBlanklineIndent3 guifg=#98C379 gui=nocombine]])
vim.cmd([[highlight IndentBlanklineIndent4 guifg=#61AFEF gui=nocombine]])
vim.cmd([[highlight IndentBlanklineIndent5 guifg=#C678DD gui=nocombine]])
vim.opt.list = true
-- vim.opt.listchars:append "space:⋅"
vim.opt.listchars:append("eol:↴")
return {
-- Add indentation guides even on blank lines
"lukas-reineke/indent-blankline.nvim",
-- Enable `lukas-reineke/indent-blankline.nvim`
-- See `:help indent_blankline.txt`
opts = {
-- space_char_blankline = " ",
-- char = '┊',
-- show_trailing_blankline_indent = false,
-- show_current_context = false,
show_current_context_start = true,
char_highlight_list = {
"IndentBlanklineIndent1",
"IndentBlanklineIndent2",
"IndentBlanklineIndent3",
"IndentBlanklineIndent4",
"IndentBlanklineIndent5",
},
},
}

206
lua/plugins/lsp.lua Normal file
View file

@ -0,0 +1,206 @@
local servers = {
rust_analyzer = {
-- rust
-- to enable rust-analyzer settings visit:
-- https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/generated_config.adoc
["rust-analyzer"] = {
cargo = {
allFeatures = true,
},
checkOnSave = {
allFeatures = true,
command = "clippy",
},
},
},
tsserver = {
-- typescript/javascript
},
pyright = {
-- python
},
lua_ls = {
-- lua
Lua = {
workspace = { checkThirdParty = false },
telemetry = { enable = false },
},
},
bashls = {
-- bash
},
cssls = {
-- css
},
cssmodules_ls = {
-- css modules
},
dockerls = {
-- docker
},
docker_compose_language_service = {
-- docker compose
},
jsonls = {
-- json
},
marksman = {
-- markdown
},
taplo = {
-- toml
},
yamlls = {
-- yaml
},
lemminx = {
-- xml
},
rnix = {
-- Nix
},
ansiblels = {
-- ansible
},
}
-- LSP config
-- Took lots of inspiration from this kickstart lua file: https://github.com/hjr3/dotfiles/blob/main/.config/nvim/init.lua
return {
{
-- Autocompletion
"hrsh7th/nvim-cmp",
dependencies = { "hrsh7th/cmp-nvim-lsp", "L3MON4D3/LuaSnip", "saadparwaiz1/cmp_luasnip" },
},
{
"williamboman/mason.nvim",
cmd = {
"Mason",
"MasonUpdate",
"MasonInstall",
"MasonInstallAll",
"MasonUninstall",
"MasonUninstallAll",
"MasonLog",
},
build = ":MasonUpdate",
opts = {},
},
{
"williamboman/mason-lspconfig.nvim",
},
{ "folke/neodev.nvim", opts = {} },
{
"neovim/nvim-lspconfig",
after = { "nvim-telescope/telescope.nvim" },
config = function()
local config = require("lspconfig")
local util = require("lspconfig/util")
local mason_lspconfig = require("mason-lspconfig")
local cmp = require("cmp")
local luasnip = require("luasnip")
-- LSP
-- This function gets run when an LSP connects to a particular buffer.
local on_attach = function(client, bufnr)
local nmap = function(keys, func, desc)
if desc then
desc = "LSP: " .. desc
end
vim.keymap.set("n", keys, func, { buffer = bufnr, desc = desc })
end
nmap("<leader>lr", vim.lsp.buf.rename, "[R]ename")
nmap("<leader>la", vim.lsp.buf.code_action, "Code [A]ction")
nmap("gd", vim.lsp.buf.definition, "[G]oto [D]efinition")
nmap("gr", require("telescope.builtin").lsp_references, "[G]oto [R]eferences")
nmap("gI", vim.lsp.buf.implementation, "[G]oto [I]mplementation")
nmap("<leader>D", vim.lsp.buf.type_definition, "Type [D]efinition")
-- See `:help K` for why this keymap
nmap("K", vim.lsp.buf.hover, "Hover Documentation")
nmap("<C-k>", vim.lsp.buf.signature_help, "Signature Documentation")
-- Lesser used LSP functionality
nmap("gD", vim.lsp.buf.declaration, "[G]oto [D]eclaration")
-- disable tsserver so it does not conflict with prettier
if client.name == "tsserver" then
client.server_capabilities.document_formatting = false
end
end
-- nvim-cmp supports additional completion capabilities, so broadcast that to servers
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require("cmp_nvim_lsp").default_capabilities(capabilities)
-- Install servers used
mason_lspconfig.setup({
ensure_installed = vim.tbl_keys(servers),
})
local flags = {
allow_incremental_sync = true,
debounce_text_changes = 200,
}
mason_lspconfig.setup_handlers({
function(server_name)
require("lspconfig")[server_name].setup({
flags = flags,
capabilities = capabilities,
on_attach = on_attach,
settings = servers[server_name],
})
end,
})
-- Completion
luasnip.config.setup({})
cmp.setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({
["<C-n>"] = cmp.mapping.select_next_item(),
["<C-p>"] = cmp.mapping.select_prev_item(),
["<C-d>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete({}),
["<CR>"] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = true,
}),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
}),
sources = {
{ name = "nvim_lsp" },
{ name = "luasnip" },
},
})
end,
},
}

View file

@ -1,25 +1,21 @@
local test = function ()
return "test 123"
end
return {
"nvim-lualine/lualine.nvim",
dependencies = {
"RingOfStorms/lualine-lsp-progress",
},
opts = {
options = {
theme = "codedark",
section_separators = { left = '', right = '' },
component_separators = '|',
},
sections = {
lualine_a = {'mode'},
lualine_b = {'branch', 'diff', 'diagnostics'},
lualine_c = {'filename', "lsp_progress", test},
lualine_x = {'encoding', 'filetype', 'filesize'},
lualine_y = {'searchcount', 'selectioncount'},
lualine_z = {'location'},
},
},
"nvim-lualine/lualine.nvim",
--dependencies = {
-- "RingOfStorms/lualine-lsp-progress",
--},
opts = {
options = {
theme = "codedark",
section_separators = { left = "", right = "" },
component_separators = "|",
},
sections = {
lualine_a = { "mode" },
lualine_b = { "branch", "diff", "diagnostics" },
lualine_c = { "filename" },
lualine_x = { "encoding", "filetype", "filesize" },
lualine_y = { "searchcount", "selectioncount" },
lualine_z = { "location" },
},
},
}

View file

@ -1,55 +1,60 @@
-- npm install -g cspell@latest
local output = vim.fn.system {
"which",
"cspell",
}
local output = vim.fn.system({
"which",
"cspell",
})
if output == nil or output == "" then
-- if v:shell_error != 0 then
vim.fn.system {
"npm",
"install",
"-g",
"cspell@latest",
}
-- if v:shell_error != 0 then
vim.fn.system({
"npm",
"install",
"-g",
"cspell@latest",
})
end
return {
"jose-elias-alvarez/null-ls.nvim",
dependencies = "williamboman/mason.nvim",
opts = function(_, config)
require('util').ensure_installed_mason({
"stylua",
"prettier",
"rustfmt",
})
{
"jose-elias-alvarez/null-ls.nvim",
after = { "williamboman/mason.nvim" },
opts = function(_, config)
-- config variable is the default definitions table for the setup function call
local null_ls = require "null-ls"
-- config variable is the default definitions table for the setup function call
local null_ls = require("null-ls")
-- Check supported formatters and linters
-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/formatting
-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/diagnostics
config.sources = {
-- Set a formatter
null_ls.builtins.formatting.prettier,
null_ls.builtins.formatting.stylua,
null_ls.builtins.formatting.rustfmt,
-- null_ls.builtins.formatting.python,
-- null_ls.builtins.code_actions.proselint, -- TODO looks interesting
null_ls.builtins.code_actions.cspell.with {
config = {
find_json = function() return vim.fn.findfile("cspell.json", vim.fn.environ().HOME .. "/.config/nvim/lua/user/;") end,
},
},
null_ls.builtins.diagnostics.cspell.with {
extra_args = { "--config", "~/.config/nvim/lua/user/cspell.json" },
},
-- Check supported formatters and linters
-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/formatting
-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/diagnostics
config.sources = {
-- Set a formatter
null_ls.builtins.formatting.prettier, -- typescript/javascript
null_ls.builtins.formatting.stylua,
null_ls.builtins.formatting.rustfmt,
null_ls.builtins.formatting.black, -- python
-- null_ls.builtins.code_actions.proselint, -- TODO looks interesting
null_ls.builtins.code_actions.cspell.with({
config = {
find_json = function()
return vim.fn.findfile("cspell.json", vim.fn.environ().HOME .. "/.config/nvim/lua/user/;")
end,
},
}),
null_ls.builtins.diagnostics.cspell.with({
extra_args = { "--config", "~/.config/nvim/lua/user/cspell.json" },
}),
}
config.update_in_insert = true
config.debug = true
return config
end,
},
{
"jay-babu/mason-null-ls.nvim",
opts = {
ensure_installed = { "rustfmt", "stylelua", "prettier", "black" }
}
config.update_in_insert = true
config.debug = true
return config
end,
}
}

View file

@ -0,0 +1,5 @@
return {
"simrat39/rust-tools.nvim",
event = "BufEnter *.rs",
after = { "mason-lspconfig.nvim", "inlay-hints." },
}

View file

@ -0,0 +1,138 @@
return {
{
"L3MON4D3/LuaSnip",
dependencies = { "rafamadriz/friendly-snippets" },
opts = {
history = true,
updateevents = "TextChanged,TextChangedI",
opts = { store_selection_keys = "<C-x>" },
},
},
{
"hrsh7th/nvim-cmp",
event = "InsertEnter",
dependencies = {
-- Snips
{
"windwp/nvim-autopairs",
opts = {
fast_wrap = {},
disable_filetype = { "TelescopePrompt", "vim" },
},
config = function(_, opts)
require("nvim-autopairs").setup(opts)
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
require("cmp").event:on("confirm_done", cmp_autopairs.on_confirm_done())
end,
},
{ "saadparwaiz1/cmp_luasnip" },
-- { "hrsh7th/cmp-nvim-lua" },
-- { "hrsh7th/cmp-nvim-lsp" },
-- { "hrsh7th/cmp-vsnip" },
{ "hrsh7th/cmp-buffer" },
{ "hrsh7th/cmp-path" },
-- { "hrsh7th/cmp-cmdline" },
{ "hrsh7th/cmp-nvim-lsp" },
},
config = function()
vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, { desc = "Go to previous diagnostic message" })
vim.keymap.set("n", "]d", vim.diagnostic.goto_next, { desc = "Go to next diagnostic message" })
local cmp = require("cmp")
local snip_status_ok, luasnip = pcall(require, "luasnip")
local lspkind_status_ok, lspkind = pcall(require, "lspkind")
if not snip_status_ok then
return
end
local border_opts = {
border = "single",
winhighlight = "Normal:Normal,FloatBorder:FloatBorder,CursorLine:Visual,Search:None",
}
cmp.setup({
enabled = function()
local dap_prompt = vim.tbl_contains(
{ "dap-repl", "dapui_watches", "dapui_hover" },
vim.api.nvim_get_option_value("filetype", { buf = 0 })
)
if vim.api.nvim_get_option_value("buftype", { buf = 0 }) == "prompt" and not dap_prompt then
return false
end
return vim.g.cmp_enabled
end,
window = {
completion = cmp.config.window.bordered(border_opts),
documentation = cmp.config.window.bordered(border_opts),
},
preselect = cmp.PreselectMode.None,
formatting = {
fields = { "kind", "abbr", "menu" },
format = lspkind_status_ok and lspkind.cmp_format(utils.plugin_opts("lspkind.nvim")) or nil,
},
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
duplicates = {
nvim_lsp = 1,
luasnip = 1,
cmp_tabnine = 1,
buffer = 1,
path = 1,
},
confirm_opts = {
behavior = cmp.ConfirmBehavior.Replace,
select = false,
},
mapping = {
["<Up>"] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }),
["<Down>"] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }),
["<C-p>"] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),
["<C-n>"] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
["<C-k>"] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),
["<C-j>"] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
["<C-u>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
["<C-d>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
["<C-y>"] = cmp.config.disable,
["<C-e>"] = cmp.mapping({ i = cmp.mapping.abort(), c = cmp.mapping.close() }),
["<CR>"] = cmp.mapping.confirm({ select = false }),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
},
sources = cmp.config.sources({
{ name = "nvim_lsp", priority = 1000 },
{ name = "luasnip", priority = 750 },
{ name = "buffer", priority = 500 },
{ name = "path", priority = 250 },
}),
})
end,
},
{
"folke/neodev.nvim",
opts = {}
}
}

View file

@ -1,5 +0,0 @@
return {
"simrat39/rust-tools.nvim",
event = "BufEnter *.rs",
after = { "mason-lspconfig.nvim", "inlay-hints." },
}

View file

@ -0,0 +1,3 @@
return {
'tpope/vim-sleuth',
}

View file

@ -27,6 +27,7 @@ require('util').keymaps({
["<leader>s"] = { "<Nop>", desc = "Scratch File" },
["<leader>ss"] = { function() scratch ".txt" end, desc = "New text scratch file" },
["<leader>sn"] = { function() scratch ".json" end, desc = "New json scratch file" },
["<leader>sm"] = { function() scratch ".md" end, desc = "New markdown scratch file" },
["<leader>sq"] = { function() scratch ".sql" end, desc = "New sql scratch file" },
["<leader>st"] = { function() scratch ".ts" end, desc = "New ts scratch file" },
["<leader>sb"] = { function() scratch ".sh" end, desc = "New shell scratch file" },

View file

@ -34,17 +34,5 @@ function M.spread(template)
end
end
function M.ensure_installed_mason(items)
local registry = require 'mason-registry'
for _, item in ipairs(items) do
if not registry.is_installed(item) then
if registry.has_package(item) then
registry.get_package(item).install()
end
end
end
registry.refresh()
end
return M