Skip to content

Commit d3bfb7d

Browse files
committed
fix: ~ should be handled on cmp side. use (...) instead for label
feat: copy the VSCode config option `typescript.suggest.completeFunctionCalls` and have it off by default
1 parent c3046cc commit d3bfb7d

6 files changed

Lines changed: 66 additions & 41 deletions

File tree

.luacheckrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
ignore = {
33
"631", -- max_line_length
44
}
5+
exclude_files = {
6+
".tests/**/*.lua",
7+
}
58
globals = { "vim", "P" }
69
read_globals = {
710
"describe",

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ require("typescript-tools").setup {
138138
-- described below
139139
tsserver_format_options = {},
140140
tsserver_file_preferences = {},
141+
-- mirror of https://github.com/microsoft/vscode/blob/885dba39a5f546c5077196a8c31b03d03a293b31/extensions/typescript-language-features/package.json#L145-L1266
142+
-- `typescript-tools.nvim` currently only supports `typescript.suggest.completeFunctionCalls`
143+
vscode_configuration = {},
141144
},
142145
}
143146
```

lua/typescript-tools/config.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
---@see https://github.com/microsoft/vscode/blob/885dba39a5f546c5077196a8c31b03d03a293b31/extensions/typescript-language-features/package.json#L145-L1266
2+
---@alias VsCodeConfiguration { typescript.suggest.completeFunctionCalls: boolean }
3+
14
---@class Settings
25
---@field plugin_name string
36
---@field separate_diagnostic_server boolean
@@ -8,6 +11,7 @@
811
---@field tsserver_format_options table|fun(filetype: string): table
912
---@field tsserver_file_preferences table|fun(filetype: string): table
1013
---@field tsserver_max_memory number|"auto"
14+
---@field vscode_configuration VsCodeConfiguration
1115
---@field expose_as_code_action ("fix_all"| "add_missing_imports"| "remove_unused")[]
1216
local M = {}
1317
local __store = {}
@@ -106,6 +110,7 @@ function M.load_settings(settings)
106110
{ "number", "string" },
107111
true,
108112
},
113+
["settings.vscode_configuration"] = { settings.vscode_configuration, "table", true },
109114
["settings.expose_as_code_action"] = {
110115
settings.expose_as_code_action,
111116
"table",
@@ -143,6 +148,10 @@ function M.load_settings(settings)
143148
__store.tsserver_max_memory = "auto"
144149
end
145150

151+
if not settings.vscode_configuration then
152+
__store.vscode_configuration = {}
153+
end
154+
146155
if not settings.expose_as_code_action then
147156
__store.expose_as_code_action = {}
148157
end

lua/typescript-tools/protocol/text_document/completion/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ function M.handler(request, response, params)
6767
sortText = "\u{ffff}" .. item.sortText
6868
end
6969

70-
local should_create_snippet = item.isSnippet
71-
or utils.should_create_function_snippet(kind, filetype)
70+
local should_create_function_snippet = utils.should_create_function_snippet(kind, filetype)
71+
local should_create_snippet = item.isSnippet or should_create_function_snippet
7272
local label = is_optional and (item.name .. "?") or item.name
73-
label = should_create_snippet and (label .. "~") or label
73+
label = should_create_function_snippet and (label .. "(...)") or label
7474

7575
return {
7676
label = label,

lua/typescript-tools/protocol/utils.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ function M.should_create_function_snippet(kind, filetype)
231231
local preferences = plugin_config.get_tsserver_file_preferences(filetype)
232232
return preferences.includeCompletionsWithSnippetText
233233
and (kind == c.CompletionItemKind.Function or kind == c.CompletionItemKind.Method)
234+
and plugin_config.vscode_configuration["typescript.suggest.completeFunctionCalls"]
234235
end
235236

236237
return M

tests/requests_spec.lua

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local utils = require "tests.utils"
22
local lsp_assert = require "tests.lsp_asserts"
33
local mocks = require "tests.mocks"
4+
local plugin_config = require "typescript-tools.config"
45
local c = require "typescript-tools.protocol.constants"
56
local methods = c.LspMethods
67
local custom_methods = c.CustomMethods
@@ -134,10 +135,11 @@ describe("Lsp request", function()
134135
utils.open_file "src/completion.ts"
135136
utils.wait_for_lsp_initialization()
136137

137-
local ret = vim.lsp.buf_request_sync(0, methods.Completion, {
138+
local req = {
138139
textDocument = utils.get_text_document(),
139140
position = utils.make_position(0, 8),
140-
})
141+
}
142+
local ret = vim.lsp.buf_request_sync(0, methods.Completion, req)
141143

142144
local result = lsp_assert.response(ret)
143145

@@ -147,36 +149,47 @@ describe("Lsp request", function()
147149
assert.is.True(#items >= 20)
148150

149151
local completions = vim.tbl_map(function(it)
150-
if it.label == "assert~" or it.label == "warn~" then
151-
assert.are.same(it.insertTextFormat, c.InsertTextFormat.Snippet)
152+
if it.kind == c.CompletionItemKind.Method or it.kind == c.CompletionItemKind.Function then
153+
assert.are.same(it.insertTextFormat, c.InsertTextFormat.PlainText)
152154
end
153155
return it.label
154156
end, items)
155157
table.sort(completions)
156158

157-
assert.are.same(completions[1], "assert~")
158-
assert.are.same(completions[#completions], "warn~")
159+
assert.are.same(completions[1], "assert")
160+
assert.are.same(completions[#completions], "warn")
159161

160-
ret = vim.lsp.buf_request_sync(0, methods.Completion, {
161-
textDocument = utils.get_text_document(),
162-
position = utils.make_position(0, 6),
163-
})
162+
-- same test as above but with function snippets enabled
163+
local prev_config =
164+
plugin_config.vscode_configuration["typescript.suggest.completeFunctionCalls"]
165+
plugin_config.vscode_configuration["typescript.suggest.completeFunctionCalls"] = true
166+
167+
ret = vim.lsp.buf_request_sync(0, methods.Completion, req)
164168
result = lsp_assert.response(ret)
165-
local can_complete_as_console = false
166-
for _, item in ipairs(result.items) do
167-
if item.label == "console" then
168-
assert.are.same(item.insertTextFormat, c.InsertTextFormat.PlainText)
169-
can_complete_as_console = true
169+
assert.is.table(result.items)
170+
171+
items = result.items
172+
assert.is.True(#items >= 20)
173+
174+
completions = vim.tbl_map(function(it)
175+
if it.kind == c.CompletionItemKind.Method or it.kind == c.CompletionItemKind.Function then
176+
assert.are.same(it.insertTextFormat, c.InsertTextFormat.Snippet)
170177
end
171-
end
172-
assert(can_complete_as_console)
178+
return it.label
179+
end, items)
180+
table.sort(completions)
181+
182+
assert.are.same(completions[1], "assert(...)")
183+
assert.are.same(completions[#completions], "warn(...)")
184+
185+
plugin_config.vscode_configuration["typescript.suggest.completeFunctionCalls"] = prev_config
173186
end)
174187

175188
it("should return correct response for " .. methods.CompletionResolve, function()
176189
utils.open_file "src/completion.ts"
177190
utils.wait_for_lsp_initialization()
178191

179-
local ret = vim.lsp.buf_request_sync(0, methods.CompletionResolve, {
192+
local req = {
180193
commitCharacters = { "(" },
181194
data = {
182195
character = 8,
@@ -186,37 +199,33 @@ describe("Lsp request", function()
186199
},
187200
filterText = "warn",
188201
insertText = "warn",
189-
insertTextFormat = c.InsertTextFormat.Snippet,
202+
insertTextFormat = c.InsertTextFormat.PlainText,
190203
kind = c.CompletionItemKind.Function,
191204
label = "warn",
192205
sortText = "11",
193-
})
206+
}
207+
local ret = vim.lsp.buf_request_sync(0, methods.CompletionResolve, req)
194208

195209
local result = lsp_assert.response(ret)
196210
assert.is.table(result)
197-
assert.are.same(result.insertText, "warn($1)$0")
211+
assert.are.same(result.insertText, "warn")
198212
assert.are.same(result.detail, "(method) Console.warn(...data: any[]): void")
199213

200-
ret = vim.lsp.buf_request_sync(0, methods.CompletionResolve, {
201-
commitCharacters = { ".", "?" },
202-
data = {
203-
character = 6,
204-
entryNames = { "console" },
205-
file = vim.fs.dirname(vim.api.nvim_buf_get_name(0)) .. "/completion.ts",
206-
line = 0,
207-
},
208-
filterText = "console",
209-
insertText = "console",
210-
insertTextFormat = c.InsertTextFormat.PlainText,
211-
kind = c.CompletionItemKind.Variable,
212-
label = "console",
213-
sortText = "15",
214-
})
214+
-- same test as above but with function snippets enabled
215+
local prev_config =
216+
plugin_config.vscode_configuration["typescript.suggest.completeFunctionCalls"]
217+
plugin_config.vscode_configuration["typescript.suggest.completeFunctionCalls"] = true
215218

219+
req.label = "warn(...)"
220+
req.insertTextFormat = c.InsertTextFormat.Snippet
221+
ret = vim.lsp.buf_request_sync(0, methods.CompletionResolve, req)
216222
result = lsp_assert.response(ret)
223+
217224
assert.is.table(result)
218-
assert.are.same(result.insertText, "console")
219-
assert.are.same(result.detail, "var console: Console")
225+
assert.are.same(result.insertText, "warn($1)$0")
226+
assert.are.same(result.detail, "(method) Console.warn(...data: any[]): void")
227+
228+
plugin_config.vscode_configuration["typescript.suggest.completeFunctionCalls"] = prev_config
220229
end)
221230

222231
it("should return correct response for " .. methods.SignatureHelp, function()

0 commit comments

Comments
 (0)