summaryrefslogtreecommitdiffstats
path: root/.config/nvim/lua/lsp.lua
diff options
context:
space:
mode:
Diffstat (limited to '.config/nvim/lua/lsp.lua')
-rw-r--r--.config/nvim/lua/lsp.lua114
1 files changed, 114 insertions, 0 deletions
diff --git a/.config/nvim/lua/lsp.lua b/.config/nvim/lua/lsp.lua
new file mode 100644
index 0000000..4b5fe02
--- /dev/null
+++ b/.config/nvim/lua/lsp.lua
@@ -0,0 +1,114 @@
+---LSP OnAttach
+---@param client vim.lsp.Client
+---@param buf integer
+function Lsp(client, buf)
+ vim.keymap.set('n', '<leader>f', function()
+ vim.lsp.buf.format { async = true }
+ end, { buffer = buf })
+
+ vim.api.nvim_buf_create_user_command(buf, "Format", function(args)
+ local range = nil
+ if args.count ~= -1 then
+ local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1]
+ range = {
+ start = { args.line1, 0 },
+ ["end"] = { args.line2, end_line:len() },
+ }
+ end
+ vim.lsp.buf.format { async = true, range = range, bufnr = buf }
+ end, { range = true })
+
+ vim.keymap.set('n', '<leader>wa', function()
+ vim.lsp.buf.add_workspace_folder()
+ end, { buffer = buf })
+
+ vim.keymap.set('n', '<leader>wr', function()
+ vim.lsp.buf.remove_workspace_folder()
+ end, { buffer = buf })
+
+ vim.keymap.set('n', '<leader>wl', function()
+ local dir = vim.lsp.buf.list_workspace_folders()
+ vim.ui.select(dir, {
+ prompt = 'Workspace Dir: ',
+ }, function(result)
+ if result then
+ vim.api.nvim_set_current_dir(result)
+ end
+ end)
+ end, { buffer = buf })
+
+ vim.api.nvim_buf_create_user_command(buf, "Symbols", function(args)
+ if args.args == ""
+ then
+ vim.lsp.buf.workspace_symbol()
+ else
+ vim.lsp.buf.workspace_symbol(args.args)
+ end
+ end, { nargs = "?" })
+
+
+ if client:supports_method('textDocument/inlayHint') then
+ vim.lsp.inlay_hint.enable()
+
+ vim.keymap.set('n', '<leader>ih', function()
+ vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled())
+ end, { buffer = buf })
+ end
+
+ if client:supports_method('textDocument/completion') then
+ 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, buf, { autotrigger = true })
+ end
+
+ if client:supports_method('textDocument/documentHighlight') then
+ local highlight_augroup = vim.api.nvim_create_augroup('lsp-highlight',
+ { clear = false })
+ vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
+ buffer = buf,
+ group = highlight_augroup,
+ callback = vim.lsp.buf.document_highlight,
+ })
+
+ vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
+ buffer = buf,
+ group = highlight_augroup,
+ callback = vim.lsp.buf.clear_references,
+ })
+
+ vim.api.nvim_create_autocmd('LspDetach', {
+ callback = function(event)
+ vim.lsp.buf.clear_references()
+ vim.api.nvim_clear_autocmds {
+ group = highlight_augroup,
+ buffer = event.buf
+ }
+ end,
+ })
+ end
+
+ if client:supports_method('textDocument/foldingRange') then
+ local win = vim.api.nvim_get_current_win()
+
+ vim.wo[win][0].foldmethod = "expr"
+ vim.wo[win][0].foldexpr = 'v:lua.vim.lsp.foldexpr()'
+ end
+
+ if client:supports_method('textDocument/codelens') then
+ vim.keymap.set({ 'n', 'v' }, 'grl', function()
+ vim.lsp.codelens.run()
+ end, { buffer = buf })
+ vim.lsp.codelens.refresh { bufnr = buf }
+ vim.api.nvim_create_autocmd({ 'CursorHold', 'InsertLeave' }, {
+ buffer = buf,
+ callback = function()
+ vim.lsp.codelens.refresh { bufnr = buf }
+ end,
+ })
+ end
+end
+
+return Lsp