irongit

My nvim configuration

hygo-nvim/lsp/typescript.lua
102 lines3.8 KBLua
1---@brief
2---
3--- https://github.com/yioneko/vtsls
4---
5--- `vtsls` can be installed with npm:
6--- ```sh
7--- npm install -g @vtsls/language-server
8--- ```
9---
10--- To configure a TypeScript project, add a
11--- [`tsconfig.json`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html)
12--- or [`jsconfig.json`](https://code.visualstudio.com/docs/languages/jsconfig) to
13--- the root of your project.
14---
15--- ### Vue support
16---
17--- Since v3.0.0, the Vue language server requires `vtsls` to support TypeScript.
18---
19--- ```
20--- -- If you are using mason.nvim, you can get the ts_plugin_path like this
21--- -- For Mason v1,
22--- -- local mason_registry = require('mason-registry')
23--- -- local vue_language_server_path = mason_registry.get_package('vue-language-server'):get_install_path() .. '/node_modules/@vue/language-server'
24--- -- For Mason v2,
25--- -- local vue_language_server_path = vim.fn.expand '$MASON/packages' .. '/vue-language-server' .. '/node_modules/@vue/language-server'
26--- -- or even
27--- -- local vue_language_server_path = vim.fn.stdpath('data') .. "/mason/packages/vue-language-server/node_modules/@vue/language-server"
28--- local vue_language_server_path = '/path/to/@vue/language-server'
29--- local vue_plugin = {
30--- name = '@vue/typescript-plugin',
31--- location = vue_language_server_path,
32--- languages = { 'vue' },
33--- configNamespace = 'typescript',
34--- }
35--- vim.lsp.config('vtsls', {
36--- settings = {
37--- vtsls = {
38--- tsserver = {
39--- globalPlugins = {
40--- vue_plugin,
41--- },
42--- },
43--- },
44--- },
45--- filetypes = { 'typescript', 'javascript', 'javascriptreact', 'typescriptreact', 'vue' },
46--- })
47--- ```
48---
49--- - `location` MUST be defined. If the plugin is installed in `node_modules`, `location` can have any value.
50--- - `languages` must include vue even if it is listed in filetypes.
51--- - `filetypes` is extended here to include Vue SFC.
52---
53--- You must make sure the Vue language server is setup. For example,
54---
55--- ```
56--- vim.lsp.enable('vue_ls')
57--- ```
58---
59--- See `vue_ls` section and https://github.com/vuejs/language-tools/wiki/Neovim for more information.
60---
61--- ### Monorepo support
62---
63--- `vtsls` supports monorepos by default. It will automatically find the `tsconfig.json` or `jsconfig.json` corresponding to the package you are working on.
64--- This works without the need of spawning multiple instances of `vtsls`, saving memory.
65---
66--- It is recommended to use the same version of TypeScript in all packages, and therefore have it available in your workspace root. The location of the TypeScript binary will be determined automatically, but only once.
67
68---@type vim.lsp.Config
69return {
70 cmd = { 'vtsls', '--stdio' },
71 init_options = {
72 hostInfo = 'neovim',
73 },
74 filetypes = {
75 'javascript',
76 'javascriptreact',
77 'javascript.jsx',
78 'typescript',
79 'typescriptreact',
80 'typescript.tsx',
81 },
82 root_dir = function(bufnr, on_dir)
83 -- The project root is where the LSP can be started from
84 -- As stated in the documentation above, this LSP supports monorepos and simple projects.
85 -- We select then from the project root, which is identified by the presence of a package
86 -- manager lock file.
87 local root_markers = { 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'bun.lockb', 'bun.lock' }
88 -- Give the root markers equal priority by wrapping them in a table
89 root_markers = vim.fn.has('nvim-0.11.3') == 1 and { root_markers, { '.git' } }
90 or vim.list_extend(root_markers, { '.git' })
91
92 -- exclude deno
93 if vim.fs.root(bufnr, { 'deno.json', 'deno.jsonc', 'deno.lock' }) then
94 return
95 end
96
97 -- We fallback to the current working directory if no project root is found
98 local project_root = vim.fs.root(bufnr, root_markers) or vim.fn.getcwd()
99
100 on_dir(project_root)
101 end,
102}