Skip to content

Commit 7757bf6

Browse files
committed
improve class def & deal with default arguments
1 parent 44d4cce commit 7757bf6

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package example
22

3+
class Bonjour() {
4+
}
5+
36
class TestNew extends C {
47
val b = new B
8+
val c = new Bonjour
59
}

semanticdb/src/dotty/semanticdb/SemanticdbConsumer.scala

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class SemanticdbConsumer extends TastyConsumer {
9191
case _ => false
9292
}
9393

94+
def isDefaultGetter: Boolean = symbol.name.contains(tpnme.DEFAULT_GETTER.toString)
95+
9496
def isParameter: Boolean = symbol.flags.is(Flags.Param)
9597

9698
def isObject: Boolean = symbol.flags.is(Flags.Object)
@@ -222,6 +224,7 @@ class SemanticdbConsumer extends TastyConsumer {
222224
def isUseless(implicit ctx: Context): Boolean = {
223225
symbol == NoSymbol ||
224226
//symbol.isInitChild ||
227+
symbol.isDefaultGetter ||
225228
symbol.isWildCard ||
226229
symbol.isAnonymousClass ||
227230
symbol.isAnonymousFunction ||
@@ -303,7 +306,7 @@ class SemanticdbConsumer extends TastyConsumer {
303306
case _ =>
304307
d.Term(symbol.name)
305308
}
306-
} else if (symbol.isMethod) {
309+
} else if (symbol.isMethod || symbol.isUsefulField) {
307310
d.Method(symbol.name,
308311
disimbiguate(previous_symbol + symbol.name, symbol))
309312
} else if (symbol.isTypeParameter) {
@@ -358,23 +361,28 @@ class SemanticdbConsumer extends TastyConsumer {
358361
case _ =>
359362
symbolToSymbolString(symbol)
360363
}
361-
362364
if (symbol_path == "" || symbol.isUselessOccurrence) return
363-
println(symbol_path, range, symbol.owner.flags)
364365

365366
val key = (symbol_path, range)
366-
if (!is_global || !(symbolPathsMap.contains(key))) {
367-
if (is_global) {
368-
symbolPathsMap += key
369-
}
370-
occurrences =
371-
occurrences :+
372-
s.SymbolOccurrence(
373-
Some(range),
374-
symbol_path,
375-
type_symbol
376-
)
377-
}
367+
// TODO: refactor the following
368+
369+
// this is to avoid duplicates symbols
370+
// For example, when we define a class as: `class foo(x: Int)`,
371+
// dotty will generate a ValDef for the x, but the x will also
372+
// be present in the constructor, thus making a double definition
373+
if (symbolPathsMap.contains(key)) return
374+
if (is_global) {
375+
symbolPathsMap += key
376+
println("duplicates", key)
377+
}
378+
println(symbol_path, range, symbol.owner.flags, is_global)
379+
occurrences =
380+
occurrences :+
381+
s.SymbolOccurrence(
382+
Some(range),
383+
symbol_path,
384+
type_symbol
385+
)
378386
}
379387

380388
val reserverdFunctions: List[String] = "apply" :: "unapply" :: Nil
@@ -522,12 +530,6 @@ class SemanticdbConsumer extends TastyConsumer {
522530
override def traversePattern(tree: Pattern)(implicit ctx: Context): Unit = {
523531
tree match {
524532
case Pattern.Bind(name, _) => {
525-
println("[bind]",
526-
tree.pos.startColumn,
527-
tree.pos.endColumn,
528-
tree.symbol,
529-
tree.symbol.pos.startColumn,
530-
tree.symbol.pos.endColumn)
531533
addOccurence(
532534
tree.symbol,
533535
s.SymbolOccurrence.Role.REFERENCE,
@@ -681,7 +683,7 @@ class SemanticdbConsumer extends TastyConsumer {
681683
s.SymbolOccurrence.Role.REFERENCE,
682684
posToRange(tree.pos).get)
683685

684-
case Term.Select(qualifier, _, _) => {
686+
case Term.Select(qualifier, _) => {
685687
val range = {
686688
val r = rangeSelect(tree.symbol.name, tree.pos)
687689
if (tree.symbol.name == "<init>")

semanticdb/test/dotty/semanticdb/Tests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class Tests {
191191
//@Test def testLocals(): Unit = checkFile("example/Locals.scala")
192192
//@Test def testMacroAnnotations(): Unit = checkFile("example/MacroAnnotations.scala")
193193
//WIP(assert) @Test def testMethods(): Unit = checkFile("example/Methods.scala")
194-
//@Test def testMultiArguments(): Unit = checkFile("example/MultiArguments.scala")
194+
@Test def testMultiArguments(): Unit = checkFile("example/MultiArguments.scala")
195195
//@Test def testMethodUsages(): Unit = checkFile("example/MethodUsages.scala")
196196
//def testObjects(): Unit = checkFile("example/Objects.scala")
197197
//@Test def testOverrides(): Unit = checkFile("example/Overrides.scala")
@@ -205,7 +205,7 @@ class Tests {
205205
//@Test def testDependantModule(): Unit = checkFile("example/DependantModule.scala")
206206
//@Test def testNew(): Unit = checkFile("example/New.scala")
207207
//@Test def testIgnoredSymbol(): Unit = checkFile("example/IgnoredSymbol.scala")
208-
@Test def testCase(): Unit = checkFile("example/Case.scala")
208+
//@Test def testCase(): Unit = checkFile("example/Case.scala")
209209

210210

211211
def testOutput(className: String, expected: String): Unit = {

0 commit comments

Comments
 (0)