Skip to content

Commit fe98c36

Browse files
committed
Add astyle to editor module
1 parent d57a107 commit fe98c36

File tree

8 files changed

+105
-46
lines changed

8 files changed

+105
-46
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ dependencies {
136136
api project(':emulatorview')
137137
api project(':libtermexec')
138138
api project(':style')
139-
api project(':libastyle')
140139

141140
implementation 'com.google.firebase:firebase-core:15.0.2'
142141
implementation 'com.google.firebase:firebase-storage:15.0.2'

app/src/androidTest/java/com/duy/astyle/AStyleInterfaceTest.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,42 @@
55
import org.junit.Test;
66
import org.junit.runner.RunWith;
77

8+
import static junit.framework.Assert.assertTrue;
9+
810
@RunWith(AndroidJUnit4.class)
911
public class AStyleInterfaceTest {
1012

1113
@Test
1214
public void testCpp() {
13-
String code = "int Foo(bool isBar){if (isBar) { bar(); return 1; }else return 0;}";
14-
String format = AStyleInterface.format(code, "--style=allman");
15-
System.out.println("format = " + format);
15+
// options to pass to AStyle
16+
// mode=java is required for Java files
17+
String options = "-A2tOP";
18+
19+
String code = getSample();
20+
21+
System.out.println("before format");
22+
System.out.println(code);
23+
24+
AStyleInterface astyle = new AStyleInterface();
25+
String formatted = astyle.formatSource(code, options);
26+
27+
System.out.println("formatted = \n");
28+
System.out.println(formatted);
29+
30+
assertTrue(formatted.length() != 0);
31+
}
32+
33+
public String getSample() {
34+
return "#include <iostream>\n" +
35+
"using namespace std;\n" +
36+
"\n" +
37+
"int main() \n" +
38+
"{" +
39+
"cout << \"Size of char: \" << sizeof(char) << \" byte\" << endl;" +
40+
"cout << \"Size of int: \" << sizeof(int) << \" bytes\" << endl;" +
41+
"cout << \"Size of float: \" << sizeof(float) << \" bytes\" << endl;" +
42+
"cout << \"Size of double: \" << sizeof(double) << \" bytes\" << endl;" +
43+
"return 0;\n" +
44+
"}";
1645
}
1746
}

app/src/main/java/com/duy/editor/CodeEditorActivity.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import android.view.SubMenu;
2525
import android.widget.Toast;
2626

27-
import com.duy.astyle.AStyleInterface;
2827
import com.duy.ccppcompiler.BuildConfig;
2928
import com.duy.ccppcompiler.R;
3029
import com.duy.ccppcompiler.compiler.CompileManager;
@@ -70,11 +69,6 @@ protected void onCreate(Bundle savedInstanceState) {
7069
mPremiumHelper = new InAppPurchaseHelper(this);
7170
// Monitor launch times and interval from installation
7271
RateThisApp.onCreate(this);
73-
74-
String code = "int Foo(bool isBar){if (isBar) { bar(); return 1; }else return 0;}";
75-
AStyleInterface aStyleInterface = new AStyleInterface();
76-
String format = aStyleInterface.format(code, "--style=allman");
77-
System.out.println("format = " + format);
7872
}
7973

8074
@Override

editor/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ dependencies {
4747
api group: 'commons-io', name: 'commons-io', version: '2.4'
4848

4949
api project(':style')
50+
api project(':libastyle')
5051
}
5152

editor/src/main/java/com/jecelyin/editor/v2/editor/EditorDelegate.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
/**
6868
* @author Jecelyin Peng <jecelyin@gmail.com>
6969
*/
70-
public class EditorDelegate implements TextWatcher {
70+
public class EditorDelegate implements TextWatcher, IEditorDelegate {
7171
public final static String KEY_CLUSTER = "is_cluster";
7272
private static final String TAG = "EditorDelegate";
7373
EditAreaView mEditText;
@@ -587,6 +587,7 @@ Parcelable onSaveInstanceState() {
587587
return savedState;
588588
}
589589

590+
@Override
590591
public Document getDocument() {
591592
return mDocument;
592593
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.jecelyin.editor.v2.editor;
2+
3+
public interface IEditorDelegate {
4+
Document getDocument();
5+
}
Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.duy.astyle;
22

3-
import android.util.Log;
4-
53
/**
64
* AStyleInterface jni
75
*
@@ -16,50 +14,82 @@ public class AStyleInterface {
1614
}
1715

1816
/**
19-
* This function is called to get the Artistic Style version number.
20-
*
21-
* @return A Java String containing the Artistic Style version number.
17+
* Error message function for this example.
2218
*/
23-
public static native String AStyleGetVersion();
19+
private static void error(String message) {
20+
System.out.println(message);
21+
System.out.println("The program has terminated!");
22+
System.exit(1);
23+
}
2424

2525
/**
26-
* This function is called to format the source code.
26+
* Calls the Java AStyleMain function in Artistic Style.
27+
* The function name is constructed from method names in this program.
2728
*
28-
* @param textIn - A string containing the source file to be formatted
29-
* @param options A Java String containing the formatting options. They should be in the same
30-
* format as in the default options file. The options may be set apart by
31-
* new-lines, commas, tabs or spaces. The long options do not need the "--" prefix.
32-
* Comments may be used but they must be terminated by a new-line "\n" character.
33-
* <p>
34-
* If the file is not a C/C++ file, the file mode option "mode=java" or "mode=cs"
35-
* must be included.
36-
* Otherwise the default mode of C/C++ is used.
37-
* @return If the function succeeds, the return value is a Java String containing the formatted
38-
* source code.
39-
* If the function fails, the return value is an empty Java String. The returned String should
40-
* be checked for a length of zero to determine if an error occurred. Before the source is
41-
* returned, an error message will be sent to the ErrorHandler method.
42-
* <p>
43-
* The function will NOT fail for an invalid option in the formatting options. In this case,
44-
* an error message is sent to the error handling function and the formatted source code is
45-
* returned without using the invalid option.
29+
* @param textIn A string containing the source code to be formatted.
30+
* @param options A string of options to Artistic Style.
31+
* @return A String containing the formatted source from Artistic Style.
4632
*/
4733
private native String AStyleMain(String textIn, String options);
4834

49-
50-
public String format(String code, String opts) {
51-
return AStyleMain(code, opts);
52-
}
35+
/**
36+
* Calls the Java AStyleGetVersion function in Artistic Style.
37+
* The function name is constructed from method names in this program.
38+
*
39+
* @return A String containing the version number of Artistic Style.
40+
*/
41+
private native String AStyleGetVersion();
5342

5443
/**
55-
* ErrorHandler is called by the Artistic Style static library when an error occurs. It should
56-
* display an error message and then either abort or continue the program depending on the error.
44+
* Error handler for messages from Artistic Style.
45+
* This method is called only if there are errors when AStyleMain is called.
46+
* This is for debugging and there should be no errors when the calling
47+
* parameters are correct.
48+
* Changing the method name requires changing Artistic Style.
49+
* Signature: (ILjava/lang/String;)V.
5750
*
58-
* @param errorNumber The error number as defined by Artistic Style.
59-
* @param errorMessage A Java String containing the error message as defined by Artistic Style.
51+
* @param errorNumber The error number from Artistic Style.
52+
* @param errorMessage The error message from Artistic Style.
6053
*/
6154
@SuppressWarnings("unused")
6255
private void ErrorHandler(int errorNumber, String errorMessage) {
63-
Log.d(TAG, "ErrorHandler() called with: errorNumber = [" + errorNumber + "], errorMessage = [" + errorMessage + "]");
56+
System.out.println("AStyle error "
57+
+ String.valueOf(errorNumber)
58+
+ " - " + errorMessage);
6459
}
60+
61+
/**
62+
* Call the AStyleMain function in Artistic Style.
63+
*
64+
* @param textIn A string containing the source code to be formatted.
65+
* @param options A string of options to Artistic Style.
66+
* @return A String containing the formatted source from Artistic Style,
67+
* or an empty string on error.
68+
*/
69+
public String formatSource(String textIn, String options) { // Return the allocated string
70+
// Memory space is allocated by OnAStyleMemAlloc, a callback function from AStyle
71+
String textOut = "";
72+
try {
73+
textOut = AStyleMain(textIn, options);
74+
} catch (UnsatisfiedLinkError e) { //~ System.out.println(e.getMessage());
75+
error("Cannot call the Java AStyleMain function");
76+
}
77+
return textOut;
78+
}
79+
80+
/**
81+
* Call the AStyleGetVersion function in Artistic Style.
82+
*
83+
* @return A String containing the version number from Artistic Style.
84+
*/
85+
public String getVersion() {
86+
String version = new String();
87+
try {
88+
version = AStyleGetVersion();
89+
} catch (UnsatisfiedLinkError e) { //~ System.out.println(e.getMessage());
90+
error("Cannot call the Java AStyleGetVersion function");
91+
}
92+
return version;
93+
}
94+
6595
}

0 commit comments

Comments
 (0)