Skip to content

Commit bf7b455

Browse files
committed
Navigate the AST within AnnotatedType and ImportType
Related to #13624
1 parent 968dd1b commit bf7b455

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
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: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,4 +895,21 @@ class CompletionTest {
895895
@Test def i12465_hkt_alias: Unit =
896896
code"""???.asInstanceOf[Seq].${m1}""".withSource
897897
.completion(m1, Set())
898-
}
898+
899+
@Test def i13624_annotType: Unit =
900+
code"""|class MyAnnotation extends annotation.StaticAnnotation
901+
|val x = 1: @MyAnnot${m1}
902+
|type X = Int @MyAnnot${m2}""".withSource
903+
.completion(
904+
m1,
905+
Set(
906+
("MyAnnotation", Class, "MyAnnotation"),
907+
("MyAnnotation", Module, "MyAnnotation")
908+
)
909+
).completion(
910+
m2,
911+
Set(
912+
("MyAnnotation", Class, "MyAnnotation"),
913+
("MyAnnotation", Module, "MyAnnotation")
914+
)
915+
)}

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)