From ac463d56b0f53243fd0b00b09065630f7aaf2c09 Mon Sep 17 00:00:00 2001 From: mrchebik Date: Tue, 7 Aug 2018 14:37:43 +0300 Subject: [PATCH] [CodeArea] Enhanced auto removing space after pressed Backspace. --- .../gui/node/codearea/CustomCodeArea.java | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/main/java/ru/mrchebik/gui/node/codearea/CustomCodeArea.java b/src/main/java/ru/mrchebik/gui/node/codearea/CustomCodeArea.java index 3e56ec9..e4837dc 100644 --- a/src/main/java/ru/mrchebik/gui/node/codearea/CustomCodeArea.java +++ b/src/main/java/ru/mrchebik/gui/node/codearea/CustomCodeArea.java @@ -82,32 +82,19 @@ public CustomCodeArea(String text, Highlight highlight, Syntax syntax, Stage sta analyzer.callAnalysis(this.getText()); }).start(); } else if (event.getCode() == ENTER) { - char open = '{'; - char close = '}'; - Stack brackets = new Stack<>(); - String textToAnalyze = this.getText(0, position); - for (int i = 0; i < position; i++) { - char charToAnalyze = textToAnalyze.charAt(i); - if (charToAnalyze == open) { - brackets.push(open); - } else if (charToAnalyze == close) { - brackets.pop(); - } - } - String tabLength = IntStream.range(0, brackets.size()).mapToObj(i -> CUSTOM_TAB).collect(Collectors.joining()); - - this.insertText(position, "\n" + tabLength); + this.insertText(position, "\n" + getTabLength(position)); analyzer.callAnalysis(this.getText()); } else if (event.getCode() == KeyCode.BACK_SPACE) { String paragraph = this.getParagraph(this.getCurrentParagraph()).getText(); - if (paragraph.trim().length() == 0) { + if (paragraph.trim().length() == 0 && + getTabLength(position).equals(paragraph)) { this.deleteText(position - paragraph.length() - 1, position); } else if (position >= 4 && CUSTOM_TAB.equals(this.getText(position - 4, position))) { int spaces = 0; - for (int i = this.getCaretColumn() - 1; i > 0; i--) { + for (int i = this.getCaretColumn() - 1; i >= 0; i--) { if (paragraph.charAt(i) == ' ') { spaces++; } else { @@ -145,6 +132,22 @@ public CustomCodeArea(String text, Highlight highlight, Syntax syntax, Stage sta this.replaceText(0, 0, text); } + private String getTabLength(int position) { + char open = '{'; + char close = '}'; + Stack brackets = new Stack<>(); + String textToAnalyze = this.getText(0, position); + for (int i = 0; i < position; i++) { + char charToAnalyze = textToAnalyze.charAt(i); + if (charToAnalyze == open) { + brackets.push(open); + } else if (charToAnalyze == close) { + brackets.pop(); + } + } + return IntStream.range(0, brackets.size()).mapToObj(i -> CUSTOM_TAB).collect(Collectors.joining()); + } + private void applyHighlighting(StyleSpans> highlighting) { codeAreaCSS = new CodeArea(this.getText());