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

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

How to setup keymaps for go-to-def, hover, etc? #28

Closed aridgupta closed 3 years ago

aridgupta commented 3 years ago

I am new to neovim 0.5. I was wondering how to set keymaps for go-to-definitions, references, typedefs, diagnostics, etc. Also, how do I organize imports and format on save?

jose-elias-alvarez commented 3 years ago

For basic setup, I recommend nvim-lspconfig's README, which should get you set up in terms of bindings.

To organize imports on save, you can add the following command to your tsserver on_attach function:

vim.cmd("autocmd BufWritePre <buffer> TSLspOrganizeSync")

To enable formatting on save, you just have to setenable_formatting and format_on_save to true in ts_utils.setup {}.

aridgupta commented 3 years ago

Thank you for the answer. I have one more query. SInce the watchDir option is set to "/src" as default, how do I make it dynamic as nextjs doesn't have an "src" directory instead its primary directory is "/pages". How do I make this option dynamic?

jose-elias-alvarez commented 3 years ago

Interesting – it's not currently an option but it would be good to add.

I've never used it, but do Next.js projects have a specific config file in the root directory? (For example, NestJS has a nest-cli.json file that would clearly mark a project as a NestJS project.) If so, I can probably put something together.

aridgupta commented 3 years ago

Setting the watchDir to "/" solves the issue currently. Is this the optimal solution though? Since nvim lsp looks for the root directory, setting it to "/" will include all the directories, however, does it ignore node_modules? Nextjs do have a config file next.config.js but it is optional and it is mostly not needed. I am not sure but would it be possible to make watchDir take an array of directories to include? However, as I have noticed setting it to "/**" solves the issue.

jose-elias-alvarez commented 3 years ago

Right, I wouldn't recommend using / because the plugin doesn't ignore node_modules. It's possible to set up a list of directories for the plugin to ignore, but I think adding the option to pass an array of directories is a cleaner solution.

The result would be that if you set watch_dir to { "pages", "src" } it would attempt to watch pages and fall back to src if pages isn't a directory (and not watch at all if neither directory exists). Does that sound like a good solution for this case?

aridgupta commented 3 years ago

This sounds like an optimal solution. But suppose I have "pages", "components" and "lib" directories, would setting the option to {"pages", "components", "lib", "src"} make it watch all the directories? I am asking this because people refactor components, utilities and helpers to "lib" and "components" directories conventionally, so it would need to watch for 3 directories simultaneously.

jose-elias-alvarez commented 3 years ago

That's another option. The plugin could check if each directory exists at the current root and watch all directories that actually exist, so in the case you mention, it would watch pages, components, and lib, but not src. In a project with a more conventional structure, it would ignore the nonexistent directories pages, components, and lib and only watch src. Does that sound right?

Managing multiple watchers does add some overhead, but I'm happy to add it if it's useful, since I imagine others have the same requirements. If that sounds good to you, I can open up another issue and start working on it a bit later this week.

Also, just so you know, development for the watcher feature is currently happening on the plugin's develop branch, which has different requirements. Specifically, it offloads formatting to a different plugin, which requires a little additional extra setup. If you want to try this feature before before it's merged into main, take a look at the README on that branch and let me know if you need any help setting it up.

aridgupta commented 3 years ago

That's another option. The plugin could check if each directory exists at the current root and watch all directories that actually exist, so in the case you mention, it would watch pages, components, and lib, but not src. In a project with a more conventional structure, it would ignore the nonexistent directories pages, components, and lib and only watch src. Does that sound right?

Yes, this sounds right.

One more option would be to check for tsconfig's include and exclude options. Either way, the above solution theoretically should work.

jose-elias-alvarez commented 3 years ago

I love the idea of checking tsconfig.json, but Vim's built-in JSON parser doesn't support JSON with comments, so I think we'll have to go with the config option.

Anyways, going to close this for now and open up another issue for the config feature. Thanks for the feedback!