Dave Jarvis' Repositories

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

MarkdownEditorPane: auto-indent implemented - copy leading whitespace characters of current line when pressing Enter - copy list markers

AuthorKarl Tauber <email>
Date2015-07-31 10:40:17 GMT+0200
Commit2a9b17ab3b41c3e2101cb617e610788703fbd383
Parent0d0f8fb
Delta22 lines added, 0 lines removed, 22-line increase
src/main/java/org/markdownwriterfx/editor/MarkdownEditorPane.java
package org.markdownwriterfx.editor;
+import static javafx.scene.input.KeyCode.*;
+import static org.fxmisc.wellbehaved.event.EventPattern.keyPressed;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import javafx.application.Platform;
import javafx.beans.InvalidationListener;
public class MarkdownEditorPane
{
+ private static final Pattern AUTO_INDENT_PATTERN = Pattern.compile(
+ "(\\s*-\\s+|\\s*[0-9]+\\.\\s+|\\s+).*");
+
private final StyleClassedTextArea textArea;
private PegDownProcessor pegDownProcessor;
markdownAST.set(astRoot);
});
+
+ EventHandlerHelper.install(textArea.onKeyPressedProperty(), EventHandlerHelper
+ .on(keyPressed(ENTER)).act(this::enterPressed)
+ .create());
// search for vertical scrollbar and add change listener to update 'scrollY' property
private void applyHighlighting(RootNode astRoot) {
MarkdownSyntaxHighlighter.highlight(textArea, astRoot);
+ }
+
+ private void enterPressed(KeyEvent e) {
+ String currentLine = textArea.getText(textArea.getCurrentParagraph());
+
+ String newText = "\n";
+ Matcher matcher = AUTO_INDENT_PATTERN.matcher(currentLine);
+ if (matcher.matches())
+ newText = newText.concat(matcher.group(1));
+ textArea.replaceSelection(newText);
}