Skip to content

Commit 369e0ab

Browse files
committed
Fixed autocomplete cancellation bug
1 parent b7feef7 commit 369e0ab

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

TextEditor.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,13 @@ void TextEditor::handleKeyboardInputs() {
692692

693693
// ignore specific keys when autocomplete is active, they will be handled later
694694
if (autocomplete.isActive() && autocomplete.isSpecialKeyPressed()) {
695-
return;
695+
if (autocomplete.hasSuggestions()) {
696+
return;
697+
698+
} else {
699+
// this is the exception, cancel autocomplete when special keys are used without any suggestions
700+
autocomplete.cancel();
701+
}
696702
}
697703

698704
// cursor movements and selections

TextEditor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,10 @@ class TextEditor {
491491
// this is left to the application so it can be context specific in case a language server is used
492492
// a pointer to the current language definition is provided so callbacks have easy access
493493
std::vector<std::string> suggestions;
494+
495+
// set this to true if you are building the suggestion list asynchronously and provide it later
496+
// this way autocomplete is not cancelled if the suggestion list is empty and the user hits tab or enter
497+
bool suggestionsPromise = false;
494498
};
495499

496500
// autocomplete configuration (defaults are like Visual Studio Code)
@@ -531,6 +535,7 @@ class TextEditor {
531535

532536
// if it takes too long, applications should do search in separate thread and
533537
// use API to report results (see SetAutoCompleteSuggestions)
538+
// callback should set suggestionsPromise to true in this case
534539
std::function<void(AutoCompleteState&)> callback;
535540

536541
// opaque void* that must be managed externally but passed to callback
@@ -1121,6 +1126,7 @@ class TextEditor {
11211126

11221127
// get information
11231128
inline bool isActive() const { return active; }
1129+
inline bool hasSuggestions() const { return state.suggestions.size() > 0 || state.suggestionsPromise; }
11241130
bool isSpecialKeyPressed() const;
11251131
inline ImGuiKeyChord getTriggerShortcut() const { return configuration.triggerShortcut; }
11261132
inline Coordinate getStart() const { return startLocation; }

docs/autocomplete.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public:
6464

6565
// if it takes too long, applications should do search in separate thread and
6666
// use API to report results (see SetAutoCompleteSuggestions)
67+
// callback should set suggestionsPromise to true in this case
6768
std::function<void(AutoCompleteState&)> callback;
6869

6970
// optional opaque void* that must be managed externally but passed to callback
@@ -104,6 +105,10 @@ public:
104105
// this is left to the application so it can be context specific in case a language server is used
105106
// a pointer to the current language definition is provided so callbacks have easy access
106107
std::vector<std::string> suggestions;
108+
109+
// set this to true if you are building the suggestion list asynchronously and provide it later
110+
// this way autocomplete is not cancelled if the suggestion list is empty and the user hits tab or enter
111+
bool suggestionsPromise = false;
107112
};
108113
```
109114

0 commit comments

Comments
 (0)