22
33import de .peeeq .wurstio .WurstCompilerJassImpl ;
44import de .peeeq .wurstscript .RunArgs ;
5+ import de .peeeq .wurstscript .ast .Ast ;
56import de .peeeq .wurstscript .ast .CompilationUnit ;
67import de .peeeq .wurstscript .ast .Element ;
8+ import de .peeeq .wurstscript .ast .WurstModel ;
79import de .peeeq .wurstscript .gui .WurstGui ;
810import de .peeeq .wurstscript .gui .WurstGuiCliImpl ;
911import org .apache .commons .lang .StringUtils ;
@@ -40,7 +42,13 @@ public static void pretty(List<String> args) throws IOException {
4042 }
4143
4244 public static String pretty (String source , String ending ) {
43- CompilationUnit cu = parse (source , ending );
45+ WurstGui gui = new WurstGuiCliImpl ();
46+ CompilationUnit cu = parse (source , ending , gui );
47+
48+ // Check for compilation errors before pretty printing
49+ if (gui .getErrorCount () > 0 ) {
50+ throw new RuntimeException ("Cannot format code with compilation errors:\n " + gui .getErrors ());
51+ }
4452
4553 Spacer spacer = new MaxOneSpacer ();
4654 StringBuilder sb = new StringBuilder ();
@@ -50,11 +58,25 @@ public static String pretty(String source, String ending) {
5058 }
5159
5260 private static void prettyPrint (String filename ) {
53- String clean = pretty (filename , filename .substring (filename .lastIndexOf ("." )));
54- System .out .println (clean );
61+ try {
62+ String clean = pretty (filename , filename .substring (filename .lastIndexOf ("." )));
63+ System .out .println (clean );
64+ } catch (RuntimeException e ) {
65+ System .err .println ("Error formatting " + filename + ": " + e .getMessage ());
66+ throw e ;
67+ }
5568 }
5669
5770 public static void pretty (CompilationUnit cu ) {
71+ pretty (cu , new WurstGuiCliImpl ());
72+ }
73+
74+ public static void pretty (CompilationUnit cu , WurstGui gui ) {
75+ // Check for compilation errors before pretty printing
76+ if (gui .getErrorCount () > 0 ) {
77+ throw new RuntimeException ("Cannot format code with compilation errors:\n " + gui .getErrors ());
78+ }
79+
5880 Spacer spacer = new MaxOneSpacer ();
5981 StringBuilder sb = new StringBuilder ();
6082 cu .prettyPrint (spacer , sb , 0 );
@@ -64,7 +86,13 @@ public static void pretty(CompilationUnit cu) {
6486
6587 private static void debug (String filename ) {
6688 String contents = readFile (filename );
67- CompilationUnit cu = parse (contents , filename .substring (filename .lastIndexOf ("." )));
89+ WurstGui gui = new WurstGuiCliImpl ();
90+ CompilationUnit cu = parse (contents , filename .substring (filename .lastIndexOf ("." )), gui );
91+
92+ // Check for compilation errors before debugging
93+ if (gui .getErrorCount () > 0 ) {
94+ throw new RuntimeException ("Cannot debug code with compilation errors:\n " + gui .getErrors ());
95+ }
6896
6997 walkTree (cu , 0 );
7098 }
@@ -91,7 +119,13 @@ private static void prettyAll(String root) throws IOException {
91119
92120 private static String pretty (File f ) {
93121 String contents = readFile (f .toString ());
94- CompilationUnit cu = parse (contents , f .getName ().substring (f .getName ().lastIndexOf ("." )));
122+ WurstGui gui = new WurstGuiCliImpl ();
123+ CompilationUnit cu = parse (contents , f .getName ().substring (f .getName ().lastIndexOf ("." )), gui );
124+
125+ // Check for compilation errors before pretty printing
126+ if (gui .getErrorCount () > 0 ) {
127+ throw new RuntimeException ("Cannot format code with compilation errors:\n " + gui .getErrors ());
128+ }
95129
96130 Spacer spacer = new MaxOneSpacer ();
97131 StringBuilder sb = new StringBuilder ();
@@ -119,7 +153,24 @@ private static String readFile(String filename) {
119153
120154 private static CompilationUnit parse (String input , String ending ) {
121155 WurstGui gui = new WurstGuiCliImpl ();
156+ return parse (input , ending , gui );
157+ }
158+
159+ private static CompilationUnit parse (String input , String ending , WurstGui gui ) {
122160 WurstCompilerJassImpl compiler = new WurstCompilerJassImpl (null , gui , null , new RunArgs ("-prettyPrint" ));
123- return compiler .parse ("format" + ending , new StringReader (input ));
161+ CompilationUnit cu = compiler .parse ("format" + ending , new StringReader (input ));
162+
163+ // Create a minimal model to run validation
164+ WurstModel model = Ast .WurstModel ();
165+ model .add (cu );
166+
167+ // Run validation to detect compilation errors
168+ try {
169+ compiler .checkProg (model );
170+ } catch (Exception e ) {
171+ // Errors are already added to gui during validation
172+ }
173+
174+ return cu ;
124175 }
125176}
0 commit comments