Skip to content

Commit 1151a5b

Browse files
committed
Some cleanup
1 parent afce364 commit 1151a5b

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

TextEditor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3266,7 +3266,7 @@ static inline bool isIdentifier(TextEditor::Color color) {
32663266
color == TextEditor::Color::knownIdentifier;
32673267
}
32683268

3269-
void TextEditor::Document::iterateIdentifiers(std::function<void(const std::string&)> callback) {
3269+
void TextEditor::Document::iterateIdentifiers(std::function<void(const std::string&)> callback) const {
32703270
for (size_t i = 0; i < size(); i++) {
32713271
auto p = at(i).begin();
32723272
auto end = at(i).end();

TextEditor.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,7 @@ class TextEditor {
130130
inline void SelectAll() { selectAll(); }
131131
inline void SelectLine(int line) { if (line >= 0 && line < document.lineCount()) selectLine(line); }
132132
inline void SelectLines(int start, int end) { if (start >= 0 && end < document.lineCount() && start <= end) selectLines(start, end); }
133-
134-
inline void SelectRegion(int startLine, int startColumn, int endLine, int endColumn) {
135-
selectRegion(startLine, startColumn, endLine, endColumn);
136-
}
137-
133+
inline void SelectRegion(int startLine, int startColumn, int endLine, int endColumn) { selectRegion(startLine, startColumn, endLine, endColumn); }
138134
inline void SelectToBrackets(bool includeBrackets=true) { selectToBrackets(includeBrackets); }
139135
inline void GrowSelectionsToCurlyBrackets() { growSelectionsToCurlyBrackets(); }
140136
inline void ShrinkSelectionsToCurlyBrackets() { shrinkSelectionsToCurlyBrackets(); }
@@ -439,7 +435,7 @@ class TextEditor {
439435
inline std::string GetLanguageName() const { return language == nullptr ? "None" : language->name; }
440436

441437
// iterate through identifiers detected by the colorizer (based on current language)
442-
inline void IterateIdentifiers(std::function<void(const std::string& identifier)> callback) { document.iterateIdentifiers(callback); }
438+
inline void IterateIdentifiers(std::function<void(const std::string& identifier)> callback) const { document.iterateIdentifiers(callback); }
443439

444440
// autocomplete state (acts as API between editor and outer application)
445441
class AutoCompleteState {
@@ -924,7 +920,7 @@ class TextEditor {
924920
void iterateUserData(std::function<void(int line, void* data)> callback) const;
925921

926922
// iterate through document to find identifiers
927-
void iterateIdentifiers(std::function<void(const std::string& identifier)> callback);
923+
void iterateIdentifiers(std::function<void(const std::string& identifier)> callback) const;
928924

929925
// utility functions
930926
bool isWholeWord(Coordinate start, Coordinate end) const;

docs/autocomplete.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,59 @@ poor-man's solution, more sophisticated solutions probably required external
114114
language engines/services that are beyond the scope of this editor. With the
115115
provided API however, connections to external capabilities can be established
116116
and context-sensitive suggestions can be provided based on the most advanced
117-
algorithms (even good-old AI slop :-)
117+
algorithms (even good-old AI slop :-).
118+
119+
Below is a quick snippet that shows how to use the Trie class to implement
120+
a poor-man's autocomplete without using a language server. This snippet
121+
was taken from the [example application](../example/) so can see it in
122+
context.
123+
124+
```c++
125+
void Editor::setAutocompleteMode(bool flag) {
126+
// see we are turning autocomplete on or off
127+
if (flag) {
128+
// rebuild word list
129+
buildAutocompleteTrie();
130+
131+
// setup autocomplete by submitting a new configuration
132+
TextEditor::AutoCompleteConfig config;
133+
134+
config.callback = [this](TextEditor::AutoCompleteState& state) {
135+
trie.findSuggestions(state.suggestions, state.searchTerm);
136+
};
137+
138+
editor.SetAutoCompleteConfig(&config);
139+
140+
// enable change tracking
141+
// we don't track every keystroke, callbacks can be delayed up to 3000 milliseconds
142+
// if you want live tracking, change 3000 to 0 (performance hit will be minimal for small documents)
143+
editor.SetChangeCallback([this]() {
144+
buildAutocompleteTrie();
145+
}, 3000);
146+
147+
} else {
148+
// disable autocomplete and change tracking
149+
editor.SetAutoCompleteConfig(nullptr);
150+
editor.SetChangeCallback(nullptr);
151+
}
152+
}
153+
154+
void Editor::buildAutocompleteTrie() {
155+
// empty list first
156+
trie.clear();
157+
158+
// add language words (if required)
159+
auto language = editor.GetLanguage();
160+
161+
if (language) {
162+
for (auto& word : language->keywords) { trie.insert(word); }
163+
for (auto& word : language->declarations) { trie.insert(word); }
164+
for (auto& word : language->identifiers) { trie.insert(word); }
165+
}
166+
167+
// add all identifiers in current document
168+
editor.IterateIdentifiers([this](const std::string& identifier) {
169+
trie.insert(identifier);
170+
});
171+
}
172+
```

example/editor.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,11 +682,12 @@ void Editor::renderConfirmError() {
682682
//
683683

684684
void Editor::setAutocompleteMode(bool flag) {
685+
// see we are turning autocomplete on or off
685686
if (flag) {
686687
// rebuild word list
687688
buildAutocompleteTrie();
688689

689-
// enable autocomplete
690+
// setup autocomplete by submitting a new configuration
690691
TextEditor::AutoCompleteConfig config;
691692

692693
config.callback = [this](TextEditor::AutoCompleteState& state) {
@@ -696,12 +697,12 @@ void Editor::setAutocompleteMode(bool flag) {
696697
editor.SetAutoCompleteConfig(&config);
697698

698699
// enable change tracking
699-
// (we don't track every keystroke, callbacks can be delayed up to 3000 milliseconds)
700+
// we don't track every keystroke, callbacks can be delayed up to 3000 milliseconds
701+
// if you want live tracking, change the 3000 to 0 (performance hit will be minimal for small documents)
700702
editor.SetChangeCallback([this]() {
701703
buildAutocompleteTrie();
702704
}, 3000);
703705

704-
705706
} else {
706707
// disable autocomplete and change tracking
707708
editor.SetAutoCompleteConfig(nullptr);
@@ -715,7 +716,7 @@ void Editor::setAutocompleteMode(bool flag) {
715716
//
716717

717718
void Editor::buildAutocompleteTrie() {
718-
// rebuild autocomplete word list
719+
// empty list first
719720
trie.clear();
720721

721722
// add language words (if required)
@@ -727,7 +728,7 @@ void Editor::buildAutocompleteTrie() {
727728
for (auto& word : language->identifiers) { trie.insert(word); }
728729
}
729730

730-
// add all identifiers in document
731+
// add all identifiers in current document
731732
editor.IterateIdentifiers([this](const std::string& identifier) {
732733
trie.insert(identifier);
733734
});

0 commit comments

Comments
 (0)