diff --git a/lib/widgets/markdown_toolbar.dart b/lib/widgets/markdown_toolbar.dart index 4446c8c8..127be37d 100644 --- a/lib/widgets/markdown_toolbar.dart +++ b/lib/widgets/markdown_toolbar.dart @@ -98,6 +98,8 @@ TextEditingValue modifyCurrentLine( var lineStartPos = text.lastIndexOf('\n', cursorPos == 0 ? 0 : cursorPos - 1); if (lineStartPos == -1) { lineStartPos = 0; + } else { + lineStartPos += 1; } var lineEndPos = text.indexOf('\n', cursorPos); diff --git a/test/markdown_toolbar_test.dart b/test/markdown_toolbar_test.dart index 36533ec1..de73218f 100644 --- a/test/markdown_toolbar_test.dart +++ b/test/markdown_toolbar_test.dart @@ -17,4 +17,55 @@ void main() { expect(modifyCurrentLine(val, '# '), expectedVal); }); + + test('Adds a header to the last line correctly', () { + var val = const TextEditingValue( + text: 'Hi\nHello', + selection: TextSelection.collapsed(offset: 8), + ); + + var expectedVal = const TextEditingValue( + text: 'Hi\n# Hello', + selection: TextSelection.collapsed(offset: 10), + ); + + expect(modifyCurrentLine(val, '# '), expectedVal); + }); + + test('Adds a header to a middle line correctly', () { + var val = const TextEditingValue( + text: 'Hi\nHello\nFire', + selection: TextSelection.collapsed(offset: 8), + ); + + var expectedVal = const TextEditingValue( + text: 'Hi\n# Hello\nFire', + selection: TextSelection.collapsed(offset: 10), + ); + + expect(modifyCurrentLine(val, '# '), expectedVal); + }); + + test('Adds a header to a middle line middle word correctly', () { + var val = const TextEditingValue( + text: 'Hi\nHello Darkness\nFire', + selection: TextSelection.collapsed(offset: 8), + ); + + var expectedVal = const TextEditingValue( + text: 'Hi\n# Hello Darkness\nFire', + selection: TextSelection.collapsed(offset: 10), + ); + + expect(modifyCurrentLine(val, '# '), expectedVal); + }); + + // Removes from first line + // Removes from last line + // Removes from middle line + // Removes when cursor is in the middle of a word + // Removes when cursor is at the start of the line + // Removes when cursor is in between '#' and ' ' + + // TODO: Simplify the tests, avoid all this code duplication }