yioneko / vtsls

LSP wrapper for typescript extension of vscode
Other
374 stars 6 forks source link

How to use `_typescript.configurePlugin`? #153

Closed wSedlacek closed 2 months ago

wSedlacek commented 2 months ago

I am attempting to use the _typescript.configurePlugin command and I get logs in the tsserver.log which indicate that the command is received but the configuration does not seem to take. This is the short version of the command I am trying to run.

      tsclient.request("workspace/executeCommand", {
        command = "_typescript.configurePlugin",
        arguments = {
          "@monodon/typescript-nx-imports-plugin",
          { externalFiles = externalFiles },
        },
      }, function(err, res)
        if err then
          vim.print("Error: ", err)
          return
        end

        vim.print "Done!"
      end)

This is the logs I see.

Info 552  [11:32:12.600] response:
    {"seq":0,"type":"response","command":"configurePlugin","request_seq":5,"success":true}
Perf 553  [11:32:12.600] 5::configurePlugin: async elapsed time (in milliseconds) 0.2472

But I do not see the intended outcome of the plugin.

yioneko commented 2 months ago

Have you checked that the typescript plugin is actually in effect? https://github.com/yioneko/vtsls#typescript-plugin-not-activated Try to set vtsls.autoUseWorkspaceTsdk to true.

wSedlacek commented 2 months ago

Okay! So this line of thinking helped a lot and I do have something working, however it isn't quite configured how I want.

I needed to add the plugins to the tsconfig.json ie

  "compilerOptions": {
    ...
    "plugins": [
      {
        "name": "@monodon/typescript-nx-imports-plugin",
        "externalFiles": []
      }
    ]
  }

I was hoping to use a configuration more like I was using with typescript-tools.nvim where I could specify the plugins in the lua configuration instead of in my projects. Specifically something like tsserver_plugins https://github.com/pmizio/typescript-tools.nvim?tab=readme-ov-file#%EF%B8%8F-configuration

I attempted to do this with pluginPaths but it seems to not be the same thing. Reading the source code from vscode and TypeScript this configuration is 1:1 with --pluginProbeLocations (defaults to node_modules from the tsserver execute path) From what I can tell this pluginProbeLocations isn't actually a list of what plugins to use, but instead a list of possible locations they could be. (Correct me if I am wrong, I only gathered context clues based on the default values from reading the source code)

I believe what I actually need to configure is not --pluginProbeLocations but instead --globalPlugins Looking at another issue, I see there is a new option being worked on for adding vtsls.tsserver.globalPlugins https://github.com/yioneko/vtsls/issues/148#issuecomment-2015051938

When using this 0.2.2-alpha.1 I believe my plugin makes it to where this workspace.getConfiguration("vtsls").get("tsserver.globalPlugins") is used, however I believe the resolvePluginPath() is not resolving my plugin in /Users/wsedlacek/Library/Application Support/fnm/node-versions/v20.11.1/installation/lib/node_modules and thus when I look at the logs I am not seeing --globalPlugins configured.

More investigation is required, but this is the right track.

I had my configuration incorrect for vtsls.tsserver.globalPlugins, I needed to nest the value with an object and name. My plugin is now loading how I want.

  settings = {
    vtsls = {
      tsserver = {
        globalPlugins = {
          { name = "@monodon/typescript-nx-imports-plugin" },
        },
      },
    },

I am interested in knowing when the 0.2.2-alpha.1 will be released.

wSedlacek commented 2 months ago

Thank you for all your hard work!

Tantol commented 1 month ago

@wSedlacek Did you manage to make vtsls work with nx monorepos similar to typescript-tools with custon NxInit function? If yes, would you share configuration please?

wSedlacek commented 1 month ago

I think this works.

  local lspClients = vim.lsp.get_active_clients()
  local tsclient
  for _, client in ipairs(lspClients) do
    if client.name == "vtsls" then
      tsclient = client
    end
  end
  ...

      tsclient.request("workspace/executeCommand", {
        command = "_typescript.configurePlugin",
        arguments = {
          "@monodon/typescript-nx-imports-plugin",
          { externalFiles = externalFiles },
        },
      }, function(err, res)
        if err then
          vim.print("Error: ", err)
          return
        end

        vim.print "Done!"
      end)

I haven't used it in a bit since I stopped using the solution pattern with Nx

Tantol commented 1 month ago

@wSedlacek Thank you. For any one who want's to make it work, remember to:

Setup with typescript-tools (described in this thread) doesn't require these two extra steps.

wSedlacek commented 1 month ago

@Tantol Oh! Right! I forgot, you can configure these to avoid needing to change your tsconfig.json.

  settings = {
    vtsls = {
      tsserver = {
        globalPlugins = {
          { name = "@monodon/typescript-nx-imports-plugin" },
        },
      },
    },

In full it looks like this.

lspconfig.vtsls.setup {
  on_init = on_init,
  on_attach = on_attach,
  capabilities = capabilities,
  root_dir = util.root_pattern ".git",
  filetypes = { "angular", "typescript", "typescriptreact", "javascript", "javascriptreact" },
  settings = {
    vtsls = {
      autoUseWorkspaceTsdk = true,
      experimental = {
        completion = {
          enableServerSideFuzzyMatch = true,
        },
      },
      tsserver = {
        globalPlugins = {
          { name = "@monodon/typescript-nx-imports-plugin" },
        },
      },
    },
    typescript = {
      prefrences = {
        quoteStyle = "auto",
      },
    },
  },
}
Tantol commented 1 month ago

@wSedlacek That is wonderful. Do you know how to avoid installing @monodon/typescript-nx-imports-plugin as devDependencie per workspace? Or should it work with globally installed @monodon/typescript-nx-imports-plugin and I'm missing something?

wSedlacek commented 1 month ago

@Tantol If you are using vtls installed with Mason then you can install it in that directory ie ~/.local/share/nvim/mason/packages/vtsls

I haven't figured out how to make it resolve to a global installation though. I haven't looked too much into it. There probably is a way to write the lua so that the name is an absolute path to the module and make it work.

Perhaps if you had vtsls installed globally instead of through mason that would also work? I haven't tried that either.