Skip to content

Commit 39be9da

Browse files
authored
Merge pull request #13834 from tgodzik/fix-postfix
Navigate the AST within AnnotatedType and ImportType
2 parents 435207d + 0fec780 commit 39be9da

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

compiler/src/dotty/tools/dotc/ast/NavigateAST.scala

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import core.Contexts._
55
import core.Decorators._
66
import util.Spans._
77
import Trees.{MemberDef, DefTree, WithLazyField}
8+
import dotty.tools.dotc.core.Annotations.ConcreteAnnotation
9+
import dotty.tools.dotc.core.Types.AnnotatedType
10+
import dotty.tools.dotc.core.Types.ImportType
11+
import dotty.tools.dotc.core.Types.Type
812

913
/** Utility functions to go from typed to untyped ASTs */
1014
// TODO: Handle trees with mixed source files
@@ -86,6 +90,18 @@ object NavigateAST {
8690
}
8791
bestFit
8892
}
93+
/*
94+
* Annotations trees are located in the Type
95+
*/
96+
def unpackAnnotations(t: Type, path: List[Positioned]): List[Positioned] =
97+
t match {
98+
case ann: AnnotatedType =>
99+
unpackAnnotations(ann.parent, childPath(ann.annot.tree.productIterator, path))
100+
case imp: ImportType =>
101+
childPath(imp.expr.productIterator, path)
102+
case other =>
103+
path
104+
}
89105
def singlePath(p: Positioned, path: List[Positioned]): List[Positioned] =
90106
if (p.span.exists && !(skipZeroExtent && p.span.isZeroExtent) && p.span.contains(span)) {
91107
// FIXME: We shouldn't be manually forcing trees here, we should replace
@@ -98,7 +114,12 @@ object NavigateAST {
98114
}
99115
childPath(p.productIterator, p :: path)
100116
}
101-
else path
117+
else {
118+
p match {
119+
case t: untpd.TypeTree => unpackAnnotations(t.typeOpt, path)
120+
case _ => path
121+
}
122+
}
102123
singlePath(from, Nil)
103124
}
104125
}

language-server/test/dotty/tools/languageserver/CompletionTest.scala

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,4 +895,55 @@ class CompletionTest {
895895
@Test def i12465_hkt_alias: Unit =
896896
code"""???.asInstanceOf[Seq].${m1}""".withSource
897897
.completion(m1, Set())
898+
899+
@Test def i13624_annotType: Unit =
900+
code"""|object Foo{
901+
| class MyAnnotation extends annotation.StaticAnnotation
902+
|}
903+
|class MyAnnotation extends annotation.StaticAnnotation
904+
|class Annotation2(a: String) extends annotation.StaticAnnotation
905+
|val x = 1: @MyAnnot${m1}
906+
|type X = Int @MyAnnot${m2}
907+
|val y = 1: @Foo.MyAnnot${m3}
908+
|val z = 1: @Foo.MyAnnotation @MyAnno${m4}
909+
|type Y = Int @MyAnnotation @Foo.MyAnnota${m5}
910+
|val w = 1: @Annotation2("abc": @Foo.MyAnnot${m6})
911+
|""".withSource
912+
.completion(
913+
m1,
914+
Set(
915+
("MyAnnotation", Class, "MyAnnotation"),
916+
("MyAnnotation", Module, "MyAnnotation")
917+
)
918+
).completion(
919+
m2,
920+
Set(
921+
("MyAnnotation", Class, "MyAnnotation"),
922+
("MyAnnotation", Module, "MyAnnotation")
923+
)
924+
).completion(
925+
m3,
926+
Set(
927+
("MyAnnotation", Class, "Foo.MyAnnotation"),
928+
("MyAnnotation", Module, "Foo.MyAnnotation")
929+
)
930+
).completion(
931+
m4,
932+
Set(
933+
("MyAnnotation", Class, "MyAnnotation"),
934+
("MyAnnotation", Module, "MyAnnotation")
935+
)
936+
).completion(
937+
m5,
938+
Set(
939+
("MyAnnotation", Class, "Foo.MyAnnotation"),
940+
("MyAnnotation", Module, "Foo.MyAnnotation")
941+
)
942+
).completion(
943+
m6,
944+
Set(
945+
("MyAnnotation", Class, "Foo.MyAnnotation"),
946+
("MyAnnotation", Module, "Foo.MyAnnotation")
947+
)
948+
)
898949
}

language-server/test/dotty/tools/languageserver/HoverTest.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class HoverTest {
177177

178178
@Test def i4678: Unit = {
179179
code"""class Foo {
180-
| val x: Int = (${m1}1:${m2} ${m3}@annot1 @annot2 @annot3 @annot4 @annot5${m4})
180+
| val x: Int = (${m1}1:${m2} ${m3}@annot1${m4} ${m5}@annot2${m6} ${m7}@annot3${m8} ${m9}@annot4${m10} ${m11}@annot5${m12})
181181
|}
182182
|class annot1 extends scala.annotation.Annotation
183183
|class annot2 extends scala.annotation.Annotation
@@ -186,7 +186,11 @@ class HoverTest {
186186
|class annot5 extends scala.annotation.Annotation
187187
|""".withSource
188188
.hover(m1 to m2, hoverContent("(1 : Int)"))
189-
.hover(m3 to m4, hoverContent("(1 : Int) @annot1 @annot2 @annot3 @annot4 @annot5"))
189+
.hover(m3 to m4, hoverContent("annot1"))
190+
.hover(m5 to m6, hoverContent("annot2"))
191+
.hover(m7 to m8, hoverContent("annot3"))
192+
.hover(m9 to m10, hoverContent("annot4"))
193+
.hover(m11 to m12, hoverContent("annot5"))
190194
}
191195

192196
@Test def unicodeChar: Unit = {

0 commit comments

Comments
 (0)