Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

MarkdownEditorPane: auto-indent improved - empty current line when pressing Enter, if it contains only whitespace characters and list markers (makes closing lists easy)

AuthorKarl Tauber <email>
Date2015-08-04 11:32:36 GMT+0200
Commit45669c4a59d485f1d4c3519ef2d4ec3ba9a90495
Parent720ca48
Delta12 lines added, 3 lines removed, 9-line increase
src/main/java/org/markdownwriterfx/editor/MarkdownEditorPane.java
{
private static final Pattern AUTO_INDENT_PATTERN = Pattern.compile(
- "(\\s*[*+-]\\s+|\\s*[0-9]+\\.\\s+|\\s+).*");
+ "(\\s*[*+-]\\s+|\\s*[0-9]+\\.\\s+|\\s+)(.*)");
private final StyleClassedTextArea textArea;
String newText = "\n";
Matcher matcher = AUTO_INDENT_PATTERN.matcher(currentLine);
- if (matcher.matches())
- newText = newText.concat(matcher.group(1));
+ if (matcher.matches()) {
+ if (!matcher.group(2).isEmpty()) {
+ // indent new line with same whitespace characters and list markers as current line
+ newText = newText.concat(matcher.group(1));
+ } else {
+ // current line contains only whitespace characters and list markers
+ // --> empty current line
+ int caretPosition = textArea.getCaretPosition();
+ textArea.selectRange(caretPosition - currentLine.length(), caretPosition);
+ }
+ }
textArea.replaceSelection(newText);
}