Dave Jarvis' Repositories

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

MarkdownEditorPane: avoid unnecessary line separator after inserted 'Horizontal Rule'

Author Karl Tauber <email>
Date 2015-08-08 12:55:07 GMT+0200
Commit 9b082c0e5b963b6879226e8e8c2384f8dc34ffe5
Parent 188be48
src/main/java/org/markdownwriterfx/editor/MarkdownEditorPane.java
// remove leading line separators from leading text
// if there are line separators before the selected text
- for (int i = start - 1; i >= 0 && leading.startsWith("\n"); i--) {
- if (!"\n".equals(textArea.getText(i, i + 1)))
- break;
- leading = leading.substring(1);
+ if (leading.startsWith("\n")) {
+ for (int i = start - 1; i >= 0 && leading.startsWith("\n"); i--) {
+ if (!"\n".equals(textArea.getText(i, i + 1)))
+ break;
+ leading = leading.substring(1);
+ }
}
- // remove trailing line separators from trailing text
+ // remove trailing line separators from trailing or leading text
// if there are line separators after the selected text
- for (int i = end; i < textArea.getLength() && trailing.endsWith("\n"); i++) {
- if (!"\n".equals(textArea.getText(i, i + 1)))
- break;
- trailing = trailing.substring(0, trailing.length() - 1);
+ boolean trailingIsEmpty = trailing.isEmpty();
+ String str = trailingIsEmpty ? leading : trailing;
+ if (str.endsWith("\n")) {
+ for (int i = end; i < textArea.getLength() && str.endsWith("\n"); i++) {
+ if (!"\n".equals(textArea.getText(i, i + 1)))
+ break;
+ str = str.substring(0, str.length() - 1);
+ }
+ if (trailingIsEmpty)
+ leading = str;
+ else
+ trailing = str;
}
Delta 19 lines added, 9 lines removed, 10-line increase