In a project, sometimes go to definition is not working.
To explain my problem, see the following code:
class Hobby:
def __init__(self, hobby):
self.hobby = hobby
def print_hobby(self):
print(self.hobby)
friends = ["Alice", "Bob"]
HobbyFriends = {}
for friend in friends:
HobbyFriends[friend] = Hobby("Encryption")
HobbyMallory = Hobby("Interception")
HobbyMallory.print_hobby() # gd on print_hobby is WORKING
for friend in friends:
HobbyFriends[friend].print_hobby() # gd on print_hobby is NOT working
HobbyFriends["Me"] = Hobby("neovim")
HobbyFriends["Me"].print_hobby() # gd on print_hobby is WORKING
When the cursor is on print_hobby in the lines marked WORKING I get to the definition of print hobby after pressing gd.
When the cursor is on print_hobby in the line marked NOT, I am not transferred to the definition.
In lsp.log it reads:
[INFO][2023-03-03 10:03:06] ...lsp/handlers.lua:364 "textDocument/definition" "No location found"
Maybe somebody knows what I am doing wrong and could provide some help. Thanks in advance!
gd on print_hobby is working in cases where no dictionaries are involved.
Expected behavior
gd should work on all instances of print_hobby (as in vscode)
Minimal config
local on_windows = vim.loop.os_uname().version:match 'Windows'
local function join_paths(...)
local path_sep = on_windows and '\\' or '/'
local result = table.concat({ ... }, path_sep)
return result
end
vim.cmd [[set runtimepath=$VIMRUNTIME]]
local temp_dir = vim.loop.os_getenv 'TEMP' or '/tmp'
vim.cmd('set packpath=' .. join_paths(temp_dir, 'nvim', 'site'))
local package_root = join_paths(temp_dir, 'nvim', 'site', 'pack')
local lspconfig_path = join_paths(package_root, 'test', 'start', 'nvim-lspconfig')
if vim.fn.isdirectory(lspconfig_path) ~= 1 then
vim.fn.system { 'git', 'clone', 'https://github.com/neovim/nvim-lspconfig', lspconfig_path }
end
vim.lsp.set_log_level 'trace'
require('vim.lsp.log').set_format_func(vim.inspect)
local nvim_lsp = require 'lspconfig'
local on_attach = function(_, bufnr)
local function buf_set_option(...)
vim.api.nvim_buf_set_option(bufnr, ...)
end
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
-- Mappings.
local opts = { buffer = bufnr, noremap = true, silent = true }
vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, opts)
vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, opts)
vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, opts)
vim.keymap.set('n', '<space>wl', function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, opts)
vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, opts)
vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts)
vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
vim.keymap.set('n', '<space>e', vim.diagnostic.open_float, opts)
vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts)
vim.keymap.set('n', ']d', vim.diagnostic.goto_next, opts)
vim.keymap.set('n', '<space>q', vim.diagnostic.setloclist, opts)
end
-- Add the server that troubles you here
local name = 'pyright'
local cmd = { 'pyright-langserver', '--stdio' } -- needed for elixirls, lua_ls, omnisharp
if not name then
print 'You have not defined a server name, please edit minimal_init.lua'
end
if not nvim_lsp[name].document_config.default_config.cmd and not cmd then
print [[You have not defined a server default cmd for a server
that requires it please edit minimal_init.lua]]
end
nvim_lsp[name].setup {
cmd = cmd,
on_attach = on_attach,
}
Description
Hello,
In a project, sometimes go to definition is not working. To explain my problem, see the following code:
When the cursor is on
print_hobby
in the lines markedWORKING
I get to the definition ofprint hobby
after pressinggd
. When the cursor is onprint_hobby
in the line markedNOT
, I am not transferred to the definition.In
lsp.log
it reads:Maybe somebody knows what I am doing wrong and could provide some help. Thanks in advance!
Neovim version
NVIM v0.9.0-dev Build type: RelWithDebInfo LuaJIT 2.1.0-beta3
Nvim-lspconfig version
No response
Operating system and version
arch linux
Affected language servers
pyright
Steps to reproduce
see code above
Actual behavior
gd
onprint_hobby
is working in cases where no dictionaries are involved.Expected behavior
gd
should work on all instances ofprint_hobby
(as invscode
)Minimal config
LSP log
using minimal config