Skip to content

Commit

Permalink
test: add test cases for snippet and non-snippet
Browse files Browse the repository at this point in the history
Also adds type annotations to function snippet feature.
  • Loading branch information
llllvvuu committed Jul 24, 2023
1 parent 1de9bd5 commit c3046cc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lua/typescript-tools/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ setmetatable(M, {
end,
})

---@param filetype vim.opt.filetype
---@return table
function M.get_tsserver_file_preferences(filetype)
local preferences = __store.tsserver_file_preferences
return vim.tbl_extend(
Expand Down
1 change: 1 addition & 0 deletions lua/typescript-tools/protocol/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ return {
Imports = "imports",
Region = "region",
},
---@enum InsertTextFormat
InsertTextFormat = {
PlainText = 1,
Snippet = 2,
Expand Down
14 changes: 12 additions & 2 deletions lua/typescript-tools/protocol/text_document/completion/resolve.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ local function make_text_edits(code_actions)
return text_edits
end

-- @see https://github.com/typescript-language-server/typescript-language-server/blob/983a6923114c39d638e0c7d419ae16e8bca8985c/src/completion.ts#L355-L371
---@alias DisplayPart { kind: string, text: string }

---@param display_parts DisplayPart[]
---@return { has_optional_parameters: boolean, parts: DisplayPart[] }
---@see https://github.com/typescript-language-server/typescript-language-server/blob/983a6923114c39d638e0c7d419ae16e8bca8985c/src/completion.ts#L355-L371
local function get_parameter_list_parts(display_parts)
local parts = {}
local is_in_method = false
Expand Down Expand Up @@ -75,7 +79,13 @@ local function get_parameter_list_parts(display_parts)
return { has_optional_parameters = has_optional_parameters, parts = parts }
end

-- @see https://github.com/typescript-language-server/typescript-language-server/blob/983a6923114c39d638e0c7d419ae16e8bca8985c/src/completion.ts#L355-L371
---@alias PartialCompletionItem
---| { insertText: string, insertTextFormat: InsertTextFormat, textEdit: { newText: string }, label: string }

---@param item PartialCompletionItem
---@param display_parts DisplayPart[]
---@return nil
---@see https://github.com/typescript-language-server/typescript-language-server/blob/983a6923114c39d638e0c7d419ae16e8bca8985c/src/completion.ts#L355-L371
local function create_snippet(item, display_parts)
local parameter_list_parts = get_parameter_list_parts(display_parts)
local has_optional_parameters = parameter_list_parts.has_optional_parameters
Expand Down
5 changes: 4 additions & 1 deletion lua/typescript-tools/protocol/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ function M.cancelled_response(data)
}
end

-- @see https://github.com/typescript-language-server/typescript-language-server/blob/983a6923114c39d638e0c7d419ae16e8bca8985c/src/completion.ts#L355-L371
---@param kind CompletionItemKind
---@param filetype vim.opt.filetype
---@return boolean
---@see https://github.com/typescript-language-server/typescript-language-server/blob/983a6923114c39d638e0c7d419ae16e8bca8985c/src/completion.ts#L355-L371
function M.should_create_function_snippet(kind, filetype)
local preferences = plugin_config.get_tsserver_file_preferences(filetype)
return preferences.includeCompletionsWithSnippetText
Expand Down
43 changes: 41 additions & 2 deletions tests/requests_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,29 @@ describe("Lsp request", function()
assert.is.True(#items >= 20)

local completions = vim.tbl_map(function(it)
if it.label == "assert~" or it.label == "warn~" then
assert.are.same(it.insertTextFormat, c.InsertTextFormat.Snippet)
end
return it.label
end, items)
table.sort(completions)

assert.are.same(completions[1], "assert~")
assert.are.same(completions[#completions], "warn~")

ret = vim.lsp.buf_request_sync(0, methods.Completion, {
textDocument = utils.get_text_document(),
position = utils.make_position(0, 6),
})
result = lsp_assert.response(ret)
local can_complete_as_console = false
for _, item in ipairs(result.items) do
if item.label == "console" then
assert.are.same(item.insertTextFormat, c.InsertTextFormat.PlainText)
can_complete_as_console = true
end
end
assert(can_complete_as_console)
end)

it("should return correct response for " .. methods.CompletionResolve, function()
Expand All @@ -169,15 +186,37 @@ describe("Lsp request", function()
},
filterText = "warn",
insertText = "warn",
insertTextFormat = 1,
kind = 2,
insertTextFormat = c.InsertTextFormat.Snippet,
kind = c.CompletionItemKind.Function,
label = "warn",
sortText = "11",
})

local result = lsp_assert.response(ret)
assert.is.table(result)
assert.are.same(result.insertText, "warn($1)$0")
assert.are.same(result.detail, "(method) Console.warn(...data: any[]): void")

ret = vim.lsp.buf_request_sync(0, methods.CompletionResolve, {
commitCharacters = { ".", "?" },
data = {
character = 6,
entryNames = { "console" },
file = vim.fs.dirname(vim.api.nvim_buf_get_name(0)) .. "/completion.ts",
line = 0,
},
filterText = "console",
insertText = "console",
insertTextFormat = c.InsertTextFormat.PlainText,
kind = c.CompletionItemKind.Variable,
label = "console",
sortText = "15",
})

result = lsp_assert.response(ret)
assert.is.table(result)
assert.are.same(result.insertText, "console")
assert.are.same(result.detail, "var console: Console")
end)

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

0 comments on commit c3046cc

Please sign in to comment.