From 2ba012487128884b5a553bc5d4942c56489cd7ad Mon Sep 17 00:00:00 2001 From: Ricardo JL Rufino Date: Thu, 14 May 2020 01:31:22 -0300 Subject: [PATCH 1/4] [editor] Allow formatting only the selection --- .../cc/arduino/packages/formatter/AStyle.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/cc/arduino/packages/formatter/AStyle.java b/app/src/cc/arduino/packages/formatter/AStyle.java index 70b6717ff66..9c0c0918550 100644 --- a/app/src/cc/arduino/packages/formatter/AStyle.java +++ b/app/src/cc/arduino/packages/formatter/AStyle.java @@ -76,7 +76,16 @@ public void init(Editor editor) { @Override public void run() { - String originalText = editor.getCurrentTab().getText(); + + String originalText = editor.getCurrentTab().getSelectedText(); + boolean selection = true; + + // If no selection use all file + if(originalText == null || originalText.isEmpty()) { + originalText = editor.getCurrentTab().getText(); + selection = false; + } + String formattedText = aStyleInterface.AStyleMain(originalText, formatterConfiguration); if (formattedText.equals(originalText)) { @@ -84,7 +93,10 @@ public void run() { return; } - editor.getCurrentTab().setText(formattedText); + if(selection) + editor.getCurrentTab().setSelectedText(formattedText); + else + editor.getCurrentTab().setText(formattedText); // mark as finished editor.statusNotice(tr("Auto Format finished.")); @@ -95,4 +107,4 @@ public String getMenuTitle() { return tr("Auto Format"); } -} +} \ No newline at end of file From 7efb118156e5d889effd838383c713ca94f57135 Mon Sep 17 00:00:00 2001 From: Ricardo JL Rufino Date: Thu, 14 May 2020 10:59:21 -0300 Subject: [PATCH 2/4] fix code formatting --- app/src/cc/arduino/packages/formatter/AStyle.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/src/cc/arduino/packages/formatter/AStyle.java b/app/src/cc/arduino/packages/formatter/AStyle.java index 9c0c0918550..7da1dde9363 100644 --- a/app/src/cc/arduino/packages/formatter/AStyle.java +++ b/app/src/cc/arduino/packages/formatter/AStyle.java @@ -76,16 +76,18 @@ public void init(Editor editor) { @Override public void run() { - + String originalText = editor.getCurrentTab().getSelectedText(); boolean selection = true; - + // If no selection use all file - if(originalText == null || originalText.isEmpty()) { + if (originalText == null || originalText.isEmpty()) { + originalText = editor.getCurrentTab().getText(); selection = false; + } - + String formattedText = aStyleInterface.AStyleMain(originalText, formatterConfiguration); if (formattedText.equals(originalText)) { @@ -93,7 +95,7 @@ public void run() { return; } - if(selection) + if (selection) editor.getCurrentTab().setSelectedText(formattedText); else editor.getCurrentTab().setText(formattedText); @@ -107,4 +109,4 @@ public String getMenuTitle() { return tr("Auto Format"); } -} \ No newline at end of file +} From cf81fdcbee05a2449b5c8c343fe0d01cf9d7c53f Mon Sep 17 00:00:00 2001 From: Ricardo JL Rufino Date: Wed, 20 May 2020 19:55:11 -0300 Subject: [PATCH 3/4] format selection using AStyle control tags --- .../cc/arduino/packages/formatter/AStyle.java | 71 ++++++++++++++----- 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/app/src/cc/arduino/packages/formatter/AStyle.java b/app/src/cc/arduino/packages/formatter/AStyle.java index 7da1dde9363..9ba2e3ab648 100644 --- a/app/src/cc/arduino/packages/formatter/AStyle.java +++ b/app/src/cc/arduino/packages/formatter/AStyle.java @@ -29,16 +29,20 @@ package cc.arduino.packages.formatter; +import static processing.app.I18n.tr; + import processing.app.Base; import processing.app.BaseNoGui; import processing.app.Editor; import processing.app.helpers.FileUtils; +import processing.app.syntax.SketchTextArea; import processing.app.tools.Tool; import java.io.File; import java.io.IOException; - -import static processing.app.I18n.tr; +import java.util.regex.Pattern; +import javax.swing.text.BadLocationException; +import org.fife.ui.rsyntaxtextarea.RSyntaxDocument; public class AStyle implements Tool { @@ -77,28 +81,63 @@ public void init(Editor editor) { @Override public void run() { - String originalText = editor.getCurrentTab().getSelectedText(); - boolean selection = true; + SketchTextArea textArea = editor.getCurrentTab().getTextArea(); + + String originalText = textArea.getSelectedText(); // If no selection use all file if (originalText == null || originalText.isEmpty()) { - originalText = editor.getCurrentTab().getText(); - selection = false; + String formattedText = aStyleInterface.AStyleMain(textArea.getText(), formatterConfiguration); + editor.getCurrentTab().setText(formattedText); - } + } else { + try { - String formattedText = aStyleInterface.AStyleMain(originalText, formatterConfiguration); + // apply indentation control keywords. + String FORMAT_ON = "\n// *INDENT-ON* DYN\n"; + String FORMAT_OFF = "\n// *INDENT-OFF* DYN\n"; - if (formattedText.equals(originalText)) { - editor.statusNotice(tr("No changes necessary for Auto Format.")); - return; - } + RSyntaxDocument content = (RSyntaxDocument) textArea.getDocument(); - if (selection) - editor.getCurrentTab().setSelectedText(formattedText); - else - editor.getCurrentTab().setText(formattedText); + textArea.beginAtomicEdit(); + + int selStart = editor.getCurrentTab().getSelectionStart(); + int selEnd = editor.getCurrentTab().getSelectionStop(); + int lineStart = textArea.getLineOfOffset(selStart); + int lineEnd = textArea.getLineOfOffset(selEnd); + + // Calculate offsets from begin and end of each line. + int fristLineOffset = textArea.getLineStartOffset(lineStart); + int lastLineOffset = textArea.getLineEndOffset(lineEnd); + + // inserts change the length, use this to calculate new positios. + int offLength = FORMAT_OFF.length(); + int onLength = FORMAT_ON.length(); + + content.insertString(0, FORMAT_OFF, null); + content.insertString(fristLineOffset + offLength, FORMAT_ON, null); + content.insertString(lastLineOffset + offLength + onLength, FORMAT_OFF,null); + originalText = content.getText(0, content.getLength()); + + String formattedText = aStyleInterface.AStyleMain(originalText, formatterConfiguration); + + // Remove format tags + formattedText = formattedText.replaceAll(Pattern.quote(FORMAT_OFF), ""); + formattedText = formattedText.replaceAll(Pattern.quote(FORMAT_ON), ""); + + textArea.setText(formattedText); + textArea.select(selStart, selStart); + + } catch (BadLocationException e) { + editor.statusNotice(tr("Auto Format Error") + ": " + e.getLocalizedMessage()); + e.printStackTrace(); + return; + } finally { + textArea.endAtomicEdit(); + } + + } // mark as finished editor.statusNotice(tr("Auto Format finished.")); From 0918a5b0b4aa5185b85373141f32396cfcc3dc88 Mon Sep 17 00:00:00 2001 From: Ricardo JL Rufino Date: Thu, 21 May 2020 17:19:49 -0300 Subject: [PATCH 4/4] Avoid erros on format multi-line comments, add Tests --- .../cc/arduino/packages/formatter/AStyle.java | 34 +++- .../app/AutoformatSelectionTest.java | 174 ++++++++++++++++++ .../SketchTextAreaComponentDriver.java | 11 ++ .../app/helpers/SketchTextAreaFixture.java | 5 + 4 files changed, 223 insertions(+), 1 deletion(-) create mode 100755 app/test/processing/app/AutoformatSelectionTest.java mode change 100644 => 100755 app/test/processing/app/helpers/SketchTextAreaComponentDriver.java mode change 100644 => 100755 app/test/processing/app/helpers/SketchTextAreaFixture.java diff --git a/app/src/cc/arduino/packages/formatter/AStyle.java b/app/src/cc/arduino/packages/formatter/AStyle.java index 9ba2e3ab648..fa004ee35d7 100644 --- a/app/src/cc/arduino/packages/formatter/AStyle.java +++ b/app/src/cc/arduino/packages/formatter/AStyle.java @@ -43,6 +43,7 @@ import java.util.regex.Pattern; import javax.swing.text.BadLocationException; import org.fife.ui.rsyntaxtextarea.RSyntaxDocument; +import org.fife.ui.rsyntaxtextarea.Token; public class AStyle implements Tool { @@ -110,7 +111,11 @@ public void run() { // Calculate offsets from begin and end of each line. int fristLineOffset = textArea.getLineStartOffset(lineStart); int lastLineOffset = textArea.getLineEndOffset(lineEnd); - + + // Avoid multi-line comments + fristLineOffset = navigateOffComments(textArea, fristLineOffset); // try caech (invalid selection) + lastLineOffset = navigateOffComments(textArea, lastLineOffset); // try caech (invalid selection) + // inserts change the length, use this to calculate new positios. int offLength = FORMAT_OFF.length(); int onLength = FORMAT_ON.length(); @@ -142,6 +147,33 @@ public void run() { // mark as finished editor.statusNotice(tr("Auto Format finished.")); } + + private int navigateOffComments(SketchTextArea textArea, int offset) throws BadLocationException { + + Token token = textArea.modelToToken(offset); + + // if line is a multiline comment, go back !! + if (token != null && token.getType() == Token.COMMENT_MULTILINE) { + + int lineStart = textArea.getLineOfOffset(offset); + token = textArea.getTokenListForLine(lineStart); + + while (token.getType() == Token.COMMENT_MULTILINE) { + if (lineStart == 0) + break; + token = token.getNextToken(); + if (token == null) + token = textArea.getTokenListForLine(--lineStart); + } + + return token.getOffset(); + + } else { + return offset; + } + + } + @Override public String getMenuTitle() { diff --git a/app/test/processing/app/AutoformatSelectionTest.java b/app/test/processing/app/AutoformatSelectionTest.java new file mode 100755 index 00000000000..1871c533955 --- /dev/null +++ b/app/test/processing/app/AutoformatSelectionTest.java @@ -0,0 +1,174 @@ +/* + * This file is part of Arduino. + * + * Copyright 2015 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ + +package processing.app; + +import org.fest.swing.fixture.JMenuItemFixture; +import org.junit.Test; +import processing.app.helpers.SketchTextAreaFixture; + +import static org.junit.Assert.assertEquals; + +public class AutoformatSelectionTest extends AbstractGUITest { + + private static String orig = "/**\n" + + " *Docs..\n" + + " */\n" + + "void loop() {\n" + + " // LED ON\n" + + "digitalWrite(LED_BUILTIN, HIGH);\n" + + "if (true) {\n" + + " delay( 1000)\n" + + "};\n" + + " digitalWrite(LED_BUILTIN, LOW);\n" + + " if (true) {delay( 1000 )}; // <<< UGLY FORMATTING\n" + + "}"; + + + @Test + public void testSuite() { + + selectIfMultilineCorrectSel(); + selectIfMultilineCorrectPartialSel(); + selectIfMultilineDocs(); + selectIfSinglelineDocs(); + + } + + /** + * select all if block + */ + public void selectIfMultilineCorrectSel() { + + + String espected = "/**\n" + + " *Docs..\n" + + " */\n" + + "void loop() {\n" + + " // LED ON\n" + + "digitalWrite(LED_BUILTIN, HIGH);\n" + + " if (true) {\n" + + " delay( 1000)\n" + + " };\n" + + " digitalWrite(LED_BUILTIN, LOW);\n" + + " if (true) {delay( 1000 )}; // <<< UGLY FORMATTING\n" + + "}"; + + validateFormat(78, 109, orig, espected); + + } + + + /** + * selection starts in if (|true) and goto '}' + * expected: format entry block + */ + public void selectIfMultilineCorrectPartialSel() { + + String espected = "/**\n" + + " *Docs..\n" + + " */\n" + + "void loop() {\n" + + " // LED ON\n" + + "digitalWrite(LED_BUILTIN, HIGH);\n" + + " if (true) {\n" + + " delay( 1000)\n" + + " };\n" + + " digitalWrite(LED_BUILTIN, LOW);\n" + + " if (true) {delay( 1000 )}; // <<< UGLY FORMATTING\n" + + "}"; + + validateFormat(81, 109, orig, espected); + + } + + /** + * selection starts in multiple docs /** *\/ + * expected: format entry block + */ + public void selectIfMultilineDocs() { + + String espected = "/**\n" + + " Docs..\n" + + "*/\n" + + "void loop() {\n" + + " // LED ON\n" + + " digitalWrite(LED_BUILTIN, HIGH);\n" + + "if (true) {\n" + + " delay( 1000)\n" + + "};\n" + + " digitalWrite(LED_BUILTIN, LOW);\n" + + " if (true) {delay( 1000 )}; // <<< UGLY FORMATTING\n" + + "}"; + + validateFormat(9, 51, orig, espected); + + } + + /** + * selection starts in single line coment // LED |ON + * expected: format comment + */ + public void selectIfSinglelineDocs() { + + String espected = "/**\n" + + " *Docs..\n" + + " */\n" + + "void loop() {\n" + + " // LED ON\n" + + " digitalWrite(LED_BUILTIN, HIGH);\n" + + "if (true) {\n" + + " delay( 1000)\n" + + "};\n" + + " digitalWrite(LED_BUILTIN, LOW);\n" + + " if (true) {delay( 1000 )}; // <<< UGLY FORMATTING\n" + + "}"; + + validateFormat(42, 56, orig, espected); + + } + + + public SketchTextAreaFixture validateFormat(int selStart, int selEnd, String orig, String espected) { + JMenuItemFixture menuToolsAutoFormat = window.menuItem("menuToolsAutoFormat"); + menuToolsAutoFormat.requireEnabled(); + SketchTextAreaFixture editor = window.textArea("editor"); + + editor.setText(orig); + + editor.select(selStart, selEnd); // select frist if + + menuToolsAutoFormat.click(); + + assertEquals(espected, editor.getText()); + + return editor; + } + +} diff --git a/app/test/processing/app/helpers/SketchTextAreaComponentDriver.java b/app/test/processing/app/helpers/SketchTextAreaComponentDriver.java old mode 100644 new mode 100755 index 7d569c3bf4d..358e8f4a022 --- a/app/test/processing/app/helpers/SketchTextAreaComponentDriver.java +++ b/app/test/processing/app/helpers/SketchTextAreaComponentDriver.java @@ -81,6 +81,17 @@ protected SketchTextArea executeInEDT() { }); } + + public SketchTextArea select(final SketchTextArea target, int selectionStart, int selectionEnd) { + return GuiActionRunner.execute(new GuiQuery() { + + protected SketchTextArea executeInEDT() { + target.select(selectionStart, selectionEnd); + return target; + } + + }); + } public Integer getCaretPosition(final SketchTextArea target) { focusAndWaitForFocusGain(target); diff --git a/app/test/processing/app/helpers/SketchTextAreaFixture.java b/app/test/processing/app/helpers/SketchTextAreaFixture.java old mode 100644 new mode 100755 index e5e1f703b69..50a3641b62f --- a/app/test/processing/app/helpers/SketchTextAreaFixture.java +++ b/app/test/processing/app/helpers/SketchTextAreaFixture.java @@ -71,6 +71,11 @@ public SketchTextAreaFixture selectAll() { driver.selectAll((SketchTextArea) target); return this; } + + public SketchTextAreaFixture select(int selectionStart, int selectionEnd) { + driver.select((SketchTextArea) target, selectionStart, selectionEnd); + return this; + } public int getCaretPosition() { return driver.getCaretPosition((SketchTextArea) target); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy