Nvim: basic LSP and Telescope setup

This commit is contained in:
Filippo Berto 2024-01-13 22:57:12 +01:00
parent a07f454331
commit a2d69430f6
No known key found for this signature in database
GPG key ID: FE98AE5EC52B1056
5 changed files with 321 additions and 139 deletions

View file

@ -0,0 +1,33 @@
local cmp = require("cmp")
local luasnip = require("luasnip")
-- loads vscode style snippets from installed plugins (eg. friendly-snippets)
require("luasnip.loaders.from_vscode").lazy_load()
cmp.setup({
completion = {
completeopt = "menu,menuone,preview,noselect"
},
snippet = { -- configura how nvim-cmp interacts with snippet engine
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({
["<C-k>"] = cmp.mapping.select_prev_item(), -- previous suggestion
["<C-j>"] = cmp.mapping.select_next_item(), -- next suggestion
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(), -- show completion suggestions
["<C-e>"] = cmp.mapping.abort(), -- close cmpletion window
["<CR>"] = cmp.mapping.confirm({ select = false })
}),
-- sources for autocompletion
sources = cmp.config.sources({
{ name = "nvim_lsp" }, -- nvim_lsp
{ name = "luasnip" }, -- snippets
{ name = "buffer" }, -- text within current buffer
{ name = "path" }, -- file system paths
}),
})

View file

@ -0,0 +1,92 @@
local lspconfig = require('lspconfig')
local capabilities = require("cmp_nvim_lsp").default_capabilities()
-- Keymaps
local keymap = vim.keymap
local opts = { noremap = true, silent = true }
local on_attach = function(client, bufnr)
opts.buffer = bufnr
opts.desc = "Show LSP references"
keymap.set("n", "gR", "<cmd>Telescope lsp_references<CR>", opts)
opts.desc = "Go to declaration"
keymap.set("n", "gD", vim.lsp.buf.declaration, opts)
opts.desc = "Go to definition"
keymap.set("n", "gd", "<cmd>Telescope lsp_definitions<CR>", opts)
opts.desc = "Show LSP implementations"
keymap.set("n", "gi", "<cmd>Telescope lsp_implementations<CR>", opts)
opts.desc = "Show LSP type definitions"
keymap.set("n", "gt", "<cmd>Telescope lsp_type_definitions<CR>", opts)
opts.desc = "See available code actions"
keymap.set({ "n", "v" }, "<leader>ca", vim.lsp.buf.code_action, opts)
opts.desc = "Smart rename"
keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts)
opts.desc = "Smart show buffer diagnostics"
keymap.set("n", "<leader>D", "<cmd>Telescope diagnostics bufnr=0<CR>", opts)
opts.desc = "Smart show line diagnostics"
keymap.set("n", "<leader>d", vim.diagnostic.open_float, opts)
opts.desc = "Go to previous diagnostic"
keymap.set("n", "[d", vim.diagnostic.goto_prev, opts)
opts.desc = "Go to next diagnostic"
keymap.set("n", "]d", vim.diagnostic.goto_next, opts)
opts.desc = "Show documentation for what is under cursor"
keymap.set("n", "K", vim.lsp.buf.hover, opts)
opts.desc = "Restart LSP"
keymap.set("n", "<leader>rs", ":LspRestart<CR>", opts)
end
-- Change the Diagnostic symbols in the sign column (gutter)
-- (not in youtube nvim video)
local signs = { Error = "", Warn = "", Hint = "󰠠 ", Info = "" }
for type, icon in pairs(signs) do
local hl = "DiagnosticSign" .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" })
end
-- LSP configuration
lspconfig["html"].setup { capabilities = capabilities, on_attach = on_attach}
lspconfig["pylsp"].setup { capabilities = capabilities, on_attach = on_attach}
lspconfig["nil_ls"].setup { capabilities = capabilities, on_attach = on_attach, settings = {
["nil"] = {
formatting = { command = { "nixpkgs-fmt" } },
autoEvalInputs = true
}
}}
lspconfig["rust_analyzer"].setup { capabilities = capabilities, on_attach = on_attach}
lspconfig["texlab"].setup { capabilities = capabilities, on_attach = on_attach, settings = {
texlab = {
formatterLineLength = 0,
bibtexFormatter = "latexindent",
latexindent = { modifyLineBreaks = false },
chktex = { onEdit = true },
}
}}
lspconfig["clangd"].setup { capabilities = capabilities, on_attach = on_attach}
lspconfig["marksman"].setup { capabilities = capabilities, on_attach = on_attach}
lspconfig["yamlls"].setup { capabilities = capabilities, on_attach = on_attach, settings = {
yaml = {
keyOrdering = false,
schemas = {
["kubernetes"] = "*.yaml",
["http://json.schemastore.org/github-workflow"] = ".github/workflows/*",
["http://json.schemastore.org/github-action"] = ".github/action.{yml,yaml}",
["http://json.schemastore.org/ansible-stable-2.9"] = "roles/tasks/*.{yml,yaml}",
["http://json.schemastore.org/prettierrc"] = ".prettierrc.{yml,yaml}",
["http://json.schemastore.org/kustomization"] = "kustomization.{yml,yaml}",
["http://json.schemastore.org/ansible-playbook"] = "*play*.{yml,yaml}",
["http://json.schemastore.org/chart"] = "Chart.{yml,yaml}",
["https://json.schemastore.org/dependabot-v2"] = ".github/dependabot.{yml,yaml}",
["https://gitlab.com/gitlab-org/gitlab/-/raw/master/app/assets/javascripts/editor/schema/ci.json"] = "*gitlab-ci*.{yml,yaml}",
["https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/schemas/v3.1/schema.json"] = "*api*.{yml,yaml}",
["https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json"] = "*docker-compose*.{yml,yaml}",
["https://raw.githubusercontent.com/argoproj/argo-workflows/master/api/jsonschema/schema.json"] = "*flow*.{yml,yaml}",
}
}
}}
lspconfig["pylsp"].setup({
capabilities = capabilities,
on_attach = on_attach,
})
-- lspconfig["cmake-language-server"].setup { capabilities = capabilities, on_attach = on_attach}
-- lspconfig["vscode-css-language-server"].setup { capabilities = capabilities, on_attach = on_attach}

View file

@ -2,131 +2,185 @@
let vp = pkgs.vimPlugins; in { let vp = pkgs.vimPlugins; in {
programs.neovim = { programs.neovim = {
enable = true; enable = true;
viAlias = true;
vimAlias = true;
vimdiffAlias = true;
plugins = [ plugins = [
# Git related plugins # # Git related plugins
vp.fugitive # vp.fugitive
vp.rhubarb # vp.rhubarb
# vp.lsp-zero-nvim
# Detect tabstop and shiftwidth automatically # vp.nvim-cmp
vp.sleuth # vp.cmp-nvim-lsp
# LSP
{
plugin = vp.fidget-nvim;
config = ''
require("fidget").setup({})
'';
type = "lua";
}
vp.neodev-nvim
# vp.nvim-lspconfig # vp.nvim-lspconfig
vp.telescope-fzf-native-nvim
{
plugin = vp.telescope-nvim;
type = "lua";
config = ''
local telescope = require("telescope")
local actions = require("telescope.actions")
telescope.setup({
defaults = {
mappings = {
i = {
["<C-k>"] = actions.move_selection_previous, -- move to prev result
["<C-j>"] = actions.move_selection_next, -- move to next result
["<C-q>"] = actions.send_selected_to_qflist + actions.open_qflist,
}
}
}
})
telescope.load_extension("fzf");
local keymap = vim.keymap
keymap.set("n", "<leader>ff", "<cmd>Telescope find_files<cr>", { desc = "Fuzzy find files in cwd" })
keymap.set("n", "<leader>fr", "<cmd>Telescope oldfiles<cr>", { desc = "Fuzzy find recent files" })
keymap.set("n", "<leader>fs", "<cmd>Telescope live_grep<cr>", { desc = "Find string in cwd" })
keymap.set("n", "<leader>fc", "<cmd>Telescope grep_string<cr>", { desc = "Fuzzy string under cursor in cwd" })
'';
}
vp.dressing-nvim # Better UI for input and selection
# Tree view
# vp.chadtree
vp.nerdtree-git-plugin
vp.vim-devicons
vp.nerdtree
vp.cmp-buffer # source for text in buffer
vp.cmp-path # source for file system path
vp.luasnip
vp.cmp_luasnip
vp.friendly-snippets
{
plugin = vp.nvim-cmp;
type = "lua";
config = builtins.readFile ./nvim/nvim-cmp-config.lua;
}
# # Detect tabstop and shiftwidth automatically
# vp.sleuth
# {
# plugin = vp.nvim-cmp;
# type = "lua";
# config = ''
# require()
# '';
# }
# # LSP
vp.fidget-nvim # fidget moving while LSP is working
# vp.neodev-nvim
# # vp.nvim-lspconfig
vp.cmp-nvim-lsp
{ {
plugin = vp.nvim-lspconfig; plugin = vp.nvim-lspconfig;
type = "lua"; type = "lua";
config = '' config = builtins.readFile ./nvim/nvim-lspconfig.lua;
local lspconfig = require('lspconfig')
local capabilities = require("cmp_nvim_lsp").default_capabilities()
lspconfig.pylsp.setup {capabilities=capabilities}
lspconfig.nil_ls.setup {capabilities=capabilities}
lspconfig.rust_analyzer.setup {capabilities=capabilities}
lspconfig.texlab.setup {capabilities=capabilities}
'';
} }
# Autocompletion # # Autocompletion
## Snippet engine & its associated nvim-cmp source # ## Snippet engine & its associated nvim-cmp source
vp.luasnip # vp.luasnip
vp.cmp_luasnip # vp.cmp_luasnip
## Adds LSP completion capabilities # ## Adds LSP completion capabilities
vp.cmp-nvim-lsp # vp.cmp-nvim-lsp
vp.cmp-path # vp.cmp-path
## Adds a number of user-friendly snippets # ## Adds a number of user-friendly snippets
vp.friendly-snippets # vp.friendly-snippets
## NVIM CMP # ## NVIM CMP
vp.nvim-cmp # vp.nvim-cmp
# Useful plugin to show you pending keybinds # # Useful plugin to show you pending keybinds
{ plugin = vp.which-key-nvim; config = ''require("which-key").setup({})''; type = "lua"; } # { plugin = vp.which-key-nvim; config = ''require("which-key").setup({})''; type = "lua"; }
# Adds git related signs to the gutter, as well as utilities for managing changes # Adds git related signs to the gutter, as well as utilities for managing changes
{ # {
plugin = vp.gitsigns-nvim; # plugin = vp.gitsigns-nvim;
config = '' # config = ''
require("gitsigns").setup({ # require("gitsigns").setup({
-- See `:help gitsigns.txt` # -- See `:help gitsigns.txt`
signs = { # signs = {
add = { text = '+' }, # add = { text = '+' },
change = { text = '~' }, # change = { text = '~' },
delete = { text = '_' }, # delete = { text = '_' },
topdelete = { text = '' }, # topdelete = { text = '‾' },
changedelete = { text = '~' }, # changedelete = { text = '~' },
}, # },
on_attach = function(bufnr) # on_attach = function(bufnr)
local gs = package.loaded.gitsigns # local gs = package.loaded.gitsigns
local function map(mode, l, r, opts) # local function map(mode, l, r, opts)
opts = opts or {} # opts = opts or {}
opts.buffer = bufnr # opts.buffer = bufnr
vim.keymap.set(mode, l, r, opts) # vim.keymap.set(mode, l, r, opts)
end # end
-- Navigation # -- Navigation
map({ 'n', 'v' }, ']c', function() # map({ 'n', 'v' }, ']c', function()
if vim.wo.diff then # if vim.wo.diff then
return ']c' # return ']c'
end # end
vim.schedule(function() # vim.schedule(function()
gs.next_hunk() # gs.next_hunk()
end) # end)
return '<Ignore>' # return '<Ignore>'
end, { expr = true, desc = 'Jump to next hunk' }) # end, { expr = true, desc = 'Jump to next hunk' })
map({ 'n', 'v' }, '[c', function() # map({ 'n', 'v' }, '[c', function()
if vim.wo.diff then # if vim.wo.diff then
return '[c' # return '[c'
end # end
vim.schedule(function() # vim.schedule(function()
gs.prev_hunk() # gs.prev_hunk()
end) # end)
return '<Ignore>' # return '<Ignore>'
end, { expr = true, desc = 'Jump to previous hunk' }) # end, { expr = true, desc = 'Jump to previous hunk' })
-- Actions # -- Actions
-- visual mode # -- visual mode
map('v', '<leader>hs', function() # map('v', '<leader>hs', function()
gs.stage_hunk { vim.fn.line '.', vim.fn.line 'v' } # gs.stage_hunk { vim.fn.line '.', vim.fn.line 'v' }
end, { desc = 'stage git hunk' }) # end, { desc = 'stage git hunk' })
map('v', '<leader>hr', function() # map('v', '<leader>hr', function()
gs.reset_hunk { vim.fn.line '.', vim.fn.line 'v' } # gs.reset_hunk { vim.fn.line '.', vim.fn.line 'v' }
end, { desc = 'reset git hunk' }) # end, { desc = 'reset git hunk' })
-- normal mode # -- normal mode
map('n', '<leader>hs', gs.stage_hunk, { desc = 'git stage hunk' }) # map('n', '<leader>hs', gs.stage_hunk, { desc = 'git stage hunk' })
map('n', '<leader>hr', gs.reset_hunk, { desc = 'git reset hunk' }) # map('n', '<leader>hr', gs.reset_hunk, { desc = 'git reset hunk' })
map('n', '<leader>hS', gs.stage_buffer, { desc = 'git Stage buffer' }) # map('n', '<leader>hS', gs.stage_buffer, { desc = 'git Stage buffer' })
map('n', '<leader>hu', gs.undo_stage_hunk, { desc = 'undo stage hunk' }) # map('n', '<leader>hu', gs.undo_stage_hunk, { desc = 'undo stage hunk' })
map('n', '<leader>hR', gs.reset_buffer, { desc = 'git Reset buffer' }) # map('n', '<leader>hR', gs.reset_buffer, { desc = 'git Reset buffer' })
map('n', '<leader>hp', gs.preview_hunk, { desc = 'preview git hunk' }) # map('n', '<leader>hp', gs.preview_hunk, { desc = 'preview git hunk' })
map('n', '<leader>hb', function() # map('n', '<leader>hb', function()
gs.blame_line { full = false } # gs.blame_line { full = false }
end, { desc = 'git blame line' }) # end, { desc = 'git blame line' })
map('n', '<leader>hd', gs.diffthis, { desc = 'git diff against index' }) # map('n', '<leader>hd', gs.diffthis, { desc = 'git diff against index' })
map('n', '<leader>hD', function() # map('n', '<leader>hD', function()
gs.diffthis '~' # gs.diffthis '~'
end, { desc = 'git diff against last commit' }) # end, { desc = 'git diff against last commit' })
-- Toggles # -- Toggles
map('n', '<leader>tb', gs.toggle_current_line_blame, { desc = 'toggle git blame line' }) # map('n', '<leader>tb', gs.toggle_current_line_blame, { desc = 'toggle git blame line' })
map('n', '<leader>td', gs.toggle_deleted, { desc = 'toggle git show deleted' }) # map('n', '<leader>td', gs.toggle_deleted, { desc = 'toggle git show deleted' })
-- Text object # -- Text object
map({ 'o', 'x' }, 'ih', ':<C-U>Gitsigns select_hunk<CR>', { desc = 'select git hunk' }) # map({ 'o', 'x' }, 'ih', ':<C-U>Gitsigns select_hunk<CR>', { desc = 'select git hunk' })
end, # end,
}) # })
''; # '';
type = "lua"; # type = "lua";
} # }
# Cool color scheme # Cool color scheme
{ {
@ -150,39 +204,42 @@ let vp = pkgs.vimPlugins; in {
type = "lua"; type = "lua";
} }
# Add indentation guides even on blank lines # # Add indentation guides even on blank lines
{ # {
# See `:help ibl` # # See `:help ibl`
plugin = vp.indent-blankline-nvim; # plugin = vp.indent-blankline-nvim;
config = '' # config = ''
require("ibl").setup({}) # require("ibl").setup({})
''; # '';
type = "lua"; # type = "lua";
} # }
# "gc" to comment visual regions/lines # # "gc" to comment visual regions/lines
{ # {
plugin = vp.comment-nvim; # plugin = vp.comment-nvim;
config = '' # config = ''
require("Comment").setup({}) # require("Comment").setup({})
''; # '';
type = "lua"; # type = "lua";
} # }
vp.plenary-nvim # vp.plenary-nvim
vp.telescope-fzf-native-nvim # vp.telescope-fzf-native-nvim
vp.telescope-nvim # vp.telescope-nvim
vp.nvim-treesitter-textobjects # vp.nvim-treesitter-textobjects
vp.nvim-treesitter # vp.nvim-treesitter
]; ];
# settings = { ignorecase = true; }; # settings = { ignorecase = true; };
# coc.enable = true; # coc.enable = true;
viAlias = true;
vimAlias = true;
vimdiffAlias = true;
extraConfig = '' extraConfig = ''
set mouse=a set mouse=a
set encoding=UTF-8
set guifont=FiraCode\ Nerd\ Font\ Mono\ 10
let g:airline_powerline_fonts = 1
'';
extraLuaConfig = ''
vim.g.mapleader = " "
''; '';
}; };
} }