2024-07-29 18:17:53 +00:00
|
|
|
-- LSP Plugins
|
2024-02-26 19:46:46 +00:00
|
|
|
return {
|
2024-07-29 18:17:53 +00:00
|
|
|
{
|
|
|
|
-- `lazydev` configures Lua LSP for your Neovim config, runtime and plugins
|
|
|
|
-- used for completion, annotations and signatures of Neovim apis
|
|
|
|
'folke/lazydev.nvim',
|
|
|
|
ft = 'lua',
|
|
|
|
opts = {
|
|
|
|
library = {
|
|
|
|
-- Load luvit types when the `vim.uv` word is found
|
|
|
|
{ path = 'luvit-meta/library', words = { 'vim%.uv' } },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ 'Bilal2453/luvit-meta', lazy = true },
|
|
|
|
{
|
|
|
|
-- Main LSP Configuration
|
2024-02-26 19:46:46 +00:00
|
|
|
'neovim/nvim-lspconfig',
|
|
|
|
dependencies = {
|
2024-03-18 17:50:47 +00:00
|
|
|
-- Automatically install LSPs and related tools to stdpath for Neovim
|
2024-04-20 12:05:56 +00:00
|
|
|
{ 'williamboman/mason.nvim', config = true }, -- NOTE: Must be loaded before dependants
|
2024-02-26 19:46:46 +00:00
|
|
|
'williamboman/mason-lspconfig.nvim',
|
|
|
|
'WhoIsSethDaniel/mason-tool-installer.nvim',
|
2024-07-28 20:04:57 +00:00
|
|
|
{ 'mfussenegger/nvim-jdtls', ft = 'java' },
|
2024-02-26 19:46:46 +00:00
|
|
|
|
|
|
|
-- Useful status updates for LSP.
|
|
|
|
-- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
|
|
|
|
{ 'j-hui/fidget.nvim', opts = {} },
|
2024-03-12 22:40:44 +00:00
|
|
|
|
2024-07-31 19:09:37 +00:00
|
|
|
-- Allows extra capabilities provided by nvim-cmp
|
|
|
|
'hrsh7th/cmp-nvim-lsp',
|
2024-11-05 10:42:23 +00:00
|
|
|
|
|
|
|
-- Needed to explore Workspace in Java
|
|
|
|
'stevearc/oil.nvim',
|
2024-02-26 19:46:46 +00:00
|
|
|
},
|
|
|
|
config = function()
|
2024-03-18 17:50:47 +00:00
|
|
|
-- Brief aside: **What is LSP?**
|
2024-02-26 19:46:46 +00:00
|
|
|
--
|
2024-03-18 17:50:47 +00:00
|
|
|
-- LSP is an initialism you've probably heard, but might not understand what it is.
|
2024-02-26 19:46:46 +00:00
|
|
|
--
|
|
|
|
-- LSP stands for Language Server Protocol. It's a protocol that helps editors
|
|
|
|
-- and language tooling communicate in a standardized fashion.
|
|
|
|
--
|
|
|
|
-- In general, you have a "server" which is some tool built to understand a particular
|
2024-03-18 17:50:47 +00:00
|
|
|
-- language (such as `gopls`, `lua_ls`, `rust_analyzer`, etc.). These Language Servers
|
2024-02-26 19:46:46 +00:00
|
|
|
-- (sometimes called LSP servers, but that's kind of like ATM Machine) are standalone
|
|
|
|
-- processes that communicate with some "client" - in this case, Neovim!
|
|
|
|
--
|
|
|
|
-- LSP provides Neovim with features like:
|
|
|
|
-- - Go to definition
|
|
|
|
-- - Find references
|
|
|
|
-- - Autocompletion
|
|
|
|
-- - Symbol Search
|
|
|
|
-- - and more!
|
|
|
|
--
|
|
|
|
-- Thus, Language Servers are external tools that must be installed separately from
|
|
|
|
-- Neovim. This is where `mason` and related plugins come into play.
|
|
|
|
--
|
|
|
|
-- If you're wondering about lsp vs treesitter, you can check out the wonderfully
|
2024-03-01 22:11:08 +00:00
|
|
|
-- and elegantly composed help section, `:help lsp-vs-treesitter`
|
2024-02-26 19:46:46 +00:00
|
|
|
|
|
|
|
-- This function gets run when an LSP attaches to a particular buffer.
|
|
|
|
-- That is to say, every time a new file is opened that is associated with
|
|
|
|
-- an lsp (for example, opening `main.rs` is associated with `rust_analyzer`) this
|
|
|
|
-- function will be executed to configure the current buffer
|
|
|
|
vim.api.nvim_create_autocmd('LspAttach', {
|
|
|
|
group = vim.api.nvim_create_augroup('kickstart-lsp-attach', { clear = true }),
|
|
|
|
callback = function(event)
|
2024-03-18 17:50:47 +00:00
|
|
|
-- NOTE: Remember that Lua is a real programming language, and as such it is possible
|
|
|
|
-- to define small helper and utility functions so you don't have to repeat yourself.
|
2024-02-26 19:46:46 +00:00
|
|
|
--
|
|
|
|
-- In this case, we create a function that lets us more easily define mappings specific
|
|
|
|
-- for LSP related items. It sets the mode, buffer and description for us each time.
|
2024-08-25 09:03:03 +00:00
|
|
|
local map = function(keys, func, desc, mode)
|
|
|
|
mode = mode or 'n'
|
|
|
|
vim.keymap.set(mode, keys, func, { buffer = event.buf, desc = 'LSP: ' .. desc })
|
2024-02-26 19:46:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Jump to the definition of the word under your cursor.
|
|
|
|
-- This is where a variable was first declared, or where a function is defined, etc.
|
2024-03-12 00:13:38 +00:00
|
|
|
-- To jump back, press <C-t>.
|
2024-02-26 19:46:46 +00:00
|
|
|
map('gd', require('telescope.builtin').lsp_definitions, '[G]oto [D]efinition')
|
|
|
|
|
|
|
|
-- Find references for the word under your cursor.
|
|
|
|
map('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
|
|
|
|
|
|
|
|
-- Jump to the implementation of the word under your cursor.
|
|
|
|
-- Useful when your language has ways of declaring types without an actual implementation.
|
|
|
|
map('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation')
|
|
|
|
|
|
|
|
-- Jump to the type of the word under your cursor.
|
|
|
|
-- Useful when you're not sure what type a variable is and you want to see
|
|
|
|
-- the definition of its *type*, not where it was *defined*.
|
|
|
|
map('<leader>D', require('telescope.builtin').lsp_type_definitions, 'Type [D]efinition')
|
|
|
|
|
|
|
|
-- Fuzzy find all the symbols in your current document.
|
|
|
|
-- Symbols are things like variables, functions, types, etc.
|
|
|
|
map('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
|
|
|
|
|
2024-03-18 17:50:47 +00:00
|
|
|
-- Fuzzy find all the symbols in your current workspace.
|
|
|
|
-- Similar to document symbols, except searches over your entire project.
|
2024-02-26 19:46:46 +00:00
|
|
|
map('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
|
2024-03-01 10:52:43 +00:00
|
|
|
-- Workspace functionality dependant on LSPs
|
2024-03-01 08:23:53 +00:00
|
|
|
map('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
|
|
|
|
map('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
|
2024-02-26 19:46:46 +00:00
|
|
|
|
2024-03-18 17:50:47 +00:00
|
|
|
-- Rename the variable under your cursor.
|
2024-02-26 19:46:46 +00:00
|
|
|
-- Most Language Servers support renaming across files, etc.
|
|
|
|
map('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
|
|
|
|
|
|
|
|
-- Execute a code action, usually your cursor needs to be on top of an error
|
|
|
|
-- or a suggestion from your LSP for this to activate.
|
2024-08-25 09:03:03 +00:00
|
|
|
map('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction', { 'n', 'x' })
|
2024-02-26 19:46:46 +00:00
|
|
|
|
|
|
|
-- Opens a popup that displays documentation about the word under your cursor
|
2024-03-18 17:50:47 +00:00
|
|
|
-- See `:help K` for why this keymap.
|
2024-02-26 19:46:46 +00:00
|
|
|
map('K', vim.lsp.buf.hover, 'Hover Documentation')
|
2024-07-31 22:23:53 +00:00
|
|
|
-- Similar as above, shows hover documentation for current function
|
|
|
|
vim.keymap.set('i', '<C-k>', vim.lsp.buf.signature_help, { buffer = event.buf, desc = 'LSP: ' .. 'Hover Signature Information' })
|
2024-02-26 19:46:46 +00:00
|
|
|
|
|
|
|
-- WARN: This is not Goto Definition, this is Goto Declaration.
|
2024-03-18 17:50:47 +00:00
|
|
|
-- For example, in C this would take you to the header.
|
2024-02-26 19:46:46 +00:00
|
|
|
map('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
|
|
|
|
|
|
|
|
-- The following two autocommands are used to highlight references of the
|
|
|
|
-- word under your cursor when your cursor rests there for a little while.
|
|
|
|
-- See `:help CursorHold` for information about when this is executed
|
|
|
|
--
|
|
|
|
-- When you move your cursor, the highlights will be cleared (the second autocommand).
|
|
|
|
local client = vim.lsp.get_client_by_id(event.data.client_id)
|
2024-07-22 18:25:55 +00:00
|
|
|
if client and client.supports_method(vim.lsp.protocol.Methods.textDocument_documentHighlight) then
|
2024-04-22 21:48:46 +00:00
|
|
|
local highlight_augroup = vim.api.nvim_create_augroup('kickstart-lsp-highlight', { clear = false })
|
2024-02-26 19:46:46 +00:00
|
|
|
vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
|
|
|
|
buffer = event.buf,
|
2024-04-22 16:07:12 +00:00
|
|
|
group = highlight_augroup,
|
2024-02-26 19:46:46 +00:00
|
|
|
callback = vim.lsp.buf.document_highlight,
|
|
|
|
})
|
|
|
|
|
|
|
|
vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
|
|
|
|
buffer = event.buf,
|
2024-04-22 16:07:12 +00:00
|
|
|
group = highlight_augroup,
|
2024-02-26 19:46:46 +00:00
|
|
|
callback = vim.lsp.buf.clear_references,
|
|
|
|
})
|
2024-05-02 22:11:56 +00:00
|
|
|
|
|
|
|
vim.api.nvim_create_autocmd('LspDetach', {
|
|
|
|
group = vim.api.nvim_create_augroup('kickstart-lsp-detach', { clear = true }),
|
|
|
|
callback = function(event2)
|
|
|
|
vim.lsp.buf.clear_references()
|
|
|
|
vim.api.nvim_clear_autocmds { group = 'kickstart-lsp-highlight', buffer = event2.buf }
|
|
|
|
end,
|
|
|
|
})
|
2024-02-26 19:46:46 +00:00
|
|
|
end
|
2024-04-17 18:42:05 +00:00
|
|
|
|
2024-07-22 18:25:55 +00:00
|
|
|
-- The following code creates a keymap to toggle inlay hints in your
|
2024-04-17 18:42:05 +00:00
|
|
|
-- code, if the language server you are using supports them
|
|
|
|
--
|
|
|
|
-- This may be unwanted, since they displace some of your code
|
2024-07-22 18:25:55 +00:00
|
|
|
if client and client.supports_method(vim.lsp.protocol.Methods.textDocument_inlayHint) then
|
2024-04-17 18:42:05 +00:00
|
|
|
map('<leader>th', function()
|
2024-07-22 18:25:55 +00:00
|
|
|
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled { bufnr = event.buf })
|
2024-04-17 18:42:05 +00:00
|
|
|
end, '[T]oggle Inlay [H]ints')
|
|
|
|
end
|
2024-02-26 19:46:46 +00:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
-- LSP servers and clients are able to communicate to each other what features they support.
|
2024-03-18 17:50:47 +00:00
|
|
|
-- By default, Neovim doesn't support everything that is in the LSP specification.
|
2024-02-26 19:46:46 +00:00
|
|
|
-- When you add nvim-cmp, luasnip, etc. Neovim now has *more* capabilities.
|
|
|
|
-- So, we create new capabilities with nvim cmp, and then broadcast that to the servers.
|
|
|
|
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
|
|
|
capabilities = vim.tbl_deep_extend('force', capabilities, require('cmp_nvim_lsp').default_capabilities())
|
|
|
|
|
|
|
|
-- Enable the following language servers
|
|
|
|
-- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
|
|
|
|
--
|
|
|
|
-- Add any additional override configuration in the following tables. Available keys are:
|
|
|
|
-- - cmd (table): Override the default command used to start the server
|
|
|
|
-- - filetypes (table): Override the default list of associated filetypes for the server
|
|
|
|
-- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
|
|
|
|
-- - settings (table): Override the default settings passed when initializing the server.
|
|
|
|
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
|
|
|
|
local servers = {
|
2024-08-15 09:38:54 +00:00
|
|
|
powershell_es = {},
|
2024-03-01 08:15:22 +00:00
|
|
|
rust_analyzer = {},
|
2024-03-01 12:42:23 +00:00
|
|
|
tailwindcss = {},
|
2024-03-01 08:15:22 +00:00
|
|
|
omnisharp = {},
|
|
|
|
pyright = {},
|
|
|
|
svelte = {},
|
2024-10-17 08:53:43 +00:00
|
|
|
ts_ls = {},
|
2024-06-04 12:23:47 +00:00
|
|
|
gopls = {},
|
2024-07-02 07:31:51 +00:00
|
|
|
intelephense = {},
|
2024-02-26 19:46:46 +00:00
|
|
|
-- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
|
|
|
|
--
|
|
|
|
-- Some languages (like typescript) have entire language plugins that can be useful:
|
|
|
|
-- https://github.com/pmizio/typescript-tools.nvim
|
|
|
|
|
|
|
|
lua_ls = {
|
|
|
|
-- cmd = {...},
|
2024-03-18 16:43:59 +00:00
|
|
|
-- filetypes = { ...},
|
2024-02-26 19:46:46 +00:00
|
|
|
-- capabilities = {},
|
|
|
|
settings = {
|
|
|
|
Lua = {
|
2024-03-01 10:09:32 +00:00
|
|
|
completion = {
|
|
|
|
callSnippet = 'Replace',
|
|
|
|
},
|
2024-02-26 19:46:46 +00:00
|
|
|
-- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings
|
|
|
|
-- diagnostics = { disable = { 'missing-fields' } },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-07-28 20:06:51 +00:00
|
|
|
local function jdtls_setup()
|
2024-07-28 20:19:32 +00:00
|
|
|
--- @param workspace_dir string
|
|
|
|
local function generate_config(workspace_dir)
|
|
|
|
-- get the mason install path
|
|
|
|
local install_path = require('mason-registry').get_package('jdtls'):get_install_path()
|
|
|
|
local jdtls_path = vim.fn.glob(install_path .. '/plugins/org.eclipse.equinox.launcher_*.jar')
|
|
|
|
|
|
|
|
-- try to detect sysname for config
|
|
|
|
local sysname = 'win'
|
|
|
|
if vim.fn.has 'unix' then
|
|
|
|
sysname = 'linux'
|
|
|
|
elseif vim.fn.has 'mac' then
|
|
|
|
sysname = 'mac'
|
|
|
|
end
|
|
|
|
-- set default config according to sysname
|
|
|
|
local config_path = install_path .. '/config_' .. sysname
|
|
|
|
|
|
|
|
-- See `:help vim.lsp.start_client` for an overview of the supported `config` options.
|
|
|
|
local config = {
|
|
|
|
-- The command that starts the language server
|
|
|
|
-- See: https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line
|
|
|
|
cmd = {
|
|
|
|
'java',
|
|
|
|
|
|
|
|
'-Declipse.application=org.eclipse.jdt.ls.core.id1',
|
|
|
|
'-Dosgi.bundles.defaultStartLevel=4',
|
|
|
|
'-Declipse.product=org.eclipse.jdt.ls.core.product',
|
|
|
|
'-Dlog.protocol=true',
|
|
|
|
'-Dlog.level=ALL',
|
|
|
|
'-Xmx1g',
|
|
|
|
'--add-modules=ALL-SYSTEM',
|
|
|
|
'--add-opens',
|
|
|
|
'java.base/java.util=ALL-UNNAMED',
|
|
|
|
'--add-opens',
|
|
|
|
'java.base/java.lang=ALL-UNNAMED',
|
|
|
|
|
|
|
|
'-jar',
|
|
|
|
jdtls_path,
|
|
|
|
|
|
|
|
'-configuration',
|
|
|
|
config_path,
|
|
|
|
|
|
|
|
'-data',
|
|
|
|
workspace_dir,
|
|
|
|
},
|
2024-07-28 20:08:18 +00:00
|
|
|
|
2024-07-28 20:19:32 +00:00
|
|
|
capabilities = capabilities,
|
|
|
|
root_dir = vim.fs.root(0, { '.git', 'mvnw', 'gradlew' }),
|
2024-07-28 20:08:18 +00:00
|
|
|
|
2024-07-28 20:19:32 +00:00
|
|
|
-- Here you can configure eclipse.jdt.ls specific settings
|
|
|
|
-- See https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request
|
|
|
|
-- for a list of options
|
|
|
|
settings = {
|
2024-11-05 10:37:16 +00:00
|
|
|
java = {},
|
2024-07-28 20:19:32 +00:00
|
|
|
},
|
2024-07-28 20:08:18 +00:00
|
|
|
|
2024-07-28 20:19:32 +00:00
|
|
|
-- Language server `initializationOptions`
|
|
|
|
-- See https://github.com/mfussenegger/nvim-jdtls#java-debug-installation
|
|
|
|
init_options = {
|
|
|
|
bundles = {},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
end
|
2024-07-28 20:08:44 +00:00
|
|
|
vim.api.nvim_create_autocmd('FileType', {
|
|
|
|
pattern = 'java',
|
2024-11-05 10:42:23 +00:00
|
|
|
callback = function(opt)
|
2024-07-28 20:19:32 +00:00
|
|
|
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t')
|
|
|
|
-- calculate workspace dir
|
|
|
|
local workspace_dir = vim.fn.stdpath 'data' .. '/site/java/workspace-root/' .. project_name
|
|
|
|
require('jdtls').start_or_attach(generate_config(workspace_dir))
|
2024-11-05 10:42:23 +00:00
|
|
|
vim.keymap.set('n', '<leader>we', '<cmd>Oil ' .. workspace_dir .. '<cr>', {
|
|
|
|
desc = '[W]orkspace [E]xplorer',
|
|
|
|
buffer = opt.buf,
|
|
|
|
})
|
2024-07-28 20:08:44 +00:00
|
|
|
end,
|
|
|
|
})
|
2024-07-28 20:06:51 +00:00
|
|
|
end
|
|
|
|
|
2024-02-26 19:46:46 +00:00
|
|
|
-- Ensure the servers and tools above are installed
|
|
|
|
-- To check the current status of installed tools and/or manually install
|
|
|
|
-- other tools, you can run
|
|
|
|
-- :Mason
|
|
|
|
--
|
2024-03-18 17:50:47 +00:00
|
|
|
-- You can press `g?` for help in this menu.
|
2024-02-26 19:46:46 +00:00
|
|
|
require('mason').setup()
|
|
|
|
|
|
|
|
-- You can add other tools here that you want Mason to install
|
|
|
|
-- for you, so that they are available from within Neovim.
|
|
|
|
local ensure_installed = vim.tbl_keys(servers or {})
|
|
|
|
vim.list_extend(ensure_installed, {
|
|
|
|
'stylua', -- Used to format lua code
|
2024-06-04 12:23:47 +00:00
|
|
|
'eslint_d',
|
|
|
|
'prettier',
|
|
|
|
'prettierd',
|
2024-07-25 18:34:16 +00:00
|
|
|
'markdownlint',
|
|
|
|
'isort',
|
|
|
|
'black',
|
2024-07-28 20:04:34 +00:00
|
|
|
'jdtls',
|
2024-02-26 19:46:46 +00:00
|
|
|
})
|
|
|
|
require('mason-tool-installer').setup { ensure_installed = ensure_installed }
|
|
|
|
|
|
|
|
require('mason-lspconfig').setup {
|
|
|
|
handlers = {
|
|
|
|
function(server_name)
|
2024-07-28 20:04:34 +00:00
|
|
|
if server_name == 'jdtls' then
|
2024-07-28 20:06:51 +00:00
|
|
|
jdtls_setup()
|
2024-07-28 20:04:34 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-02-26 19:46:46 +00:00
|
|
|
local server = servers[server_name] or {}
|
2024-03-01 10:09:32 +00:00
|
|
|
-- This handles overriding only values explicitly passed
|
|
|
|
-- by the server configuration above. Useful when disabling
|
2024-09-10 20:34:43 +00:00
|
|
|
-- certain features of an LSP (for example, turning off formatting for ts_ls)
|
2024-03-01 10:09:32 +00:00
|
|
|
server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
|
|
|
|
require('lspconfig')[server_name].setup(server)
|
2024-02-26 19:46:46 +00:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
-- vim: ts=2 sts=2 sw=2 et
|