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

Code actions for ESLint/TSLint diagnostics #5

Closed VVKot closed 3 years ago

VVKot commented 3 years ago

Curious if you'd be interested in including this in your plugin or should this go someplace else? The last blocker for me to jump the ship from CoC to native LSP is great code actions integration from coc-eslint, coc-tslint, coc-stylelint, etc. Generic language servers like diagnosticls work fine - but they don't produce code actions. It seems that, for example, coc-eslint has bits of code the converts suggested fixes into actions, so we need the matching code for native LSP somewhere.

jose-elias-alvarez commented 3 years ago

One potential solution – and the only one that I can think of that remains remotely near the scope of this plugin – is to create a custom handler for code action requests that'll create synthetic actions from ESLint JSON output and add them to the actual LSP code actions. (I don't have experience with TSLint or stylelint, but I imagine the process would be similar.)

That would entail running the current file through a linter whenever the user requests code actions, which wouldn't be great for performance (though something like eslint_d would help greatly).

Frankly, I think the only way to replicate the coc-eslint experience would be to write an actual language server that handles both getting the diagnostics and creating code actions from suggestions. Still, the custom handler solution might be an acceptable stopgap for people who are okay with the performance hit, so I'll give this some more thought and play around with it for now.

Let me know what you think and if you (or anyone else!) has any ideas for how this might be implemented.

jose-elias-alvarez commented 3 years ago

Update: I've put together a preliminary implementation for ESLint, and it works remarkably well. As predicted, eslint is fairly slow, but eslint_d is even faster than expected, so much so that it feels like a genuine alternative to a language server (though getting diagnostics and code actions separately still feels faintly ridiculous).

I've pushed the changes to the repo's develop branch (instructions in the readme) in case you want to give them a shot. I'll put this implementation through its paces this week at work and merge it into main this weekend if everything goes well.

jose-elias-alvarez commented 3 years ago

I've been using this all week and I'm pretty happy with the results, though I'm sure there's some edge cases that haven't come up in my workflow, so I went ahead and merged it. Closing this issue for now, but feel free to open another re: TSLint / stylelint.

VVKot commented 3 years ago

@jose-elias-alvarez hey, thanks a lot, only now got time to play around with it. Is there any catch in setting this up? E.g. I would expect that after adding the config from readme I can trigger the default code action code and I'll get the ESLint actions - but that does not seems to be the case. Running :!eslint % or :!eslint_d % does produce output I would expect.

VVKot commented 3 years ago

Pushed my config here. Any tips on how I can debug this? I am simply getting "No code actions available" message, LSP log does not have anything in it.

https://github.com/VVKot/dotfiles/blob/fd93785eb23d44a73600d144161ded93c69c3ffc/config/nvim/lua/plugin/lsp.lua#L240-L256

VVKot commented 3 years ago

Okay, print debugging FTW! I found some things, see #7.