Update contains method to include start position (#34)#37
Open
tothambrus11 wants to merge 2 commits into
Open
Conversation
Closed
Contributor
|
Ahh yes, this is a tricky one. Ranges are always subtle. I was planning on adding some testing around this to make sure I understood what "correct" was. What do you think about that? |
Author
|
Ranges are half-open, as specified by the LSP. When implementing language servers to resolve a node under a cursor, we would typically not use this, but something that matches on both ends inclusively though. E.g. if we have an identifier import FrontEnd
import Logging
extension Program {
/// - Requires: The source file of `position` is present in `self`.
public func innermostTree(
containing position: SourcePosition, reportingLogsTo logger: Logger, in f: SourceFile.ID
) -> AnySyntaxIdentity? {
var v = NodeFinder(position)
visit(topLevelDeclarations(in: f), calling: &v)
return v.deepestMatch
}
}
/// Requires that the visiting happens in a depth-first order.
private struct NodeFinder: SyntaxVisitor {
// todo use binary search for efficiency if we can assume AST entries are sorted by position (probably they aren't though)
let targetPosition: SourcePosition
private(set) var deepestMatch: AnySyntaxIdentity?
private var deepestMatchDepth: Int = -1
private var currentDepth: Int = 0
public init(_ targetPosition: SourcePosition) {
self.targetPosition = targetPosition
}
mutating func willEnter(_ n: AnySyntaxIdentity, in program: Program) -> Bool {
if program[n].site.region.containsInclusive(targetPosition.index) {
if currentDepth > deepestMatchDepth {
deepestMatchDepth = currentDepth
deepestMatch = n
}
} else {
if program.isScope(n) {
// If it's a scope, we know its children's sites are strictly subsumed, so we can skip them.
return false
}
}
currentDepth += 1
return true // continue visiting children
}
public mutating func willExit(_ node: AnySyntaxIdentity, in program: Program) {
currentDepth -= 1
}
}
extension Range {
func containsInclusive(_ i: Bound) -> Bool {
return self.lowerBound <= i && i <= self.upperBound
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #34