Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Commit e83c58d

Browse files
committed
feat: context aware snippets expansion
fix
1 parent ec41ef7 commit e83c58d

File tree

1 file changed

+49
-32
lines changed

1 file changed

+49
-32
lines changed

lua/completion/source/lsp.lua

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ local function get_completion_word(item, prefix, suffix)
3636
return item.label
3737
end
3838

39+
local function get_context_aware_snippets(item, completion_item, line_to_cursor)
40+
if protocol.InsertTextFormat[completion_item.insertTextFormat] == "PlainText" then
41+
return
42+
end
43+
local line = vim.api.nvim_get_current_line()
44+
local nextWord = line:sub(#line_to_cursor+1, #line_to_cursor+1)
45+
print(nextWord == " " or #nextWord == 0)
46+
if nextWord == " " or #nextWord == 0 then
47+
return
48+
else
49+
local matches
50+
word, matches = item.word:gsub("%(.*%)$", "")
51+
if matches == 0 then
52+
word, matches = item.word:gsub("<.*>$", "")
53+
end
54+
if matches ~= 0 then
55+
item.word = word
56+
end
57+
end
58+
end
59+
3960
local function text_document_completion_list_to_complete_items(result, opt)
4061
local items = vim.lsp.util.extract_completion_items(result)
4162
if vim.tbl_isempty(items) then
@@ -50,42 +71,38 @@ local function text_document_completion_list_to_complete_items(result, opt)
5071

5172
for _, completion_item in ipairs(items) do
5273
local item = {}
53-
if vim.fn.exists('g:loaded_vsnip_integ') == 1 or
54-
protocol.CompletionItemKind[completion_item.kind] ~= 'Snippet' then
55-
local info = ' '
56-
local documentation = completion_item.documentation
57-
if documentation then
58-
if type(documentation) == 'string' and documentation ~= '' then
59-
info = documentation
60-
elseif type(documentation) == 'table' and type(documentation.value) == 'string' then
61-
info = documentation.value
62-
end
74+
local info = ' '
75+
local documentation = completion_item.documentation
76+
if documentation then
77+
if type(documentation) == 'string' and documentation ~= '' then
78+
info = documentation
79+
elseif type(documentation) == 'table' and type(documentation.value) == 'string' then
80+
info = documentation.value
6381
end
64-
item.info = info
82+
end
83+
item.info = info
6584

66-
item.word = get_completion_word(completion_item, opt.prefix, opt.suffix)
67-
if opt.suffix ~= nil and #opt.suffix ~= 0 then
68-
local index = item.word:find(opt.suffix)
69-
if index ~= nil then
70-
local newWord = item.word
71-
newWord = newWord:sub(1, index-1)
72-
item.word = newWord
73-
print(item.word, index, newWord)
74-
end
85+
item.word = get_completion_word(completion_item, opt.prefix, opt.suffix)
86+
if opt.suffix ~= nil and #opt.suffix ~= 0 then
87+
local index = item.word:find(opt.suffix)
88+
if index ~= nil then
89+
local newWord = item.word
90+
newWord = newWord:sub(1, index-1)
91+
item.word = newWord
7592
end
76-
item.user_data = {
77-
lsp = {
78-
completion_item = completion_item,
79-
}
80-
}
81-
local kind = protocol.CompletionItemKind[completion_item.kind]
82-
item.kind = customize_label[kind] or kind
83-
item.abbr = completion_item.label
84-
-- get_context_aware_snippets(item, completion_item, opt.textMatch)
85-
item.priority = vim.g.completion_items_priority[item.kind]
86-
item.menu = completion_item.detail or ''
87-
match.matching(matches, opt.prefix, item)
8893
end
94+
item.user_data = {
95+
lsp = {
96+
completion_item = completion_item,
97+
}
98+
}
99+
local kind = protocol.CompletionItemKind[completion_item.kind]
100+
item.kind = customize_label[kind] or kind
101+
item.abbr = completion_item.label
102+
get_context_aware_snippets(item, completion_item, opt.line_to_cursor)
103+
item.priority = vim.g.completion_items_priority[item.kind]
104+
item.menu = completion_item.detail or ''
105+
match.matching(matches, opt.prefix, item)
89106
end
90107

91108
return matches

0 commit comments

Comments
 (0)