Skip to content

Commit 8a83849

Browse files
committed
Handle correct positions of reversed binary operators (eg #::)
1 parent 98ff433 commit 8a83849

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

semanticdb/input/src/main/scala/example/BinaryOp.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ package example
22

33
class BinaryOp {
44
val y = 1 #:: 2 #:: Stream.empty[Int]
5-
val x = 1 + 2 + 3
5+
val x = 1 :: 2 :: 3 :: Nil
6+
val z = 1 + 2
67
}

semanticdb/src/dotty/semanticdb/SemanticdbConsumer.scala

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
423423
symbolToSymbolString(symbol)
424424
}
425425

426-
println(symbol_path)
426+
println(symbol_path + " : " + range)
427427
// We want to add symbols coming from our file
428428
// if (symbol.pos.sourceFile != sourceFile) return
429429
if (symbol_path == "" || symbol.isUselessOccurrence) return
@@ -533,10 +533,26 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
533533
val len =
534534
if (name == "<init>") 0
535535
else name.length
536-
return s.Range(range.endLine,
537-
range.endColumn - len,
538-
range.endLine,
539-
range.endColumn)
536+
/* The position of a select is the position of the whole select expression,
537+
from the start to the end.
538+
To get the position of only the selected operand, we distinguish two cases:
539+
- either we are selecting an operator ending with a ':' (for those the execution
540+
order is reversed), so the selected expression is at the start.
541+
Ex: A #:: B -> the position of the select is the range "#:: B", so we pick the range "#::"
542+
- either the select is in normal order, in this case we select the end of it.
543+
Ex: A + B -> the position of the select is the range "A +", so we pick the range "+"
544+
*/
545+
if (name.endsWith(":")) {
546+
return s.Range(range.startLine,
547+
range.startColumn,
548+
range.startLine,
549+
range.startColumn + len)
550+
} else {
551+
return s.Range(range.endLine,
552+
range.endColumn - len,
553+
range.endLine,
554+
range.endColumn)
555+
}
540556
}
541557

542558
def getImportPath(path_term: Term): String = {
@@ -831,7 +847,6 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
831847
}
832848

833849
case Term.Select(qualifier, _) => {
834-
println("SELECT => " + tree)
835850
val range = {
836851
val r = rangeSelect(tree.symbol.trueName, tree.pos)
837852
if (tree.symbol.trueName == "<init>")
@@ -841,6 +856,7 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
841856
r.endCharacter + 1)
842857
else r
843858
}
859+
println("SELECT => " + tree + tree.pos.start + ":" + tree.pos.end)
844860
addOccurenceTree(tree, s.SymbolOccurrence.Role.REFERENCE, range, forceAddBecauseParents)
845861
super.traverseTree(tree)
846862
}

semanticdb/test/dotty/semanticdb/Tests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ class Tests {
116116
@Test def testSuper(): Unit = checkFile("example/Super.scala")
117117
@Test def testTypeBug(): Unit = checkFile("example/TypeBug.scala")
118118
@Test def testSynthetic(): Unit = checkFile("example/Synthetic.scala")
119-
@Test def testBinaryOp(): Unit = checkFile("example/BinaryOp.scala") // Failure
119+
@Test def testBinaryOp(): Unit = checkFile("example/BinaryOp.scala")
120120
@Test def testDottyPredef(): Unit = checkFile("example/DottyPredef.scala")
121121
*/
122-
122+
@Test def testBinaryOp(): Unit = checkFile("example/BinaryOp.scala")
123123
}

0 commit comments

Comments
 (0)