Skip to content

Commit 42f1484

Browse files
authored
Merge pull request #597 from processing/static-mode-object
2 parents 510aa2a + 1ca265a commit 42f1484

File tree

8 files changed

+141
-22
lines changed

8 files changed

+141
-22
lines changed

java/src/processing/mode/java/preproc/PdeParseTreeListener.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
8686
private boolean sizeIsFullscreen = false;
8787
private boolean noSmoothRequiresRewrite = false;
8888
private boolean smoothRequiresRewrite = false;
89+
private boolean userImportingManually = false;
8990
private RewriteResult headerResult;
9091
private RewriteResult footerResult;
9192

@@ -443,6 +444,10 @@ public void exitImportDeclaration(ProcessingParser.ImportDeclarationContext ctx)
443444

444445
foundImports.add(ImportStatement.parse(importStringNoSemi));
445446

447+
if (importStringNoSemi.startsWith("processing.core.")) {
448+
userImportingManually = true;
449+
}
450+
446451
delete(ctx.start, ctx.stop);
447452
}
448453

@@ -498,7 +503,7 @@ public void exitMultilineStringLiteral(ProcessingParser.MultilineStringLiteralCo
498503
* @param ctx ANTLR context for the sketch.
499504
*/
500505
public void exitStaticProcessingSketch(ProcessingParser.StaticProcessingSketchContext ctx) {
501-
mode = Mode.STATIC;
506+
mode = foundMain ? Mode.JAVA : Mode.STATIC;
502507
}
503508

504509
/**
@@ -1072,10 +1077,16 @@ protected void writePreprocessorComment(PrintWriterWithEditGen headerWriter,
10721077
protected void writeImports(PrintWriterWithEditGen headerWriter,
10731078
RewriteResultBuilder resultBuilder) {
10741079

1075-
writeImportList(headerWriter, coreImports, resultBuilder);
1080+
if (!userImportingManually) {
1081+
writeImportList(headerWriter, coreImports, resultBuilder);
1082+
}
1083+
10761084
writeImportList(headerWriter, codeFolderImports, resultBuilder);
10771085
writeImportList(headerWriter, foundImports, resultBuilder);
1078-
writeImportList(headerWriter, defaultImports, resultBuilder);
1086+
1087+
if (!userImportingManually) {
1088+
writeImportList(headerWriter, defaultImports, resultBuilder);
1089+
}
10791090
}
10801091

10811092
/**

java/src/processing/mode/java/preproc/Processing.g4

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import JavaParser;
2020

2121
// main entry point, select sketch type
2222
processingSketch
23-
: javaProcessingSketch
24-
| staticProcessingSketch
23+
: staticProcessingSketch
24+
| javaProcessingSketch
2525
| activeProcessingSketch
2626
// | warnMixedModes
2727
;
@@ -33,7 +33,7 @@ javaProcessingSketch
3333

3434
// No method declarations, just statements
3535
staticProcessingSketch
36-
: (importDeclaration | blockStatement)* EOF
36+
: (importDeclaration | blockStatement | typeDeclaration)* EOF
3737
;
3838

3939
// active mode, has function definitions

java/test/processing/mode/java/ParserTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ public void testSizeThis() {
419419

420420
@Test
421421
public void testMixing() {
422-
expectRunnerException("mixing", 1);
422+
expectRunnerException("mixing", 6);
423423
}
424424

425425
@Test
@@ -443,4 +443,14 @@ public void testMultiMultilineString() {
443443
expectGood("fullscreen_export");
444444
}
445445

446+
@Test
447+
public void testStaticClass() {
448+
expectGood("staticclass");
449+
}
450+
451+
@Test
452+
public void testCustomRootClass() {
453+
expectGood("customrootclass");
454+
}
455+
446456
}

java/test/processing/mode/java/ProblemFactoryTest.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ProblemFactoryTest {
1515

1616
private PdePreprocessIssue pdePreprocessIssue;
1717
private List<Integer> tabStarts;
18-
private Editor editor;
18+
1919
private List<Integer> starts;
2020

2121
@Before
@@ -25,26 +25,12 @@ public void setUp() {
2525
tabStarts = new ArrayList<>();
2626
tabStarts.add(5);
2727

28-
editor = Mockito.mock(Editor.class);
29-
Mockito.when(editor.getLineStartOffset(3)).thenReturn(10);
30-
Mockito.when(editor.getLineStopOffset(3)).thenReturn(12);
31-
3228
starts = new ArrayList<>();
3329
starts.add(0);
3430
starts.add(5);
3531
starts.add(10);
3632
}
3733

38-
@Test
39-
public void buildWithEditor() {
40-
Problem problem = ProblemFactory.build(pdePreprocessIssue, tabStarts, 15, editor);
41-
42-
Assert.assertEquals(3, problem.getLineNumber());
43-
Assert.assertEquals("test", problem.getMessage());
44-
Assert.assertEquals(10, problem.getStartOffset());
45-
Assert.assertEquals(11, problem.getStopOffset());
46-
}
47-
4834
@Test
4935
public void buildWithoutEditor() {
5036
Problem problem = ProblemFactory.build(pdePreprocessIssue, tabStarts);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import processing.core.*;
2+
import processing.data.*;
3+
import processing.event.*;
4+
import processing.opengl.*;
5+
6+
import java.util.HashMap;
7+
import java.util.ArrayList;
8+
import java.io.File;
9+
import java.io.BufferedReader;
10+
import java.io.PrintWriter;
11+
import java.io.InputStream;
12+
import java.io.OutputStream;
13+
import java.io.IOException;
14+
15+
public class CustomObj extends PApplet {
16+
17+
public static void main(String[] argv) {
18+
19+
System.out.println("here");
20+
21+
}
22+
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import processing.core.*;
2+
import processing.data.*;
3+
import processing.event.*;
4+
import processing.opengl.*;
5+
6+
import java.util.HashMap;
7+
import java.util.ArrayList;
8+
import java.io.File;
9+
import java.io.BufferedReader;
10+
import java.io.PrintWriter;
11+
import java.io.InputStream;
12+
import java.io.OutputStream;
13+
import java.io.IOException;
14+
15+
public class CustomObj extends PApplet {
16+
17+
public static void main(String[] argv) {
18+
19+
System.out.println("here");
20+
21+
}
22+
23+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import processing.core.*;
2+
import processing.data.*;
3+
import processing.event.*;
4+
import processing.opengl.*;
5+
6+
import java.util.HashMap;
7+
import java.util.ArrayList;
8+
import java.io.File;
9+
import java.io.BufferedReader;
10+
import java.io.PrintWriter;
11+
import java.io.InputStream;
12+
import java.io.OutputStream;
13+
import java.io.IOException;
14+
15+
public class staticclass extends PApplet {
16+
public void setup() {
17+
class Button {
18+
19+
int x, y, radius;
20+
21+
public Button (int x, int y, int radius) {
22+
this.x = x;
23+
this.y = y;
24+
this.radius = radius;
25+
}
26+
27+
public boolean over() {
28+
return dist(mouseX, mouseY, this.x, this.y) < this.radius;
29+
}
30+
31+
public void draw() {
32+
ellipse(this.x, this.y, this.radius * 2, this.radius * 2);
33+
}
34+
35+
}
36+
noLoop();
37+
}
38+
39+
static public void main(String[] passedArgs) {
40+
String[] appletArgs = new String[] { "staticclass" };
41+
if (passedArgs != null) {
42+
PApplet.main(concat(appletArgs, passedArgs));
43+
} else {
44+
PApplet.main(appletArgs);
45+
}
46+
}
47+
}

java/test/resources/staticclass.pde

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Button {
2+
3+
int x, y, radius;
4+
5+
public Button (int x, int y, int radius) {
6+
this.x = x;
7+
this.y = y;
8+
this.radius = radius;
9+
}
10+
11+
boolean over() {
12+
return dist(mouseX, mouseY, this.x, this.y) < this.radius;
13+
}
14+
15+
void draw() {
16+
ellipse(this.x, this.y, this.radius * 2, this.radius * 2);
17+
}
18+
19+
}

0 commit comments

Comments
 (0)