mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-20 14:54:31 +01:00
format selection using AStyle control tags
This commit is contained in:
parent
7efb118156
commit
cf81fdcbee
@ -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,29 +81,64 @@ public class AStyle implements Tool {
|
||||
@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(originalText, formatterConfiguration);
|
||||
|
||||
if (formattedText.equals(originalText)) {
|
||||
editor.statusNotice(tr("No changes necessary for Auto Format."));
|
||||
return;
|
||||
}
|
||||
|
||||
if (selection)
|
||||
editor.getCurrentTab().setSelectedText(formattedText);
|
||||
else
|
||||
String formattedText = aStyleInterface.AStyleMain(textArea.getText(), formatterConfiguration);
|
||||
editor.getCurrentTab().setText(formattedText);
|
||||
|
||||
} else {
|
||||
try {
|
||||
|
||||
// apply indentation control keywords.
|
||||
String FORMAT_ON = "\n// *INDENT-ON* DYN\n";
|
||||
String FORMAT_OFF = "\n// *INDENT-OFF* DYN\n";
|
||||
|
||||
RSyntaxDocument content = (RSyntaxDocument) textArea.getDocument();
|
||||
|
||||
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."));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user