debug completion
This commit is contained in:
		@@ -63,19 +63,32 @@ local function show_suggestion(suggestion, start_col)
 | 
			
		||||
    
 | 
			
		||||
    -- Process first line of suggestion
 | 
			
		||||
    local first_line = suggestion_lines[1]
 | 
			
		||||
    if not first_line:find("^" .. vim.pesc(prefix), 1, true) then
 | 
			
		||||
        first_line = prefix .. first_line
 | 
			
		||||
    
 | 
			
		||||
    -- Find the longest common prefix between the input and suggestion
 | 
			
		||||
    local common_length = 0
 | 
			
		||||
    local input_after_cursor = string.sub(line_text, start_col + 1)
 | 
			
		||||
    local suggestion_text = first_line
 | 
			
		||||
    
 | 
			
		||||
    while common_length < #input_after_cursor and common_length < #suggestion_text do
 | 
			
		||||
        if string.sub(input_after_cursor, common_length + 1, common_length + 1) ==
 | 
			
		||||
           string.sub(suggestion_text, common_length + 1, common_length + 1) then
 | 
			
		||||
            common_length = common_length + 1
 | 
			
		||||
        else
 | 
			
		||||
            break
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
    first_line = first_line:sub(#prefix + 1)
 | 
			
		||||
    
 | 
			
		||||
    -- Only show the part of suggestion that doesn't overlap with existing text
 | 
			
		||||
    first_line = string.sub(suggestion_text, common_length + 1)
 | 
			
		||||
    
 | 
			
		||||
    if first_line == "" and #suggestion_lines == 1 then return end
 | 
			
		||||
    
 | 
			
		||||
    current_suggestion.text = suggestion
 | 
			
		||||
    current_suggestion.start_col = start_col
 | 
			
		||||
    
 | 
			
		||||
    -- Show first line as virtual text
 | 
			
		||||
    -- Show first line as virtual text only if there's non-overlapping content
 | 
			
		||||
    if first_line ~= "" then
 | 
			
		||||
        vim.api.nvim_buf_set_extmark(0, current_suggestion.namespace_id, line, start_col, {
 | 
			
		||||
        vim.api.nvim_buf_set_extmark(0, current_suggestion.namespace_id, line, start_col + common_length, {
 | 
			
		||||
            virt_text = {{first_line, 'GeminiSuggestion'}},
 | 
			
		||||
            virt_text_pos = 'inline',
 | 
			
		||||
            virt_text_hide = true,
 | 
			
		||||
@@ -89,13 +102,13 @@ local function show_suggestion(suggestion, start_col)
 | 
			
		||||
            table.insert(virt_lines, {{suggestion_lines[i], 'GeminiSuggestion'}})
 | 
			
		||||
        end
 | 
			
		||||
        
 | 
			
		||||
        vim.api.nvim_buf_set_extmark(0, current_suggestion.namespace_id, line, start_col, {
 | 
			
		||||
        vim.api.nvim_buf_set_extmark(0, current_suggestion.namespace_id, line, start_col + common_length, {
 | 
			
		||||
            virt_lines = virt_lines,
 | 
			
		||||
            virt_lines_above = false,
 | 
			
		||||
        })
 | 
			
		||||
    end
 | 
			
		||||
    
 | 
			
		||||
    debug_print("Showing suggestion (%d lines)", #suggestion_lines)
 | 
			
		||||
    debug_print("Showing suggestion (%d lines, common prefix length: %d)", #suggestion_lines, common_length)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function M.accept_suggestion()
 | 
			
		||||
@@ -111,19 +124,38 @@ function M.accept_suggestion()
 | 
			
		||||
        return false
 | 
			
		||||
    end
 | 
			
		||||
    
 | 
			
		||||
    -- Get current line text
 | 
			
		||||
    local line_text = vim.api.nvim_buf_get_lines(0, line, line + 1, true)[1]
 | 
			
		||||
    local text_after_cursor = string.sub(line_text, col + 1)
 | 
			
		||||
    
 | 
			
		||||
    -- Split suggestion into lines
 | 
			
		||||
    local suggestion_lines = vim.split(current_suggestion.text, "\n")
 | 
			
		||||
    if #suggestion_lines == 0 then return false end
 | 
			
		||||
    
 | 
			
		||||
    -- Insert first line at cursor position
 | 
			
		||||
    vim.api.nvim_buf_set_text(
 | 
			
		||||
        0,
 | 
			
		||||
        line,
 | 
			
		||||
        col,
 | 
			
		||||
        line,
 | 
			
		||||
        col,
 | 
			
		||||
        {suggestion_lines[1]}
 | 
			
		||||
    )
 | 
			
		||||
    -- For the first line, only insert the part that doesn't overlap with existing text
 | 
			
		||||
    local first_line = suggestion_lines[1]
 | 
			
		||||
    local common_length = 0
 | 
			
		||||
    while common_length < #text_after_cursor and common_length < #first_line do
 | 
			
		||||
        if string.sub(text_after_cursor, common_length + 1, common_length + 1) ==
 | 
			
		||||
           string.sub(first_line, common_length + 1, common_length + 1) then
 | 
			
		||||
            common_length = common_length + 1
 | 
			
		||||
        else
 | 
			
		||||
            break
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
    
 | 
			
		||||
    -- Only insert the non-overlapping part
 | 
			
		||||
    local to_insert = string.sub(first_line, common_length + 1)
 | 
			
		||||
    if to_insert ~= "" then
 | 
			
		||||
        vim.api.nvim_buf_set_text(
 | 
			
		||||
            0,
 | 
			
		||||
            line,
 | 
			
		||||
            col,
 | 
			
		||||
            line,
 | 
			
		||||
            col,
 | 
			
		||||
            {to_insert}
 | 
			
		||||
        )
 | 
			
		||||
    end
 | 
			
		||||
    
 | 
			
		||||
    -- Insert remaining lines below
 | 
			
		||||
    if #suggestion_lines > 1 then
 | 
			
		||||
@@ -138,7 +170,12 @@ function M.accept_suggestion()
 | 
			
		||||
    
 | 
			
		||||
    -- Move cursor to end of inserted text
 | 
			
		||||
    local final_line = line + #suggestion_lines - 1
 | 
			
		||||
    local final_col = #suggestion_lines[#suggestion_lines]
 | 
			
		||||
    local final_col
 | 
			
		||||
    if final_line == line then
 | 
			
		||||
        final_col = col + #to_insert
 | 
			
		||||
    else
 | 
			
		||||
        final_col = #suggestion_lines[#suggestion_lines]
 | 
			
		||||
    end
 | 
			
		||||
    vim.api.nvim_win_set_cursor(0, {final_line + 1, final_col})
 | 
			
		||||
    
 | 
			
		||||
    clear_suggestion()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user