VonHeikemen / lsp-zero.nvim

A starting point to setup some lsp related features in neovim.
https://lsp-zero.netlify.app/v3.x/
MIT License
3.68k stars 94 forks source link

Key mapping not working properly for lsp-zero #261

Closed vaibhav135 closed 1 year ago

vaibhav135 commented 1 year ago

The Problem

So basically if you check my lsp-zero.lua only initial 2-3 keymappings are working and other than those none are working. When I execute :nmap gl (gl is mapped to open_float()) it says no mapping found.

neovim version: 0.9.0

folder Structure

full folder structure image

Some other configs.

Here is my lsp-zero.lua ``` lua local lsp = require("lsp-zero").preset({}) local status_cmp_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp") if not status_cmp_ok then return end lsp.format_on_save({ format_opts = { timeout_ms = 30000, }, servers = { ["rust_analyzer"] = { "rust" }, }, }) --local lsp = require("lsp-zero") --lsp.preset("recommended") lsp.ensure_installed({ "prismals", "rust_analyzer", "yamlls", "jsonls", "lua_ls", "cssls", "tailwindcss", "tsserver", "vimls", "eslint", }) -- Fix Undefined global 'vim' lsp.nvim_workspace() --lsp.setup_nvim_cmp({ --mapping = cmp_mappings, --}) lsp.set_preferences({ suggest_lsp_servers = false, sign_icons = { error = "E", warn = "W", hint = "H", info = "I", }, }) local function lsp_keymaps(bufnr) local opts = { noremap = true, silent = true } local keymap = vim.api.nvim_buf_set_keymap keymap(bufnr, "n", "gD", "lua vim.lsp.buf.declaration()", opts) keymap(bufnr, "n", "gd", "lua vim.lsp.buf.definition()", opts) keymap(bufnr, "n", "K", "lua vim.lsp.buf.hover()", opts) keymap(bufnr, "n", "gI", "lua vim.lsp.buf.implementation()", opts) keymap(bufnr, "n", "gr", "lua vim.lsp.buf.references()", opts) keymap(bufnr, "n", "gl", "lua vim.diagnostic.open_float()", opts) keymap(bufnr, "n", "lf", "lua vim.lsp.buf.format{ async = true }", opts) keymap(bufnr, "n", "li", "LspInfo", opts) keymap(bufnr, "n", "lI", "LspInstallInfo", opts) keymap(bufnr, "n", "la", "lua vim.lsp.buf.code_action()", opts) keymap(bufnr, "n", "lj", "lua vim.diagnostic.goto_next({buffer=0})", opts) keymap(bufnr, "n", "lk", "lua vim.diagnostic.goto_prev({buffer=0})", opts) keymap(bufnr, "n", "lr", "lua vim.lsp.buf.rename()", opts) keymap(bufnr, "n", "ls", "lua vim.lsp.buf.signature_help()", opts) keymap(bufnr, "n", "lq", "lua vim.diagnostic.setloclist()", opts) end lsp.on_attach(function(client, bufnr) local opts = { buffer = bufnr, remap = false } if client.name == "tsserver" or client.name == "eslint" then client.server_capabilities.documentFormattingProvider = false end if client.name == "lua_ls" then client.server_capabilities.documentFormattingProvider = false end lsp.default_keymaps({ buffer = bufnr }) --vim.cmd([[autocmd BufWritePre lua vim.lsp.buf.format()]]) lsp_keymaps(bufnr) end) vim.diagnostic.config({ virtual_text = true, }) lsp.setup() --local cmp = require('cmp') --local cmp_config = lsp.defaults.cmp_config({}) --cmp.setup(cmp_config) ```
My main init.lua ``` lua vim.cmd.packadd("packer.nvim") --Required modules require("lualine").setup({}) require("nvim-autopairs").setup({}) require("impatient") require("vaibhav135.dashboard") require("null-ls") require("impatient") require("telescope") require("vaibhav135.toggleterm") require("vaibhav135.set") require("nvim-web-devicons").get_icons() vim.cmd.colorscheme("catppuccin") --require("gitsigns").setup() require("packer").startup(function(use) -- packer can update itself use("wbthomason/packer.nvim") --Startup screen for neovim use({ "glepnir/dashboard-nvim", event = "VimEnter", requires = { "nvim-tree/nvim-web-devicons" }, }) -- Telescope use({ "nvim-telescope/telescope.nvim", tag = "0.1.1", -- or , branch = '0.1.x', requires = { { "nvim-lua/plenary.nvim" } }, }) use({ "nvim-telescope/telescope-fzf-native.nvim", run = "make" }) --Markdown support for neovim -- install without yarn or npm use({ "iamcco/markdown-preview.nvim", run = function() vim.fn["mkdp#util#install"]() end, }) --use({ --"iamcco/markdown-preview.nvim", --run = "cd app && npm install", --setup = function() --vim.g.mkdp_filetypes = { "markdown" } --end, --ft = { "markdown" }, --}) --Get warnings and errors -- Lua use({ "folke/trouble.nvim", requires = "nvim-tree/nvim-web-devicons", config = function() require("trouble").setup({ -- your configuration comes here -- or leave it empty to use the default settings -- refer to the configuration section below }) end, }) --Decrease startup for neovim use("lewis6991/impatient.nvim") --Terminal in neovim use("akinsho/toggleterm.nvim") --Code formatting for lua use({ "ckipp01/stylua-nvim" }) --Git signs "git blame" use({ "lewis6991/gitsigns.nvim", -- tag = 'release' -- To use the latest release (do not use this if you run Neovim nightly or dev builds!) }) -- LSP use({ "nvim-treesitter/nvim-treesitter", run = function() local ts_update = require("nvim-treesitter.install").update({ with_sync = true }) ts_update() end, }) --Themes use({ "catppuccin/nvim", as = "catppuccin" }) use("jose-elias-alvarez/null-ls.nvim") use("nvim-lua/plenary.nvim") --Fancy tabs. use({ "akinsho/bufferline.nvim", tag = "*", requires = "nvim-tree/nvim-web-devicons" }) --Fancy taskbar use({ "nvim-lualine/lualine.nvim", requires = { "nvim-tree/nvim-web-devicons", opt = true }, }) --Bracket Provider. use({ "windwp/nvim-autopairs", config = function() require("nvim-autopairs").setup({}) end, }) use({ "VonHeikemen/lsp-zero.nvim", branch = "v1.x", requires = { -- LSP Support { "neovim/nvim-lspconfig" }, { "williamboman/mason.nvim" }, { "williamboman/mason-lspconfig.nvim" }, -- Autocompletion { "hrsh7th/nvim-cmp" }, { "hrsh7th/cmp-buffer" }, { "hrsh7th/cmp-path" }, { "saadparwaiz1/cmp_luasnip" }, { "hrsh7th/cmp-nvim-lsp" }, { "hrsh7th/cmp-nvim-lua" }, -- Snippets { "L3MON4D3/LuaSnip" }, { "rafamadriz/friendly-snippets" }, }, }) end) ```
Here is my init.vim ``` vim call plug#begin() Plug 'preservim/nerdtree' Plug 'tpope/vim-surround' Plug 'tpope/vim-fugitive' Plug 'scrooloose/nerdcommenter' Plug 'puremourning/vimspector' Plug 'numirias/semshi', { 'do': ':UpdateRemotePlugins' } "" Plug 'ctrlpvim/ctrlp.vim' call plug#end() " -------------------------------------------------------------------------------------------------------------------------------- " --------------------------------------------- Default configuration ----------------------------------------------------------- " enable termguicolors if (has("termguicolors")) set termguicolors endif filetype off filetype plugin indent on filetype plugin on "let g:onedark_termcolors=256 " Change to be comma let mapleader="," let g:mapleader="," set encoding=UTF-8 set nocompatible| " be iMproved, required set number set hidden set switchbuf=useopen,usetab set relativenumber set t_Co=256 set backspace=indent,eol,start| "more powerful backspacing set nowrap set tabstop=4 set shiftwidth=4 set noswapfile set autoindent set smartindent set cindent set showcmd set ma set nocompatible| "Limit search to your project set path+=** "Search all subdirectories and recursively set wildmenu "Shows multiple matches on one line set background=dark set scrolloff=8 set updatetime=50 set colorcolumn="80" set statusline+=%{get(b:,'gitsigns_status','')} hi Visual term=reverse cterm=reverse guibg=Grey "Per default netrw leaves unmodified buffers open. This autocommand "delets netrw's buffer once it's hidden (using ' :q,for example) autocmd FileType netrw setl bufhidden=delete| "or use :qa! "autocmd FileType python let b:coc_root_patterns = ['.git', '.env', 'venv', '.venv', 'setup.cfg', 'setup.py', 'setup.ini', 'pyproject.toml', 'pyrightconfig.json'] " configuration for sass or scss autocmd FileType scss setl iskeyword+=@-@ "Ref: https://akhatib.com/auto-save-load-views-in-vim/ au BufWinLeave *.* mkview au BufWinEnter *.* silent loadview "fonts and colorschemes "syntax on "colorscheme onedark "let g:molokai_original = 1 "buffer splitting nnoremap ev :vsplit $MYVIMRC nnoremap eh :split $MYVIMRC "source vimrc or inti.vim nnoremap sv :source $MYVIMRC "buffer control map bn :bn map bp :bp map bd :bd " comments in vimrc start with " in normal mode nnoremap : echoe "Use h" nnoremap : echoe "Use l" nnoremap : echoe "Use k" nnoremap : echoe "Use j" " " in insert mode "inoremap : echoe "Use h" "inoremap : echoe "Use l" "inoremap : echoe "Use k" "inoremap : echoe "Use j" "Sidebar "settings for netrw " Shortcut to use blackhole register by default nnoremap d "_d vnoremap d "_d nnoremap D "_D vnoremap D "_D nnoremap c "_c vnoremap c "_c nnoremap C "_C vnoremap C "_C nnoremap x "_x vnoremap x "_x nnoremap X "_X vnoremap X "_X " Shortcut to use clipboard with nnoremap d d vnoremap d d nnoremap D D vnoremap D D nnoremap c c vnoremap c c nnoremap C C vnoremap C C nnoremap x x vnoremap x x nnoremap X X vnoremap X X " path for python2 and python3 in neovim let g:python3_host_prog = '/usr/bin/python3' let g:python2_host_prog = '/usr/bin/python2' " enable and disable for certain file extensions "let s:my_coc_file_types = ['c', 'cpp', '*js', '*ts', 'rs', 'go', 'py'] "function! s:disable_coc_for_type() "if index(g:my_coc_file_types, &filetype) == -1 "let b:coc_enabled = 0 "endif "endfunction "augroup CocGroup "autocmd! "autocmd BufNew,BufEnter * call s:disable_coc_for_type() "augroup end " "let g:coc_global_extensions = [ "\ 'coc-tsserver' "\ ] "autocmd FileType scss setl iskeyword+=@-@ "set re=0 "if isdirectory('./node_modules') && isdirectory('./node_modules/prettier') "let g:coc_global_extensions += ['coc-prettier'] "endif "if isdirectory('./node_modules') && isdirectory('./node_modules/eslint') "let g:coc_global_extensions += ['coc-eslint'] "endif " disabling coc for python files "autocmd FileType python,venv,ASCII text,vim let b:coc_suggest_disable = 1 "disabling deoplete for certain file types "autocmd FileType tex,javascript,typescript,dart "\ call deoplete#custom#buffer_option('auto_complete', v:false) "COC flutter configuration aap for current paragraph, aw for "the current word "xmap a (coc-codeaction-selected) "nmap a (coc-codeaction-selected) "let dart_html_in_string=v:true "let g:lsc_server_commands = {'dart': 'dart_language_server'} "let g:lsc_auto_map = {'defaults': v:true,'PreviousReference': '',} "let g:dart_style_guide = 2 "let g:dart_format_on_save = 1 "let g:lsc_auto_map = v:true "let g:dartfmt_options "" Use `[g` and `]g` to navigate diagnostics "nmap [g (coc-diagnostic-prev) "nmap ]g (coc-diagnostic-next) "" GoTo code navigation. "nmap gd (coc-definition) "nmap gy (coc-type-definition) "nmap gi (coc-implementation) "nmap gr (coc-references) ""performing code-action "nmap do (coc-codeaction) "nmap rn (coc-rename) "jedi-vim keybindings "let g:jedi#goto_command = "cd" "let g:jedi#goto_assignments_command = "g" "let g:jedi#goto_stubs_command = "s" "let g:jedi#goto_definitions_command = "gd" "let g:jedi#documentation_command = "K" "let g:jedi#usages_command = "n" "let g:jedi#completions_command = "" "let g:jedi#rename_command = "r" " -------------------------------------------------------------------------------------------------------------------------------- " --------------------------------------------- NerdTree configuration ---------------------------------------------------------------- " nerdtree keybindings let NERDTreeShowHidden=1 nnoremap n :NERDTreeFocus nnoremap :NERDTree nnoremap :NERDTreeToggle % nnoremap r :NERDTreeFind " Exit Vim if NERDTree is the only window remaining in the only tab. autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif " -------------------------------------------------------------------------------------------------------------------------------- " --------------------------------------------- FZF configuration ---------------------------------------------------------------- "let g:fzf_layout = { 'down': '20%' } "autocmd! FileType fzf "autocmd FileType fzf set laststatus=0 noshowmode noruler "\| autocmd BufLeave set laststatus=2 showmode ruler "" PLUGIN: FZF "nnoremap bf :Buffers "nnoremap :Files "nnoremap f :Rg "nnoremap / :BLines "nnoremap ' :Marks "nnoremap g :Commits "nnoremap H :Helptags "nnoremap hh :History "nnoremap h: :History: "nnoremap h/ :History/ "command! -bang -nargs=* Rg call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case ".shellescape(), 1, {'options': '--delimiter : --nth 4..'}, 0) " --------------------------------------------------------------------------------------------------------------------------------- " --------------------------------------------- Vimspector configuration --------------------------------------------------------- fun! GotoWindow(id) call win_gotoid(a:id) "MaximizerToggle endfun " keymappings for Vimspector " vimspector configuration let g:vimspector_enable_mappings = 'HUMAN' " mnemonic 'di' = 'debug inspect' (pick your own, if you prefer!) " for normal mode - the word under the cursor nmap db VimspectorBalloonEval " for visual mode, the visually selected text xmap db VimspectorBalloonEval nmap dd :call vimspector#Launch() nmap dgc :call GotoWindow(g:vimspector_session_windows.code) nmap dgt :call GotoWindow(g:vimspector_session_windows.tagpage) nmap dgv :call GotoWindow(g:vimspector_session_windows.variables) nmap dgw :call GotoWindow(g:vimspector_session_windows.watches) nmap dgs :call GotoWindow(g:vimspector_session_windows.stack_trace) nmap dgo :call GotoWindow(g:vimspector_session_windows.output) nmap dx :VimspectorReset nmap de :VimspectorEval nmap dw :VimspectorWatch nmap do :VimspectorShowOutput nmap dso VimspectorStepOver nmap dsi VimspectorStepInto nmap dss VimspectorStepOut " ----------------------------------------------------------------------------------------------------------------------------------------- " --------------------------------------------- Vim-Lua configuration -------------------------------------------------------------------- lua << EOF require "vaibhav135" EOF ```

Note:- I am a noob in neovim.

VonHeikemen commented 1 year ago

What keymaps are working?

vaibhav135 commented 1 year ago

@VonHeikemen in the lsp-zero I have provided above, only these are working

keymap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
keymap(bufnr, "n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
VonHeikemen commented 1 year ago

I tested your lsp-zero config and everything works fine.

You can try with a minimal config. Use the -u flag to test with a different configuration file.

Create a script called test.lua and add this config.

require('mason').setup({})
require('mason-lspconfig').setup({})

require('lspconfig').lua_ls.setup({
  on_attach = function(client, bufnr)
    local opts = { noremap = true, silent = true }
    local keymap = vim.api.nvim_buf_set_keymap
    keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
    keymap(bufnr, "n", "gl", "<cmd>lua vim.diagnostic.open_float()<CR>", opts)
  end
})

Then start neovim using the test script: nvim -u /path/to/test.lua

vaibhav135 commented 1 year ago

@VonHeikemen it's still the same. gl is not working. If you want I can also share my github repo with my local nvim configuation.

vaibhav135 commented 1 year ago

@VonHeikemen this is my neovim configuration repo

VonHeikemen commented 1 year ago

Your lsp.lua file might be overriding the settings you have in lsp-zero.lua

I suggest you keep all your lsp configuration in one file.

vaibhav135 commented 1 year ago

sure I will try that.

vaibhav135 commented 1 year ago

@VonHeikemen Thank you so much It started working. Really appreciate your help sir. Also if possible can you please share some resources on how a beginner can start nvim plugins development. Peace.

VonHeikemen commented 1 year ago

Great. I'm glad that's working for you.

how a beginner can start nvim plugins development.

This video is pretty cool: Neovim lua plugin from scratch.