Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.

Commit

Permalink
Support multiple start_offset
Browse files Browse the repository at this point in the history
  • Loading branch information
hrsh7th committed Nov 5, 2020
1 parent 26f04ff commit 1c10241
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 52 deletions.
7 changes: 5 additions & 2 deletions lua/compe/completion/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,12 @@ function Completion:display(context)
local source_items = Matcher.match(context, source)
if #source_items > 0 and (source.is_triggered_by_character or source.is_triggered_by_character == use_trigger_character) then
use_trigger_character = use_trigger_character or source.is_triggered_by_character
local gap = string.sub(context.before_line, start_offset, source:get_start_offset() - 1)
for _, item in ipairs(source_items) do
if words[item.word] == nil or item.dup ~= true then
words[item.word] = true
if words[item.original_word] == nil or item.dup ~= true then
words[item.original_word] = true
item.word = gap .. item.original_word
item.abbr = gap .. item.original_abbr
table.insert(items, item)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lua/compe/completion/matcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ function Matcher.match(context, source)

local matches = {}
for _, item in ipairs(source:get_items()) do
local word = item.word
local word = item.original_word
if item.filter_text and #input > 0 and (string.sub(item.filter_text, 1, 1) == string.sub(input, 1, 1)) then
word = item.filter_text
end

item.score = 0
if #item.word >= #input then
if #word >= #input then
item.score = Matcher.score(input, word)
item.exact = word == input
end
Expand Down
56 changes: 8 additions & 48 deletions lua/compe/completion/source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,10 @@ end
-- This method add special attributes for each items.
-- * priority
-- * asis
function Source:normalize_items(context, items)
local start_offset = self:get_start_offset()
function Source:normalize_items(_, items)
local metadata = self:get_metadata()
local normalized = {}

local _, _, before = string.find(string.sub(context.before_line, 1, start_offset - 1), '([^%s]*)$')
local _, _, after = string.find(context.after_line, '^([^%s]*)')

for _, item in pairs(items) do
-- string to completed_item
if type(item) == 'string' then
Expand All @@ -175,23 +171,9 @@ function Source:normalize_items(context, items)
}
end

local word = self:trim_word(before, after, item.word)

if word ~= item.word then
Debug:log(vim.inspect({
before = before;
after = after;
fixed_word = word;
item_word = item.word;
}))
end

item.word = word

-- add abbr if does not exists
if item.abbr == nil then
item.abbr = item.word
end
-- create word/abbr
item.word = item.word
item.abbr = item.abbr or item.word

-- required properties
item.dup = metadata.dup ~= nil and metadata.dup or 1
Expand All @@ -203,36 +185,14 @@ function Source:normalize_items(context, items)
item.priority = metadata.priority or 0
item.asis = string.find(item.abbr, item.word, 1, true) == 1

-- restore original word/abbr
item.original_word = item.word
item.original_abbr = item.abbr

table.insert(normalized, item)
end
return normalized
end

-- trim_word
function Source:trim_word(before, after, word)
local word_len = #word

if before ~= nil then
for prefix_overlap = word_len, 1, -1 do
if string.sub(before, #before - prefix_overlap + 1) == string.sub(word, 1, prefix_overlap) then
word = string.sub(word, prefix_overlap + 1)
break
end
end
end

if after ~= nil then
for postfix_overlap = word_len, 1, -1 do
local word_index = word_len - postfix_overlap + 1
if string.sub(after, 1, postfix_overlap) == string.sub(word, word_index) then
word = string.sub(word, 1, word_index - 1)
break
end
end
end

return word
end

return Source

0 comments on commit 1c10241

Please sign in to comment.