Skip to content

Commit

Permalink
fix: complete_function_calls behavior with JSX, imports
Browse files Browse the repository at this point in the history
  • Loading branch information
m-gail committed Feb 23, 2024
1 parent c43d958 commit 6e4de04
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ function M.handler(request, response, params)
local context = params.context or {}
local trigger_character = context.triggerCharacter
local requested_bufnr = vim.uri_to_bufnr(text_document.uri)
local filename = vim.uri_to_fname(text_document.uri)
local filetype = vim.bo[requested_bufnr].filetype
local is_valid_context_for_function_snippet = utils.is_valid_context_for_function_snippet(requested_bufnr, params.position, filename, request)

-- tsserver protocol reference:
-- https//github.com/microsoft/TypeScript/blob/8b482b513d87c6fcda8ece18b99f8a01cff5c605/lib/protocol.d.ts#L1631
Expand Down Expand Up @@ -110,7 +112,7 @@ function M.handler(request, response, params)
end
end

local should_create_function_snippet = utils.should_create_function_snippet(kind, filetype)
local should_create_function_snippet = utils.should_create_function_snippet(kind, insertText, filetype) and is_valid_context_for_function_snippet
local should_create_snippet = item.isSnippet or should_create_function_snippet
local label = is_optional and (item.name .. "?") or item.name
label = should_create_function_snippet and (label .. "(...)") or label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ end
function M.handler(request, response, params)
local requested_bufnr = vim.uri_to_bufnr(vim.uri_from_fname(params.data.file))
local filetype = vim.bo[requested_bufnr].filetype
local is_valid_context_for_function_snippet = utils.is_valid_context_for_function_snippet(requested_bufnr, params.data, params.data.file, request)

-- tsserver protocol reference:
-- https://github.com/microsoft/TypeScript/blob/549e61d0af1ba885be29d69f341e7d3a00686071/lib/protocol.d.ts#L1661
Expand Down Expand Up @@ -173,7 +174,7 @@ function M.handler(request, response, params)
-- or neovim even handle that for now i skip this
})

if utils.should_create_function_snippet(item.kind, item.insertText, filetype) then
if utils.should_create_function_snippet(item.kind, item.insertText, filetype) and is_valid_context_for_function_snippet then
create_snippet(item, details.displayParts)
end

Expand Down
42 changes: 42 additions & 0 deletions lua/typescript-tools/protocol/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,48 @@ function M.should_create_function_snippet(kind, insertText, filetype)
and not string.find(insertText, "%$")
end

---@param bufnr number
---@param position LspPosition
---@return boolean
local function is_completing_jsx(bufnr, position)
local requested_line = vim.api.nvim_buf_get_lines(bufnr, position.line, position.line + 1, false)[1]
local requested_line_until_position = requested_line:sub(1, position.character)
local last_word_before_position = nil

for word in requested_line_until_position:gmatch("%S+") do
last_word_before_position = word
end

return last_word_before_position ~= nil and last_word_before_position:sub(1, 1) == "<"
end

---@param position LspPosition
---@param file string
---@param request TsserverRequest
---@return boolean
local function should_omit_function_snippet_in_context(position, file, request)
request {
command = c.CommandTypes.Quickinfo,
arguments = vim.tbl_extend("force", {
file = file
}, M.convert_lsp_position_to_tsserver(position))
}

local body = coroutine.yield()

print(vim.inspect(body))

return vim.tbl_contains({ "var", "let", "const", "alias" }, body.kind)
end

---@param bufnr number
---@param position LspPosition
---@param request TsserverRequest
---@return boolean
function M.is_valid_context_for_function_snippet(bufnr, position, file, request)
return not is_completing_jsx(bufnr, position) and not should_omit_function_snippet_in_context(position, file, request)
end

---@param content string
---@param kind "plaintext"|"markdown"|nil
function M.make_markup_content(content, kind)
Expand Down

0 comments on commit 6e4de04

Please sign in to comment.