Configuring Vim/Neovim ALE plugin to support :ALEGoToDefinition in JavaScript files
Asked Answered
F

1

10

I installed the ALE plugin for Vim using vim-plug:

Plug 'dense-analysis/ale'

The plugin seems to have been installed correctly. I can use ALE to automatically format files with Prettier, for example. But I can't seem to get anything that uses the language server to work.

For example, in the following JavaScript file, putting my cursor over the name add on line 5 and using the :ALEGoToDefinition command has no effect.

function add(x, y) {
  return x + y;
}

console.log(add(1, 2));

I have really made an effort to read the documentation. The ALE help file says that "ALE will do nothing" if an LSP server does not provide a location to jump to. That seems to be my problem. But the ALE documentation on GitHub also says that "ALE integrates with almost all JavaScript tools very well, and most things should work without requiring any configuration."

I must be missing something. Aside from installing ALE, is anything needed to enable features which use a language server? Should I install some kind of language server globally on my machine?

Fishing answered 12/4, 2020 at 21:22 Comment(2)
I'm continuing to look into this in hopes of answering my own question, but I haven't quite found the answer yet. I see that langserver.org links to a JavaScript language server, but running it doesn't seem to do the trick. I also haven't seen it mentioned in any ALE documentation or blog posts.Fishing
Yes, you need one of ale’s supported language servers for that filetype for it to use a language server. See :help ale-supportedSteffi
F
8

User toupeira on Reddit answered this question for me. At the time of this writing, the only JavaScript language server that ALE supports is tsserver. It ships with TypeScript. To enable ALE's language server features, I needed to install the typescript package globally.

npm install -g typescript

I don't need to start the server manually. ALE seems to take care of that.

The only other requirement is that tsserver is enabled as a JavaScript "linter." It is by default. Run :ALEInfo to see which linters are enabled for the current file.

Fishing answered 21/4, 2020 at 1:19 Comment(3)
I used yarn via yarn global add typescript which worked perfectly too. I had to set the linter in my .vimrc: let g:ale_linters = {'javascript': ['tsserver']}Ernaldus
Ah, I further see this in the FAQ: "By default, all available tools for all supported languages will be run. If you want to only select a subset of the tools, you can define b:ale_linters for a single buffer, or g:ale_linters globally.". It'a only because I had already set 'eslint' as my linter that I had to add tsserver as well. I could just remove g:ale_linters.Ernaldus
In case it helps anyone else, installing the Node package globally did not work for me, but installing via Homebrew (brew install typescript) did.Mown

© 2022 - 2024 — McMap. All rights reserved.