Skip to content

Commit fbc6cde

Browse files
committed
implement FIXME from previous commit
1 parent ecfc869 commit fbc6cde

File tree

2 files changed

+17
-58
lines changed

2 files changed

+17
-58
lines changed

presentation-compiler/src/main/dotty/tools/pc/SelectionRangeProvider.scala

Lines changed: 17 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.util as ju
66
import scala.jdk.CollectionConverters._
77
import scala.meta.pc.OffsetParams
88

9+
import dotty.tools.dotc.core.Comments.Comment
910
import dotty.tools.dotc.core.Contexts.Context
1011
import dotty.tools.dotc.interactive.Interactive
1112
import dotty.tools.dotc.interactive.InteractiveDriver
@@ -36,7 +37,7 @@ class SelectionRangeProvider(
3637
def selectionRange(): List[SelectionRange] =
3738
given ctx: Context = driver.currentCtx
3839

39-
val selectionRanges = params.asScala.toList.map { param =>
40+
params.asScala.toList.map { param =>
4041

4142
val uri = param.uri
4243
val filePath = Paths.get(uri)
@@ -53,10 +54,22 @@ class SelectionRangeProvider(
5354
selectionRange
5455
}
5556

56-
bareRanges.reduceRight(setParent)
57-
}
57+
val comments = driver.compilationUnits.get(uri).map(_.comments).toList.flatten
58+
59+
val commentRanges = comments.find(_.span.contains(pos.span)).map { comment =>
60+
val startLine = source.offsetToLine(comment.span.start)
61+
val endLine = source.offsetToLine(comment.span.end)
62+
val startChar = source.column(comment.span.start)
63+
val endChar = source.column(comment.span.end)
5864

59-
selectionRanges
65+
new SelectionRange():
66+
setRange(new lsp4j.Range(lsp4j.Position(startLine, startChar), lsp4j.Position(endLine, endChar)))
67+
}.toList
68+
69+
(commentRanges ++ bareRanges)
70+
.reduceRightOption(setParent)
71+
.getOrElse(new SelectionRange())
72+
}
6073
end selectionRange
6174

6275
private def setParent(
@@ -88,55 +101,3 @@ class SelectionRangeProvider(
88101
child
89102

90103
end SelectionRangeProvider
91-
92-
// FIXME: update with latest mtags implementation, requires rewrite to dotty because fo scalameta
93-
94-
// object SelectionRangeProvider:
95-
96-
// import dotty.tools.dotc.ast.tpd
97-
98-
// def commentRangesFromTokens(
99-
// tokenList: List[Token],
100-
// cursorStart: SourcePosition,
101-
// offsetStart: Int
102-
// ) =
103-
// val cursorStartShifted = cursorStart.start - offsetStart
104-
105-
// tokenList
106-
// .collect { case x: Comment =>
107-
// (x.start, x.end, x.pos)
108-
// }
109-
// .collect {
110-
// case (commentStart, commentEnd, _)
111-
// if commentStart <= cursorStartShifted && cursorStartShifted <= commentEnd =>
112-
// cursorStart
113-
// .withStart(commentStart + offsetStart)
114-
// .withEnd(commentEnd + offsetStart)
115-
// .toLsp
116-
117-
// }
118-
// end commentRangesFromTokens
119-
120-
// /** get comments under cursor */
121-
// def getCommentRanges(
122-
// cursor: SourcePosition,
123-
// path: List[tpd.Tree],
124-
// srcText: String
125-
// )(using Context): List[lsp4j.Range] =
126-
// val (treeStart, treeEnd) = path.headOption
127-
// .map(t => (t.sourcePos.start, t.sourcePos.end))
128-
// .getOrElse((0, srcText.size))
129-
130-
// // only parse comments from first range to reduce computation
131-
// val srcSliced = srcText.slice(treeStart, treeEnd)
132-
133-
// val tokens = srcSliced.tokenize.toOption
134-
// if tokens.isEmpty then Nil
135-
// else
136-
// commentRangesFromTokens(
137-
// tokens.toList.flatten,
138-
// cursor,
139-
// treeStart
140-
// )
141-
// end getCommentRanges
142-
// end SelectionRangeProvider

presentation-compiler/test/dotty/tools/pc/tests/SelectionRangeCommentSuite.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import org.junit.Test
77

88
class SelectionRangeCommentSuite extends BaseSelectionRangeSuite:
99

10-
// FIXME requires latest implementation from mtags for SelectionRangeProvider
11-
@Ignore
1210
@Test def `match` =
1311
check(
1412
"""|object Main extends App {

0 commit comments

Comments
 (0)