neovim / nvim-lspconfig

Quickstart configs for Nvim LSP
Apache License 2.0
9.98k stars 2.04k forks source link

`on_attach` not working whatever i do #3222

Closed daUnknownCoder closed 1 week ago

daUnknownCoder commented 1 week ago

Description

plainly, on_attach isnt working

this is my on_attach:

      local on_attach = function(bufnr, client)
        if client == nil then
          print("LSP client is nil!")
          return
        end
        local function buf_set_keymap(...)
          vim.api.nvim_buf_set_keymap(bufnr, ...)
        end
        local function buf_set_option(...)
          vim.api.nvim_buf_set_option(bufnr, ...)
        end
        local function opts(desc)
          return { desc = desc, noremap = true, silent = true }
        end
        local methods = vim.lsp.protocol.Methods
        buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")
        buf_set_keymap("n", "gr", "<cmd>Lspsaga finder ref<CR>", opts("LSP Finder [References] "))
        buf_set_keymap("n", "gd", "<cmd>Lspsaga finder def<CR>", opts("LSP Finder [Definition] "))
        buf_set_keymap("n", "gI", "<cmd>Lspsaga finder imp<CR>", opts("LSP Finder [Implementation] "))
        buf_set_keymap("n", "gD", "<cmd>Lspsaga finder tyd<CR>", opts("LSP Finder [Type Definition] "))
        buf_set_keymap("n", "[d", "<cmd>lua vim.diagnostic.goto_prev()<CR>", opts("Diagnostic Jump Prev"))
        buf_set_keymap("n", "]d", "<cmd>lua vim.diagnostic.goto_next()<CR>", opts("Diagnostic Jump Next"))
        client.server_capabilities.document_formatting = true
        require("lsp_signature").on_attach({
          bind = true,
          debug = true,
          floating_window = true,
          floating_window_above_cur_line = false,
          hint_enable = true,
          fix_pos = true,
          floating_window_above_first = true,
          log_path = vim.fn.expand("$HOME") .. "/tmp/sig.log",
          padding = " ",
          handler_opts = {
            border = "rounded",
          },
        })
        if client and client.server_capabilities.documentHighlightProvider then
          local highlight_augroup = vim.api.nvim_create_augroup("NeutronVimLSPHighlight", { clear = false })
          vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
            buffer = client.buf,
            group = highlight_augroup,
            callback = vim.lsp.buf.document_highlight,
          })

          vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
            buffer = client.buf,
            group = highlight_augroup,
            callback = vim.lsp.buf.clear_references,
          })

          vim.api.nvim_create_autocmd("LspDetach", {
            group = vim.api.nvim_create_augroup("NeutronVimLSPDetach", { clear = true }),
            callback = function(bufnr2)
              vim.lsp.buf.clear_references()
              vim.api.nvim_clear_autocmds({ group = "NeutronVimLSPHighlight", buffer = bufnr2.buf })
            end,
          })
        end
        if client.supports_method(methods.textDocument_signatureHelp) then
          buf_set_keymap("i", "<C-t>", function()
            -- Close the completion menu first (if open).
            local cmp = require("cmp")
            if cmp.visible() then
              cmp.close()
            end

            vim.lsp.buf.signature_help()
          end, opts("Signature help"))
        end
        if vim.fn.has("nvim-0.10.0") == 1 then
          if client.supports_method(methods.textDocument_inlayHint) then
            local inlay_hints_group = vim.api.nvim_create_augroup("toggle_inlay_hints", { clear = false })
            vim.defer_fn(function()
              local mode = vim.api.nvim_get_mode().mode
              vim.lsp.inlay_hint.enable(mode == "n" or mode == "v", { bufnr = bufnr })
            end, 500)

            vim.api.nvim_create_autocmd("InsertEnter", {
              group = inlay_hints_group,
              desc = "Enable inlay hints",
              buffer = bufnr,
              callback = function()
                vim.lsp.inlay_hint.enable(false, { bufnr = bufnr })
              end,
            })
            vim.api.nvim_create_autocmd("InsertLeave", {
              group = inlay_hints_group,
              desc = "Disable inlay hints",
              buffer = bufnr,
              callback = function()
                vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
              end,
            })
          end
          if client.supports_method(methods.textDocument_codeLens) then
            local code_lens_group = vim.api.nvim_create_augroup("toggle_code_lens", { clear = false })
            vim.defer_fn(function()
              vim.lsp.codelens.refresh()
            end, 500)

            vim.api.nvim_create_autocmd({ "BufEnter", "CursorHold", "InsertLeave" }, {
              buffer = bufnr,
              callback = vim.lsp.codelens.refresh,
              desc = "Refresh Code Lens",
              group = code_lens_group,
            })
          end
        end
      end

so idk whats wrong.

this is my whole config ```lua return { { "neovim/nvim-lspconfig", event = { "LazyFile" }, keys = { { "gT", function() local buf = vim.api.nvim_get_current_buf() local clients = vim.lsp.get_clients({ bufnr = buf }) if not vim.tbl_isempty(clients) then vim.cmd("LspStop") else vim.cmd("LspStart") end end, desc = "Toggle LSP", }, }, dependencies = { { "hrsh7th/cmp-nvim-lsp", event = { "LspAttach", "InsertEnter" }, lazy = true, }, { "antosha417/nvim-lsp-file-operations", event = { "LspAttach" }, dependencies = { "nvim-lua/plenary.nvim", "nvim-tree/nvim-tree.lua", }, lazy = true, config = true, }, { "ray-x/lsp_signature.nvim", lazy = true, event = "InsertEnter", }, { "LukasPietzschmann/boo.nvim", lazy = true, event = "LspAttach", opts = { focus_on_open = false }, }, { "zeioth/garbage-day.nvim", dependencies = "neovim/nvim-lspconfig", enabled = function() if vim.fn.has("nvim-0.10.0") == 1 then return true end return false end, event = { "LspAttach" }, opts = { notifications = true, }, }, }, config = function() local lspconfig_status_ok, lspconfig = pcall(require, "lspconfig") if not lspconfig_status_ok then print("lspconfig not found!") end local cmp_nvim_lsp_status_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp") if not cmp_nvim_lsp_status_ok then print("cmp_nvim_lsp not found!") end local lsp_file_operations_status_ok, lsp_file_operations = pcall(require, "lsp-file-operations") if not lsp_file_operations_status_ok then print("lsp_file_operations not found!") end local icons_ok, icons = pcall(require, "NeutronVim.core.icons") if not icons_ok then print("Unable to import icons!") end lsp_file_operations.setup() local on_attach = function(bufnr, client) if client == nil then print("LSP client is nil!") return end local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end local function opts(desc) return { desc = desc, noremap = true, silent = true } end local methods = vim.lsp.protocol.Methods buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc") buf_set_keymap("n", "gr", "Lspsaga finder ref", opts("LSP Finder [References] ")) buf_set_keymap("n", "gd", "Lspsaga finder def", opts("LSP Finder [Definition] ")) buf_set_keymap("n", "gI", "Lspsaga finder imp", opts("LSP Finder [Implementation] ")) buf_set_keymap("n", "gD", "Lspsaga finder tyd", opts("LSP Finder [Type Definition] ")) buf_set_keymap("n", "[d", "lua vim.diagnostic.goto_prev()", opts("Diagnostic Jump Prev")) buf_set_keymap("n", "]d", "lua vim.diagnostic.goto_next()", opts("Diagnostic Jump Next")) client.server_capabilities.document_formatting = true require("lsp_signature").on_attach({ bind = true, debug = true, floating_window = true, floating_window_above_cur_line = false, hint_enable = true, fix_pos = true, floating_window_above_first = true, log_path = vim.fn.expand("$HOME") .. "/tmp/sig.log", padding = " ", handler_opts = { border = "rounded", }, }) if client and client.server_capabilities.documentHighlightProvider then local highlight_augroup = vim.api.nvim_create_augroup("NeutronVimLSPHighlight", { clear = false }) vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, { buffer = client.buf, group = highlight_augroup, callback = vim.lsp.buf.document_highlight, }) vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, { buffer = client.buf, group = highlight_augroup, callback = vim.lsp.buf.clear_references, }) vim.api.nvim_create_autocmd("LspDetach", { group = vim.api.nvim_create_augroup("NeutronVimLSPDetach", { clear = true }), callback = function(bufnr2) vim.lsp.buf.clear_references() vim.api.nvim_clear_autocmds({ group = "NeutronVimLSPHighlight", buffer = bufnr2.buf }) end, }) end if client.supports_method(methods.textDocument_signatureHelp) then buf_set_keymap("i", "", function() -- Close the completion menu first (if open). local cmp = require("cmp") if cmp.visible() then cmp.close() end vim.lsp.buf.signature_help() end, opts("Signature help")) end if vim.fn.has("nvim-0.10.0") == 1 then if client.supports_method(methods.textDocument_inlayHint) then local inlay_hints_group = vim.api.nvim_create_augroup("toggle_inlay_hints", { clear = false }) vim.defer_fn(function() local mode = vim.api.nvim_get_mode().mode vim.lsp.inlay_hint.enable(mode == "n" or mode == "v", { bufnr = bufnr }) end, 500) vim.api.nvim_create_autocmd("InsertEnter", { group = inlay_hints_group, desc = "Enable inlay hints", buffer = bufnr, callback = function() vim.lsp.inlay_hint.enable(false, { bufnr = bufnr }) end, }) vim.api.nvim_create_autocmd("InsertLeave", { group = inlay_hints_group, desc = "Disable inlay hints", buffer = bufnr, callback = function() vim.lsp.inlay_hint.enable(true, { bufnr = bufnr }) end, }) end if client.supports_method(methods.textDocument_codeLens) then local code_lens_group = vim.api.nvim_create_augroup("toggle_code_lens", { clear = false }) vim.defer_fn(function() vim.lsp.codelens.refresh() end, 500) vim.api.nvim_create_autocmd({ "BufEnter", "CursorHold", "InsertLeave" }, { buffer = bufnr, callback = vim.lsp.codelens.refresh, desc = "Refresh Code Lens", group = code_lens_group, }) end end end local capabilities = vim.tbl_deep_extend("force", vim.lsp.protocol.make_client_capabilities(), cmp_nvim_lsp.default_capabilities()) capabilities.textDocument.completion = cmp_nvim_lsp.default_capabilities().textDocument.completion local signs = { Error = icons.diagnostics.Error, Warn = icons.diagnostics.Warning, Hint = icons.diagnostics.Hint, Info = icons.diagnostics.Info, } for type, icon in pairs(signs) do local hl = "DiagnosticSign" .. type vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl }) end vim.diagnostic.config({ signs = { text = { "", "", "", "" } }, -- {"", "", ""} float = { show_header = true, border = "rounded", suffix = "", focusable = false, enabled = false, source = "always", header = { "  Diagnostics", "String" }, prefix = function(_, _, _) return "  ", "String" end, -- icons:        }, update_in_insert = false, underline = true, severity_sort = true, }) require("lspconfig.ui.windows").default_options.border = "rounded" lspconfig["rust_analyzer"].setup({ capabilities = capabilities, on_attach = on_attach, cmd = { "rustup", "run", "stable", "rust-analyzer", }, }) local util = require("lspconfig/util") local path = util.path lspconfig["html"].setup({ capabilities = capabilities, on_attach = on_attach, }) lspconfig["tsserver"].setup({ capabilities = capabilities, on_attach = on_attach, }) lspconfig["clangd"].setup({ capabilities = capabilities, on_attach = on_attach, }) lspconfig["cmake"].setup({ capabilities = capabilities, on_attach = on_attach, }) lspconfig["bashls"].setup({ capabilities = capabilities, on_attach = on_attach, }) lspconfig["basedpyright"].setup({ capabilities = capabilities, on_attach = on_attach, settings = { python = { analysis = { autoSearchPaths = true, useLibraryCodeForTypes = true, diagnosticMode = "openFilesOnly", }, }, }, root_dir = function(fname) return util.root_pattern(".git", "setup.py", "setup.cfg", "pyproject.toml", "requirements.txt")(fname) or util.path.dirname(fname) end, before_init = function(_, config) local default_venv_path = path.join(vim.env.HOME, "virtualenvs", os.getenv("VIRTUAL_ENV"), "bin", "python") config.settings.python.pythonPath = default_venv_path end, }) lspconfig["cssls"].setup({ capabilities = capabilities, on_attach = on_attach, }) lspconfig["marksman"].setup({ capabilities = capabilities, on_attach = on_attach, }) lspconfig["vimls"].setup({ capabilities = capabilities, on_attach = on_attach, }) lspconfig["lua_ls"].setup({ capabilities = capabilities, on_attach = on_attach, settings = { Lua = { runtime = { version = "LuaJIT", path = vim.split(package.path, ";") }, completion = { keywordSnippet = "Disable", callSnippet = "Replace" }, diagnostics = { globals = { "vim", "describe", "it", "before_each", "after_each" }, }, hint = { enable = true, }, codeLens = { enable = true, }, workspace = { library = { [vim.fn.expand("$VIMRUNTIME/lua")] = true, [vim.fn.expand("$VIMRUNTIME/lua/vim/lsp")] = true, [vim.fn.stdpath("config") .. "/lua"] = true, }, checkThirdParty = false, }, -- telemetry = { enable = false }, }, }, }) end, }, { "folke/lazydev.nvim", ft = "lua", config = true, enabled = function() if vim.fn.has("nvim-0.10.0") == 1 then return true end return false end, }, } ```

maybe unrelated but i wanted help with lua_ls: https://new.reddit.com/r/neovim/comments/1debxtc/lua_ls_sweating_up_my_device/

check this too! while using this keymaps: ```lua -- Move text up and down [in all modes] with `J` and `K` map("n", "J", ":m .+1==", { desc = "Move down", silent = true }) map("n", "K", ":m .-2==", { desc = "Move up", silent = true }) map("i", "", "m .-2==gi", { desc = "Move up", silent = true }) map("i", "", "m .+1==gi", { desc = "Move down", silent = true }) map("v", "J", ":m '>+1gv=gv", { desc = "Move down", silent = true }) map("v", "K", ":m '<-2gv=gv", { desc = "Move up", silent = true }) ``` i noticed this: ![image](https://github.com/neovim/nvim-lspconfig/assets/84800625/729dc566-7abc-41ae-b301-7fbf98277126) so how to minimize that damage to my cpu: my luals config: ```lua lspconfig["lua_ls"].setup({ capabilities = capabilities, on_attach = on_attach, settings = { Lua = { runtime = { version = "LuaJIT", path = vim.split(package.path, ";") }, completion = { keywordSnippet = "Disable", callSnippet = "Replace" }, diagnostics = { globals = { "vim", "describe", "it", "before_each", "after_each" }, }, hint = { enable = true, }, codeLens = { enable = true, }, workspace = { library = { [vim.fn.expand("$VIMRUNTIME/lua")] = true, [vim.fn.expand("$VIMRUNTIME/lua/vim/lsp")] = true, [vim.fn.stdpath("config") .. "/lua"] = true, }, checkThirdParty = false, }, -- telemetry = { enable = false }, }, }, }) ```

and yeah pls dont close this for not providing minimal.lua because i suck at it 🙏🏻

glepnir commented 1 week ago

first param is client

daUnknownCoder commented 1 week ago

first param is client

Sorry about that but I just tried switching places, and it doesn't matter (earlier first param was client)

daUnknownCoder commented 1 week ago

It doesn't matter whatever I put there but it doesn't attach (not only for lua_ls but for every language) Check : https://www.reddit.com/r/neovim/comments/1dnl0f9/lsp_stuff_not_working/

daUnknownCoder commented 1 week ago

hi @glepnir are we re-opening this?

idk if this is related but, i recently started using lsp_lines.nvim in favor of vim.diagnostic.open_float() which stopped working recently due to this