jose-elias-alvarez / nvim-lsp-ts-utils

Utilities to improve the TypeScript development experience for Neovim's built-in LSP client.
The Unlicense
438 stars 18 forks source link

[BUG] :TSLspImportAll and :TSLspImportCurrent doesn't working. #112

Closed singlexyz closed 2 years ago

singlexyz commented 2 years ago

FAQ

Issues

Neovim Version

NVIM v0.6.1

Steps to reproduce

import React from "react"

function App() {
  const div = useRef(null)
}

export default App

Cursor on useRef, then execute :TSLspImportAll or :TSLspImportCurrent, nothing happen. Buy actually I had those actions

截屏2022-03-14 上午3 56 21

if execute them, it works.

Expected behavior

Auto Import.

Actual behavior

nothing happens.

Debug log

Command git exited with code 0
No code actions available

jsconfig:

{
  "compilerOptions": {
    "checkJs": true,
    "jsx": "preserve"
  }
}

I made a recording:

https://user-images.githubusercontent.com/4456413/158076857-ead3e4fb-7d46-4e87-977c-7972aea9956a.mov

And here's my lsp config:

local capabilities = require("cmp_nvim_lsp").update_capabilities(vim.lsp.protocol.make_client_capabilities())
capabilities.textDocument.completion.completionItem.snippetSupport = true
capabilities.textDocument.completion.completionItem.resolveSupport = {
  properties = {
    "documentation",
    "detail",
    "additionalTextEdits",
  },
}

local nullls = require("null-ls")
nullls.setup({
  sources = {
    nullls.builtins.formatting.stylua.with({
      extra_args = { "--indent-type", "Spaces", "--indent-width", "2" },
    }),
    nullls.builtins.formatting.prettier_d_slim.with({
      extra_args = { "--bracket-spacing", true },
    }),
    nullls.builtins.code_actions.gitsigns,
    nullls.builtins.code_actions.refactoring,
  },
})

lspconfig.tsserver.setup({
  on_attach = function(client, bufnr)
  -- all copy from https://github.com/jose-elias-alvarez/nvim-lsp-ts-utils#setup
  end,
  capabilities = capabilities,
  commands = {
    OrganizeImports = {
      organize_imports,
      description = "Organize Imports",
    },
  },

Help

No

Implementation help

No response

jose-elias-alvarez commented 2 years ago

Your example does work for me, both on a production project and on a blank skeleton project. There's too many potential variables (versions, project configurations / structures) for me to figure out exactly what's going on. I can see that your tsserver code actions are slightly different from the ones I get:

Screen Shot 2022-03-13 at 6 22 35 PM

This leads me to believe that something about your local environment is causing this not to work. If you can put together a repository containing the code I need to replicate this, including the necessary versions of all the dependencies (React and TypeScript), I'm happy to look into this more.

sinnrrr commented 2 years ago

+1, having the same issue and configuration as the author, :TSLspImportAll and :TSLspImportCurrent doesn't work even if the cursor is on the unimported part of the code (getting "No code actions available" message)

however :TSLspOrganize and :TSLspRenameFile seem to work fine

screenshots

observing code actions

image

running :TSLspImportCurrent

image
singlexyz commented 2 years ago

Your example does work for me, both on a production project and on a blank skeleton project. There's too many potential variables (versions, project configurations / structures) for me to figure out exactly what's going on. I can see that your tsserver code actions are slightly different from the ones I get:

Screen Shot 2022-03-13 at 6 22 35 PM

This leads me to believe that something about your local environment is causing this not to work. If you can put together a repository containing the code I need to replicate this, including the necessary versions of all the dependencies (React and TypeScript), I'm happy to look into this more.

This leads me to believe that something about your local environment is causing this not to work. If you can put together a repository containing the code I need to replicate this, including the necessary versions of all the dependencies (React and TypeScript), I'm happy to look into this more.

Yeah, global package

typescript-language-server@0.9.7
typescript@4.6.2

and the project create from vite:

yarn create vite@latest my-app

framework and variant options select react both. and create-react-app also the same.

npx create-react-app my-app

Same jsconfig.json

{
  "compilerOptions": {
    "checkJs": true,
    "jsx": "preserve"
  }
}
jose-elias-alvarez commented 2 years ago

I see, I didn't realize you were using JavaScript. I can replicate this on a new CRA project that's not initialized with TypeScript. I briefly looked into this and the issue seems to be that the title of the actions doesn't match the TypeScript equivalent, so :TSLspImportAll ignores it.

JavaScript support in this plugin is, at best, accidental, since I don't personally use JavaScript and there's a ton of edge cases like this. If it's possible to add support without increasing complexity, I'm happy to review a PR, but I don't plan on fixing this myself. I'll also add a note to the README to specify that only TypeScript is officially supported.

Edit: For anyone who wants to work on this, this line is where the pattern fails to match. I briefly tried updating the pattern to make it pass, but the function still didn't work, so there's some more detective work to be done here.

sinnrrr commented 2 years ago

@jose-elias-alvarez I have fully typescript project bootstrapped with npx create-next-app here, _app.tsx file - AppProps can't be imported neither using :TSLspImportAll nor :TSLspImportCurrent, although we have import code action available

can you confirm this?

jose-elias-alvarez commented 2 years ago

@jose-elias-alvarez I have fully typescript project bootstrapped with npx create-next-app here, _app.tsx file - AppProps can't be imported neither using :TSLspImportAll nor :TSLspImportCurrent, although we have import code action available

can you confirm this?

Excellent, this repo is exactly what I needed to figure this out. Can you try #115?