diff --git a/lib/widgets/autocompleter.dart b/lib/widgets/autocompleter.dart index 84d505bc..449fa597 100644 --- a/lib/widgets/autocompleter.dart +++ b/lib/widgets/autocompleter.dart @@ -61,6 +61,7 @@ class _AutoCompleterState extends State { 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 { } } } + +/// 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 { // 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 diff --git a/test/autocompleter_test.dart b/test/autocompleter_test.dart new file mode 100644 index 00000000..4abab153 --- /dev/null +++ b/test/autocompleter_test.dart @@ -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"); + }); +}