1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| package net.sourceforge.astyleclipse; |
13 |
| |
14 |
| import java.io.BufferedReader; |
15 |
| import java.io.StringReader; |
16 |
| import java.util.HashMap; |
17 |
| import java.util.Map; |
18 |
| |
19 |
| import net.sourceforge.astyleclipse.astyle.ASFormatter; |
20 |
| import net.sourceforge.astyleclipse.astyle.ASStreamIterator; |
21 |
| import net.sourceforge.astyleclipse.preferences.PreferenceConstants; |
22 |
| import net.sourceforge.astyleclipse.astyle.ASResource; |
23 |
| |
24 |
| import org.eclipse.cdt.core.formatter.CodeFormatter; |
25 |
| import org.eclipse.cdt.utils.spawner.ProcessFactory; |
26 |
| import org.eclipse.core.runtime.IStatus; |
27 |
| import org.eclipse.core.runtime.Preferences; |
28 |
| import org.eclipse.core.runtime.Status; |
29 |
| import org.eclipse.text.edits.MultiTextEdit; |
30 |
| import org.eclipse.text.edits.ReplaceEdit; |
31 |
| import org.eclipse.text.edits.TextEdit; |
32 |
| |
33 |
| |
34 |
| |
35 |
| public class AstyleFormatter extends CodeFormatter { |
36 |
| Map astyleOptions = new HashMap(); |
37 |
| Map fOptions = new HashMap(); |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
0
| public TextEdit format(int kind, String source, int offset, int length,
|
46 |
| int indentationLevel, String lineSeparator) { |
47 |
0
| try {
|
48 |
0
| AstyleLog.logInfo(source);
|
49 |
| |
50 |
0
| ASFormatter formatter = new ASFormatter();
|
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
0
| setOptions(formatter, astyleOptions);
|
62 |
| |
63 |
0
| StringBuffer target = new StringBuffer("");
|
64 |
0
| BufferedReader in = null;
|
65 |
0
| in = new BufferedReader(new StringReader(source));
|
66 |
0
| formatter.init(new ASStreamIterator(in));
|
67 |
0
| while (formatter.hasMoreLines()) {
|
68 |
0
| String line = formatter.nextLine();
|
69 |
| |
70 |
0
| target.append(line);
|
71 |
0
| if (formatter.hasMoreLines()) {
|
72 |
0
| target.append("\n");
|
73 |
| } |
74 |
| } |
75 |
0
| AstyleLog.logInfo("result is " + target.toString());
|
76 |
| |
77 |
0
| offset = 0;
|
78 |
0
| length = source.length();
|
79 |
0
| MultiTextEdit textEdit = new MultiTextEdit(offset, length);
|
80 |
0
| textEdit
|
81 |
| .addChild(new ReplaceEdit(offset, length, target.toString())); |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
0
| return textEdit;
|
105 |
| } catch (Throwable e) { |
106 |
0
| Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID,
|
107 |
| IStatus.ERROR, "Error", e); |
108 |
0
| Activator.getDefault().getLog().log(status);
|
109 |
| } |
110 |
0
| return null;
|
111 |
| } |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
0
| public void setOptions(Map options) {
|
118 |
0
| fOptions.putAll(options);
|
119 |
| } |
120 |
| |
121 |
0
| public void setOptions(ASFormatter formatter, Map options) {
|
122 |
0
| Preferences prefs = Activator.getDefault().getPluginPreferences();
|
123 |
| |
124 |
0
| String style = prefs.getString(PreferenceConstants.STYLE_CHOICE);
|
125 |
0
| AstyleLog.logInfo("style is " + style);
|
126 |
0
| formatter.setCStyle(true);
|
127 |
0
| if (style.equalsIgnoreCase("ansi")) {
|
128 |
0
| formatter.setBracketIndent(false);
|
129 |
0
| formatter.setSpaceIndentation(4);
|
130 |
0
| formatter.setBracketFormatMode(ASResource.BREAK_MODE);
|
131 |
0
| formatter.setClassIndent(false);
|
132 |
0
| formatter.setSwitchIndent(false);
|
133 |
0
| formatter.setNamespaceIndent(false);
|
134 |
| } |
135 |
0
| if (style.equalsIgnoreCase("gnu")) {
|
136 |
0
| formatter.setBlockIndent(true);
|
137 |
0
| formatter.setSpaceIndentation(2);
|
138 |
0
| formatter.setBracketFormatMode(ASResource.BREAK_MODE);
|
139 |
0
| formatter.setClassIndent(false);
|
140 |
0
| formatter.setSwitchIndent(false);
|
141 |
0
| formatter.setNamespaceIndent(false);
|
142 |
| } |
143 |
0
| if (style.equalsIgnoreCase("java")) {
|
144 |
0
| formatter.setCStyle(false);
|
145 |
0
| formatter.setBracketIndent(false);
|
146 |
0
| formatter.setSpaceIndentation(4);
|
147 |
0
| formatter.setBracketFormatMode(ASResource.ATTACH_MODE);
|
148 |
0
| formatter.setSwitchIndent(false);
|
149 |
| } |
150 |
0
| if (style.equalsIgnoreCase("kr")) {
|
151 |
| |
152 |
0
| formatter.setBracketIndent(false);
|
153 |
0
| formatter.setSpaceIndentation(4);
|
154 |
0
| formatter.setBracketFormatMode(ASResource.ATTACH_MODE);
|
155 |
0
| formatter.setClassIndent(false);
|
156 |
0
| formatter.setSwitchIndent(false);
|
157 |
0
| formatter.setNamespaceIndent(false);
|
158 |
| } |
159 |
0
| if (style.equalsIgnoreCase("linux")) {
|
160 |
0
| formatter.setBracketIndent(false);
|
161 |
0
| formatter.setSpaceIndentation(8);
|
162 |
0
| formatter.setBracketFormatMode(ASResource.BDAC_MODE);
|
163 |
0
| formatter.setClassIndent(false);
|
164 |
0
| formatter.setSwitchIndent(false);
|
165 |
0
| formatter.setNamespaceIndent(false);
|
166 |
| } |
167 |
| } |
168 |
| |
169 |
0
| public boolean supportProperty(String propertyID) {
|
170 |
| |
171 |
0
| return false;
|
172 |
| } |
173 |
| |
174 |
0
| public String[][] getEnumerationProperty(String propertyID) {
|
175 |
| |
176 |
0
| return null;
|
177 |
| } |
178 |
| } |