Skip to content
Ryan Olson edited this page Feb 19, 2021 · 4 revisions

JavaScript

The typescript-language-server is capable of giving intelligent LSP functionality for JavaScript.

Installing server

Note: tsserver is searched in workspace node_modules followed by global install. Use -g to install globally.

npm install -g typescript typescript-language-server

Once the typescript-language-server is installed, register the typescript-language-server in your .vimrc

Registering in .vimrc

Here's an example that shows how to manually set up a language server for JavaScript.

You can either set it up and use your git root directory as your project root OR find the root that has a package.json.

Use directory with .git as root

if executable('typescript-language-server')
    au User lsp_setup call lsp#register_server({
      \ 'name': 'javascript support using typescript-language-server',
      \ 'cmd': { server_info->[&shell, &shellcmdflag, 'typescript-language-server --stdio']},
      \ 'root_uri': { server_info->lsp#utils#path_to_uri(lsp#utils#find_nearest_parent_directory(lsp#utils#get_buffer_path(), '.git/..'))},
      \ 'whitelist': ['javascript', 'javascript.jsx', 'javascriptreact']
      \ })
endif

Use directory with package.json as root

  au User lsp_setup call lsp#register_server({
    \ 'name': 'javascript support using typescript-language-server',
    \ 'cmd': {server_info->[&shell, &shellcmdflag, 'typescript-language-server --stdio']},
    \ 'root_uri':{server_info->lsp#utils#path_to_uri(lsp#utils#find_nearest_parent_file_directory(lsp#utils#get_buffer_path(), 'package.json'))},
    \ 'whitelist': ['javascript', 'javascript.jsx', 'javascriptreact'],
    \ })