Check first (n?) characters of a suggestion · Issue #487 · zbirenbaum/copilot.lua · GitHub
Skip to content

Check first (n?) characters of a suggestion #487

Open

Description

@doggobit0

So I probably hate myself more than most people and want to use <Tab> for completions :)

But I'm trying to dynamically check if the first (few?) characters of a suggestion are \t, in which case I'll simply passthrough the Tab key. I am sort of able to do this right now with suggestion.accept(<function>), but I think it'd be a quick and user friendly addition to either provide a method to get the first (n?) chars of a suggestion, or take in a string and match it against its text. E.g.:

local first_5 = suggestion.get_n_chars(5)
-- or
local starts_with_tab = suggestion.startswith('\t')

Anyway, cheers for the amazing plugin!

Activity

  1. doggobit0 commented on May 15, 2025

    @doggobit0
    Author

    P.S. If any unfortunate soul also has this same bad idea, here's how I ended up doing it; I'm not sure if it's the best way to do it, but it works :)

    vim.keymap.set("i", "<Tab>", function()
    	local suggestion = require("copilot.suggestion")
    
    	if suggestion.is_visible() then
    		suggestion.accept(function(suggestion)
    			local current_char = suggestion.position.character
    			local first_char = suggestion.text:sub(current_char + 1, current_char + 1)
    
    			if first_char == '\t' then
    				suggestion.text = string.rep("\t", current_char + 1)
    				suggestion.range["end"].character = current_char + 1
    				return suggestion
    			end
    
    			return suggestion
    		end)
    		return ""
    	end
    
    	return vim.api.nvim_replace_termcodes("<Tab>", true, false, true)
    end, { expr = true, silent = true })

    If you're using spaces as indents, you can probably easily adapt it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions