Support import completion

This commit is contained in:
hrsh7th
2022-01-18 19:17:51 +09:00
parent 682818b211
commit 8fc8d31cf4
4 changed files with 50 additions and 37 deletions

View File

@@ -106,33 +106,36 @@ end
---get_word
---@param text string
---@param stop_char number
---@param min_length number
---@return string
str.get_word = function(text, stop_char)
str.get_word = function(text, stop_char, min_length)
min_length = min_length or 0
local has_alnum = false
local stack = {}
local word = {}
local add = function(c)
table.insert(word, string.char(c))
if stack[#stack] == c then
table.remove(stack, #stack)
else
if PAIRS[c] then
table.insert(stack, c)
end
end
end
for i = 1, #text do
local c = string.byte(text, i, i)
if not INVALIDS[c] then
if PAIRS[c] then
table.insert(stack, c)
end
if #word < min_length then
table.insert(word, string.char(c))
elseif not INVALIDS[c] then
add(c)
has_alnum = has_alnum or char.is_alnum(c)
elseif not has_alnum then
if PAIRS[c] then
table.insert(stack, c)
end
table.insert(word, string.char(c))
add(c)
elseif #stack ~= 0 then
table.insert(word, string.char(c))
if stack[#stack] == c then
table.remove(stack, #stack)
else
if PAIRS[c] then
table.insert(stack, c)
end
end
add(c)
if has_alnum and #stack == 0 then
break
end

View File

@@ -9,6 +9,7 @@ describe('utils.str', function()
assert.are.equal(str.get_word('"devDependencies":', string.byte('"')), '"devDependencies')
assert.are.equal(str.get_word('"devDependencies": ${1},', string.byte('"')), '"devDependencies')
assert.are.equal(str.get_word('#[cfg(test)]'), '#[cfg(test)]')
assert.are.equal(str.get_word('import { GetStaticProps$1 } from "next";', nil, 9), 'import { GetStaticProps')
end)
it('strikethrough', function()