sublime_text icon indicating copy to clipboard operation
sublime_text copied to clipboard

The prefix replacement logic when inserting completions

Open rchl opened this issue 4 months ago • 2 comments

Description of the bug

I would want to disable the built-in prefix removal by using the sublime.COMPLETION_FLAG_KEEP_PREFIX flag and replace the prefix manually (after small delay) but it's not clear to me what logic ST uses for removing the preceding text. Is it possible to get info on how this is implemented?

There are some less trivial cases like:

  • text: $f, completion: $foo, expected result: $foo
  • text: b, completion abc, expected result: abc

When thinking about the second case above, I have a feeling this is not really doable without having some more info that is only available internally. We'd need to know what ST considers the prefix to remove (either text or range).

While on_query_completions provides a prefix, it's not updated on continued typing so it would report incomplete prefix a when typing ab. Also in the first case the prefix somehow never includes $ (probably related to word boundaries) but ST still replaces it.

Steps to reproduce

Expected behavior

Actual behavior

Sublime Text build number

4199

Operating system & version

macOS

(Linux) Desktop environment and/or window manager

No response

Additional information

No response

OpenGL context information


rchl avatar Sep 04 '25 19:09 rchl

This is a response from John from 2020 while debugging a bug with DYNAMIC_COMPLETIONS, copying the response here as it might be useful/related (and it doesn't contain any sensitive information.)

Nothing at all is removed from the buffer when COMPLETION_FLAG_KEEP_PREFIX is specified

In reality, it's actually really complex, because we try and make triggers with punctuation do what the user expects, such that the actual range of characters removed when a completion is inserted is a function of both the buffer contents and the trigger. However, if COMPLETION_FLAG_KEEP_PREFIX is given, none of this comes into play.

Actually, looking at the code, it's not correct to say that nothing is removed when COMPLETION_FLAG_KEEP_PREFIX is given

ST internally tracks the state of the text buffer when it calls on_query_completions, and when it inserts a completion with COMPLETION_FLAG_KEEP_PREFIX, it reverts the text buffer to that state before inserting the completion

ST only store that change token when ST get the results back from on_query_completions, rather than when we issue on_query_completions

predragnikolic avatar Sep 23 '25 17:09 predragnikolic

That actually makes it clear that it's not trivial... I think the only way this could work for us if ST would provide an "edit" equivalent to what what would be removed but I suppose this might be a too specific ask for it to be considered.

rchl avatar Sep 23 '25 19:09 rchl