diff --git a/lib/editors/heuristics.dart b/lib/editors/heuristics.dart index 6e9831f9..b7c6e3e8 100644 --- a/lib/editors/heuristics.dart +++ b/lib/editors/heuristics.dart @@ -12,17 +12,11 @@ EditorHeuristicResult autoAddBulletList( return null; } - // Only when getting a new line + // Only when adding a new line if (curText[cursorPos - 1] != '\n') { return null; } - // Only add the bullets at the end of the document - // FIXME: We should do this anywhere - if (curText.length != cursorPos) { - return null; - } - var prevLineStart = curText.lastIndexOf('\n', cursorPos - 2); prevLineStart = prevLineStart == -1 ? 0 : prevLineStart + 1; var prevLine = curText.substring(prevLineStart, cursorPos - 1); @@ -35,6 +29,11 @@ EditorHeuristicResult autoAddBulletList( var indent = match.group(1) ?? ""; var text = curText.substring(0, cursorPos) + indent + match.group(2) + ' '; + if (cursorPos == curText.length) { + return EditorHeuristicResult(text, text.length); + } - return EditorHeuristicResult(text, text.length); + text += '\n' + curText.substring(cursorPos + 1); + var extraChars = indent.length + match.group(2).length + 1; + return EditorHeuristicResult(text, cursorPos + extraChars); } diff --git a/test/editor_heuristics_test.dart b/test/editor_heuristics_test.dart index 9c1f037f..8298e979 100644 --- a/test/editor_heuristics_test.dart +++ b/test/editor_heuristics_test.dart @@ -11,7 +11,7 @@ void main() { expect(result, null); }); - test('Adds a bullet item', () { + test('Adds a bullet point at the end', () { var origText = "Hello\n* One"; var newText = "Hello\n* One\n"; @@ -19,5 +19,14 @@ void main() { expect(result.text, "Hello\n* One\n* "); expect(result.cursorPos, result.text.length); }); + + test('Adds a bullet point in the middle', () { + var origText = "Hello\n* One\n* Three"; + var newText = "Hello\n* One\n\n* Three"; + + var result = autoAddBulletList(origText, newText, 12); + expect(result.text, "Hello\n* One\n* \n* Three"); + expect(result.cursorPos, 14); + }); }); }