From ec9ebd38e138a8da39d87a6208df45b2a1f54506 Mon Sep 17 00:00:00 2001 From: luozhiya Date: Mon, 27 May 2024 18:04:26 +0800 Subject: [PATCH] Step 3 --- lua/fittencode/base.lua | 15 ----- lua/fittencode/engines/inline/init.lua | 2 +- .../engines/inline/suggestions_cache.lua | 13 +++-- lua/fittencode/unicode.lua | 58 ++++++++++++++++++- 4 files changed, 64 insertions(+), 24 deletions(-) diff --git a/lua/fittencode/base.lua b/lua/fittencode/base.lua index bd9f36b..01a59d7 100644 --- a/lua/fittencode/base.lua +++ b/lua/fittencode/base.lua @@ -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 diff --git a/lua/fittencode/engines/inline/init.lua b/lua/fittencode/engines/inline/init.lua index cde14b2..224684d 100644 --- a/lua/fittencode/engines/inline/init.lua +++ b/lua/fittencode/engines/inline/init.lua @@ -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 diff --git a/lua/fittencode/engines/inline/suggestions_cache.lua b/lua/fittencode/engines/inline/suggestions_cache.lua index cefd6cd..3c2a1e6 100644 --- a/lua/fittencode/engines/inline/suggestions_cache.lua +++ b/lua/fittencode/engines/inline/suggestions_cache.lua @@ -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() diff --git a/lua/fittencode/unicode.lua b/lua/fittencode/unicode.lua index af2c95b..806970b 100644 --- a/lua/fittencode/unicode.lua +++ b/lua/fittencode/unicode.lua @@ -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 @@ -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