2

I am using Typescript and I have this config for going to the definition when hovering over a function name:

vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts) -- does not work
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) -- works

When I enter gD, it returns the following error:

method textDocument/declaration is not supported by any of the servers registered for the current buffer

I'd like to open the file containing the function definition in a new tab. Is this possible?

1
  • Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one answer using the v button next to the arrow voting buttons. It allow the question to rest :-) Sep 27 at 7:36

1 Answer 1

2

I believe you don't have the distinction declaration and definition in typescript like you have it in C or C++.

myfile.h

// declaration
int myfunction(int a, char *name);

myfile.c

// definition
int myfunction(int a, char *name)
{
  return a * a;
}

In typescript you have only a definition that that is probably why gD is not working but gd should do the job for you.

To open the definition in a new tab if it is another file you could do:

vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts)

vim.keymap.set('n', 'gD', function()
        local org_path = vim.api.nvim_buf_get_name(0)

        -- Go to definition:
        vim.api.nvim_command('normal gd')

        -- Wait LSP server response
        vim.wait(100, function() end)

        local new_path = vim.api.nvim_buf_get_name(0)
        if not (org_path == new_path) then
            -- Create a new tab for the original file
            vim.api.nvim_command('0tabnew %')

            -- Restore the cursor position
            vim.api.nvim_command('b ' .. org_path)
            vim.api.nvim_command('normal! `"')

            -- Switch to the original tab
            vim.api.nvim_command('normal! gt')
        end
    end, bufopts)
9
  • ooo that makes sense. Is there a way to make gd open a new file? I can just map gD to that
    – bigpotato
    Sep 21 at 17:00
  • 1
    New tab within vim, like what :tabe [filename] does
    – bigpotato
    Sep 21 at 19:33
  • 1
    I'll try to do that for you tomorrow ;-) Sep 21 at 19:59
  • 1
    egad, what a hack! :P
    – D. Ben Knoble
    Sep 22 at 14:56
  • 1
    this worked!! had to add local in front of org_path and new_path
    – bigpotato
    Sep 30 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.