@@ -589,9 +589,9 @@ public void exitMethodDeclaration(ProcessingParser.MethodDeclarationContext ctx)
589589 // Insert at start of method or after annoation
590590 if (!hasVisibilityModifier ) {
591591 if (annoationPoint == null ) {
592- insertBefore (possibleModifiers .getStart (), " public " );
592+ insertBefore (possibleModifiers .getStart (), "public " );
593593 } else {
594- insertAfter (annoationPoint .getStop (), " public " );
594+ insertAfter (annoationPoint .getStop (), "public " );
595595 }
596596 }
597597
@@ -693,17 +693,17 @@ protected void handleSizeCall(ParserRuleContext ctx) {
693693 if (isSize && argsContext .getChildCount () > 2 ) {
694694 thisRequiresRewrite = true ;
695695
696- sketchWidth = argsContext .getChild (0 ). getText ( );
697- boolean invalidWidth = PApplet . parseInt ( sketchWidth , - 1 ) == - 1 ;
698- invalidWidth = invalidWidth && ! sketchWidth . equals ( "displayWidth" );
699- if ( invalidWidth ) {
696+ boolean widthValid = sizeParamValid ( argsContext .getChild (0 ));
697+ if ( widthValid ) {
698+ sketchWidth = argsContext . getChild ( 0 ). getText ( );
699+ } else {
700700 thisRequiresRewrite = false ;
701701 }
702702
703- sketchHeight = argsContext .getChild (2 ). getText ( );
704- boolean invalidHeight = PApplet . parseInt ( sketchHeight , - 1 ) == - 1 ;
705- invalidHeight = invalidHeight && ! sketchHeight . equals ( "displayHeight" );
706- if ( invalidHeight ) {
703+ boolean validHeight = sizeParamValid ( argsContext .getChild (2 ));
704+ if ( validHeight ) {
705+ sketchHeight = argsContext . getChild ( 2 ). getText ( );
706+ } else {
707707 thisRequiresRewrite = false ;
708708 }
709709
@@ -1535,4 +1535,36 @@ private ImportStatement createPlainImportStatementInfo(String fullyQualifiedName
15351535 return ImportStatement .parse (fullyQualifiedName );
15361536 }
15371537
1538+ private boolean isMethodCall (ParseTree ctx ) {
1539+ return ctx instanceof ProcessingParser .MethodCallContext ;
1540+ }
1541+
1542+ private boolean isVariable (ParseTree ctx ) {
1543+ boolean isPrimary = ctx instanceof ProcessingParser .PrimaryContext ;
1544+ if (!isPrimary ) {
1545+ return false ;
1546+ }
1547+
1548+ String text = ctx .getText ();
1549+ boolean startsWithAlpha = text .length () > 0 && Character .isAlphabetic (text .charAt (0 ));
1550+ return startsWithAlpha ;
1551+ }
1552+
1553+ private boolean sizeParamValid (ParseTree ctx ) {
1554+ // Method calls and variables not allowed.
1555+ if (isMethodCall (ctx ) || isVariable (ctx )) {
1556+ return false ;
1557+ }
1558+
1559+ // If user passed an expression, check subexpressions.
1560+ for (int i = 0 ; i < ctx .getChildCount (); i ++) {
1561+ if (!sizeParamValid (ctx .getChild (i ))) {
1562+ return false ;
1563+ }
1564+ }
1565+
1566+ // If all sub-expressions passed and not identifier, is valid.
1567+ return true ;
1568+ }
1569+
15381570}
0 commit comments