Skip to content

Commit

Permalink
docs(volar): fix dead links and remove outdated content (#3498)
Browse files Browse the repository at this point in the history
* docs(volar): refine Typescript SDK path lookup process

* docs(volar): fix dead links and improve clarity
  • Loading branch information
vaga authored Dec 10, 2024
1 parent b8b7256 commit cef5a11
Showing 1 changed file with 38 additions and 44 deletions.
82 changes: 38 additions & 44 deletions lua/lspconfig/configs/volar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local function get_typescript_server_path(root_dir)
return project_root and (util.path.join(project_root, 'typescript', 'lib')) or ''
end

-- https://github.com/johnsoncodehk/volar/blob/20d713b/packages/shared/src/types.ts
-- https://github.com/vuejs/language-tools/blob/master/packages/language-server/lib/types.ts
local volar_init_options = {
typescript = {
tsdk = '',
Expand All @@ -30,82 +30,76 @@ return {
},
docs = {
description = [[
https://github.com/johnsoncodehk/volar/tree/20d713b/packages/vue-language-server
https://github.com/vuejs/language-tools/tree/master/packages/language-server
Volar language server for Vue
Volar can be installed via npm:
```sh
npm install -g @vue/language-server
```
Volar by default supports Vue 3 projects. Vue 2 projects need
[additional configuration](https://github.com/vuejs/language-tools/tree/master/packages/vscode-vue#usage).
**TypeScript support**
As of release 2.0.0, Volar no longer wraps around ts_ls. For typescript
support, `ts_ls` needs to be configured with the `@vue/typescript-plugin`
plugin.
Volar by default supports Vue 3 projects.
For Vue 2 projects, [additional configuration](https://github.com/vuejs/language-tools/blob/master/extensions/vscode/README.md?plain=1#L19) are required.
**Take Over Mode**
**Hybrid Mode (by default)**
Volar (prior to 2.0.0), can serve as a language server for both Vue and TypeScript via [Take Over Mode](https://github.com/johnsoncodehk/volar/discussions/471).
In this mode, the Vue Language Server exclusively manages the CSS/HTML sections.
You need the `ts_ls` server with the `@vue/typescript-plugin` plugin to support TypeScript in `.vue` files.
See `ts_ls` section for more information
To enable Take Over Mode, override the default filetypes in `setup{}` as follows:
**No Hybrid Mode**
Volar will run embedded `ts_ls` therefore there is no need to run it separately.
```lua
require'lspconfig'.volar.setup{
filetypes = {'typescript', 'javascript', 'javascriptreact', 'typescriptreact', 'vue', 'json'}
local lspconfig = require('lspconfig')
lspconfig.volar.setup {
-- add filetypes for typescript, javascript and vue
filetypes = { 'typescript', 'javascript', 'javascriptreact', 'typescriptreact', 'vue' },
init_options = {
vue = {
-- disable hybrid mode
hybridMode = false,
},
},
}
-- you must remove ts_ls setup
-- lspconfig.ts_ls.setup {}
```
**Overriding the default TypeScript Server used by Volar**
The default config looks for TS in the local `node_modules`. This can lead to issues
The default config looks for TypeScript in the local `node_modules`. This can lead to issues
e.g. when working on a [monorepo](https://monorepo.tools/). The alternatives are:
- use a global TypeScript Server installation
```lua
require'lspconfig'.volar.setup{
require'lspconfig'.volar.setup {
init_options = {
typescript = {
tsdk = '/path/to/.npm/lib/node_modules/typescript/lib'
-- Alternative location if installed as root:
-- tsdk = '/usr/local/lib/node_modules/typescript/lib'
-- replace with your global TypeScript library path
tsdk = '/path/to/node_modules/typescript/lib'
}
}
}
```
- use a local server and fall back to a global TypeScript Server installation
```lua
local util = require 'lspconfig.util'
local function get_typescript_server_path(root_dir)
local global_ts = '/home/[yourusernamehere]/.npm/lib/node_modules/typescript/lib'
-- Alternative location if installed as root:
-- local global_ts = '/usr/local/lib/node_modules/typescript/lib'
local found_ts = ''
local function check_dir(path)
found_ts = util.path.join(path, 'node_modules', 'typescript', 'lib')
if vim.loop.fs_stat(found_ts) then
return path
require'lspconfig'.volar.setup {
init_options = {
typescript = {
-- replace with your global TypeScript library path
tsdk = '/path/to/node_modules/typescript/lib'
}
},
on_new_config = function(new_config, new_root_dir)
local lib_path = vim.fs.find('node_modules/typescript/lib', { path = new_root_dir, upward = true })[1]
if lib_path then
new_config.init_options.typescript.tsdk = lib_path
end
end
if util.search_ancestors(root_dir, check_dir) then
return found_ts
else
return global_ts
end
end
require'lspconfig'.volar.setup{
on_new_config = function(new_config, new_root_dir)
new_config.init_options.typescript.tsdk = get_typescript_server_path(new_root_dir)
end,
}
```
]],
Expand Down

0 comments on commit cef5a11

Please sign in to comment.