Skip to content

Commit 8fb3e0c

Browse files
committed
style: improve java import pattern formatting
1 parent 5fa5ec8 commit 8fb3e0c

File tree

3 files changed

+44
-51
lines changed

3 files changed

+44
-51
lines changed

.grit/patterns/java/_test_java_imports.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ tag: [java]
77

88
```grit
99
language java
10+
1011
file($body) where {
11-
$delete_import = `import a.b.Text;`,
12-
$body<: maybe contains remove_import_statement($delete_import)
12+
$delete_import = `import a.b.Text;`,
13+
$body <: maybe contains remove_import_statement($delete_import)
1314
}
1415
```
1516

@@ -89,9 +90,10 @@ public class AClass {
8990

9091
```grit
9192
language java
93+
9294
file($body) where {
93-
$new_import = `import a.b.Text;`,
94-
$body<: maybe contains ensure_import_statement($new_import)
95+
$new_import = `import a.b.Text;`,
96+
$body <: maybe contains ensure_import_statement($new_import)
9597
}
9698
```
9799

@@ -171,6 +173,7 @@ public class AClass {
171173

172174
```grit
173175
language java
176+
174177
replace_import_statement(`import a.b.Text;`, `import a.c.Text;`)
175178
```
176179

@@ -221,6 +224,7 @@ public class AClass {
221224

222225
```grit
223226
language java
227+
224228
remove_duplicate_import_statements()
225229
```
226230

.grit/patterns/java/import.grit

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
language java
22

33
pattern remove_duplicate_import_statements() {
4-
$_ where {
5-
$GLOBAL_REMOVE_DUPLICATE_EXIST_IMPORTS = true,
6-
}
4+
$_ where { $GLOBAL_REMOVE_DUPLICATE_EXIST_IMPORTS = true }
75
}
86

97
pattern ensure_import_statement($import_decl) {
10-
$_ where {
11-
$GLOBAL_EXIST_IMPORTS <: not some $import_decl,
12-
$GLOBAL_EXIST_IMPORTS += $import_decl,
13-
$GLOBAL_NEW_FROM_IMPORTS += $import_decl,
14-
$GLOBAL_NEW_FROM_IMPORTS = distinct($GLOBAL_NEW_FROM_IMPORTS)
15-
}
8+
$_ where {
9+
$GLOBAL_EXIST_IMPORTS <: not some $import_decl,
10+
$GLOBAL_EXIST_IMPORTS += $import_decl,
11+
$GLOBAL_NEW_FROM_IMPORTS += $import_decl,
12+
$GLOBAL_NEW_FROM_IMPORTS = distinct($GLOBAL_NEW_FROM_IMPORTS)
13+
}
1614
}
1715

1816
pattern replace_import_statement($old_import, $new_import) {
19-
$body where {
20-
$find_all_import = [],
21-
$body <: program(statements = $statements),
22-
$statements <: bubble($find_all_import, $old_import, $new_import) every {
23-
or {
24-
bubble($old_import, $new_import, $find_all_import) import_declaration() as $this_import where {
25-
if ($this_import <: $old_import) {
26-
$this_import => `$new_import`
27-
}
28-
},
29-
$_
30-
}
31-
},
32-
}
17+
$body where {
18+
$find_all_import = [],
19+
$body <: program(statements=$statements),
20+
$statements <: bubble($find_all_import, $old_import, $new_import) every {
21+
or {
22+
bubble($old_import, $new_import, $find_all_import) import_declaration() as $this_import where {
23+
if ($this_import <: $old_import) { $this_import => `$new_import` }
24+
},
25+
$_
26+
}
27+
}
28+
}
3329
}
3430

3531
pattern remove_import_statement($import_statement) {
36-
$_ where {
37-
$GLOBAL_REMOVE_IMPORTS += $import_statement
38-
}
32+
$_ where { $GLOBAL_REMOVE_IMPORTS += $import_statement }
3933
}

.grit/patterns/java/java.grit

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,38 @@
11
language java
22

33
// All core stdlib functions can be done here
4-
private pattern before_each_file_stdlib() {
5-
file($body) where {
6-
$body <: init_vars(),
7-
$body <: maybe contains bubble import_declaration() as $this_import where {
8-
$GLOBAL_EXIST_IMPORTS += $this_import,
9-
}
10-
}
4+
private pattern before_each_file_stdlib() {
5+
file($body) where {
6+
$body <: init_vars(),
7+
$body <: maybe contains bubble import_declaration() as $this_import where {
8+
$GLOBAL_EXIST_IMPORTS += $this_import
9+
}
10+
}
1111
}
1212

1313
private pattern after_each_file_stdlib() {
14-
after_rewrite(),
15-
after_each_file_global_rewrites()
14+
after_rewrite(),
15+
after_each_file_global_rewrites()
1616
}
1717

18-
1918
// These could be redefined in the future (not presently supported)
2019
pattern before_each_file() { before_each_file_stdlib() }
2120

2221
pattern after_each_file() { after_each_file_stdlib() }
2322

24-
2523
private pattern after_rewrite() {
26-
file($body) where {
27-
$body <: maybe process_all_import(),
28-
}
24+
file($body) where { $body <: maybe process_all_import() }
2925
}
3026

3127
private pattern init_vars() {
32-
$_ where {
33-
$GLOBAL_EXIST_IMPORTS = [],
34-
$GLOBAL_NEW_FROM_IMPORTS = [],
35-
$GLOBAL_REMOVE_IMPORTS = [],
36-
$GLOABL_REMOVE_DUPLICATE_EXIST_IMPORTS = false,
37-
}
28+
$_ where {
29+
$GLOBAL_EXIST_IMPORTS = [],
30+
$GLOBAL_NEW_FROM_IMPORTS = [],
31+
$GLOBAL_REMOVE_IMPORTS = [],
32+
$GLOABL_REMOVE_DUPLICATE_EXIST_IMPORTS = false
33+
}
3834
}
3935

40-
4136
private pattern process_all_import() {
4237
$body where {
4338
// short-circuit if no changes

0 commit comments

Comments
 (0)