@@ -26,6 +26,7 @@ class LeftCurlyCheck extends Check {
2626
2727 public static inline var EOL : String = " eol" ;
2828 public static inline var NL : String = " nl" ;
29+ public static inline var NLOW : String = " nlow" ;
2930
3031 public var tokens : Array <String >;
3132 public var option : String ;
@@ -101,7 +102,7 @@ class LeftCurlyCheck extends Check {
101102 switch (field .kind ) {
102103 case FFun (f ):
103104 if (! hasToken (FUNCTION )) return ;
104- checkBlocks (f .expr );
105+ checkBlocks (f .expr , isFieldWrapped ( field ) );
105106 default :
106107 }
107108 }
@@ -140,25 +141,25 @@ class LeftCurlyCheck extends Check {
140141 checkBlocks (f .expr );
141142 case EFor (it , expr ):
142143 if (! hasToken (FOR )) return ;
143- checkBlocks (expr );
144+ checkBlocks (expr , isWrapped ( it ) );
144145 case EIf (econd , eif , eelse ):
145146 if (! hasToken (IF )) return ;
146- checkBlocks (eif );
147+ checkBlocks (eif , isWrapped ( econd ) );
147148 checkBlocks (eelse );
148149 case EWhile (econd , expr , _ ):
149150 if (! hasToken (WHILE )) return ;
150- checkBlocks (expr );
151+ checkBlocks (expr , isWrapped ( econd ) );
151152 case ESwitch (expr , cases , edef ):
152153 if (! hasToken (SWITCH )) return ;
153154 var firstCase : Expr = edef ;
154155 if (cases .length > 0 ) {
155156 firstCase = cases [0 ].values [0 ];
156157 }
157158 if (firstCase == null ) {
158- checkLinesBetween (e .pos .min , e .pos .max , e .pos );
159+ checkLinesBetween (e .pos .min , e .pos .max , isWrapped ( expr ), e .pos );
159160 return ;
160161 }
161- checkLinesBetween (expr .pos .max , firstCase .pos .min , e .pos );
162+ checkLinesBetween (expr .pos .max , firstCase .pos .min , isWrapped ( expr ), e .pos );
162163 case ETry (expr , catches ):
163164 if (! hasToken (TRY )) return ;
164165 checkBlocks (expr );
@@ -170,30 +171,53 @@ class LeftCurlyCheck extends Check {
170171 });
171172 }
172173
173- function checkBlocks (e : Expr ) {
174+ function isFieldWrapped (field : Field ): Bool {
175+ var pos1 : Int = field .pos .min ;
176+ var pos2 : Int = pos1 ;
177+ switch (field .kind ) {
178+ case FFun (f ):
179+ if (f .expr == null ) {
180+ return false ;
181+ }
182+ pos2 = f .expr .pos .min ;
183+ default :
184+ return false ;
185+ }
186+
187+ var functionDef : String = checker .file .content .substring (pos1 , pos2 );
188+ return (functionDef .indexOf (' \n ' ) >= 0 ) ||
189+ (functionDef .indexOf (' \r ' ) >= 0 );
190+ }
191+
192+ function isWrapped (e : Expr ): Bool {
193+ if (e == null ) return false ;
194+ return (checker .getLinePos (e .pos .min ).line != checker .getLinePos (e .pos .max ).line );
195+ }
196+
197+ function checkBlocks (e : Expr , wrapped : Bool = false ) {
174198 if ((e == null ) || (e .expr == null )) return ;
175199
176200 switch (e .expr ) {
177201 case EBlock (_ ):
178202 var linePos : LinePos = checker .getLinePos (e .pos .min );
179203 var line : String = checker .lines [linePos .line ];
180- checkLeftCurly (line , e .pos );
204+ checkLeftCurly (line , wrapped , e .pos );
181205 default :
182206 }
183207 }
184208
185- function checkLinesBetween (min : Int , max : Int , pos : Position ) {
209+ function checkLinesBetween (min : Int , max : Int , wrapped : Bool = false , pos : Position ) {
186210 if (isPosSuppressed (pos )) return ;
187211 var bracePos : Int = checker .file .content .lastIndexOf (" {" , max );
188212 if (bracePos < 0 || bracePos < min ) return ;
189213
190214 var lineNum : Int = checker .getLinePos (bracePos ).line ;
191215 var line : String = checker .lines [lineNum ];
192- checkLeftCurly (line , pos );
216+ checkLeftCurly (line , wrapped , pos );
193217 }
194218
195219 @SuppressWarnings (" checkstyle:BlockFormat" )
196- function checkLeftCurly (line : String , pos : Position ) {
220+ function checkLeftCurly (line : String , wrapped : Bool = false , pos : Position ) {
197221 var lineLength : Int = line .length ;
198222
199223 // must have at least one non whitespace character before curly
@@ -205,12 +229,14 @@ class LeftCurlyCheck extends Check {
205229 try {
206230 if (curlyAtEOL ) {
207231 logErrorIf ((option == NL ), ' Left curly should be on new line (only whitespace before curly)' , pos );
208- logErrorIf ((option != EOL ), ' Left curly unknown option ${option }' , pos );
232+ logErrorIf ((option == NLOW ) && wrapped , ' Left curly should be on new line (previous expression is split over muliple lines)' , pos );
233+ logErrorIf ((option != EOL ) && (option != NLOW ), ' Left curly unknown option ${option }' , pos );
209234 return ;
210235 }
211236 logErrorIf ((option == EOL ), ' Left curly should be at EOL (only linebreak or comment after curly)' , pos );
212237 logErrorIf ((! curlyOnNL ), ' Left curly should be on new line (only whitespace before curly)' , pos );
213- logErrorIf ((option != NL ), ' Left curly unknown option ${option }' , pos );
238+ logErrorIf ((option == NLOW ) && ! wrapped , ' Left curly should be at EOL (previous expression is not split over muliple lines)' , pos );
239+ logErrorIf ((option != NL ) && (option != NLOW ), ' Left curly unknown option ${option }' , pos );
214240 }
215241 catch (e : String ) {
216242 // one of the error messages fired -> do nothing
0 commit comments