Skip to content

Commit db26915

Browse files
authored
Add runnables queries (#79)
This PR adds run options to run main classes and tests via the IDE. ~One problem I wasnt able to overcome was me not being able to dynamically decide whether to use maven or gradle to run.~ ~An option would be to ship a script that, proxys maven and gradle and decides which one to use. But i don't know how elegant that would be. The Solution rn is just to show both options always.~
1 parent 6b56755 commit db26915

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

languages/java/runnables.scm

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
; Run the main function
2+
(
3+
(package_declaration
4+
(scoped_identifier) @java_package_name
5+
)
6+
(class_declaration
7+
(modifiers) @class-modifier
8+
(#match? @class-modifier "public")
9+
name: (identifier) @java_class_name
10+
body: (class_body
11+
(method_declaration
12+
(modifiers) @modifier
13+
name: (identifier) @run
14+
(#eq? @run "main")
15+
(#match? @modifier "public")
16+
(#match? @modifier "static")
17+
)
18+
)
19+
) @_
20+
(#set! tag java-main)
21+
)
22+
23+
; Run the main class
24+
(
25+
(package_declaration
26+
(scoped_identifier) @java_package_name
27+
)
28+
(class_declaration
29+
(modifiers) @class-modifier
30+
(#match? @class-modifier "public")
31+
name: (identifier) @java_class_name @run
32+
body: (class_body
33+
(method_declaration
34+
(modifiers) @modifier
35+
name: (identifier) @method_name
36+
(#eq? @method_name "main")
37+
(#match? @modifier "public")
38+
(#match? @modifier "static")
39+
)
40+
)
41+
) @_
42+
(#set! tag java-main)
43+
)
44+
45+
; Run test function
46+
(
47+
(package_declaration
48+
(scoped_identifier) @java_package_name
49+
)
50+
(class_declaration
51+
name: (identifier) @java_class_name
52+
body: (class_body
53+
(method_declaration
54+
(modifiers
55+
(marker_annotation
56+
name: (identifier) @annotation_name
57+
)
58+
)
59+
name: (identifier) @run @java_method_name
60+
(#eq? @annotation_name "Test")
61+
)
62+
)
63+
) @_
64+
(#set! tag java-test-method)
65+
)
66+
67+
; Run test class
68+
(
69+
(package_declaration
70+
(scoped_identifier) @java_package_name
71+
)
72+
(class_declaration
73+
name: (identifier) @java_class_name @run
74+
body: (class_body
75+
(method_declaration
76+
(modifiers
77+
(marker_annotation
78+
name: (identifier) @annotation_name
79+
)
80+
)
81+
(#eq? @annotation_name "Test")
82+
)
83+
)
84+
) @_
85+
(#set! tag java-test-class)
86+
)

languages/java/tasks.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[
2+
{
3+
"label": "Run $ZED_CUSTOM_java_class_name",
4+
"command": "c=\"$ZED_CUSTOM_java_package_name.$ZED_CUSTOM_java_class_name\"; if [ -f pom.xml ]; then mvn clean compile exec:java -Dexec.mainClass=$c; elif [ -f gradlew ]; then ./gradlew run -PmainClass=$c; else >&2 echo 'No build system found'; exit 1; fi;",
5+
"use_new_terminal": false,
6+
"reveal": "always",
7+
"tags": ["java-main"]
8+
},
9+
{
10+
"label": "Test $ZED_CUSTOM_java_class_name.$ZED_CUSTOM_java_method_name",
11+
"command": "c=\"$ZED_CUSTOM_java_package_name.$ZED_CUSTOM_java_class_name\"; m=\"$ZED_CUSTOM_java_method_name\"; if [ -f pom.xml ]; then mvn clean test -Dtest=\"$c#$m\"; elif [ -f gradlew ]; then ./gradlew test --tests $c.$m; else >&2 echo 'No build system found'; exit 1; fi;",
12+
"use_new_terminal": false,
13+
"reveal": "always",
14+
"tags": ["java-test-method"]
15+
},
16+
{
17+
"label": "Test class $ZED_CUSTOM_java_class_name",
18+
"command": "c=\"$ZED_CUSTOM_java_package_name.$ZED_CUSTOM_java_class_name\"; if [ -f pom.xml ]; then mvn clean test -Dtest=\"$c\"; elif [ -f gradlew ]; then ./gradlew test --tests $c; else >&2 echo 'No build system found'; exit 1; fi;",
19+
"use_new_terminal": false,
20+
"reveal": "always",
21+
"tags": ["java-test-class"]
22+
},
23+
{
24+
"label": "Run tests",
25+
"command": "if [ -f pom.xml ]; then mvn clean test; elif [ -f gradlew ]; then ./gradlew test; else >&2 echo 'No build system found'; exit 1; fi;",
26+
"use_new_terminal": false,
27+
"reveal": "always"
28+
}
29+
]

0 commit comments

Comments
 (0)