From f3ad0144c5391aba9777cd2accaa5baa76a2179d Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Thu, 13 Aug 2020 00:29:17 +0200 Subject: [PATCH] Add a test for MarkdownToolbar's replace current line The logic is non trivial --- lib/widgets/markdown_toolbar.dart | 84 +++++++++++++++++-------------- test/markdown_toolbar_test.dart | 20 ++++++++ 2 files changed, 65 insertions(+), 39 deletions(-) create mode 100644 test/markdown_toolbar_test.dart diff --git a/lib/widgets/markdown_toolbar.dart b/lib/widgets/markdown_toolbar.dart index 559afee8..4446c8c8 100644 --- a/lib/widgets/markdown_toolbar.dart +++ b/lib/widgets/markdown_toolbar.dart @@ -28,45 +28,7 @@ class MarkdownToolBar extends StatelessWidget { } void _modifyCurrentLine(String char) { - var selection = textController.value.selection; - var text = textController.value.text; - - print('Base offset: ${selection.baseOffset}'); - print('Extent offset: ${selection.extentOffset}'); - var cursorPos = selection.baseOffset; - if (cursorPos == -1) { - cursorPos = 0; - } - print('CursorPos: $cursorPos'); - - var lineStartPos = - text.lastIndexOf('\n', cursorPos == 0 ? 0 : cursorPos - 1); - if (lineStartPos == -1) { - lineStartPos = 0; - } - - var lineEndPos = text.indexOf('\n', cursorPos); - if (lineEndPos == -1) { - lineEndPos = text.length; - } - - print('Line Start: $lineStartPos'); - print('Line End: $lineEndPos'); - print('Line: ${text.substring(lineStartPos, lineEndPos)}'); - - // Check if already present - if (text.startsWith(char, lineStartPos)) { - print('Removing `$char`'); - textController.text = text.replaceFirst(char, '', lineStartPos); - textController.selection = - TextSelection.collapsed(offset: cursorPos - char.length); - return; - } - - print('Adding `$char`'); - textController.text = text.replaceRange(lineStartPos, lineStartPos, char); - textController.selection = - TextSelection.collapsed(offset: cursorPos + char.length); + textController.value = modifyCurrentLine(textController.value, char); } void _modifyCurrentWord(String char) { @@ -117,3 +79,47 @@ class MarkdownToolBar extends StatelessWidget { print('$char'); } } + +TextEditingValue modifyCurrentLine( + TextEditingValue textEditingValue, + String char, +) { + var selection = textEditingValue.selection; + var text = textEditingValue.text; + + //print('Base offset: ${selection.baseOffset}'); + //print('Extent offset: ${selection.extentOffset}'); + var cursorPos = selection.baseOffset; + if (cursorPos == -1) { + cursorPos = 0; + } + //print('CursorPos: $cursorPos'); + + var lineStartPos = text.lastIndexOf('\n', cursorPos == 0 ? 0 : cursorPos - 1); + if (lineStartPos == -1) { + lineStartPos = 0; + } + + var lineEndPos = text.indexOf('\n', cursorPos); + if (lineEndPos == -1) { + lineEndPos = text.length; + } + + //print('Line Start: $lineStartPos'); + //print('Line End: $lineEndPos'); + //print('Line: ${text.substring(lineStartPos, lineEndPos)}'); + + // Check if already present + if (text.startsWith(char, lineStartPos)) { + //print('Removing `$char`'); + return TextEditingValue( + text: text.replaceFirst(char, '', lineStartPos), + selection: TextSelection.collapsed(offset: cursorPos - char.length), + ); + } + + return TextEditingValue( + text: text.replaceRange(lineStartPos, lineStartPos, char), + selection: TextSelection.collapsed(offset: cursorPos + char.length), + ); +} diff --git a/test/markdown_toolbar_test.dart b/test/markdown_toolbar_test.dart new file mode 100644 index 00000000..36533ec1 --- /dev/null +++ b/test/markdown_toolbar_test.dart @@ -0,0 +1,20 @@ +import 'package:flutter/material.dart'; +import 'package:test/test.dart'; + +import 'package:gitjournal/widgets/markdown_toolbar.dart'; + +void main() { + test('Adds a header to the first line correctly', () { + var val = const TextEditingValue( + text: 'Hello', + selection: TextSelection.collapsed(offset: 5), + ); + + var expectedVal = const TextEditingValue( + text: '# Hello', + selection: TextSelection.collapsed(offset: 7), + ); + + expect(modifyCurrentLine(val, '# '), expectedVal); + }); +}