Skip to content

Commit

Permalink
feat(entry): add context field and get_offset method
Browse files Browse the repository at this point in the history
  • Loading branch information
max397574 committed Jul 8, 2024
1 parent 61905df commit 5bab294
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
21 changes: 19 additions & 2 deletions docs/entry.norg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ categories: [
types
]
created: 2023-11-15T17:42:46+0100
updated: 2024-06-23T14:32:14+0100
updated: 2024-07-07T21:15:43+0100
tangle: {
languages: {
lua: ../lua/neocomplete/types/entry.lua
Expand All @@ -36,7 +36,7 @@ version: 1.1.1
#tangle
@code lua
--- Creates a new entry
---@field new fun(completion_item: lsp.CompletionItem, source: neocomplete.internal_source): neocomplete.entry
---@field new fun(completion_item: lsp.CompletionItem, source: neocomplete.internal_source, context: neocomplete.context): neocomplete.entry
@end

** Get insert text
Expand Down Expand Up @@ -69,6 +69,15 @@ version: 1.1.1
---@field source neocomplete.internal_source
@end

** Context
This is the context in which the entry was completed. This is important to now what context text-
edits of the entry target.
#tangle
@code lua
--- Context in which entry was completed
---@field context neocomplete.context
@end

** Matches
Position of matches which were found during filtering. This is just used to highlight them in the
completion menu with `@neocomplete.match`.
Expand All @@ -86,3 +95,11 @@ version: 1.1.1
--- Score from filtering
---@field score number
@end

** Get Offset
Essentially where entry insertion should happen (column)
#tangle
@code lua
--- Get offset of entry
---@field get_offset fun(self: neocomplete.entry): integer
@end
24 changes: 23 additions & 1 deletion lua/neocomplete/entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,36 @@
---@diagnostic disable-next-line: missing-fields
local Entry = {}

function Entry.new(completion_item, source)
function Entry.new(completion_item, source, context)
---@type neocomplete.entry
local self = setmetatable({}, { __index = Entry })
self.completion_item = completion_item
self.source = source
self.context = context
self.matches = {}
return self
end
local function is_white(str)
return string.match(str, "^%s*$") ~= nil
end

function Entry:get_offset()
local offset, _ = vim.regex(self.source:get_keyword_pattern() .. "\\m$"):match_str(self.context.line_before_cursor)
offset = offset or #self.context.line_before_cursor
if self.completion_item.textEdit then
local range = self.completion_item.textEdit.insert or self.completion_item.textEdit.range
if range then
local c = range.start.character
for idx = c, self.context.cursor.col do
if not is_white(string.byte(self.context.line, idx)) then
offset = idx
break
end
end
end
end
return offset
end

function Entry:get_insert_text()
local completion_item = self.completion_item
Expand Down
8 changes: 6 additions & 2 deletions lua/neocomplete/types/entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
---@class neocomplete.entry
---@field completion_item lsp.CompletionItem
--- Creates a new entry
---@field new fun(completion_item: lsp.CompletionItem, source: neocomplete.internal_source): neocomplete.entry
---@field new fun(completion_item: lsp.CompletionItem, source: neocomplete.internal_source, context: neocomplete.context): neocomplete.entry
--- Get insert text
---@field get_insert_text fun(self: neocomplete.entry): string
--- Get insert word
---@field get_insert_word fun(self: neocomplete.entry): string
--- Source from which the entry came
---@field source neocomplete.internal_source
--- Context in which entry was completed
---@field context neocomplete.context
--- Matches in filter text
---@field matches integer[]
--- Score from filtering
---@field score number
---@field score number
--- Get offset of entry
---@field get_offset fun(self: neocomplete.entry): integer

0 comments on commit 5bab294

Please sign in to comment.