This repository was archived by the owner on Feb 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathts_ls.lua
More file actions
116 lines (109 loc) · 4.23 KB
/
Copy pathts_ls.lua
File metadata and controls
116 lines (109 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
---@type vim.lsp.Config
return {
init_options = { hostInfo = 'neovim' },
cmd = { 'typescript-language-server', '--stdio' },
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 = { root_markers, { '.git' } }
-- exclude deno
local deno_path = vim.fs.root(bufnr, { 'deno.json', 'deno.jsonc', 'deno.lock' })
local project_root = vim.fs.root(bufnr, root_markers)
if deno_path and (not project_root or #deno_path >= #project_root) then return end
-- We fallback to the current working directory if no project root is found
on_dir(project_root or vim.fn.getcwd())
end,
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,
},
commands = {
['editor.action.showReferences'] = function(command, ctx)
local client = assert(vim.lsp.get_client_by_id(ctx.client_id))
local file_uri, position, references = unpack(command.arguments)
local quickfix_items =
vim.lsp.util.locations_to_items(references --[[@as any]], client.offset_encoding)
vim.fn.setqflist({}, ' ', {
title = command.title,
items = quickfix_items,
context = {
command = command,
bufnr = ctx.bufnr,
},
})
vim.lsp.util.show_document({
uri = file_uri --[[@as string]],
range = {
start = position --[[@as lsp.Position]],
['end'] = position --[[@as lsp.Position]],
},
}, client.offset_encoding)
---@diagnostic enable: assign-type-mismatch
vim.cmd 'botright copen'
end,
},
on_attach = function(client, bufnr)
-- 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(bufnr, '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, {})
-- Go to source definition command
vim.api.nvim_buf_create_user_command(bufnr, 'LspTypescriptGoToSourceDefinition', function()
local win = vim.api.nvim_get_current_win()
local params = vim.lsp.util.make_position_params(win, client.offset_encoding)
client:exec_cmd({
command = '_typescript.goToSourceDefinition',
title = 'Go to source definition',
arguments = { params.textDocument.uri, params.position },
}, { bufnr = bufnr }, function(err, result)
if err then
vim.notify('Go to source definition failed: ' .. err.message, vim.log.levels.ERROR)
return
end
if not result or vim.tbl_isempty(result) then
vim.notify('No source definition found', vim.log.levels.INFO)
return
end
vim.lsp.util.show_document(result[1], client.offset_encoding, { focus = true })
end)
end, { desc = 'Go to source definition' })
end,
on_init = function(client)
client.server_capabilities.documentFormattingProvider = false
client.server_capabilities.documentRangeFormattingProvider = false
end,
}