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

:TSLspImportAll isn't working. #49

Closed bushblade closed 3 years ago

bushblade commented 3 years ago

Hi, first off thanks for creating this plugin. Prior to discovering your tutorial and plugin no linting in JS files was the one thing that held be back from moving away from COC to Nvim LSP.

I am not able to get the :TSLspImportAll command to work. I set debug = true, and I see command git exited with 0 when I try to use the command. To test I created a module.js file with

export function test() {
  return 'testing'
}

And a app.js file with

console.log(foo())

And ran :TSLspImportAll, and I don't get any import. I also tried in .ts files but see the same result. I thought perhaps that the plugin looks for a .git directory to determine project root so initialized a repo in the test project but that did not work either. Am I doing something wrong or is this a bug? I would also like to put forward a feature request to make available a command to toggle the linting on or off, I can create a new issue if that would be more appropriate? Or perhaps an option to only have the linting output if there is a .eslintrc in the project root, if that is possible.

jose-elias-alvarez commented 3 years ago

Does your example have a typo? I'm guessing app.js should say console.log(test()), right?

The debug output you're seeing is from ESLint, not from :TSLspImportAll. It doesn't rely on a root marker, but it does rely on tsserver, which (if you're using nvim-lspconfig) will only start up in a directory that contains a package.json or a tsconfig.json / jsconfig.json file. Do you see tsserver under clients attached to this buffer when you run :LspInfo?

If the client is attached, then the issue may be with your project configuration. I just put together this repo, which contains the minimal configuration required to get this feature to work on JavaScript files. Running :TSLspImportAll in app.js should work – could you let me know if it does for you?

Toggling diagnostics is definitely possible, and I could add a config flag to suppress ESLint errors (detecting whether the current directory has a valid configuration isn't that easy, unfortunately, since the configuration could be embedded in a package.json file). Please open another issue for those if you're interested.

bushblade commented 3 years ago

Oh sorry I pasted in the wrong code. Yeah it should be test, I used foo in my .ts tests and test() in my .js tests. I also tested this in a couple of React and Gatsby projects and get the same results - no import.

Do you see tsserver under clients attached to this buffer when you run :LspInfo?

Yes I do... Screenshot_2021-07-19_17-52-28

And here is the output from :LspInfo in a React project... Screenshot_2021-07-19_17-55-14

I can confirm that cloning your repository and running :TSLspImportAll does indeed import the function but imports it as a common JS module.

jose-elias-alvarez commented 3 years ago

In that case, it most likely has to do with your project configuration. Could you verify whether replicating the scenario in an existing project, moving the cursor to the missing import, and calling :lua vim.lsp.buf.code_action() shows a code action prompting you to add the missing import?

bushblade commented 3 years ago

This happens in any React or Gatsby project I try.

Could you verify whether replicating the scenario in an existing project, moving the cursor to the missing import, and calling :lua > vim.lsp.buf.code_action() shows a code action prompting you to add the missing import?

Screenshot_2021-07-19_18-19-54

jose-elias-alvarez commented 3 years ago

Thanks for verifying! What :TSLspImportAll does is aggregate code actions and run them all at once. If the code actions aren't available, it won't do anything, so I think you need to look at your project's configuration and make sure that tsserver is properly scanning your files and providing code actions / diagnostics.

I don't really know about JavaScript, but I think you need a jsconfig.json file (or a tsconfig.json file with allowJs set to true, as in the repo I linked) at the project's root.

bushblade commented 3 years ago

Ok so I just tried pasting in the same tsconfig.json from your repo into the React project, but I still get the same output from :lua vim.lsp.buf.code_action() and still no import. Also in your example repo it imports as a common JS module, not ES module, is that correct?

bushblade commented 3 years ago

Here is how I set up my tsserver

jose-elias-alvarez commented 3 years ago

I think the format of the imports has to do with the tsconfig.json settings, but again, I don't work with JavaScript, so I can't say for sure.

Your config seems fine, and your screenshots show that tsserver itself is working (plus the sample repo worked), so this is fundamentally an issue with your project configuration. I wish I could help more, but I really don't know anything about tsserver + JavaScript, and as long as you don't have access to code actions, there's nothing we can do on the plugin side.

bushblade commented 3 years ago

Thanks that does make sense. I don't suppose you have a example repo where it is working correctly? Typescript project is fine. Just so I can test that?

jose-elias-alvarez commented 3 years ago

create-react-app has an option to generate a project with TypeScript, and Gatsby has an example repo. I recommend getting started there.