@@ -3,6 +3,7 @@ package scalafix.internal.patch
3
3
import scala .meta ._
4
4
import scala .meta .internal .trees ._
5
5
6
+ import scalafix .internal .util .SymbolOps
6
7
import scalafix .internal .util .SymbolOps .Root
7
8
import scalafix .internal .util .SymbolOps .SignatureName
8
9
import scalafix .patch .Patch
@@ -38,14 +39,19 @@ object ReplaceSymbolOps {
38
39
(Symbol .Global (qual, Signature .Type (name)).syntax -> to) ::
39
40
Nil
40
41
}.toMap
41
- def loop (ref : Ref , sym : Symbol ): (Patch , Symbol ) = {
42
+ def loop (ref : Ref , sym : Symbol , isImport : Boolean ): (Patch , Symbol ) = {
42
43
(ref, sym) match {
43
44
// same length
44
45
case (a @ Name (_), Symbol .Global (Symbol .None , SignatureName (b))) =>
45
46
ctx.replaceTree(a, b) -> Symbol .None
46
47
// ref is shorter
47
- case (a @ Name (_), sym @ Symbol .Global (_, SignatureName (b))) =>
48
- ctx.replaceTree(a, b) -> sym
48
+ case (a @ Name (_), sym @ Symbol .Global (owner, SignatureName (b))) =>
49
+ if (isImport) {
50
+ val qual = SymbolOps .toTermRef(sym)
51
+ ctx.replaceTree(a, qual.syntax) -> Symbol .None
52
+ } else {
53
+ ctx.replaceTree(a, b) -> sym
54
+ }
49
55
// ref is longer
50
56
case (
51
57
Select (qual, Name (_)),
@@ -57,7 +63,7 @@ object ReplaceSymbolOps {
57
63
Select (qual : Ref , a @ Name (_)),
58
64
Symbol .Global (symQual, SignatureName (b))
59
65
) =>
60
- val (patch, toImport) = loop(qual, symQual)
66
+ val (patch, toImport) = loop(qual, symQual, isImport )
61
67
(patch + ctx.replaceTree(a, b)) -> toImport
62
68
}
63
69
}
@@ -79,6 +85,13 @@ object ReplaceSymbolOps {
79
85
result
80
86
}
81
87
}
88
+ object Identifier {
89
+ def unapply (tree : Tree ): Option [(Name , Symbol )] = tree match {
90
+ case n : Name => n.symbol.map(s => n -> s)
91
+ case Init (n : Name , _, _) => n.symbol.map(s => n -> s)
92
+ case _ => None
93
+ }
94
+ }
82
95
val patches = ctx.tree.collect { case n @ Move (to) =>
83
96
// was this written as `to = "blah"` instead of `to = _root_.blah`
84
97
val isSelected = to match {
@@ -88,14 +101,37 @@ object ReplaceSymbolOps {
88
101
n.parent match {
89
102
case Some (i @ Importee .Name (_)) =>
90
103
ctx.removeImportee(i)
104
+ case Some (i @ Importee .Rename (name, rename)) =>
105
+ i.parent match {
106
+ case Some (Importer (ref, `i` :: Nil )) =>
107
+ Patch .replaceTree(ref, SymbolOps .toTermRef(to.owner).syntax) +
108
+ Patch .replaceTree(name, to.signature.name)
109
+ case Some (Importer (ref, _)) =>
110
+ Patch .removeImportee(i) +
111
+ Patch .addGlobalImport(
112
+ Importer (
113
+ SymbolOps .toTermRef(to.owner),
114
+ List (
115
+ Importee .Rename (Term .Name (to.signature.name), rename)
116
+ )
117
+ )
118
+ )
119
+ case _ => Patch .empty
120
+ }
91
121
case Some (parent @ Select (_, `n`)) if isSelected =>
92
- val (patch, imp) = loop(parent, to)
122
+ val (patch, imp) =
123
+ loop(parent, to, isImport = n.parents.exists(_.is[Import ]))
93
124
ctx.addGlobalImport(imp) + patch
94
- case Some (_) =>
125
+ case Some (Identifier (parent, Symbol .Global (_, sig)))
126
+ if sig.name != parent.value =>
127
+ Patch .empty // do nothing because it was a renamed symbol
128
+ case Some (parent) =>
95
129
val addImport =
96
130
if (n.isDefinition) Patch .empty
97
131
else ctx.addGlobalImport(to)
98
132
addImport + ctx.replaceTree(n, to.signature.name)
133
+ case _ =>
134
+ Patch .empty
99
135
}
100
136
}
101
137
patches.asPatch
0 commit comments