yioneko / vtsls

LSP wrapper for typescript extension of vscode
Other
486 stars 7 forks source link

Not working Goto Definition hotkey with project with yarn pnp #177

Closed utrumo closed 3 months ago

utrumo commented 3 months ago

Here is another problem related to yarn plug and play with the same conditions as here https://github.com/yioneko/vtsls/issues/169

I get an error when calling the go to definition command:

Error executing vim.schedule lua callback: /usr/share/nvim/runtime/lua/vim/lsp/util.lua:132: index out of range
stack traceback:
    [C]: in function 'str_utfindex'
    /usr/share/nvim/runtime/lua/vim/lsp/util.lua:132: in function '_str_utfindex_enc'
    ...nvim/lazy/telescope.nvim/lua/telescope/builtin/__lsp.lua:108: in function 'item_to_location'
    ...nvim/lazy/telescope.nvim/lua/telescope/builtin/__lsp.lua:207: in function 'handler'
    /usr/share/nvim/runtime/lua/vim/lsp/client.lua:687: in function ''
    vim/_editor.lua: in function <vim/_editor.lua:0>

If I add to .yarnrc.yml: nodeLinker: node-modules

and run yarn install packages are installed in the node_module - go to definition command works well. In vscode, in both cases, everything works without errors

image

My config: https://github.com/utrumo/myConf/blob/master/nvim/lua/plugins/typescript.lua https://www.lazyvim.org/extras/editor/telescope https://www.lazyvim.org/extras/lang/typescript

yioneko commented 3 months ago

I made a PR to partially resolve the issue. This means that some additional adjustments needs to be done on nvim side. Though the mutation of zipfile uri could be done on server side before sending to nvim, but I'd like not to implement that since keeping compatible with specific client's non-standard behavior is a bad practice from my view. So the current solution is not ideal and Iet me know what do you think about it.

utrumo commented 3 months ago

@yioneko, please tell me how I can try to run your edits on my computer? I immediately could not figure out how to install vtsls from your branch via mason.nvim

The yarn documentation advises using the vim-rzip plugin. Could that be the problem? https://yarnpkg.com/getting-started/editor-sdks

yioneko commented 3 months ago

I just published a new verision 0.2.5-alpha.0 to npm and you can directly use that for testing.

Regarding vim-rzip: I think no, as that plugin also uses vim builtin zip plugin under the hood.

utrumo commented 3 months ago

I can't make go to definition work.

I updated vtsls

MasonInstall vtsls@0.2.5-alpha.0

added autocmd, which is described in your pull request, I tried in my config in lazy.lua to comment on the lines "gzip" and "zipPlugin" in the disabled_plugins section, as well as disable vim-rzip plugin.

I still get an error:

Error executing vim.schedule lua callback: /usr/share/nvim/runtime/lua/vim/lsp/util.lua:1801: index out of range
stack traceback:
    [C]: in function '_str_byteindex_enc'
    /usr/share/nvim/runtime/lua/vim/lsp/util.lua:1801: in function 'locations_to_items'
    ...nvim/lazy/telescope.nvim/lua/telescope/builtin/__lsp.lua:197: in function 'handler'
    /usr/share/nvim/runtime/lua/vim/lsp/client.lua:687: in function ''
    vim/_editor.lua: in function <vim/_editor.lua:0>

Can you help me figure out what I'm doing wrong?

yioneko commented 3 months ago

Can you verify the zip plugin is actually working? If you start editing a .zip archive in nvim, it will list the file contained in the zip.

utrumo commented 3 months ago

Yes, I open zip archives inside the same .yarn/cache and their contents are available in nvim without the new autocmd command and with it, no difference. Only go to definition does not work

yioneko commented 3 months ago

What's your lsp config for it? Maybe you didn't set init_options.hostInfo = "neovim". It is required to inform server to send zipfile: uri insted of zip: (it is for vscode).

utrumo commented 3 months ago

Exactly! I did not set this option. Can you tell me how to set it up?

yioneko commented 3 months ago

It depends on what you use for lsp config, but generally it is as simple as setting init_options is your setup call:

require("lspconfig").vtsls.setup({
  init_options = {
    hostInfo = "neovim",
  },
  settings = ...
})

If you still cannot figure out how to do that, you can provide your lsp setup for it.

utrumo commented 3 months ago

It worked! Go to definition earned. True, previewing the contents of the file does not work. The presence of an autocommand does not affect it in any way.

Here is my minimum config that turned out:

return {
  "neovim/nvim-lspconfig",
  opts = {
    servers = {
      vtsls = {
        init_options = { hostInfo = "neovim" },
        settings = {
          typescript = {
            tsdk = "./.yarn/sdks/typescript/lib",
          },
        },
      },
    },
  },
}

And here's the full one, where I turned off automatic formatting to avoid conflicts with prettier. Can you tell me if I did it correctly?

return {
  {
    "neovim/nvim-lspconfig",
    opts = {
      inlay_hints = { enabled = false },
      servers = {
        vtsls = {
          init_options = { hostInfo = "neovim" },
          settings = {
            typescript = {
              tsdk = "./.yarn/sdks/typescript/lib",
            },
          },
        },
      },
      setup = {
        vtsls = function(_, _opts)
          require("lazyvim.util").lsp.on_attach(function(client)
            if client.name == "vtsls" then
              client.server_capabilities.documentFormattingProvider = false
            end
          end)
        end,
      },
    },
  },
  {
    "stevearc/conform.nvim",
    ---@class ConformOpts
    opts = {
      formatters = {
        prettier = {
          command = function(self, bufnr)
            local util = require("conform.util")
            local cmd = util.find_executable({ ".yarn/sdks/prettier/bin/prettier.cjs" }, "")(self, bufnr)

            if cmd ~= "" then
              return cmd
            end

            -- return type of util.from_node_modules is fun(self: conform.FormatterConfig, ctx: conform.Context): string
            ---@diagnostic disable-next-line
            return util.from_node_modules("prettier")(self, bufnr)
          end,
        },
      },
    },
  },
}

Is there any other way to preview the files, or is this a problem with a completely different plugin? image

yioneko commented 3 months ago

Yes, the setup is correct. It looks like you are using Telescope, which I tested to have the same problem. I didn't dive deep into it as I tried fzf-lua where the preview works fine, so I think this is the problem on Telescope side.

utrumo commented 3 months ago

Thank you. The inclusion of editor.fzf in LazyExtra solved the problem. Plugin telescope was replaced by fzf-lua and preview earned.

No additional auto-command was needed:

vim.api.nvim_create_autocmd("BufReadCmd", {
  pattern = "zipfile:/[^/]*",
  callback = function(args)
    -- the uri is like zipfile:/a/b/c
    local uri = args.match --[[@as string]]
    -- transform it to be zipfile:///a/b/c
    vim.fn["zip#Read"](uri:gsub("^zipfile:", "zipfile://"), 1)
    local printInspect = function(value)
      print(vim.inspect(value))
    end
    printInspect(uri)
  end,
})

go to definition works even without it on 0.2.5-alpha.0

yioneko commented 3 months ago

I did some trick to make the zipfile:/// returned in some cases, so the snippet is not always needed if your usage is within these cases. For other cases like diagnostics and code actions on the zip file, the snippet could come to save.

utrumo commented 3 months ago

In what cases is the auto command exactly needed, how can I check that it works?

yioneko commented 3 months ago

Never mind, I created a new test package and checked it again and now everything works as expected without the autocmd. Maybe I did something wrong before. Thanks for your support!