Skip to content

Commit 44e7651

Browse files
committed
symbol generations for terms and types
1 parent b4fc5b3 commit 44e7651

File tree

5 files changed

+98
-33
lines changed

5 files changed

+98
-33
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ trait PositionOpsImpl extends scala.tasty.reflect.PositionOps with CoreImpl {
66
def start: Int = pos.start
77
def end: Int = pos.end
88

9+
def exists: Boolean = pos.exists
10+
911
def sourceFile: java.nio.file.Path = pos.source.file.jpath
1012

1113
def startLine: Int = pos.startLine

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ trait TypeOrBoundsTreesOpsImpl extends scala.tasty.reflect.TypeOrBoundsTreeOps w
3131

3232
object TypeTree extends TypeTreeModule {
3333

34+
def symbol(x: TypeTree)(implicit ctx: Context): Symbol =
35+
x.symbol
3436
object Synthetic extends SyntheticExtractor {
3537
def unapply(x: TypeTree)(implicit ctx: Context): Boolean = x match {
3638
case x @ Trees.TypeTree() => !x.tpe.isInstanceOf[Types.TypeBounds]

library/src/scala/tasty/reflect/PositionOps.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ trait PositionOps extends Core {
44

55
trait PositionAPI {
66

7-
/** The path of source file */
7+
def exists: Boolean
8+
89
def sourceFile: java.nio.file.Path
910

1011
/** The start index in the source file */

library/src/scala/tasty/reflect/TypeOrBoundsTreeOps.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ trait TypeOrBoundsTreeOps extends Core {
2828
val TypeTree: TypeTreeModule
2929
abstract class TypeTreeModule {
3030

31+
def symbol(x: TypeTree)(implicit ctx: Context): Symbol
32+
3133
/** TypeTree containing an inferred type */
3234
val Synthetic: SyntheticExtractor
3335
abstract class SyntheticExtractor {

semanticdb/src/dotty/semanticdb/SemanticdbConsumer.scala

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,17 @@ class SemanticdbConsumer extends TastyConsumer {
7979
} else {
8080
val previous_symbol = iterateParent(symbol.owner)
8181
val next_atom =
82-
symbol match {
83-
case IsPackageSymbol(symbol) => symbol.name + "/"
84-
case IsClassSymbol(symbol) => symbol.name + "#"
85-
case IsDefSymbol(symbol) =>
86-
symbol.name + disimbiguate(previous_symbol + symbol.name) + "."
87-
case IsValSymbol(symbol) => symbol.name + "."
88-
case owner => {
89-
""
82+
if (symbol.flags.isParam) "(" + symbol.name + ")"
83+
else {
84+
symbol match {
85+
case IsPackageSymbol(symbol) => symbol.name + "/"
86+
case IsClassSymbol(symbol) => symbol.name + "#"
87+
case IsDefSymbol(symbol) =>
88+
symbol.name + disimbiguate(previous_symbol + symbol.name) + "."
89+
case IsValSymbol(symbol) => symbol.name + "."
90+
case owner => {
91+
""
92+
}
9093
}
9194
}
9295
previous_symbol + next_atom
@@ -96,40 +99,95 @@ class SemanticdbConsumer extends TastyConsumer {
9699
}
97100
}
98101

99-
override def traverseTree(tree: Tree)(implicit ctx: Context): Unit = {
100-
val previous_path = stack.head
102+
def addOccurence(symbol: Symbol,
103+
type_symbol: s.SymbolOccurrence.Role,
104+
range: s.Range): Unit = {
105+
//if (symbolsCache.contains(symbol)) return
106+
107+
val symbol_path = iterateParent(symbol)
108+
if (symbol_path == "" || symbol.name == "<init>") return
109+
110+
//println(symbol_path, symbol, range)
111+
occurrences =
112+
occurrences :+
113+
s.SymbolOccurrence(
114+
Some(range),
115+
symbol_path,
116+
type_symbol
117+
)
118+
}
119+
120+
def range(pos: Position, name: String): s.Range = {
121+
val range_end_column =
122+
if (name == "<init>") {
123+
pos.endColumn
124+
} else {
125+
pos.startColumn + name.length
126+
}
127+
128+
s.Range(pos.startLine, pos.startColumn, pos.startLine, range_end_column)
129+
}
130+
131+
def rangeExclude(range: Position, exclude: Position): s.Range = {
132+
def max(a: Int, b: Int): Int = { if (a > b) a else b }
133+
return s.Range(max(range.startLine, exclude.startLine),
134+
max(range.startColumn, exclude.startColumn) + 1,
135+
range.endLine,
136+
range.endColumn)
137+
}
101138

139+
override def traverseTree(tree: Tree)(implicit ctx: Context): Unit = {
140+
//println(tree.pos.startColumn, tree.symbol.name, tree.pos.endColumn)
102141
tree match {
103-
case IsDefinition(body) =>
104-
//println("[definition] ", body)
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)
142+
case IsDefinition(body) => {
143+
144+
def typetreeSymbol(typetree: TypeTree): Unit =
145+
typetree match {
146+
case TypeTree.Synthetic => ()
147+
case _ =>
148+
println(tree.symbol,
149+
typetree,
150+
iterateParent(TypeTree.symbol(typetree)))
151+
addOccurence(
152+
TypeTree.symbol(typetree),
153+
s.SymbolOccurrence.Role.REFERENCE,
154+
range(typetree.pos, TypeTree.symbol(typetree).name))
118155
}
119-
occurrences =
120-
occurrences :+
121-
s.SymbolOccurrence(
122-
Some(range),
123-
symbol_path,
124-
s.SymbolOccurrence.Role.DEFINITION
125-
)
156+
157+
val _ = tree match {
158+
case DefDef(_, _, _, typetree, _) => typetreeSymbol(typetree)
159+
case ValDef(_, typetree, _) => typetreeSymbol(typetree)
160+
case _ => ()
161+
}
162+
163+
addOccurence(tree.symbol,
164+
s.SymbolOccurrence.Role.DEFINITION,
165+
range(tree.symbol.pos, tree.symbol.name))
166+
126167
super.traverseTree(body)
168+
}
169+
170+
case Term.Select(qualifier, _, _) => {
171+
val range = rangeExclude(tree.pos, qualifier.pos)
172+
addOccurence(tree.symbol, s.SymbolOccurrence.Role.REFERENCE, range)
173+
super.traverseTree(tree)
174+
}
175+
176+
case Term.Ident(body) => {
177+
//println(tree.pos.startColumn, tree.pos.endColumn)
178+
//println(tree.namePos.startColumn, tree.namePos.endColumn)
179+
addOccurence(tree.symbol,
180+
s.SymbolOccurrence.Role.REFERENCE,
181+
range(tree.pos, tree.symbol.name))
182+
super.traverseTree(tree)
183+
}
127184
case tree =>
128185
super.traverseTree(tree)
129186
}
130187
}
131188

132189
}
190+
133191
Traverser.traverseTree(root)(reflect.rootContext)
134192
}
135193

0 commit comments

Comments
 (0)