Skip to content

Commit

Permalink
Step 3
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed May 27, 2024
1 parent 53645fe commit ec9ebd3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 24 deletions.
15 changes: 0 additions & 15 deletions lua/fittencode/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,6 @@ function M.is_bsd()
end, sys) > 0
end

---@param char string
---@return boolean
function M.is_alpha(char)
---@type integer
local byte = char:byte()
return (byte >= 65 and byte <= 90) or (byte >= 97 and byte <= 122)
end

---@param char string
---@return boolean
function M.is_space(char)
local byte = string.byte(char)
return byte == 32 or byte == 9
end

function M.tbl_keys_by_value(tbl, value)
local keys = {}
for k, v in pairs(tbl) do
Expand Down
2 changes: 1 addition & 1 deletion lua/fittencode/engines/inline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ end
local function calculate_next_word_index(line, utf8_index)
local prev_ctype = nil
for i = 1, string.len(line) do
local char, pos = Unicode.find_first_character(line, utf8_index, i)
local char, pos = Unicode.find_next_character(line, utf8_index, i)
if not pos or not char then
break
end
Expand Down
13 changes: 8 additions & 5 deletions lua/fittencode/engines/inline/suggestions_cache.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
---@class SuggestionsCache
---@field private task_id? integer
---@field private lines? string[]
---@field private row? integer
---@field private col? integer
---@field private count? integer
---@field task_id? integer
---@field lines? string[]
---@field triggered_cursor? integer[]
---@field commit_cursor? integer[]
---@field stage_cursor? integer[]
---@field utf_start? integer[][]
---@field utf_end? integer[][]
---@field utf_pos? integer[][]
local SuggestionsCache = {}

function SuggestionsCache.new()
Expand Down
58 changes: 55 additions & 3 deletions lua/fittencode/unicode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ function M.find_zero(tbl, start_index)
end
end

function M.find_first_character(s, tbl, start_index)
function M.find_zero_reverse(tbl, start_index)
for i = start_index, 1, -1 do
if tbl[i] == 0 then
return i
end
end
end

function M.find_next_character(s, tbl, start_index)
if #tbl == 0 then
return nil
end

local v1 = M.find_zero(tbl, start_index)
assert(v1 == start_index)
if v1 == nil then
-- Invalid UTF-8 sequence
return nil
end

Expand All @@ -48,4 +54,50 @@ function M.find_first_character(s, tbl, start_index)
return char, { v1, v2 }
end

function M.utf_pos(line)
return vim.str_utf_pos(line)
end

function M.utf_pos_list(lines)
local utf_pos = {}
for i, line in ipairs(lines) do
utf_pos[i] = vim.str_utf_pos(line)
end
return utf_pos
end

function M.utf_start(line)
local index = {}
for i = 1, #line do
table.insert(index, #index + 1, vim.str_utf_start(line, i))
end
return index
end

function M.utf_start_list(lines)
local index = {}
for i, line in ipairs(lines) do
local line_index = M.utf_start(line)
index[i] = line_index
end
return index
end

function M.utf_end(line)
local index = {}
for i = 1, #line do
table.insert(index, #index + 1, vim.str_utf_end(line, i))
end
return index
end

function M.utf_end_list(lines)
local index = {}
for i, line in ipairs(lines) do
local line_index = M.utf_end(line)
index[i] = line_index
end
return index
end

return M

0 comments on commit ec9ebd3

Please sign in to comment.