Add some tests for autocompletion

All of these tests fail, I'm just trying to get an idea of what all I'll
need to implement. And what the interface should look like.
This commit is contained in:
Vishesh Handa
2021-01-27 21:41:28 +01:00
parent 7b8c7479dd
commit 07947d637a
2 changed files with 93 additions and 0 deletions

View File

@ -61,6 +61,7 @@ class _AutoCompleterState extends State<AutoCompleter> {
if (word.startsWith(widget.startToken)) {
_showOverlayTag(context, text.substring(0, cursorPos));
} else if (word.endsWith(widget.endToken)) {
// Hide when ]] is added
_hideOverlay();
}
@ -132,6 +133,49 @@ class _AutoCompleterState extends State<AutoCompleter> {
}
}
}
/// if endToken is empty, then the token can only be alpha numeric
String extractToken(
String text, int cursorPos, String startToken, String endToken) {
var start = text.lastIndexOf(RegExp(r' |^'), cursorPos - 1);
if (start == -1) {
var word = text.substring(0, cursorPos);
if (word.startsWith('[[')) {
return word.substring(2, cursorPos);
}
return "";
}
return text;
}
bool enterPressed(String oldText, String newText, int cursorPos) {
if (cursorPos <= 0) {
return false;
}
var charEnterred = newText[cursorPos - 1];
if (charEnterred == '\n') {
return true;
}
return false;
}
class CompletionResult {
String text;
int cursorPos;
CompletionResult(this.text, this.cursorPos);
}
CompletionResult completeText(String oldText, String newText, int cursorPos) {
return null;
}
bool hideAutoCompleter(String oldText, String newText, int cursorPos) {
return false;
}
// https://levelup.gitconnected.com/flutter-medium-like-text-editor-b41157f50f0e
// https://stackoverflow.com/questions/59243627/flutter-how-to-get-the-coordinates-of-the-cursor-in-a-textfield
@ -141,3 +185,7 @@ class _AutoCompleterState extends State<AutoCompleter> {
// Bug 7: Clicking on the text should result in auto-completion
// Bug 8: On clicking somewhere else the suggestion box should disappear
// Bug 9: RTL support
// Bug : What about when a letter is added to an existing tag with more words
// or an existing wiki link which has the closing brackets
// Bug : Show auto-completion on top if no space at the bottom
// Bug : Handle physical tab or Enter key

View File

@ -0,0 +1,45 @@
import 'package:test/test.dart';
import 'package:gitjournal/widgets/autocompleter.dart';
void main() {
test('Extract word at start', () {
var result = extractToken("[[Hel", 5, '[[', ']]');
expect(result, "Hel");
});
test('Extract word at start - cursor not at end', () {
var result = extractToken("[[Hel", 4, '[[', ']]');
expect(result, "Hel");
});
test('Extract second word', () {
var result = extractToken("Hi [[Hel", 8, '[[', ']]');
expect(result, "Hel");
});
test('Extract second word after newline', () {
var result = extractToken("Hi\n[[Hel", 8, '[[', ']]');
expect(result, "Hel");
});
test('Extract second word with more words', () {
var result = extractToken("Hi [[Hel ]] Flower.", 8, '[[', ']]');
expect(result, "Hel ");
});
test('Extract second word with more words after newline', () {
var result = extractToken("Hi\n[[Hel]]\nFlower ", 8, '[[', ']]');
expect(result, "Hel");
});
test('Extract word when cursor in the middle', () {
var result = extractToken("Hi\n[[Hello]]", 8, '[[', ']]');
expect(result, "Hello");
});
test('Extract word with spaces', () {
var result = extractToken("Hi\n[[Hello There]]", 8, '[[', ']]');
expect(result, "Hello There");
});
}