Skip to content

Commit b4fc5b3

Browse files
committed
add namePos to tastyreflect symbol and start generating valid semanticdb
1 parent ba36954 commit b4fc5b3

File tree

6 files changed

+43
-58
lines changed

6 files changed

+43
-58
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/PositionOpsImpl.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@ trait PositionOpsImpl extends scala.tasty.reflect.PositionOps with CoreImpl {
1414
def startColumn: Int = pos.startColumn
1515
def endColumn: Int = pos.endColumn
1616
}
17-
1817
}

compiler/src/dotty/tools/dotc/tastyreflect/SymbolOpsImpl.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with CoreImpl {
2626
def name(implicit ctx: Context): String = symbol.name.toString
2727
def fullName(implicit ctx: Context): String = symbol.fullName.toString
2828

29+
def pos(implicit ctx: Context): Position = symbol.pos
30+
2931
def owner(implicit ctx: Context): Symbol = symbol.owner
3032

3133
def localContext(implicit ctx: Context): Context = {

library/src/scala/tasty/reflect/SymbolOps.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ trait SymbolOps extends Core {
2626
/** The full name of this symbol up to the root package. */
2727
def fullName(implicit ctx: Context): String
2828

29-
/** Returns the context within this symbol. */
29+
def pos(implicit ctx: Context): Position
30+
3031
def localContext(implicit ctx: Context): Context
3132

3233
/** Unsafe cast as to PackageSymbol. Use IsPackageSymbol to safly check and cast to PackageSymbol */

project/Build.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ object Build {
393393
baseDirectory in Test := baseDirectory.value / "..",
394394
unmanagedSourceDirectories in Test += baseDirectory.value / "input" / "src" / "main" / "scala",
395395
libraryDependencies ++= List(
396+
("org.scalameta" %% "semanticdb" % "4.0.0" % Compile).withDottyCompat(scalaVersion.value),
397+
"com.novocode" % "junit-interface" % "0.11" % Compile,
398+
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0" % Compile,
396399
("org.scalameta" %% "semanticdb" % "4.0.0" % Test).withDottyCompat(scalaVersion.value),
397400
"com.novocode" % "junit-interface" % "0.11" % Test,
398401
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0" % Test

semanticdb/src/dotty/semanticdb/SemanticdbConsumer.scala

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import scala.tasty.file.TastyConsumer
66
import scala.tasty.util.TreeTraverser
77
import dotty.tools.dotc.tastyreflect
88
import scala.collection.mutable.HashMap
9+
import scala.meta.internal.{semanticdb => s}
910

1011
class SemanticdbConsumer extends TastyConsumer {
1112
var stack: List[String] = Nil
@@ -32,6 +33,12 @@ class SemanticdbConsumer extends TastyConsumer {
3233
""
3334
}
3435
}*/
36+
val semantic: s.TextDocument = s.TextDocument()
37+
var occurrences: Seq[s.SymbolOccurrence] = Seq()
38+
39+
def toSemanticdb(text: String): s.TextDocument = {
40+
s.TextDocument(text = text, occurrences = occurrences)
41+
}
3542

3643
final def apply(reflect: Reflection)(root: reflect.Tree): Unit = {
3744
import reflect._
@@ -65,6 +72,10 @@ class SemanticdbConsumer extends TastyConsumer {
6572
// TODO had a "NoDenotation" test to avoid
6673
// relying on the name itself
6774
""
75+
} else if (symbol.name == "<root>") then {
76+
// TODO had a "NoDenotation" test to avoid
77+
// relying on the name itself
78+
""
6879
} else {
6980
val previous_symbol = iterateParent(symbol.owner)
7081
val next_atom =
@@ -89,63 +100,30 @@ class SemanticdbConsumer extends TastyConsumer {
89100
val previous_path = stack.head
90101

91102
tree match {
92-
/*case IsClassDef(body) =>
93-
val ClassDef(name, _, _, _, _) = body
94-
//println("[classdef] ", body)
95-
val path = stack.head + name + "#"
96-
println(path)
97-
stack = path :: stack
98-
super.traverseTree(body)
99-
stack = stack.tail
100-
case IsTypeDef(body) =>
101-
println("[typedef] ", body)
102-
super.traverseTree(body)
103-
case IsDefDef(body) =>
104-
val DefDef(name, _, _, _, _) = body
105-
val def_atom =
106-
name match {
107-
case "<init>" => "`<init>`"
108-
case _ => name
109-
}
110-
val path_repr = stack.head + def_atom
111-
val path = path_repr + "(" + insertPathDefDef(path_repr) + ")."
112-
println(path)
113-
//println("[defdef] ", body)
114-
stack = path :: stack
115-
super.traverseTree(body)
116-
stack = stack.tail
117-
case IsValDef(body) =>
118-
val ValDef(name, _, _) = body
119-
val path_repr = stack.head + name
120-
val path = path_repr + "(" + insertPathValDef(path_repr) + ")"
121-
println(path)
122-
//println("[defdef] ", body)
123-
stack = path :: stack
124-
super.traverseTree(body)
125-
stack = stack.tail
126-
case IsPackageDef(body) =>
127-
println("[packagedef] ", body)
128-
super.traverseTree(body)*/
129103
case IsDefinition(body) =>
130104
//println("[definition] ", body)
131-
println(iterateParent(tree.symbol))
132-
super.traverseTree(body)
133-
/*case IsPackageClause(body) =>
134-
//println(body.pos.start, body.pos.end)
135-
val PackageClause(name, list_tree : List[Tree]) = body
136-
//println(tree)
137-
val path = packageDefToOccurence(name)
138-
println(path)
139-
stack = path :: stack
140-
// call to traverse tree instead of super.traverseTree to avoid
141-
// skipping this child entirely (super.xx will traverse subtrees)
142-
//list_tree.foreach{traverseTree}
143-
/*case IsTerm(body) =>
144-
//println("[term] ", body)
145-
super.traverseTree(body)*/
146-
// iterating this way will probably make us see terms we don't want
105+
val symbol_path = iterateParent(tree.symbol)
106+
107+
val range =
108+
if (tree.symbol.name == "<init>") {
109+
s.Range(tree.symbol.pos.startLine,
110+
tree.symbol.pos.startColumn,
111+
tree.symbol.pos.startLine,
112+
tree.symbol.pos.endColumn)
113+
} else {
114+
s.Range(tree.symbol.pos.startLine,
115+
tree.symbol.pos.startColumn,
116+
tree.symbol.pos.startLine,
117+
tree.symbol.pos.startColumn + tree.symbol.name.length)
118+
}
119+
occurrences =
120+
occurrences :+
121+
s.SymbolOccurrence(
122+
Some(range),
123+
symbol_path,
124+
s.SymbolOccurrence.Role.DEFINITION
125+
)
147126
super.traverseTree(body)
148-
stack = stack.tail*/
149127
case tree =>
150128
super.traverseTree(tree)
151129
}

semanticdb/test/dotty/semanticdb/Tests.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ class Tests {
3434
case pat(name) => name
3535
case _ => ""
3636
}
37-
val _ = ConsumeTasty(classpath, (modulename + "." + sourcename) :: Nil, new SemanticdbConsumer)
38-
return s.TextDocument(text = scalac.text)
37+
val sdbconsumer = new SemanticdbConsumer
38+
val _ = ConsumeTasty(classpath, (modulename + "." + sourcename) :: Nil, sdbconsumer)
39+
sdbconsumer.toSemanticdb(scalac.text)
40+
3941
}
4042

4143
/** Fails the test if the s.TextDocument from tasty and semanticdb-scalac are not the same. */

0 commit comments

Comments
 (0)