mirror of
https://github.com/belsabbagh/dotfiles.git
synced 2026-08-09 20:47:55 +00:00
switched to typescript 7
This commit is contained in:
@@ -208,8 +208,7 @@ vim.lsp.enable {
|
|||||||
'rust-analyzer',
|
'rust-analyzer',
|
||||||
'emmet-language-server',
|
'emmet-language-server',
|
||||||
'tinymist',
|
'tinymist',
|
||||||
'tsgo',
|
'ts7',
|
||||||
'tsls',
|
|
||||||
'qmlls',
|
'qmlls',
|
||||||
'yamlls',
|
'yamlls',
|
||||||
'ty',
|
'ty',
|
||||||
|
|||||||
54
.config/nvim/lsp/ts7.lua
Normal file
54
.config/nvim/lsp/ts7.lua
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
local function resolve_bin(root_dir)
|
||||||
|
local candidates = { 'tsc' }
|
||||||
|
if root_dir then
|
||||||
|
table.insert(candidates, 1, root_dir .. '/node_modules/.bin/tsc')
|
||||||
|
end
|
||||||
|
for _, bin in ipairs(candidates) do
|
||||||
|
if vim.fn.executable(bin) == 1 then
|
||||||
|
return bin
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function check_version(bin)
|
||||||
|
local ver = vim.fn.system({ bin, '--version' })
|
||||||
|
local major = tonumber(ver:match('Version (%d+)'))
|
||||||
|
return major and major >= 7, major
|
||||||
|
end
|
||||||
|
|
||||||
|
---@type vim.lsp.Config
|
||||||
|
return {
|
||||||
|
cmd = function(dispatchers, config)
|
||||||
|
local bin = resolve_bin(config.root_dir)
|
||||||
|
return vim.lsp.rpc.start({ bin, '--lsp', '--stdio' }, dispatchers)
|
||||||
|
end,
|
||||||
|
filetypes = {
|
||||||
|
'javascript',
|
||||||
|
'javascriptreact',
|
||||||
|
'javascript.jsx',
|
||||||
|
'typescript',
|
||||||
|
'typescriptreact',
|
||||||
|
'typescript.tsx',
|
||||||
|
},
|
||||||
|
root_dir = function(bufnr, on_dir)
|
||||||
|
local root_markers = { 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'bun.lockb', 'bun.lock' }
|
||||||
|
root_markers = vim.fn.has 'nvim-0.11.3' == 1 and { root_markers, { '.git' } } or vim.list_extend(root_markers, { '.git' })
|
||||||
|
|
||||||
|
if vim.fs.root(bufnr, { 'deno.json', 'deno.jsonc', 'deno.lock' }) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local project_root = vim.fs.root(bufnr, root_markers) or vim.fn.getcwd()
|
||||||
|
|
||||||
|
local bin = resolve_bin(project_root)
|
||||||
|
if not bin then return end
|
||||||
|
|
||||||
|
local ok, major = check_version(bin)
|
||||||
|
if not ok then
|
||||||
|
vim.notify(('ts7: need TypeScript >=7, got %d'):format(major or 0), vim.log.levels.WARN)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
on_dir(project_root)
|
||||||
|
end,
|
||||||
|
}
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
---@brief
|
|
||||||
---
|
|
||||||
--- https://github.com/microsoft/typescript-go
|
|
||||||
---
|
|
||||||
--- `typescript-go` is experimental port of the TypeScript compiler (tsc) and language server (tsserver) to the Go programming language.
|
|
||||||
---
|
|
||||||
--- `tsgo` can be installed via npm `npm install @typescript/native-preview`.
|
|
||||||
---
|
|
||||||
--- ### Monorepo support
|
|
||||||
---
|
|
||||||
--- `tsgo` supports monorepos by default. It will automatically find the `tsconfig.json` or `jsconfig.json` corresponding to the package you are working on.
|
|
||||||
--- This works without the need of spawning multiple instances of `tsgo`, saving memory.
|
|
||||||
---
|
|
||||||
--- 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.
|
|
||||||
---
|
|
||||||
|
|
||||||
---@type vim.lsp.Config
|
|
||||||
return {
|
|
||||||
cmd = function(dispatchers, config)
|
|
||||||
local cmd = 'tsgo'
|
|
||||||
local local_cmd = (config or {}).root_dir and config.root_dir .. '/node_modules/.bin/tsgo'
|
|
||||||
if local_cmd and vim.fn.executable(local_cmd) == 1 then
|
|
||||||
cmd = local_cmd
|
|
||||||
end
|
|
||||||
return vim.lsp.rpc.start({ cmd, '--lsp', '--stdio' }, dispatchers)
|
|
||||||
end,
|
|
||||||
filetypes = {
|
|
||||||
'javascript',
|
|
||||||
'javascriptreact',
|
|
||||||
'javascript.jsx',
|
|
||||||
'typescript',
|
|
||||||
'typescriptreact',
|
|
||||||
'typescript.tsx',
|
|
||||||
},
|
|
||||||
root_dir = function(bufnr, on_dir)
|
|
||||||
-- The project root is where the LSP can be started from
|
|
||||||
-- As stated in the documentation above, this LSP supports monorepos and simple projects.
|
|
||||||
-- We select then from the project root, which is identified by the presence of a package
|
|
||||||
-- manager lock file.
|
|
||||||
local root_markers = { 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'bun.lockb', 'bun.lock' }
|
|
||||||
-- Give the root markers equal priority by wrapping them in a table
|
|
||||||
root_markers = vim.fn.has 'nvim-0.11.3' == 1 and { root_markers, { '.git' } } or vim.list_extend(root_markers, { '.git' })
|
|
||||||
|
|
||||||
-- exclude deno
|
|
||||||
if vim.fs.root(bufnr, { 'deno.json', 'deno.jsonc', 'deno.lock' }) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- We fallback to the current working directory if no project root is found
|
|
||||||
local project_root = vim.fs.root(bufnr, root_markers) or vim.fn.getcwd()
|
|
||||||
|
|
||||||
on_dir(project_root)
|
|
||||||
end,
|
|
||||||
}
|
|
||||||
@@ -1,127 +0,0 @@
|
|||||||
---@brief
|
|
||||||
---
|
|
||||||
--- https://github.com/typescript-language-server/typescript-language-server
|
|
||||||
---
|
|
||||||
--- `ts_ls`, aka `typescript-language-server`, is a Language Server Protocol implementation for TypeScript wrapping `tsserver`. Note that `ts_ls` is not `tsserver`.
|
|
||||||
---
|
|
||||||
--- `typescript-language-server` depends on `typescript`. Both packages can be installed via `npm`:
|
|
||||||
--- ```sh
|
|
||||||
--- npm install -g typescript typescript-language-server
|
|
||||||
--- ```
|
|
||||||
---
|
|
||||||
--- To configure typescript language server, add a
|
|
||||||
--- [`tsconfig.json`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) or
|
|
||||||
--- [`jsconfig.json`](https://code.visualstudio.com/docs/languages/jsconfig) to the root of your
|
|
||||||
--- project.
|
|
||||||
---
|
|
||||||
--- Here's an example that disables type checking in JavaScript files.
|
|
||||||
---
|
|
||||||
--- ```json
|
|
||||||
--- {
|
|
||||||
--- "compilerOptions": {
|
|
||||||
--- "module": "commonjs",
|
|
||||||
--- "target": "es6",
|
|
||||||
--- "checkJs": false
|
|
||||||
--- },
|
|
||||||
--- "exclude": [
|
|
||||||
--- "node_modules"
|
|
||||||
--- ]
|
|
||||||
--- }
|
|
||||||
--- ```
|
|
||||||
---
|
|
||||||
--- Use the `:LspTypescriptSourceAction` command to see "whole file" ("source") code-actions such as:
|
|
||||||
--- - organize imports
|
|
||||||
--- - remove unused code
|
|
||||||
---
|
|
||||||
--- ### Vue support
|
|
||||||
---
|
|
||||||
--- As of 2.0.0, Volar no longer supports TypeScript itself. Instead, a plugin
|
|
||||||
--- adds Vue support to this language server.
|
|
||||||
---
|
|
||||||
--- *IMPORTANT*: It is crucial to ensure that `@vue/typescript-plugin` and `volar `are of identical versions.
|
|
||||||
---
|
|
||||||
--- ```lua
|
|
||||||
--- vim.lsp.config('ts_ls', {
|
|
||||||
--- init_options = {
|
|
||||||
--- plugins = {
|
|
||||||
--- {
|
|
||||||
--- name = "@vue/typescript-plugin",
|
|
||||||
--- location = "/usr/local/lib/node_modules/@vue/typescript-plugin",
|
|
||||||
--- languages = {"javascript", "typescript", "vue"},
|
|
||||||
--- },
|
|
||||||
--- },
|
|
||||||
--- },
|
|
||||||
--- filetypes = {
|
|
||||||
--- "javascript",
|
|
||||||
--- "typescript",
|
|
||||||
--- "vue",
|
|
||||||
--- },
|
|
||||||
--- })
|
|
||||||
---
|
|
||||||
--- -- You must make sure volar is setup
|
|
||||||
--- -- e.g. vim.lsp.config('volar')
|
|
||||||
--- -- See volar's section for more information
|
|
||||||
--- ```
|
|
||||||
---
|
|
||||||
--- `location` MUST be defined. If the plugin is installed in `node_modules`,
|
|
||||||
--- `location` can have any value.
|
|
||||||
---
|
|
||||||
--- `languages` must include `vue` even if it is listed in `filetypes`.
|
|
||||||
---
|
|
||||||
--- `filetypes` is extended here to include Vue SFC.
|
|
||||||
|
|
||||||
return {
|
|
||||||
init_options = {
|
|
||||||
hostInfo = 'neovim',
|
|
||||||
init_options = {
|
|
||||||
plugins = {
|
|
||||||
{
|
|
||||||
name = '@vue/typescript-plugin',
|
|
||||||
location = '/usr/local/lib/node_modules/@vue/typescript-plugin',
|
|
||||||
languages = { 'javascript', 'typescript', 'vue' },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
filetypes = {
|
|
||||||
'javascript',
|
|
||||||
'typescript',
|
|
||||||
'vue',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
cmd = { 'typescript-language-server', '--stdio' },
|
|
||||||
filetypes = {
|
|
||||||
'vue',
|
|
||||||
},
|
|
||||||
root_markers = { 'tsconfig.json', 'jsconfig.json', 'package.json', '.git' },
|
|
||||||
handlers = {
|
|
||||||
-- handle rename request for certain code actions like extracting functions / types
|
|
||||||
['_typescript.rename'] = function(_, result, ctx)
|
|
||||||
local client = assert(vim.lsp.get_client_by_id(ctx.client_id))
|
|
||||||
vim.lsp.util.show_document({
|
|
||||||
uri = result.textDocument.uri,
|
|
||||||
range = {
|
|
||||||
start = result.position,
|
|
||||||
['end'] = result.position,
|
|
||||||
},
|
|
||||||
}, client.offset_encoding)
|
|
||||||
vim.lsp.buf.rename()
|
|
||||||
return vim.NIL
|
|
||||||
end,
|
|
||||||
},
|
|
||||||
on_attach = function(client)
|
|
||||||
-- ts_ls provides `source.*` code actions that apply to the whole file. These only appear in
|
|
||||||
-- `vim.lsp.buf.code_action()` if specified in `context.only`.
|
|
||||||
vim.api.nvim_buf_create_user_command(0, 'LspTypescriptSourceAction', function()
|
|
||||||
local source_actions = vim.tbl_filter(function(action)
|
|
||||||
return vim.startswith(action, 'source.')
|
|
||||||
end, client.server_capabilities.codeActionProvider.codeActionKinds)
|
|
||||||
|
|
||||||
vim.lsp.buf.code_action {
|
|
||||||
context = {
|
|
||||||
only = source_actions,
|
|
||||||
diagnostics = {},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end, {})
|
|
||||||
end,
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user