@@ -6,6 +6,7 @@ import java.util as ju
6
6
import scala .jdk .CollectionConverters ._
7
7
import scala .meta .pc .OffsetParams
8
8
9
+ import dotty .tools .dotc .core .Comments .Comment
9
10
import dotty .tools .dotc .core .Contexts .Context
10
11
import dotty .tools .dotc .interactive .Interactive
11
12
import dotty .tools .dotc .interactive .InteractiveDriver
@@ -36,7 +37,7 @@ class SelectionRangeProvider(
36
37
def selectionRange (): List [SelectionRange ] =
37
38
given ctx : Context = driver.currentCtx
38
39
39
- val selectionRanges = params.asScala.toList.map { param =>
40
+ params.asScala.toList.map { param =>
40
41
41
42
val uri = param.uri
42
43
val filePath = Paths .get(uri)
@@ -53,10 +54,22 @@ class SelectionRangeProvider(
53
54
selectionRange
54
55
}
55
56
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)
58
64
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
+ }
60
73
end selectionRange
61
74
62
75
private def setParent (
@@ -88,55 +101,3 @@ class SelectionRangeProvider(
88
101
child
89
102
90
103
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
0 commit comments