@@ -4,7 +4,9 @@ language java
44private pattern before_each_file_stdlib() {
55 file($body) where {
66 $body <: init_vars(),
7- $body <: init_import()
7+ $body <: maybe contains bubble import_declaration() as $this_import where {
8+ $GLOBAL_EXIST_IMPORTS += $this_import,
9+ }
810 }
911}
1012
@@ -26,7 +28,6 @@ private pattern after_rewrite() {
2628 }
2729}
2830
29-
3031private pattern init_vars() {
3132 $_ where {
3233 $GLOBAL_EXIST_IMPORTS = [],
@@ -36,60 +37,47 @@ private pattern init_vars() {
3637 }
3738}
3839
39- private pattern init_import() {
40- $_ where {
41- $program <: accumulate_all_imports(),
42- }
43- }
44-
45- private pattern accumulate_all_imports() {
46- $_ where {
47- $program <: maybe contains bubble() import_declaration() as $this_import where {
48- $GLOBAL_EXIST_IMPORTS += text(`$this_import`),
49- },
50- }
51- }
5240
5341private pattern process_all_import() {
5442 $body where {
43+ // short-circuit if no changes
44+ or {
45+ $GLOBAL_NEW_FROM_IMPORTS <: not [],
46+ $GLOBAL_REMOVE_IMPORTS <: not [],
47+ $GLOBAL_REMOVE_DUPLICATE_EXIST_IMPORTS <: true,
48+ },
5549 $last_import = "",
5650 $exists_imports = [],
51+ $body <: contains package_declaration() as $pkg_decl,
52+ // no existing imports, just add all new imports
53+ $all_import = join(distinct($GLOBAL_NEW_FROM_IMPORTS), `\n`),
5754 if ($body <: not contains bubble() import_declaration()) {
58- $body <: contains bubble() package_declaration() as $pkg_decl where {
59- $all_import = join(distinct($GLOBAL_NEW_FROM_IMPORTS), `\n`),
60- if ($all_import <: not ``) {
61- $pkg_decl => `$pkg_decl\n\n$all_import`
62- }
55+ if ($all_import <: not ``) {
56+ $pkg_decl => `$pkg_decl\n\n$all_import`
6357 }
6458 }
6559 else {
66- $body <: contains bubble($last_import, $exists_imports) import_declaration() as $bef where {
67- $last_import = $bef ,
68- if ($exists_imports <: some $bef ) {
60+ $body <: contains bubble($last_import, $exists_imports) import_declaration() as $this_import where {
61+ $last_import = $this_import ,
62+ if ($exists_imports <: some $this_import ) {
6963 // duplicate import, remove it
7064 if ($GLOBAL_REMOVE_DUPLICATE_EXIST_IMPORTS <: true ) {
71- $bef => .
65+ $this_import => .
7266 }
7367 } else {
74- if ($GLOBAL_REMOVE_IMPORTS <: some $bef ) {
75- $bef => .
68+ if ($GLOBAL_REMOVE_IMPORTS <: some $this_import ) {
69+ $this_import => .
7670 },
77- $exists_imports += text($bef )
71+ $exists_imports += text($this_import )
7872 }
7973 },
80- $all_import = join(distinct($GLOBAL_NEW_FROM_IMPORTS), `\n`),
8174 // last_import need to be deleted if in remove imports
8275 if ($GLOBAL_REMOVE_IMPORTS <: some $last_import) {
8376 $last_import => `$all_import`
8477 } else if ($last_import <: not ""){
8578 if ($all_import <: not ``) {
8679 $last_import => `$last_import\n$all_import`
8780 }
88- } else {
89- $body <: contains bubble($last_import) package_declaration() as $pkg_decl,
90- if ($all_import <: not ``) {
91- $pkg_decl => `$pkg_decl\n$all_import`
92- }
9381 }
9482 }
9583 }
0 commit comments