Bullet Lists: Auto add bullets lists even when modifying in the middle

This commit is contained in:
Vishesh Handa
2020-06-01 21:13:11 +02:00
parent f4cc8e2b47
commit a5a7211788
2 changed files with 17 additions and 9 deletions

View File

@ -12,17 +12,11 @@ EditorHeuristicResult autoAddBulletList(
return null; return null;
} }
// Only when getting a new line // Only when adding a new line
if (curText[cursorPos - 1] != '\n') { if (curText[cursorPos - 1] != '\n') {
return null; 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); var prevLineStart = curText.lastIndexOf('\n', cursorPos - 2);
prevLineStart = prevLineStart == -1 ? 0 : prevLineStart + 1; prevLineStart = prevLineStart == -1 ? 0 : prevLineStart + 1;
var prevLine = curText.substring(prevLineStart, cursorPos - 1); var prevLine = curText.substring(prevLineStart, cursorPos - 1);
@ -35,6 +29,11 @@ EditorHeuristicResult autoAddBulletList(
var indent = match.group(1) ?? ""; var indent = match.group(1) ?? "";
var text = curText.substring(0, cursorPos) + indent + match.group(2) + ' '; 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);
}

View File

@ -11,7 +11,7 @@ void main() {
expect(result, null); expect(result, null);
}); });
test('Adds a bullet item', () { test('Adds a bullet point at the end', () {
var origText = "Hello\n* One"; var origText = "Hello\n* One";
var newText = "Hello\n* One\n"; var newText = "Hello\n* One\n";
@ -19,5 +19,14 @@ void main() {
expect(result.text, "Hello\n* One\n* "); expect(result.text, "Hello\n* One\n* ");
expect(result.cursorPos, result.text.length); 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);
});
}); });
} }