lspsaga.nvim icon indicating copy to clipboard operation
lspsaga.nvim copied to clipboard

[ts] support `goToSourceDefinition`

Open Hanaasagi opened this issue 2 years ago • 3 comments

Is your feature request related to a problem? Please describe.

Currently, Lspsaga's goto_definition in TypeScript files jumps to the .d.ts file instead of the original JavaScript source code. tsserver has introduced a new command called _typescript.goToSourceDefinition to jump to the original .js file. It requires TS 4.7+.

Ref: https://github.com/typescript-language-server/typescript-language-server#go-to-source-definition

Describe the solution you'd like

Support goToSourceDefinition just like vscode does (https://github.com/microsoft/vscode/commit/d851ea5d49b0ec89b98765d9e0084c25f2ff5031)

vscode has introduced a new option here called "go to source definition" rather than using the original "go to definition" directly. You can see them in this answer https://stackoverflow.com/a/72333657. Btw, I'm not sure how lspsaga will handle this. Maybe add a setup option and reuse "go to definition" ?

Additional context

Below is a simple demo demonstrating how to use goToSourceDefinition.

diff --git a/lua/lspsaga/definition.lua b/lua/lspsaga/definition.lua
index 13a889e..09aa5f8 100644
--- a/lua/lspsaga/definition.lua
+++ b/lua/lspsaga/definition.lua
@@ -300,7 +300,15 @@ function def:goto_definition(method, args)
     beacon({ res.range.start.line, vim.fn.col('.') }, width)
   end
   if method == 1 then
-    lsp.buf.definition()
+    -- lsp.buf.definition()
+    local params = {
+      command = "_typescript.goToSourceDefinition",
+      arguments = {
+        vim.uri_from_bufnr(0),
+        vim.lsp.util.make_position_params()['position'],
+      }
+    }
+    vim.lsp.buf_request(0, "workspace/executeCommand", params, lsp.handlers[get_method(1)])
   elseif method == 2 then
     lsp.buf.type_definition()
   end

More info, please see

  • https://github.com/jose-elias-alvarez/typescript.nvim/pull/27/files
  • https://github.com/pmizio/typescript-tools.nvim/blob/ebddb35900bbf5fa41fffa0463b676bf08e474e6/lua/typescript-tools/api.lua#L88 pure Lua implementation

Hanaasagi avatar Oct 07 '23 14:10 Hanaasagi

diff code is old

glepnir avatar Oct 08 '23 00:10 glepnir

I think I can open a pr this weekend, but I'm wondering which one would be better:

  • Adding a new Command, e.g. Lspsaga goto_source_definition
  • Automatically detecting ts files and jump to source code, reusing the Lspsaga goto_definition

Which one do you think would be better?

Hanaasagi avatar Oct 24 '23 11:10 Hanaasagi

keep goto

glepnir avatar Oct 24 '23 11:10 glepnir