Add suport for nvim's LSP

This commit is contained in:
ItsDrike 2021-12-08 23:10:05 +01:00
parent 4eee9b4398
commit 46ca9189af
No known key found for this signature in database
GPG key ID: FB8CA11A2CF3A843
6 changed files with 180 additions and 11 deletions

View file

@ -0,0 +1,5 @@
local vim = require("vim")
local cmd = vim.cmd
-- Always automatically format on write for given filenames
cmd[[autocmd BufWritePre *.js lua vim.lsp.buf.formatting_sync(nil, 100)]]

View file

@ -0,0 +1,31 @@
-- Neovim has built in support for the language server protocol (LSP). For
-- which reason I decided to utilize it directly instead of relying on plugins
-- such as COC, which reimplement it completely just to support default vim
-- too, since I don't need pure vim support, utilizing this built in support
-- makes a lot more sense and will be faster.
--
-- By default, setting up LSP doesn't technically require any plugins, however
-- I still do use the recommended neovim/nvim-lspconfig plugin, along with
-- williamboman/nvim-lsp-installer, because it makes things a lot easier to get
-- working. The lspconfig holds the default configurations for the individual
-- language servers, avoiding the need of tediously configuring them manually,
-- and the lspinstaller gives us a way to automatically install selected
-- language servers locally for nvim only, which means we won't need to look at
-- the install instructions for each language server and install it user or
-- system wide.
--
-- However, since this configuration shouldn't be plugin dependant, as it is
-- outside of the plugins/ directory and LSP is supported by neovim directly,
-- this folder is here to provide the non-plugin dependant LSP configuration,
-- but I do not manually define the configuration for each language server,
-- since I do actually utilize those plugins, but sturcturing like this still
-- does give space for completely custom configurations written from scratch
-- without relying on any external plugins at all. If you do want to do that,
-- this would be the place to set this up.
--
-- NOTE: With my current configuration, this file is only ran by being required
-- in the lsp plugin config file, if you don't wish to run with plugins, you'll
-- want to require this file from init.lua directly.
require("lsp.keymaps")
require("lsp.autoformat")

View file

@ -0,0 +1,57 @@
local m = require("utility.mappings")
-- See `:help vim.lsp.*` for documentation on any of the below mapped functions
-- Check if certain plugins are installed, if so, they should be used
-- to define some mappings
local telescope_installed, _ = pcall(require, "telescope")
local lsp_signature_installed, _ = pcall(require, "lsp_signature")
-- Lookups
m.keymap("n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>")
m.keymap('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<cr>')
m.keymap("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>")
m.keymap("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>")
m.keymap("n", "gt", "<cmd>lua vim.lsp.buf.type_definition()<CR>")
if telescope_installed then
m.keymap('n', 'gd', '<cmd>lua require("telescope.builtin").lsp_definitions()<cr>')
m.keymap('n', 'gr', '<cmd>lua require("telescope.builtin").lsp_references()<cr>')
m.keymap('n', 'gi', '<cmd>lua require("telescope.builtin").lsp_implementations()<cr>')
m.keymap('n', 'gt', '<cmd>lua require("telescope.builtin").lsp_type_definitions()<cr>')
end
-- Formatting
m.keymap('n', '<leader>gf', '<cmd>lua vim.lsp.buf.formatting()<cr>')
m.keymap('v', '<leader>gf', '<cmd>lua vim.lsp.buf.range_formatting()<cr>')
-- Hover info
m.keymap("n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>")
m.keymap("n", "M", "<cmd>lua vim.lsp.buf.hover()<CR>")
if lsp_signature_installed then
m.keymap('n', '<C-M>', '<cmd>lua require("lsp_signature").signature()<cr>')
end
-- Diagnostics
m.keymap('n', '[g', '<cmd>lua vim.diagnostic.goto_prev()<cr>')
m.keymap('n', ']g', '<cmd>lua vim.diagnostic.goto_next()<cr>')
m.keymap('n', 'ge', '<cmd>lua vim.diagnostic.open_float(nil, { scope = "line", })<cr>')
if telescope_installed then
m.keymap('n', '<leader>ge', '<cmd>lua require("telescope.builtin").lsp_document_diagnostics()<cr>')
end
-- LSP Workspace
m.keymap('n', '<leader>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<cr>')
m.keymap('n', '<leader>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<cr>')
m.keymap('n', '<leader>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<cr>')
--Actions
if telescope_installed then
m.keymap('n', '<leader>ga', '<cmd>lua require("telescope.builtin").lsp_code_actions()<cr>')
m.keymap('v', '<leader>ga', '<cmd>lua require("telescope.builtin").lsp_range_code_actions()<cr>')
end
-- Use custom implementation for renaming all references
m.keymap('n', 'gn', '<cmd>lua require("lsp/rename").rename()<cr>')

View file

@ -0,0 +1,3 @@
-- Nvim's LSP lacks rename-all functionality which plugins like Coc provide
-- this is a manual implementation of this feature
-- TODO: Actually implement this