hardhat-vscode
hardhat-vscode copied to clipboard
Make the language server work cleanly with vim CoC
The advantage of a language server is it should be reusable across clients. Vim and neovim are popular among solidity developers so we should support the VIM CoC plugin and its integration with the hardhat language server.
This is likely a documentation and coding task.
I would like to use this too with nvim.
Currently the best is solc LSP.
But it crashes a lot.
I would love to try this out if hardhat indeed made their own lsp
Refer to #279
So here's what I found about using our lsp on vim using coc.
We can just bundle our server and it will work right away, with support for most features (exceptions later). Some features require a client-side implementation like formatting, file watching, and status items. The last 2 are not provided by coc alone but require other plugins.
Coc allows us to write a client-side extension similar to our client in vs code, but with more limited capabilities. It's not possible to port it directly, it needs to be copied and adapted. If we wanted this, we'd have to maintain both the vscode client and the coc client (maybe we could have a minimal version of the latter). I'm not sure yet if we need to have a minimal implementation of the coc client to be able to distribute our lsp or we can just do it without it. So far I've been testing by modifying coc settings to point to our lsp but at some point we would want to do :CocInstall coc-hardhat-solidity. By the way coc-solidity is already taken.
Workspace folders is a concept that doesn't exist on vim, so the way to support it is to specify some patterns that allow to identify a given folder as a root - workspace folder. I just went with .git, hardhat.config, foundry.toml and seems to work on my tests (pure foundry, mixed, pure hardhat, monorepos, projectless).
The lack of status items is a problem, since in some cases we make use of them to show the user that there's no validation/navigation because the project is not properly set up. i.e. hardhat not installed, wrong hardhat config, missing node modules, forge not installed, etc. In these cases there are no diagnostics on files themselves, since we consider them project-wide errors and a good way to show it is on status items. A possible workaround would be to show a small diagnostic on first line, first character, with the project-wide errors, on all files belonging to that project. This would also show on vs code of course.
The output logging is not the best because you have to run some coc command to access it, but I assume coc users know how to do it.
Besides the mentioned points, these are other specific points I found that don't work right away:
- Files not validated after lsp finishes initialization. On vscode, we have the server send a custom message when initialization ends, and the client then responds to that by re-sending a validate request for open files. But we don't have a client on coc. I'm not 100% sure but this probably could be solved by moving our initialization from the onInitialized step to the onInitialize one. There is a task somewhere for this, and it may solve our issues and allow us to remove the custom messaging thing.
- No prettier formatting - this is on the client side
- Import completion doesn't work unless you have auto-closing quotes. So import "
won't trigger completions but import " " will. feels very clunky, maybe we could adjust it so it works every time without the closing pair. - Renames that are multi-file don't work, we get a coc error. Not sure if this could be worked around by changing the rename implementation but in any case I'd say it's out of scope for now.
This is what I could find by testing around using the smoke tests, there might be other things that I missed. In any case, I think 95% of our server-side features work fine which is a huge win. Navigation, completions, validation, quickfixes, etc.
Avid coc-vim user and would love for this to be ported. Would love to help you out and make this a reality :)
We have an experimental coc extension that leverages our solidity language server:
https://www.npmjs.com/package/@ignored/coc-solidity
Warning: It is still alpha and so under our @ignored namespace, we will move it out to @nomicfoundation proper once it leaves alpha.
The experimental coc-solidity extension has been out for several weeks now. I am going to close this issue in favour of our general prep for a full release of vim support here: https://github.com/NomicFoundation/hardhat-vscode/issues/351