Tags AutoCompleter: More Tests

This commit is contained in:
Vishesh Handa
2021-01-28 15:20:04 +01:00
parent 79207d9422
commit df6cba7098
2 changed files with 69 additions and 44 deletions

View File

@ -50,22 +50,25 @@ class _AutoCompletionWidgetState extends State<AutoCompletionWidget> {
var selection = widget.textController.selection; var selection = widget.textController.selection;
var text = widget.textController.text; var text = widget.textController.text;
var prefix = ""; var range = TextRange(0, 0);
try { try {
prefix = var es = EditorState(text, selection.baseOffset);
autoCompleter.textChanged(EditorState(text, selection.baseOffset)); range = autoCompleter.textChanged(es);
} catch (e) { } catch (e) {
print(e); print(e);
print("bah");
} }
if (prefix.isEmpty) { if (range.isEmpty) {
_hideOverlay(); _hideOverlay();
return; return;
} }
var prefix = text.substring(range.start, range.end);
if (prefix == "\n") { if (prefix == "\n") {
// Pressed Enter
} else { } else {
_showOverlayTag(context, prefix); _showOverlayTag(context, text.substring(0, range.start));
} }
} }
@ -110,14 +113,15 @@ class _AutoCompletionWidgetState extends State<AutoCompletionWidget> {
// Tag code. // Tag code.
child: const Material( child: const Material(
elevation: 4.0, elevation: 4.0,
color: Colors.lightBlueAccent, color: Colors.lightBlueAccent,
child: Text( child: Text(
'Show tag here', 'Show tag here',
style: TextStyle( style: TextStyle(
fontSize: 20.0, fontSize: 20.0,
), ),
)), ),
),
); );
}); });
Overlay.of(context).insert(overlayEntry); Overlay.of(context).insert(overlayEntry);
@ -193,6 +197,7 @@ class EditorState {
// Bug : Show auto-completion on top if no space at the bottom // Bug : Show auto-completion on top if no space at the bottom
// Bug : Handle physical tab or Enter key // Bug : Handle physical tab or Enter key
/*
abstract class AutoCompletionLogic { abstract class AutoCompletionLogic {
/// Return an empty string if the overlay should be hidden /// Return an empty string if the overlay should be hidden
/// Return \n if enter has been pressed /// Return \n if enter has been pressed
@ -201,6 +206,7 @@ abstract class AutoCompletionLogic {
EditorState completeText(String text); EditorState completeText(String text);
} }
*/
/* /*
class WikiLinksAutoCompleter implements AutoCompletionLogic { class WikiLinksAutoCompleter implements AutoCompletionLogic {
@ -242,42 +248,40 @@ class WikiLinksAutoCompleter implements AutoCompletionLogic {
} }
*/ */
class TagsAutoCompleter implements AutoCompletionLogic { class TextRange {
var _oldState = EditorState("", 0); final int start;
final int end;
@override TextRange(this.start, this.end);
String textChanged(EditorState es) {
_oldState = es;
//print("${es.text} ${es.cursorPos}"); bool get isEmpty => start == end;
var start = es.text.lastIndexOf(RegExp(r'^|[ #.?!]'), es.cursorPos); }
if (start < 0) {
class TagsAutoCompleter {
TextRange textChanged(EditorState es) {
// print("${es.text} ${es.cursorPos}");
var start = es.text.lastIndexOf(RegExp(r'^|[ .?!:;\n]'), es.cursorPos);
if (start <= 0) {
start = 0; start = 0;
} else {
start += 1;
}
if (start == es.text.length) {
return TextRange(0, 0);
} }
var end = es.text.indexOf(RegExp(r' |$'), es.cursorPos); var end = es.text.indexOf(RegExp(r'[ .?!:;\n]|$'), es.cursorPos);
if (end == -1) { if (end == -1) {
end = es.cursorPos; end = es.cursorPos;
} }
// print("start end: $start $end"); // print("start end: $start $end ${es.text.length}");
var text = es.text.substring(start, end).trim(); // var text = es.text.substring(start, end);
// print("text $text"); // print("text $text");
if (!text.startsWith('#')) { if (es.text[start] != '#') {
return ""; return TextRange(0, 0);
} }
return text.substring(1); return TextRange(start + 1, end);
}
@override
EditorState completeText(String text) {
var start = _oldState.text.lastIndexOf(r'#', _oldState.cursorPos);
if (start == -1) {
throw Exception("completeText should not have been called");
}
var es = _oldState;
return es;
} }
} }

View File

@ -5,18 +5,39 @@ import 'package:gitjournal/autocompletion/widget.dart';
void main() { void main() {
var c = TagsAutoCompleter(); var c = TagsAutoCompleter();
test('Extract first word', () {
var es = EditorState("#Hel", 3);
var r = c.textChanged(es);
expect(es.text.substring(r.start, r.end), "Hel");
});
test('Extract second word', () { test('Extract second word', () {
var p = c.textChanged(EditorState("Hi #Hel", 7)); var es = EditorState("Hi #Hel", 7);
expect(p, "Hel"); var r = c.textChanged(es);
expect(es.text.substring(r.start, r.end), "Hel");
}); });
test('Extract second word - cursor not at end', () { test('Extract second word - cursor not at end', () {
var p = c.textChanged(EditorState("Hi #Hell", 7)); var es = EditorState("Hi #Hell", 7);
expect(p, "Hell"); var r = c.textChanged(es);
expect(es.text.substring(r.start, r.end), "Hell");
}); });
test("Second word with dot", () { test("Second word with dot", () {
var p = c.textChanged(EditorState("Hi.#Hel", 6)); var es = EditorState("Hi.#Hel", 6);
expect(p, "Hel"); var r = c.textChanged(es);
expect(es.text.substring(r.start, r.end), "Hel");
});
test("Second word with newline", () {
var es = EditorState("Hi\n#H", 5);
var r = c.textChanged(es);
expect(es.text.substring(r.start, r.end), "H");
});
test('Nothing to extract', () {
var es = EditorState("#Hel hi ", 8);
var r = c.textChanged(es);
expect(r.isEmpty, true);
}); });
} }