ray-x / navigator.lua

Code analysis & navigation plugin for Neovim. Navigate codes like a breeze๐ŸŽ Exploring LSP and ๐ŸŒฒTreesitter symbols a piece of ๐Ÿฐ Take control like a boss ๐Ÿฆ
MIT License
1.27k stars 54 forks source link

gp require('navigator.definition').definition_preview() doesn't work #308

Open MikeLemo1 opened 2 weeks ago

MikeLemo1 commented 2 weeks ago

when I try to :lua require('navigator.definition').definition_preview() or gp or gP on a symbol it wont open the definition in a preview for some reason unlike gr which does work as expected, any idea why is that?

my config:

local M = {
  'ray-x/navigator.lua',
  dependencies = {
    { 'ray-x/guihua.lua', build = 'cd lua/fzy && make' },
    { 'neovim/nvim-lspconfig' },
    {
      "ray-x/lsp_signature.nvim",
      event = "VeryLazy",
      opts = {},
      -- config = function(_, opts) require'lsp_signature'.setup(opts) end
    },
  },

}

function M.config()
  -- Navigator
  -- require'navigator'.setup()
  local cfg = {
    debug = false, -- set to true to enable debug logging
    log_path = vim.fn.stdpath("cache") .. "/lsp_signature.log", -- log dir when debug is on
    -- default is  ~/.cache/nvim/lsp_signature.log
    verbose = false, -- show debug line number

    bind = true, -- This is mandatory, otherwise border config won't get registered.
                 -- If you want to hook lspsaga or other signature handler, pls set to false
    doc_lines = 10, -- will show two lines of comment/doc(if there are more than two lines in doc, will be truncated);
                   -- set to 0 if you DO NOT want any API comments be shown
                   -- This setting only take effect in insert mode, it does not affect signature help in normal
                   -- mode, 10 by default

    max_height = 12, -- max height of signature floating_window
    max_width = 80, -- max_width of signature floating_window, line will be wrapped if exceed max_width
                    -- the value need >= 40
    wrap = true, -- allow doc/signature text wrap inside floating_window, useful if your lsp return doc/sig is too long
    floating_window = true, -- show hint in a floating window, set to false for virtual text only mode

    floating_window_above_cur_line = true, -- try to place the floating above the current line when possible Note:
    -- will set to true when fully tested, set to false will use whichever side has more space
    -- this setting will be helpful if you do not want the PUM and floating win overlap

    floating_window_off_x = 1, -- adjust float windows x position.
                               -- can be either a number or function
    floating_window_off_y = 0, -- adjust float windows y position. e.g -2 move window up 2 lines; 2 move down 2 lines
                                -- can be either number or function, see examples

    close_timeout = 4000, -- close floating window after ms when laster parameter is entered
    fix_pos = false,  -- set to true, the floating window will not auto-close until finish all parameters
    hint_enable = true, -- virtual hint enable
    hint_prefix = "๐Ÿผ ",  -- Panda for parameter, NOTE: for the terminal not support emoji, might crash
    hint_scheme = "String",
    hint_inline = function() return false end,  -- should the hint be inline(nvim 0.10 only)?  default false
    -- return true | 'inline' to show hint inline, return 'eol' to show hint at end of line, return false to disable
    -- return 'right_align' to display hint right aligned in the current line
    hi_parameter = "LspSignatureActiveParameter", -- how your parameter will be highlight
    handler_opts = {
      border = "rounded"   -- double, rounded, single, shadow, none, or a table of borders
    },

    always_trigger = false, -- sometime show signature on new line or in middle of parameter can be confusing, set it to false for #58

    auto_close_after = nil, -- autoclose signature float win after x sec, disabled if nil.
    extra_trigger_chars = {}, -- Array of extra characters that will trigger signature completion, e.g., {"(", ","}
    zindex = 200, -- by default it will be on top of all floating windows, set to <= 50 send it to bottom

    padding = '', -- character to pad on left and right of signature can be ' ', or '|'  etc

    transparency = nil, -- disabled by default, allow floating win transparent value 1~100
    shadow_blend = 36, -- if you using shadow as border use this set the opacity
    shadow_guibg = 'Black', -- if you using shadow as border use this set the color e.g. 'Green' or '#121315'
    timer_interval = 200, -- default timer check interval set to lower value if you want to reduce latency
    toggle_key = nil, -- toggle signature on and off in insert mode,  e.g. toggle_key = '<M-x>'
    toggle_key_flip_floatwin_setting = false, -- true: toggle floating_windows: true|false setting after toggle key pressed
       -- false: floating_windows setup will not change, toggle_key will pop up signature helper, but signature
       -- may not popup when typing depends on floating_window setting

    select_signature_key = nil, -- cycle to next signature, e.g. '<M-n>' function overloading
    move_cursor_key = nil, -- imap, use nvim_set_current_win to move cursor between current win and floating
  }
  -- require'lsp_signature'.on_attach(cfg, 0) -- no need to specify bufnr if you don't use toggle_key

  local util = require('navigator.util')
  local remap = util.binding_remap

  local function fallback_fn(key)
    return function()
      fallback_keymap(key)
    end
  end

  require'navigator'.setup({
    debug = false, -- log output, set to true and log path: ~/.cache/nvim/gh.log
                   -- slowdownd startup and some actions
    width = 0.75, -- max width ratio (number of cols for the floating window) / (window width)
    height = 0.3, -- max list window height, 0.3 by default
    preview_height = 0.35, -- max height of preview windows
    border = {"โ•ญ", "โ”€", "โ•ฎ", "โ”‚", "โ•ฏ", "โ”€", "โ•ฐ", "โ”‚"}, -- border style, can be one of 'none', 'single', 'double',
                                                       -- 'shadow', or a list of chars which defines the border
    on_attach = function(client, bufnr)
      -- your hook
    end,
    -- put a on_attach of your own here, e.g
    -- function(client, bufnr)
    --   -- the on_attach will be called at end of navigator on_attach
    -- end,
    -- The attach code will apply to all LSP clients

    ts_fold = {
      enable = false,
      -- comment_fold = true, -- fold with comment string
      max_lines_scan_comments = 20, -- only fold when the fold level higher than this value
      disable_filetypes = {'help', 'guihua', 'text'}, -- list of filetypes which doesn't fold using treesitter
    },  -- modified version of treesitter folding
    default_mapping = false,  -- set to false if you will remap every key or if you using old version of nvim-
    keymaps = {
      { key = "<M-/>", mode= 'i', func = vim.lsp.buf.hover, desc = 'hover'},
      { key = 'gr',            func = require('navigator.reference').async_ref,                             desc = 'async_ref' },
      { key = '<Leader>gr',    func = require('navigator.reference').reference,                             desc = 'reference' }, -- reference deprecated
      -- { mode = 'i',            key = '<M-k>',                                                               func = vim.lsp.buf.signature_help,                        desc = 'signature_help' },
      { key = '<c-k>',         func = vim.lsp.buf.signature_help,                                           desc = 'signature_help' },
      { key = '<Leader>g0',    func = require('navigator.symbols').document_symbols,                        desc = 'document_symbols' },
      { key = 'gW',            func = require('navigator.workspace').workspace_symbol_live,                 desc = 'workspace_symbol_live' },
      { key = '<c-]>',         func = require('navigator.definition').definition,                           desc = 'definition' },
      { key = 'gd',            func = remap(require('navigator.definition').definition, 'gd'),              desc = 'definition' },
      { key = 'gD',            func = vim.lsp.buf.declaration,                                              desc = 'declaration',                                     fallback = fallback_fn('gD') }, -- fallback used
      -- for lsp handler
      { key = 'gp',            func = remap(require('navigator.definition').definition_preview, 'gp'),      desc = 'definition_preview' }, -- paste
      { key = 'gP',            func = remap(require('navigator.definition').type_definition_preview, 'gP'), desc = 'type_definition_preview' }, -- paste
      { key = '<Leader>gt',    func = require('navigator.treesitter').buf_ts,                               desc = 'buf_ts' },
      { key = '<Leader>gT',    func = require('navigator.treesitter').bufs_ts,                              desc = 'bufs_ts' },
      { key = '<Leader>ct',    func = require('navigator.ctags').ctags,                                     desc = 'ctags' },
      { key = '<Space>ca',     mode = 'n',                                                                  func = require('navigator.codeAction').code_action,       desc = 'code_action' },
      { key = '<Space>ca',     mode = 'v',                                                                  func = require('navigator.codeAction').range_code_action, desc = 'range_code_action' },
      -- { key = '<Leader>re', func = 'rename()' },
      { key = '<Space>rn',     func = require('navigator.rename').rename,                                   desc = 'rename' },
      { key = '<Leader>gi',    func = vim.lsp.buf.incoming_calls,                                           desc = 'incoming_calls' },
      { key = '<Leader>go',    func = vim.lsp.buf.outgoing_calls,                                           desc = 'outgoing_calls' },
      { key = 'gi',            func = vim.lsp.buf.implementation, desc = 'implementation',  fallback = fallback_fn('gi') }, -- insert
      { key = '<Space>D',      func = vim.lsp.buf.type_definition,                                          desc = 'type_definition' },
      { key = 'gL',            func = require('navigator.diagnostics').show_diagnostics,                    desc = 'show_diagnostics' },
      { key = 'gG',            func = require('navigator.diagnostics').show_buf_diagnostics,                desc = 'show_buf_diagnostics' },
      { key = '<Leader>dt',    func = require('navigator.diagnostics').toggle_diagnostics,                  desc = 'toggle_diagnostics' },
      { key = ']d',            func = require('navigator.diagnostics').goto_next,                           desc = 'next diagnostics error or fallback' },
      { key = '[d',            func = require('navigator.diagnostics').goto_prev,                           desc = 'prev diagnostics error or fallback' },
      { key = ']O',            func = vim.diagnostic.set_loclist,                                           desc = 'diagnostics set loclist' },
      { key = ']r',            func = require('navigator.treesitter').goto_next_usage,                      desc = 'goto_next_usage' },
      { key = '[r',            func = require('navigator.treesitter').goto_previous_usage,                  desc = 'goto_previous_usage' },
      { key = '<C-LeftMouse>', func = vim.lsp.buf.definition,                                               desc = 'definition',                               fallback = fallback_fn('<C-LeftMouse>')},
      { key = 'g<LeftMouse>',  func = vim.lsp.buf.implementation,                                           desc = 'implementation' },
      { key = '<Leader>k',     func = require('navigator.dochighlight').hi_symbol,                          desc = 'hi_symbol' },
      { key = '<Space>wa',     func = require('navigator.workspace').add_workspace_folder,                  desc = 'add_workspace_folder' },
      { key = '<Space>wr',     func = require('navigator.workspace').remove_workspace_folder,               desc = 'remove_workspace_folder' },
      { key = '<Space>ff',     func = vim.lsp.buf.format,                                             mode = 'n',                                               desc = 'format' },
      { key = '<Space>ff',     func = vim.lsp.buf.range_formatting,                                   mode = 'v',                                               desc = 'range format' },
      { key = '<Space>gm',     func = require('navigator.formatting').range_format,                         mode = 'n',                                         desc = 'range format operator e.g gmip' },
      { key = '<Space>wl',     func = require('navigator.workspace').list_workspace_folders,                desc = 'list_workspace_folders' },
      {
        key = '<Space>la',
        mode = 'n',
        func = require('navigator.codelens').run_action,
        desc = 'run code lens action',
      }
    }, -- a list of key maps
    -- this kepmap gK will override "gD" mapping function declaration()  in default kepmap
    -- please check mapping.lua for all keymaps
    -- rule of overriding: if func and mode ('n' by default) is same
    -- it can be overrided
    treesitter_analysis = true, -- treesitter variable context
    treesitter_navigation = true, -- bool|table false: use lsp to navigate between symbol ']r/[r', table: a list of
    --lang using TS navigation
    treesitter_analysis_max_num = 100, -- how many items to run treesitter analysis
    treesitter_analysis_condense = true, -- condense form for treesitter analysis
    -- this value prevent slow in large projects, e.g. found 100000 reference in a project
    transparency = 50, -- 0 ~ 100 blur the main window, 100: fully transparent, 0: opaque,  set to nil or 100 to disable it

    lsp_signature_help = true, -- if you would like to hook ray-x/lsp_signature plugin in navigator
    -- setup here. if it is nil, navigator will not init signature help
    signature_help_cfg = cfg, -- if you would like to init ray-x/lsp_signature plugin in navigator, and pass in your own config to signature help
    icons = { -- refer to lua/navigator.lua for more icons config
      -- requires nerd fonts or nvim-web-devicons
      icons = true,
      -- Code action
      code_action_icon = "๐Ÿ", -- note: need terminal support, for those not support unicode, might crash
      -- Diagnostics
      diagnostic_head = '๐Ÿ›',
      diagnostic_head_severity_1 = "๐Ÿˆฒ",
      fold = {
        prefix = 'โšก',  -- icon to show before the folding need to be 2 spaces in display width
        separator = '๏‘›',  -- e.g. shows ๏‘›๏‘›๏‘›๏‘›  3 lines ๏‘›๏‘›๏‘›๏‘›
      },
    },
    mason = true, -- set to true if you would like use the lsp installed by williamboman/mason
    lsp = {
      enable = true,  -- skip lsp setup, and only use treesitter in navigator.
                      -- Use this if you are not using LSP servers, and only want to enable treesitter support.
                      -- If you only want to prevent navigator from touching your LSP server configs,
                      -- use `disable_lsp = "all"` instead.
                      -- If disabled, make sure add require('navigator.lspclient.mapping').setup({bufnr=bufnr, client=client}) in your
                      -- own on_attach
      code_action = {enable = true, sign = true, sign_priority = 40, virtual_text = true},
      code_lens_action = {enable = true, sign = true, sign_priority = 40, virtual_text = true},
      document_highlight = true, -- LSP reference highlight,
                                 -- it might already supported by you setup, e.g. LunarVim
      format_on_save = false, -- {true|false} set to false to disasble lsp code format on save (if you are using prettier/efm/formater etc)
                             -- table: {enable = {'lua', 'go'}, disable = {'javascript', 'typescript'}} to enable/disable specific language
                                -- enable: a whitelist of language that will be formatted on save
                                -- disable: a blacklist of language that will not be formatted on save
                             -- function: function(bufnr) return true end to enable/disable lsp format on save
      format_options = {async=false}, -- async: disable by default, the option used in vim.lsp.buf.format({async={true|false}, name = 'xxx'})
      -- disable_format_cap = {"sqlls", "lua_ls", "gopls"},  -- a list of lsp disable format capacity (e.g. if you using efm or vim-codeformat etc), empty {} by default
                                                              -- If you using null-ls and want null-ls format your code
                                                              -- you should disable all other lsp and allow only null-ls.
      -- disable_lsp = {'pylsd', 'sqlls'},  -- prevents navigator from setting up this list of servers.
                                            -- if you use your own LSP setup, and don't want navigator to setup
                                            -- any LSP server for you, use `disable_lsp = "all"`.
                                            -- you may need to add this to your own on_attach hook:
                                            -- require('navigator.lspclient.mapping').setup({bufnr=bufnr, client=client})
                                            -- for e.g. denols and tsserver you may want to enable one lsp server at a time.
                                            -- default value: {}
      diagnostic = {
        underline = true,
        virtual_text = true, -- show virtual for diagnostic message
        update_in_insert = false, -- update diagnostic message in insert mode
        float = {                 -- setup for floating windows style
          focusable = false,
          sytle = 'minimal',
          border = 'rounded',
          source = 'always',
          header = '',
          prefix = '๏†ˆ',
        },
      },

      hover = {
        enable = true,
        -- keymap = {
        --   ['<C-m>'] = {
        --     go = function()
        --       local w = vim.fn.expand('<cWORD>')
        --       vim.cmd('GoDoc ' .. w)
        --     end,
        --     default = function()
        --       local w = vim.fn.expand('<cWORD>')
        --       vim.lsp.buf.workspace_symbol(w)
        --     end,
        --   },
        -- },

      diagnostic_scrollbar_sign = {'โ–ƒ', 'โ–†', 'โ–ˆ'}, -- experimental:  diagnostic status in scroll bar area; set to false to disable the diagnostic sign,
                                                   --                for other style, set to {'โ•', '๏ฎ†'} or {'-', '='}
      diagnostic_virtual_text = true,  -- show virtual for diagnostic message
      diagnostic_update_in_insert = false, -- update diagnostic message in insert mode
      display_diagnostic_qf = true, -- always show quickfix if there are diagnostic errors, set to false if you want to ignore it
                                    -- set to 'trouble' to show diagnositcs in Trouble
      tsserver = {
        filetypes = {'typescript'} -- disable javascript etc,
        -- set to {} to disable the lspclient for all filetypes
      },
      ctags ={
        cmd = 'ctags',
        tagfile = 'tags',
        options = '-R --exclude=.git --exclude=node_modules --exclude=test --exclude=vendor --excmd=number',
      },

      lua_ls = {
        sumneko_root_path = vim.fn.expand("$HOME") .. "/github/sumneko/lua-language-server",
        sumneko_binary = vim.fn.expand("$HOME") .. "/github/sumneko/lua-language-server/bin/macOS/lua-language-server",
      },
    }}
  })

end

return M

BTW How do you you remap the esc key to exit nav preview after editing? Also how do you set a single actions for multiple MODEs simuntaniusly? e.g. mode={'n', 'i', 'v'}

ray-x commented 1 week ago

What is the LSP you using? Also do you see any error messages?

esc keymap was handled by guihua.lua

You can set mode={'n', 'i', 'v'} in your keymap. It should work.

MikeLemo1 commented 1 week ago

What is the LSP you using? Also do you see any error messages?

Well pretty much trying to do that on a nvim lua plugin which I guess uses lua_ls and python with pyright

You can set mode={'n', 'i', 'v'} in your keymap. It should work. When I { key = "<M-/>", mode={'n', 'i', 'v'}, func = vim.lsp.buf.hover, desc = 'hover'}, It error me:

LSP[lua_ls]: Error ON_ATTACH_ERROR: "...m/lazy/navigator.lua/lua/navigator/lspclient/mapping.lua:243: attempt to concatenate a table value"
ray-x commented 1 week ago

Error ON_ATTACH_ERROR should been fixed. I did not see the error you mentioned regarding code preview with lua_ls

MikeLemo1 commented 1 week ago

I might be using it correctly but I try to move cursor to preview with j in :LspSymbols blinks the preview window and doesn't move the cursor to it as expected but the same shortcut does work in the gR menu