-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fix:selection expansion in comments #4921
Conversation
val programStr = param.text() | ||
// deal with multiple source text. probably a better way ? | ||
val tkSrc = programStr.parse[Source].toOption.map(_.tokens) | ||
val tkExpr = programStr.parse[Stat].toOption.map(_.tokens) | ||
val tkType = programStr.parse[Type].toOption.map(_.tokens) | ||
|
||
val tokens = tkSrc orElse tkExpr orElse tkType // orElse tkTopDecls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use something along the lines of:
val (treeStart, treeEnd) = path.headOption
.map(t => (t.pos.start, t.pos.end))
.getOrElse((0, text.size))
val offset = pos.start
val tokens = text.slice(treeStart, treeEnd)tokenize.toOption
tokens
.flatMap(t =>
t.collectFirst {
case t: Token.Comment
if treeStart + t.pos.start < currentOffset &&
treeStart + t.pos.end >= currentOffset =>
val range = new SelectionRange()
range.setRange(t.pos.toLSP)
Some(range)
case _ =>
Noene
}
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for suggestion,but i'm not sure how to update offset to input ,which will cause error if it's not updated (used later in def toLsp: l.Range
of extension (pos: SourcePosition)
):
(commentList) // ++ merged)
.filter((commentStart, commentEnd, _) =>
commentStart <= cursorStartShifted && cursorStartShifted <= commentEnd
)
.map { (commentStart, commentEnd, oldPos) =>
// todo! need to upg input if offset is nonzero!
val newPos: Position =
meta.Position.Range(
oldPos.input,
commentStart + offsetStart,
commentEnd + offsetStart,
)
newPos
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's simplify it a bit and could you write some tests?
} | ||
|
||
// WIP: when there are multiple single line comments,select them together | ||
val merged = tokenList |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should merge mutiple single line comments, that is usually not that for anything else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed this
val commentList: List[(Int, Int, Position)] = tokenList.collect { | ||
case x: Comment => | ||
println( | ||
s"find Comment \"${x}\" at ${(x.start, x.`end`)} " // : | ||
) // , ${(x.start, x.`end`, x.pos)} | ||
(x.start, x.`end`, x.pos) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
val commentList: List[(Int, Int, Position)] = tokenList.collect { | |
case x: Comment => | |
println( | |
s"find Comment \"${x}\" at ${(x.start, x.`end`)} " // : | |
) // , ${(x.start, x.`end`, x.pos)} | |
(x.start, x.`end`, x.pos) | |
} | |
val commentList: Option[lsp4j.Range] = tokenList.collectFirst { | |
case x: Comment if cursorStartShifted && cursorStartShifted <= commentEnd => | |
SourcePosition( | |
cursorStart.source, | |
Spans.Span( | |
commentStart + offsetStart, | |
commentEnd + offsetStart, | |
), | |
).toLsp | |
} |
this should be enough and can be returned from the function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems not quite so clear how to simplify this since some filter are complicated, but I have cleaned up a bit
I write the test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the liberty to change a couple of small things. Thanks for contributing!
Thanks ! very excited to my first contribution merged ! |
solves #3615
only works with pure scala 3 project,doesn't seem to work with worksheet
There's redundancy in code but I'm not sure how to reduce it :