Skip to content

Commit 789988f

Browse files
committed
abort formatting on unrecognizable syntax
1 parent 9f24c90 commit 789988f

File tree

17 files changed

+282
-7
lines changed

17 files changed

+282
-7
lines changed

base/org.codehaus.groovy21/.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
<classpathentry exported="true" kind="lib" path="lib/ivy-2.2.0.jar"/>
88
<classpathentry exported="true" kind="lib" path="lib/bsf-2.4.0.jar"/>
99
<classpathentry exported="true" kind="lib" path="lib/groovy-all-2.1.5.jar" sourcepath="lib/groovy-all-2.1.5-sources.jar"/>
10+
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
1011
<classpathentry kind="output" path="bin"/>
1112
</classpath>

base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/GroovyParser.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
* Andrew Eisenberg - Additional work
1111
* Kris De Volder - Grails 1.4/2.0 support
1212
*******************************************************************************/
13+
/*
14+
* This file has been modified by Nick Joodi on August, 2013.
15+
*/
16+
1317
package org.codehaus.jdt.groovy.internal.compiler.ast;
1418

1519
import groovy.lang.GroovyClassLoader;
@@ -449,7 +453,9 @@ public CompilationUnitDeclaration dietParse(ICompilationUnit sourceUnit, Compila
449453
}
450454
}
451455
}
452-
gcuDeclaration.processToPhase(Phases.CONVERSION);
456+
if (!gcuDeclaration.processToPhase(Phases.CONVERSION)) {
457+
return null;
458+
}
453459

454460
// Groovy moduleNode is null when there is a fatal error
455461
// Otherwise, recover what we can

extras/org.codehaus.groovy.m2eclipse/.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
44
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
55
<classpathentry kind="src" path="src"/>
6+
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
67
<classpathentry kind="output" path="bin"/>
78
</classpath>

ide-test/org.codehaus.groovy.eclipse.codeassist.completion.test/.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
55
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
6+
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
67
<classpathentry kind="output" path="bin"/>
78
</classpath>

ide-test/org.codehaus.groovy.eclipse.codebrowsing.test/.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
55
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
6+
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
67
<classpathentry kind="output" path="bin"/>
78
</classpath>

ide-test/org.codehaus.groovy.eclipse.core.test/.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
55
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
66
<classpathentry kind="src" path="src"/>
7+
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
78
<classpathentry kind="output" path="bin"/>
89
</classpath>

ide-test/org.codehaus.groovy.eclipse.junit.test/.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
44
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
55
<classpathentry kind="src" path="src"/>
6+
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
67
<classpathentry kind="output" path="bin"/>
78
</classpath>

ide-test/org.codehaus.groovy.eclipse.refactoring.test/.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
55
<classpathentry kind="src" path="src"/>
66
<classpathentry kind="src" path="test plugin"/>
7+
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
78
<classpathentry kind="output" path="bin"/>
89
</classpath>

ide-test/org.codehaus.groovy.eclipse.tests/.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
55
<classpathentry kind="src" path="src"/>
66
<classpathentry kind="lib" path="lib/commons-io-1.3.1.jar"/>
7+
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
78
<classpathentry kind="output" path="bin"/>
89
</classpath>

ide/org.codehaus.groovy.eclipse.astviews/.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
44
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
55
<classpathentry kind="src" path="src"/>
6+
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
67
<classpathentry kind="output" path="bin"/>
78
</classpath>

ide/org.codehaus.groovy.eclipse.core/src/org/codehaus/groovy/eclipse/core/compiler/GroovySnippetParser.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* Andrew Eisenberg - initial API and implementation
1010
*******************************************************************************/
1111

12+
/*
13+
* This file has been modified by Nick Joodi on August, 2013.
14+
*/
15+
1216
package org.codehaus.groovy.eclipse.core.compiler;
1317

1418
import groovyjarjarasm.asm.Opcodes;
@@ -101,10 +105,12 @@ public ModuleNode parse(String source) {
101105
ICompilationUnit unit = new MockCompilationUnit(source.toCharArray(), "Hello.groovy".toCharArray());
102106
CompilationResult compilationResult = new CompilationResult(unit, 0, 0, options.maxProblemsPerUnit);
103107

104-
105108
GroovyCompilationUnitDeclaration decl =
106109
(GroovyCompilationUnitDeclaration)
107-
parser.dietParse(unit, compilationResult);
110+
parser.dietParse(unit, compilationResult);
111+
if (decl == null) {
112+
return null;
113+
}
108114
ModuleNode node = decl.getModuleNode();
109115

110116
if (node == null) {
@@ -141,10 +147,9 @@ public GroovySourceAST parseForCST(String source) {
141147
ICompilationUnit unit = new MockCompilationUnit(source.toCharArray(), "Hello.groovy".toCharArray());
142148
CompilationResult compilationResult = new CompilationResult(unit, 0, 0, options.maxProblemsPerUnit);
143149

144-
145150
GroovyCompilationUnitDeclaration decl =
146151
(GroovyCompilationUnitDeclaration)
147-
parser.dietParse(unit, compilationResult);
152+
parser.dietParse(unit, compilationResult);
148153
SourceUnit sourceUnit = decl.getSourceUnit();
149154
ParserPlugin parserPlugin = (ParserPlugin) ReflectionUtils.getPrivateField(SourceUnit.class, "parserPlugin", sourceUnit);
150155
if (parserPlugin instanceof AntlrParserPlugin) {

ide/org.codehaus.groovy.eclipse.refactoring/src/org/codehaus/groovy/eclipse/refactoring/core/utils/ASTTools.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19+
20+
/*
21+
* This file has been modified by Nick Joodi on August, 2013.
22+
*/
23+
1924
package org.codehaus.groovy.eclipse.refactoring.core.utils;
2025

2126
import java.util.ArrayList;
@@ -233,9 +238,9 @@ public static IDocument getDocumentWithSystemLineBreak(String text) {
233238
return document;
234239
}
235240

236-
public static ModuleNode getASTNodeFromSource(String source) {
241+
public static ModuleNode getASTNodeFromSource(String source) {
237242
GroovySnippetParser parser = new GroovySnippetParser();
238-
ModuleNode node = parser.parse(source);
243+
ModuleNode node = parser.parse(source);
239244
return node;
240245
}
241246

ide/org.codehaus.groovy.eclipse.refactoring/src/org/codehaus/groovy/eclipse/refactoring/formatter/DefaultGroovyFormatter.java

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19+
20+
/*
21+
* This file has been modified by Nick Joodi on August, 2013.
22+
*/
23+
1924
package org.codehaus.groovy.eclipse.refactoring.formatter;
2025

2126
import groovyjarjarantlr.Token;
@@ -82,13 +87,16 @@ public DefaultGroovyFormatter(ITextSelection sel, IDocument doc, IFormatterPrefe
8287

8388
if (selection.getLength() != 0) {
8489
try {
90+
91+
// I believe we have no offset
8592
// expand selection to include start of line
8693
int startLine = document.getLineOfOffset(selection.getOffset());
8794
IRegion startLineInfo = document.getLineInformation(startLine);
8895

8996
// -1 because we don't want a selection at the start of a new
9097
// line to cause that line to be formatted
9198
int endLine = document.getLineOfOffset(selection.getOffset() + selection.getLength() - 1);
99+
92100
IRegion endLineInfo = document.getLineInformation(endLine);
93101

94102
formatOffset = startLineInfo.getOffset();
@@ -146,6 +154,7 @@ public TextEdit format() {
146154
// }
147155
} catch (Exception e) {
148156
GroovyCore.logWarning("Cannot format, probably due to compilation errors. Please fix and try again.", e);
157+
return null;
149158
}
150159

151160
if (formattedDocument.get().equals(document.get())) {
@@ -522,4 +531,5 @@ private int nextTabStop(int spaces, int tabSize) {
522531
int tabs = spaces / tabSize + 1;
523532
return tabs * tabSize;
524533
}
534+
525535
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.codehaus.groovy.eclipse.refactoring.formatter;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
import java.io.PrintWriter;
8+
import java.text.SimpleDateFormat;
9+
import java.util.Date;
10+
11+
public class Formatter {
12+
13+
public static void main(String[] args) throws Exception {
14+
String code = readInFile(args[0]);
15+
String extension = args[0].substring(args[0].lastIndexOf(".") + 1, args[0].length());
16+
if (code != null && extension.equals("java")) {
17+
JavaFormat javaFormatter = new JavaFormat();
18+
javaFormatter.format(args[0], code);
19+
if (javaFormatter.isFormatted()) {
20+
createBackupFile(args[0], code);
21+
}
22+
23+
} else if (code != null && extension.equals("groovy")) {
24+
GroovyFormat groovyFormatter = new GroovyFormat();
25+
groovyFormatter.format(args[0], code);
26+
if (groovyFormatter.isFormatted()) {
27+
createBackupFile(args[0], code);
28+
}
29+
30+
} else {
31+
System.out.println("Sorry, no formatting could be applied to " + args[0]);
32+
}
33+
34+
}
35+
36+
@SuppressWarnings("resource")
37+
private static String readInFile(String fileName) {
38+
BufferedReader inStream = null;
39+
StringBuilder code = new StringBuilder("");
40+
String line;
41+
try {
42+
FileReader file = new FileReader(fileName);
43+
inStream = new BufferedReader(file);
44+
while ((line = inStream.readLine()) != null) {
45+
code.append(line + "\n");
46+
}
47+
} catch (IOException e) {
48+
System.out.println("Error occured when opening " + fileName);
49+
return null;
50+
}
51+
return code.toString();
52+
}
53+
54+
public static void createBackupFile(String fileName, String before) {
55+
try {
56+
Date date = new Date();
57+
SimpleDateFormat sdf = new SimpleDateFormat("MM_dd_yyyy_h_mm_ss");
58+
String formattedDate = sdf.format(date);
59+
String nameWithDate = fileName + "_BACKUP_" + formattedDate;
60+
FileWriter file = new FileWriter(nameWithDate);
61+
PrintWriter safety = new PrintWriter(file);
62+
safety.println(before);
63+
safety.close();
64+
// System.out.println("*** A backup file was placed in " +
65+
// nameWithDate + " ***");
66+
} catch (IOException e) {
67+
e.printStackTrace();
68+
}
69+
}
70+
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.codehaus.groovy.eclipse.refactoring.formatter;
2+
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
import java.io.PrintWriter;
6+
import java.text.SimpleDateFormat;
7+
import java.util.Date;
8+
9+
import org.eclipse.jface.preference.IPreferenceStore;
10+
import org.eclipse.jface.text.BadLocationException;
11+
import org.eclipse.jface.text.Document;
12+
import org.eclipse.jface.text.IDocument;
13+
import org.eclipse.jface.text.TextSelection;
14+
import org.eclipse.text.edits.MalformedTreeException;
15+
import org.eclipse.text.edits.TextEdit;
16+
17+
public class GroovyFormat {
18+
19+
private boolean correctlyFormatted;
20+
21+
public GroovyFormat() {
22+
correctlyFormatted = false;
23+
}
24+
25+
26+
public void format(String fileName, String code) {
27+
try {
28+
DefaultGroovyFormatter cf = initializeFormatter(code);
29+
TextEdit te = cf.format();
30+
IDocument dc = new Document(code.toString());
31+
if (te == null || code.length() == 0) {
32+
System.out.println("!!! Could not format " + fileName + " !!!");
33+
}
34+
te.apply(dc);
35+
36+
PrintWriter out = new PrintWriter(new FileWriter(fileName));
37+
out.println(dc.get());
38+
out.close();
39+
40+
System.out
41+
.println("*** Groovy standard formatting conventions have been applied to "
42+
+ fileName + " ***");
43+
correctlyFormatted = true;
44+
} catch (MalformedTreeException e) {
45+
e.printStackTrace();
46+
} catch (BadLocationException e) {
47+
e.printStackTrace();
48+
} catch (IOException e) {
49+
e.printStackTrace();
50+
} catch (NullPointerException e) {
51+
System.out.println("Cannot format " + fileName + ", probably due to compilation errors. Please fix and try again.");
52+
e.printStackTrace();
53+
} catch (Exception e) {
54+
e.printStackTrace();
55+
System.out.println("Cannot format " + fileName + ", probably due to compilation errors. Please fix and try again.");
56+
}
57+
}
58+
59+
public boolean isFormatted() {
60+
return correctlyFormatted;
61+
}
62+
63+
public static void createBackupFile(String fileName, IDocument dc) {
64+
try {
65+
Date date = new Date();
66+
SimpleDateFormat sdf = new SimpleDateFormat("MM_dd_yyyy_h_mm_ss");
67+
String formattedDate = sdf.format(date);
68+
String nameWithDate = fileName + "_BACKUP_" + formattedDate;
69+
FileWriter file = new FileWriter(nameWithDate);
70+
PrintWriter safety = new PrintWriter(file);
71+
safety.println(dc.get());
72+
safety.close();
73+
} catch (IOException e) {
74+
e.printStackTrace();
75+
}
76+
}
77+
78+
public static DefaultGroovyFormatter initializeFormatter(String code) {
79+
IPreferenceStore pref = null;
80+
FormatterPreferencesOnStore defaultPrefs = new FormatterPreferencesOnStore(
81+
pref);
82+
System.out.println("sfsdfsdfsdf" + defaultPrefs.isSmartPaste());
83+
IDocument doc = new Document(code.toString());
84+
TextSelection sel = new TextSelection(0, code.length());
85+
DefaultGroovyFormatter formatter = new DefaultGroovyFormatter(sel, doc,
86+
defaultPrefs, false);
87+
return formatter;
88+
89+
}
90+
91+
92+
}

0 commit comments

Comments
 (0)