diff --git a/pom.xml b/pom.xml
index af87593..fb5fc7e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
info.istlab.IoTP
IoTP
jar
- 0.12
+ 0.13
IoTP
http://maven.apache.org
diff --git a/src/main/java/info/istlab/IoTP/Editor.java b/src/main/java/info/istlab/IoTP/Editor.java
index 47a0b97..e40f595 100644
--- a/src/main/java/info/istlab/IoTP/Editor.java
+++ b/src/main/java/info/istlab/IoTP/Editor.java
@@ -44,7 +44,7 @@
JButton runB; // for ShellScript
JButton compileB;
JButton execB;
- JButton layoutB;
+ JButton fixIndentB;
JTextField comlineOption;
ScriptRunner runner;
@@ -62,11 +62,11 @@
textArea.setTabsEmulated(true);
try {
- String d = new String(App.workingDir+File.separator);
+ String d = new String(App.workingDir + File.separator);
if (d.contains("\\")) {
d = d.replace("\\", "\\\\");
}
- setTitle(file.getCanonicalPath().replaceAll(d , ""));
+ setTitle(file.getCanonicalPath().replaceAll(d, ""));
} catch (IOException e) {
e.printStackTrace();
}
@@ -147,9 +147,9 @@
// topP.add(comlineOption);
}
- // layoutB = new JButton("layout");
- // topP.add(layoutB);
- // layoutB.addActionListener(e -> fixIndent() );
+ fixIndentB = new JButton("インデント調整");
+ topP.add(fixIndentB);
+ fixIndentB.addActionListener(e -> fixIndent());
getContentPane().add(topP, BorderLayout.NORTH);
setVisible(true);
@@ -160,15 +160,19 @@
}
private void fixIndent() {
+ int lineNo = textArea.getCaretPosition();
String src = textArea.getText();
- System.out.println(src);
+ // System.out.println(src);
String out = "";
try {
out = IndentProgram.indent(src);
} catch (IOException e) {
e.printStackTrace();
}
- // textArea.setText(out);
+ textArea.setText(out);
+ textArea.setCaretPosition(lineNo);
+
+ saveB.setEnabled(!src.equals(out)); // ソースが変化していたら保存ボタンをEnable
// System.out.println(out);
}
@@ -204,7 +208,6 @@
public void save() {
String src = textArea.getText();
- // System.out.println(src);
try {
PrintWriter out = new PrintWriter(file.getAbsolutePath(), "UTF-8");
out.println(src);
@@ -251,6 +254,9 @@
if (e.getKeyCode() == 87) { // W (Close)
setVisible(false);
}
+ if (e.getKeyCode() == 70) { // F (format)
+ fixIndent();
+ }
}
}
diff --git a/src/main/java/info/istlab/IoTP/IndentProgram.java b/src/main/java/info/istlab/IoTP/IndentProgram.java
index bc63b13..144f52a 100644
--- a/src/main/java/info/istlab/IoTP/IndentProgram.java
+++ b/src/main/java/info/istlab/IoTP/IndentProgram.java
@@ -3,51 +3,53 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
+import java.util.ArrayList;
public class IndentProgram {
public static String indent(String input) throws IOException {
BufferedReader br = new BufferedReader(new StringReader(input));
- StringBuffer sb = new StringBuffer();
+ ArrayList sb; // 一時保存用
+ sb = new ArrayList();
+ ArrayList idt;
+ idt = new ArrayList();
+ ArrayList out; // 一時保存用
+ out = new ArrayList();
String line;
- int indentLevel = 0;
while ((line = br.readLine()) != null) {
line = line.trim(); // 行頭と行末の空白を除去する
- if (line.isEmpty()) {
- // 空行はそのまま出力する
- sb.append(line + "\n");
- continue;
- }
-
- if (line.startsWith("}") && line.endsWith("{")) {
- // 終了括弧がある場合はインデントレベルを減らす
- sb = new StringBuffer(sb.substring(0, sb.length()-2));
- sb.append(line + "\n");
- // indentLevel += 2;
- sb.append(printIndent(indentLevel));
- // sb.append(printIndent(indentLevel));
- } else if (line.endsWith("{")) {
- // 開始括弧がある場合はインデントレベルを増やす
- sb.append(line + "\n");
- indentLevel += 2;
- sb.append(printIndent(indentLevel));
- // sb.append(printIndent(indentLevel));
- } else if (line.startsWith("}")) {
- // 終了括弧がある場合はインデントレベルを減らす
- sb = new StringBuffer(sb.substring(0, sb.length()-2));
- sb.append(line + "\n");
- indentLevel -= 2;
- sb.append(printIndent(indentLevel));
- // sb.append(printIndent(indentLevel));
- } else {
- // それ以外の行は単にインデントして出力する
- sb.append(line + "\n");
- sb.append(printIndent(indentLevel));
- }
+ sb.add(line);
}
-
br.close();
- return sb.toString();
+ int indentLevel = 0;
+ boolean tobeaddindent = false;
+ for (String lorig : sb) {
+ String l = removeComment(lorig);
+ l = l.trim();
+
+ if (tobeaddindent){
+ indentLevel += 2;
+ tobeaddindent = false;
+ }
+ // } があれば、その行から-2
+ if (l.startsWith("}")) {
+ indentLevel -= 2;
+ }
+ if (l.endsWith("{")) { // { があれば、次の行から+2
+ tobeaddindent = true;
+ }
+ idt.add(indentLevel);
+ // それ以外は、プラマイZero
+ }
+ for (int i = 0; i < sb.size(); i++) {
+ int il = idt.get(i);
+ out.add(printIndent(il) + sb.get(i));
+ }
+ StringBuffer strb = new StringBuffer();
+ for(String l: out){
+ strb.append(l+"\n");
+ }
+ return strb.toString();
}
// インデントを出力するためのヘルパー関数
@@ -58,4 +60,11 @@
}
return sb.toString();
}
+ public static String removeComment(String input) {
+ int index = input.indexOf("//"); // "//"のインデックスを検索する
+ if (index != -1) { // "//"が見つかった場合
+ return input.substring(0, index); // "//"より前の部分文字列を返す
+ }
+ return input; // "//"が見つからなかった場合、元の文字列を返す
+ }
}
diff --git a/src/main/java/info/istlab/IoTP/Launcher.java b/src/main/java/info/istlab/IoTP/Launcher.java
index c25016f..af831b8 100644
--- a/src/main/java/info/istlab/IoTP/Launcher.java
+++ b/src/main/java/info/istlab/IoTP/Launcher.java
@@ -26,7 +26,7 @@
public class Launcher extends JFrame implements MouseInputListener, KeyListener {
public static Launcher theapp;
- public static String version = "0.12";
+ public static String version = "0.13";
// JPanel mainP;
File root;
JTree tree;