mirror of
https://github.com/arduino/Arduino.git
synced 2024-11-28 09:24:14 +01:00
Avoid erros on format multi-line comments, add Tests
This commit is contained in:
parent
cf81fdcbee
commit
0918a5b0b4
@ -43,6 +43,7 @@ import java.io.IOException;
|
||||
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 class AStyle implements Tool {
|
||||
// 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 class AStyle implements Tool {
|
||||
// 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() {
|
||||
|
174
app/test/processing/app/AutoformatSelectionTest.java
Executable file
174
app/test/processing/app/AutoformatSelectionTest.java
Executable file
@ -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;
|
||||
}
|
||||
|
||||
}
|
11
app/test/processing/app/helpers/SketchTextAreaComponentDriver.java
Normal file → Executable file
11
app/test/processing/app/helpers/SketchTextAreaComponentDriver.java
Normal file → Executable file
@ -81,6 +81,17 @@ public class SketchTextAreaComponentDriver extends JComponentDriver {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
public SketchTextArea select(final SketchTextArea target, int selectionStart, int selectionEnd) {
|
||||
return GuiActionRunner.execute(new GuiQuery<SketchTextArea>() {
|
||||
|
||||
protected SketchTextArea executeInEDT() {
|
||||
target.select(selectionStart, selectionEnd);
|
||||
return target;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
public Integer getCaretPosition(final SketchTextArea target) {
|
||||
focusAndWaitForFocusGain(target);
|
||||
|
5
app/test/processing/app/helpers/SketchTextAreaFixture.java
Normal file → Executable file
5
app/test/processing/app/helpers/SketchTextAreaFixture.java
Normal file → Executable file
@ -71,6 +71,11 @@ public class SketchTextAreaFixture extends ComponentFixture {
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user