diff --git a/Sources/SwiftIfConfig/BuildConfiguration.swift b/Sources/SwiftIfConfig/BuildConfiguration.swift index d15b35f4fca..03af328ea02 100644 --- a/Sources/SwiftIfConfig/BuildConfiguration.swift +++ b/Sources/SwiftIfConfig/BuildConfiguration.swift @@ -9,6 +9,7 @@ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// + import SwiftSyntax /// Describes the ordering of a sequence of bytes that make up a word of diff --git a/Sources/SwiftIfConfig/ConfiguredRegions.swift b/Sources/SwiftIfConfig/ConfiguredRegions.swift index e3e94e657c8..bd83252c399 100644 --- a/Sources/SwiftIfConfig/ConfiguredRegions.swift +++ b/Sources/SwiftIfConfig/ConfiguredRegions.swift @@ -37,7 +37,7 @@ import SwiftSyntax /// - Unparsed region for the `#elseif compiler(>= 12.0)`. /// - Inactive region for the final `#else`. public struct ConfiguredRegions { - let regions: [Element] + let regions: [(ifClause: IfConfigClauseSyntax, state: IfConfigRegionState)] /// The set of diagnostics produced when evaluating the configured regions. public let diagnostics: [Diagnostic] @@ -59,13 +59,13 @@ public struct ConfiguredRegions { let middle = currentSlice.startIndex + currentSlice.count / 2 // If the node is prior to the start of the middle, take the left-hand side. - if node.position < currentSlice[middle].0.regionStart { + if node.position < currentSlice[middle].ifClause.regionStart { currentSlice = currentSlice[.. currentSlice[middle].0.endPosition { + if node.position > currentSlice[middle].ifClause.endPosition { currentSlice = currentSlice[(middle + 1)...] continue } @@ -77,13 +77,13 @@ public struct ConfiguredRegions { // Find the last region in which this node lands. If there is no such // region, this is active. return currentSlice.last { region in - node.position >= region.0.regionStart && node.position <= region.0.endPosition - }?.1 ?? .active + (region.ifClause.regionStart...region.ifClause.endPosition).contains(node.position) + }?.state ?? .active } } extension ConfiguredRegions: RandomAccessCollection { - public typealias Element = (IfConfigClauseSyntax, IfConfigRegionState) + public typealias Element = (ifClause: IfConfigClauseSyntax, state: IfConfigRegionState) public var startIndex: Int { regions.startIndex } public var endIndex: Int { regions.endIndex } @@ -99,7 +99,7 @@ extension ConfiguredRegions: CustomDebugStringConvertible { return "[]" } - let root = firstRegion.0.root + let root = firstRegion.ifClause.root let converter = SourceLocationConverter(fileName: "", tree: root) let regionDescriptions = regions.map { (ifClause, state) in let startPosition = converter.location(for: ifClause.position) @@ -198,7 +198,6 @@ fileprivate class ConfiguredRegionVisitor: Sy // In an active region, evaluate the condition to determine whether // this clause is active. Otherwise, this clause is inactive. - // inactive. if inActiveRegion { let (thisIsActive, _, evalDiagnostics) = evaluateIfConfig( condition: foldedCondition, diff --git a/Sources/SwiftIfConfig/SwiftIfConfig.docc/SwiftIfConfig.md b/Sources/SwiftIfConfig/SwiftIfConfig.docc/SwiftIfConfig.md index cc1d40a57c4..e2a4636650e 100644 --- a/Sources/SwiftIfConfig/SwiftIfConfig.docc/SwiftIfConfig.md +++ b/Sources/SwiftIfConfig/SwiftIfConfig.docc/SwiftIfConfig.md @@ -29,8 +29,4 @@ The `SwiftIfConfig` library provides utilities to determine which syntax nodes a * and are visitor types that only visit the syntax nodes that are included ("active") for a given build configuration, implicitly skipping any nodes within inactive `#if` clauses. * `SyntaxProtocol.removingInactive(in:)` produces a syntax node that removes all inactive regions (and their corresponding `IfConfigDeclSyntax` nodes) from the given syntax tree, returning a new tree that is free of `#if` conditions. * `IfConfigDeclSyntax.activeClause(in:)` determines which of the clauses of an `#if` is active for the given build configuration, returning the active clause. -* `SyntaxProtocol.isActive(in:)` determines whether the given syntax node is active for the given build configuration. The result is one of "active" - (the node is included in the program), "inactive" (the node is not included - in the program), or "unparsed" (the node is not included in the program and - is also allowed to have syntax errors). -* `SyntaxProtocol.configuredRegions(in:)` produces an array describing the various regions in which a configuration has an effect, indicating active, inactive, and unparsed regions in the source tree. The array can be used as an input to `SyntaxProtocol.isActive(inConfiguredRegions:)` to determine whether a given syntax node is active. +* `SyntaxProtocol.configuredRegions(in:)` produces a `ConfiguredRegions` value that can be used to efficiently test whether a given syntax node is in an active, inactive, or unparsed region (via `isActive`). diff --git a/Sources/SwiftIfConfig/SyntaxProtocol+IfConfig.swift b/Sources/SwiftIfConfig/SyntaxProtocol+IfConfig.swift index 801e626ac15..afc79939130 100644 --- a/Sources/SwiftIfConfig/SyntaxProtocol+IfConfig.swift +++ b/Sources/SwiftIfConfig/SyntaxProtocol+IfConfig.swift @@ -47,17 +47,4 @@ extension SyntaxProtocol { let configuredRegions = root.configuredRegions(in: configuration) return (configuredRegions.isActive(self), configuredRegions.diagnostics) } - - /// Determine whether the given syntax node is active given a set of - /// configured regions as produced by `configuredRegions(in:)`. - /// - /// If you are querying whether many syntax nodes in a particular file are - /// active, consider calling `configuredRegions(in:)` once and using - /// this function. For occasional queries, use `isActive(in:)`. - @available(*, deprecated, message: "Please use ConfiguredRegions.isActive(_:)") - public func isActive( - inConfiguredRegions regions: ConfiguredRegions - ) -> IfConfigRegionState { - regions.isActive(self) - } }