113 lines
		
	
	
	
		
			4.1 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
		
		
			
		
	
	
			113 lines
		
	
	
	
		
			4.1 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
| 
								 | 
							
								local function lsp_highlight_document(client)
							 | 
						||
| 
								 | 
							
								    if client.server_capabilities.document_highlight then
							 | 
						||
| 
								 | 
							
								        vim.api.nvim_create_augroup("lsp_document_highlight", { clear = true })
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        vim.api.nvim_create_autocmd("CursorHold", {
							 | 
						||
| 
								 | 
							
								            group = "lsp_document_highlight",
							 | 
						||
| 
								 | 
							
								            buffer = 0,
							 | 
						||
| 
								 | 
							
								            callback = function()
							 | 
						||
| 
								 | 
							
								                vim.lsp.buf.document_highlight()
							 | 
						||
| 
								 | 
							
								            end,
							 | 
						||
| 
								 | 
							
								        })
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        vim.api.nvim_create_autocmd("CursorMoved", {
							 | 
						||
| 
								 | 
							
								            group = "lsp_document_highlight",
							 | 
						||
| 
								 | 
							
								            buffer = 0,
							 | 
						||
| 
								 | 
							
								            callback = function()
							 | 
						||
| 
								 | 
							
								                vim.lsp.buf.clear_references()
							 | 
						||
| 
								 | 
							
								            end,
							 | 
						||
| 
								 | 
							
								        })
							 | 
						||
| 
								 | 
							
								    end
							 | 
						||
| 
								 | 
							
								end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								local function lsp_keymaps(bufnr)
							 | 
						||
| 
								 | 
							
								    local opts = { noremap = true, silent = true }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    vim.api.nvim_buf_set_keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
							 | 
						||
| 
								 | 
							
								    vim.api.nvim_buf_set_keymap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
							 | 
						||
| 
								 | 
							
								    vim.api.nvim_buf_set_keymap(bufnr, "n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
							 | 
						||
| 
								 | 
							
								    vim.api.nvim_buf_set_keymap(bufnr, "n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
							 | 
						||
| 
								 | 
							
								    vim.api.nvim_buf_set_keymap(bufnr, "n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
							 | 
						||
| 
								 | 
							
								    vim.api.nvim_buf_set_keymap(bufnr, "n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
							 | 
						||
| 
								 | 
							
								    vim.api.nvim_buf_set_keymap(bufnr, "n", "[d", '<cmd>lua vim.diagnostic.goto_prev({border="rounded"})<CR>', opts)
							 | 
						||
| 
								 | 
							
								    vim.api.nvim_buf_set_keymap(bufnr, "n", "]d", '<cmd>lua vim.diagnostic.goto_next({border="rounded"})<CR>', opts)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>lr", "<cmd>lua vim.lsp.buf.rename()<cr>", opts)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>q", "<cmd>lua vim.diagnostic.setloclist()<CR>", opts)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    vim.api.nvim_buf_set_keymap(
							 | 
						||
| 
								 | 
							
								        bufnr,
							 | 
						||
| 
								 | 
							
								        "n",
							 | 
						||
| 
								 | 
							
								        "<leader>td",
							 | 
						||
| 
								 | 
							
								        "<cmd>lua require 'telescope.builtin'.diagnostics()<cr>",
							 | 
						||
| 
								 | 
							
								        opts
							 | 
						||
| 
								 | 
							
								    )
							 | 
						||
| 
								 | 
							
								    vim.api.nvim_buf_set_keymap(
							 | 
						||
| 
								 | 
							
								        bufnr,
							 | 
						||
| 
								 | 
							
								        "n",
							 | 
						||
| 
								 | 
							
								        "<leader>tr",
							 | 
						||
| 
								 | 
							
								        "<cmd>lua require 'telescope.builtin'.lsp_references()<CR>",
							 | 
						||
| 
								 | 
							
								        opts
							 | 
						||
| 
								 | 
							
								    )
							 | 
						||
| 
								 | 
							
								end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								local status_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
							 | 
						||
| 
								 | 
							
								if not status_ok then
							 | 
						||
| 
								 | 
							
								    print("Failed to require cmp_nvim_lsp")
							 | 
						||
| 
								 | 
							
								    return
							 | 
						||
| 
								 | 
							
								end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								local client_capabilities = vim.lsp.protocol.make_client_capabilities()
							 | 
						||
| 
								 | 
							
								local capabilities = cmp_nvim_lsp.default_capabilities(client_capabilities)
							 | 
						||
| 
								 | 
							
								capabilities.offsetEncoding = { "utf-8", "utf-16" }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								---@brief
							 | 
						||
| 
								 | 
							
								---
							 | 
						||
| 
								 | 
							
								--- https://github.com/hrsh7th/vscode-langservers-extracted
							 | 
						||
| 
								 | 
							
								---
							 | 
						||
| 
								 | 
							
								--- `css-languageserver` can be installed via `npm`:
							 | 
						||
| 
								 | 
							
								---
							 | 
						||
| 
								 | 
							
								--- ```sh
							 | 
						||
| 
								 | 
							
								--- npm i -g vscode-langservers-extracted
							 | 
						||
| 
								 | 
							
								--- ```
							 | 
						||
| 
								 | 
							
								---
							 | 
						||
| 
								 | 
							
								--- Neovim does not currently include built-in snippets. `vscode-css-language-server` only provides completions when snippet support is enabled. To enable completion, install a snippet plugin and add the following override to your language client capabilities during setup.
							 | 
						||
| 
								 | 
							
								---
							 | 
						||
| 
								 | 
							
								--- ```lua
							 | 
						||
| 
								 | 
							
								--- --Enable (broadcasting) snippet capability for completion
							 | 
						||
| 
								 | 
							
								--- local capabilities = vim.lsp.protocol.make_client_capabilities()
							 | 
						||
| 
								 | 
							
								--- capabilities.textDocument.completion.completionItem.snippetSupport = true
							 | 
						||
| 
								 | 
							
								---
							 | 
						||
| 
								 | 
							
								--- vim.lsp.config('cssls', {
							 | 
						||
| 
								 | 
							
								---   capabilities = capabilities,
							 | 
						||
| 
								 | 
							
								--- })
							 | 
						||
| 
								 | 
							
								--- ```
							 | 
						||
| 
								 | 
							
								return {
							 | 
						||
| 
								 | 
							
								    cmd = { 'vscode-css-language-server', '--stdio' },
							 | 
						||
| 
								 | 
							
								    filetypes = { 'css', 'scss', 'less' },
							 | 
						||
| 
								 | 
							
								    init_options = { provideFormatter = true }, -- needed to enable formatting capabilities
							 | 
						||
| 
								 | 
							
								    root_markers = { 'package.json', '.git' },
							 | 
						||
| 
								 | 
							
								    settings = {
							 | 
						||
| 
								 | 
							
								        css = { validate = true },
							 | 
						||
| 
								 | 
							
								        scss = { validate = true },
							 | 
						||
| 
								 | 
							
								        less = { validate = true },
							 | 
						||
| 
								 | 
							
								    },
							 | 
						||
| 
								 | 
							
								    capabilities = capabilities,
							 | 
						||
| 
								 | 
							
								    on_attach = function(client, bufnr)
							 | 
						||
| 
								 | 
							
								        lsp_keymaps(bufnr)
							 | 
						||
| 
								 | 
							
								        lsp_highlight_document(client)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if client.supports_method("textDocument/formatting") then
							 | 
						||
| 
								 | 
							
								            vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
							 | 
						||
| 
								 | 
							
								            vim.api.nvim_create_autocmd("BufWritePre", {
							 | 
						||
| 
								 | 
							
								                group = augroup,
							 | 
						||
| 
								 | 
							
								                buffer = bufnr,
							 | 
						||
| 
								 | 
							
								                callback = function()
							 | 
						||
| 
								 | 
							
								                    vim.lsp.buf.format()
							 | 
						||
| 
								 | 
							
								                end,
							 | 
						||
| 
								 | 
							
								            })
							 | 
						||
| 
								 | 
							
								        end
							 | 
						||
| 
								 | 
							
								    end,
							 | 
						||
| 
								 | 
							
								}
							 |