Skip to content

Incorrect autocomplete inserts to lines with non-latin chars #834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions lua/obsidian/completion/refs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ local find_search_start = function(input)
return nil
end

---Count the number of non-Latin characters in a string
---
---@param str string
---@return integer
local function count_non_latin_chars(str)
local count = 0
for uchar in str:gmatch "[%z\1-\127\194-\244][\128-\191]*" do
-- Check that the character is not in the Latin alphabet (A-Z, a-z) and is not an ASCII character
if not uchar:match "^[A-Za-z0-9%s%p]$" then
count = count + 1
end
end
return count
end

---Check if a completion request can/should be carried out. Returns a boolean
---and, if true, the search string and the column indices of where the completion
---items should be inserted.
Expand All @@ -39,19 +54,33 @@ M.can_complete = function(request)
return false
end

local start_col = nil
local end_col = nil
local ref_type = nil
local cursor_col = request.context.cursor.col

if vim.startswith(input, "[[") then
local suffix = string.sub(request.context.cursor_after_line, 1, 2)
local cursor_col = request.context.cursor.col
local insert_end_offset = suffix == "]]" and 1 or -1
return true, search, cursor_col - 1 - #input, cursor_col + insert_end_offset, M.RefType.Wiki
start_col = cursor_col - 1 - #input
end_col = cursor_col + insert_end_offset
ref_type = M.RefType.Wiki
elseif vim.startswith(input, "[") then
local suffix = string.sub(request.context.cursor_after_line, 1, 1)
local cursor_col = request.context.cursor.col
local insert_end_offset = suffix == "]" and 0 or -1
return true, search, cursor_col - 1 - #input, cursor_col + insert_end_offset, M.RefType.Markdown
start_col = cursor_col - 1 - #input
end_col = cursor_col + insert_end_offset
ref_type = M.RefType.Markdown
else
return false
end

-- Adjust the column indices to account for non-Latin and non-ASCII characters
local non_latin_chars = count_non_latin_chars(request.context.cursor_before_line)
start_col = start_col - non_latin_chars
end_col = end_col - non_latin_chars

return true, search, start_col, end_col, ref_type
end

M.get_trigger_characters = function()
Expand Down
Loading