From b9d765f726f05b7d39ce5eed3aa322af52751c6c Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 2 Feb 2021 16:08:15 +0100 Subject: [PATCH] Rename EditorState to TextEditorState Also remove the EditorHeuristicsResult class. This way all of these are being combined together. --- lib/editors/autocompletion_widget.dart | 13 ++++--------- lib/editors/common.dart | 7 +++++++ lib/editors/heuristics.dart | 13 ++++--------- test/autocompletion/tags_test.dart | 13 +++++++------ 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/editors/autocompletion_widget.dart b/lib/editors/autocompletion_widget.dart index 3503296d..db107d9c 100644 --- a/lib/editors/autocompletion_widget.dart +++ b/lib/editors/autocompletion_widget.dart @@ -4,6 +4,8 @@ import 'package:flutter/material.dart'; import 'package:time/time.dart'; +import 'package:gitjournal/editors/common.dart'; + class AutoCompletionWidget extends StatefulWidget { final FocusNode textFieldFocusNode; final GlobalKey textFieldKey; @@ -52,7 +54,7 @@ class _AutoCompletionWidgetState extends State { var range = TextRange(0, 0); try { - var es = EditorState(text, selection.baseOffset); + var es = TextEditorState(text, selection.baseOffset); range = autoCompleter.textChanged(es); } catch (e) { print(e); @@ -183,13 +185,6 @@ bool enterPressed(String oldText, String newText, int cursorPos) { } */ -class EditorState { - String text; - int cursorPos; - - EditorState(this.text, this.cursorPos); -} - // 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 @@ -265,7 +260,7 @@ class TextRange { } class TagsAutoCompleter { - TextRange textChanged(EditorState es) { + TextRange textChanged(TextEditorState es) { // print("${es.text} ${es.cursorPos}"); var start = es.text.lastIndexOf(RegExp(r'^|[ .?!:;\n]'), es.cursorPos); if (start <= 0) { diff --git a/lib/editors/common.dart b/lib/editors/common.dart index 29120c4a..ab822693 100644 --- a/lib/editors/common.dart +++ b/lib/editors/common.dart @@ -27,6 +27,13 @@ abstract class EditorState with ChangeNotifier { bool get noteModified; } +class TextEditorState { + String text; + int cursorPos; + + TextEditorState(this.text, this.cursorPos); +} + class EditorAppBar extends StatelessWidget implements PreferredSizeWidget { final Editor editor; final EditorState editorState; diff --git a/lib/editors/heuristics.dart b/lib/editors/heuristics.dart index eaab8856..76c91bd8 100644 --- a/lib/editors/heuristics.dart +++ b/lib/editors/heuristics.dart @@ -1,11 +1,6 @@ -class EditorHeuristicResult { - String text; - int cursorPos; +import 'package:gitjournal/editors/common.dart'; - EditorHeuristicResult(this.text, this.cursorPos); -} - -EditorHeuristicResult autoAddBulletList( +TextEditorState autoAddBulletList( String oldText, String curText, final int cursorPos) { // We only want to do this on inserts if (curText.length <= oldText.length) { @@ -64,7 +59,7 @@ EditorHeuristicResult autoAddBulletList( var newCursorPos = text.length; text += remainingText; - return EditorHeuristicResult(text, newCursorPos); + return TextEditorState(text, newCursorPos); } var extraText = indent + bulletType + spacesBeforeContent; @@ -72,5 +67,5 @@ EditorHeuristicResult autoAddBulletList( var newCursorPos = text.length; text += remainingText; - return EditorHeuristicResult(text, newCursorPos); + return TextEditorState(text, newCursorPos); } diff --git a/test/autocompletion/tags_test.dart b/test/autocompletion/tags_test.dart index 8a889110..9fe1eb1f 100644 --- a/test/autocompletion/tags_test.dart +++ b/test/autocompletion/tags_test.dart @@ -1,42 +1,43 @@ import 'package:test/test.dart'; import 'package:gitjournal/editors/autocompletion_widget.dart'; +import 'package:gitjournal/editors/common.dart'; void main() { var c = TagsAutoCompleter(); test('Extract first word', () { - var es = EditorState("#Hel", 3); + var es = TextEditorState("#Hel", 3); var r = c.textChanged(es); expect(es.text.substring(r.start, r.end), "Hel"); }); test('Extract second word', () { - var es = EditorState("Hi #Hel", 7); + var es = TextEditorState("Hi #Hel", 7); var r = c.textChanged(es); expect(es.text.substring(r.start, r.end), "Hel"); }); test('Extract second word - cursor not at end', () { - var es = EditorState("Hi #Hell", 7); + var es = TextEditorState("Hi #Hell", 7); var r = c.textChanged(es); expect(es.text.substring(r.start, r.end), "Hell"); }); test("Second word with dot", () { - var es = EditorState("Hi.#Hel", 6); + var es = TextEditorState("Hi.#Hel", 6); 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 es = TextEditorState("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 es = TextEditorState("#Hel hi ", 8); var r = c.textChanged(es); expect(r.isEmpty, true); });