Cache folded syntax tree (#4744)
This commit is contained in:
parent
eb5712582f
commit
0163ffd328
|
@ -1,10 +0,0 @@
|
||||||
import SwiftOperators
|
|
||||||
import SwiftSyntax
|
|
||||||
|
|
||||||
extension SourceFileSyntax {
|
|
||||||
func folded() -> SourceFileSyntax? {
|
|
||||||
OperatorTable.standardOperators
|
|
||||||
.foldAll(self) { _ in }
|
|
||||||
.as(SourceFileSyntax.self)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -4,6 +4,7 @@ import Darwin
|
||||||
import Foundation
|
import Foundation
|
||||||
import IDEUtils
|
import IDEUtils
|
||||||
import SourceKittenFramework
|
import SourceKittenFramework
|
||||||
|
import SwiftOperators
|
||||||
import SwiftParser
|
import SwiftParser
|
||||||
import SwiftParserDiagnostics
|
import SwiftParserDiagnostics
|
||||||
import SwiftSyntax
|
import SwiftSyntax
|
||||||
|
@ -25,6 +26,11 @@ private let structureDictionaryCache = Cache { file in
|
||||||
private let syntaxTreeCache = Cache { file -> SourceFileSyntax in
|
private let syntaxTreeCache = Cache { file -> SourceFileSyntax in
|
||||||
return Parser.parse(source: file.contents)
|
return Parser.parse(source: file.contents)
|
||||||
}
|
}
|
||||||
|
private let foldedSyntaxTreeCache = Cache { file -> SourceFileSyntax? in
|
||||||
|
return OperatorTable.standardOperators
|
||||||
|
.foldAll(file.syntaxTree) { _ in }
|
||||||
|
.as(SourceFileSyntax.self)
|
||||||
|
}
|
||||||
private let locationConverterCache = Cache { file -> SourceLocationConverter in
|
private let locationConverterCache = Cache { file -> SourceLocationConverter in
|
||||||
return SourceLocationConverter(file: file.path ?? "<nopath>", tree: file.syntaxTree)
|
return SourceLocationConverter(file: file.path ?? "<nopath>", tree: file.syntaxTree)
|
||||||
}
|
}
|
||||||
|
@ -155,6 +161,8 @@ extension SwiftLintFile {
|
||||||
|
|
||||||
internal var syntaxTree: SourceFileSyntax { syntaxTreeCache.get(self) }
|
internal var syntaxTree: SourceFileSyntax { syntaxTreeCache.get(self) }
|
||||||
|
|
||||||
|
internal var foldedSyntaxTree: SourceFileSyntax? { foldedSyntaxTreeCache.get(self) }
|
||||||
|
|
||||||
internal var locationConverter: SourceLocationConverter { locationConverterCache.get(self) }
|
internal var locationConverter: SourceLocationConverter { locationConverterCache.get(self) }
|
||||||
|
|
||||||
internal var commands: [Command] { commandsCache.get(self) }
|
internal var commands: [Command] { commandsCache.get(self) }
|
||||||
|
@ -192,6 +200,7 @@ extension SwiftLintFile {
|
||||||
syntaxTokensByLinesCache.invalidate(self)
|
syntaxTokensByLinesCache.invalidate(self)
|
||||||
syntaxKindsByLinesCache.invalidate(self)
|
syntaxKindsByLinesCache.invalidate(self)
|
||||||
syntaxTreeCache.invalidate(self)
|
syntaxTreeCache.invalidate(self)
|
||||||
|
foldedSyntaxTreeCache.invalidate(self)
|
||||||
locationConverterCache.invalidate(self)
|
locationConverterCache.invalidate(self)
|
||||||
commandsCache.invalidate(self)
|
commandsCache.invalidate(self)
|
||||||
linesWithTokensCache.invalidate(self)
|
linesWithTokensCache.invalidate(self)
|
||||||
|
@ -207,6 +216,7 @@ extension SwiftLintFile {
|
||||||
syntaxTokensByLinesCache.clear()
|
syntaxTokensByLinesCache.clear()
|
||||||
syntaxKindsByLinesCache.clear()
|
syntaxKindsByLinesCache.clear()
|
||||||
syntaxTreeCache.clear()
|
syntaxTreeCache.clear()
|
||||||
|
foldedSyntaxTreeCache.clear()
|
||||||
locationConverterCache.clear()
|
locationConverterCache.clear()
|
||||||
commandsCache.clear()
|
commandsCache.clear()
|
||||||
linesWithTokensCache.clear()
|
linesWithTokensCache.clear()
|
||||||
|
|
|
@ -24,7 +24,7 @@ public protocol SwiftSyntaxRule: SourceKitFreeRule {
|
||||||
/// - parameter syntaxTree: The syntax tree to run pre-processing on
|
/// - parameter syntaxTree: The syntax tree to run pre-processing on
|
||||||
///
|
///
|
||||||
/// - returns: The tree that will be used to check for violations. If `nil`, this rule will return no violations.
|
/// - returns: The tree that will be used to check for violations. If `nil`, this rule will return no violations.
|
||||||
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax?
|
func preprocess(file: SwiftLintFile) -> SourceFileSyntax?
|
||||||
}
|
}
|
||||||
|
|
||||||
public extension SwiftSyntaxRule where Self: ConfigurationProviderRule,
|
public extension SwiftSyntaxRule where Self: ConfigurationProviderRule,
|
||||||
|
@ -53,7 +53,7 @@ public extension SwiftSyntaxRule {
|
||||||
}
|
}
|
||||||
|
|
||||||
func validate(file: SwiftLintFile) -> [StyleViolation] {
|
func validate(file: SwiftLintFile) -> [StyleViolation] {
|
||||||
guard let syntaxTree = preprocess(syntaxTree: file.syntaxTree) else {
|
guard let syntaxTree = preprocess(file: file) else {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,8 +79,8 @@ public extension SwiftSyntaxRule {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
|
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
|
||||||
syntaxTree
|
file.syntaxTree
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,8 +39,8 @@ struct LegacyMultipleRule: OptInRule, ConfigurationProviderRule, SwiftSyntaxRule
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
|
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
|
||||||
syntaxTree.folded()
|
file.foldedSyntaxTree
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
||||||
|
|
|
@ -72,8 +72,8 @@ struct IdenticalOperandsRule: ConfigurationProviderRule, SwiftSyntaxRule, OptInR
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
|
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
|
||||||
syntaxTree.folded()
|
file.foldedSyntaxTree
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
||||||
|
|
|
@ -34,8 +34,8 @@ struct ContainsOverFirstNotNilRule: SwiftSyntaxRule, OptInRule, ConfigurationPro
|
||||||
Visitor(viewMode: .sourceAccurate)
|
Visitor(viewMode: .sourceAccurate)
|
||||||
}
|
}
|
||||||
|
|
||||||
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
|
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
|
||||||
syntaxTree.folded()
|
file.foldedSyntaxTree
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,8 +23,8 @@ struct ContainsOverRangeNilComparisonRule: SwiftSyntaxRule, OptInRule, Configura
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
|
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
|
||||||
syntaxTree.folded()
|
file.foldedSyntaxTree
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
||||||
|
|
|
@ -35,8 +35,8 @@ struct EmptyCountRule: ConfigurationProviderRule, OptInRule, SwiftSyntaxRule {
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
|
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
|
||||||
syntaxTree.folded()
|
file.foldedSyntaxTree
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
||||||
|
|
|
@ -47,8 +47,8 @@ struct ShorthandOperatorRule: ConfigurationProviderRule, SwiftSyntaxRule {
|
||||||
|
|
||||||
fileprivate static let allOperators = ["-", "/", "+", "*"]
|
fileprivate static let allOperators = ["-", "/", "+", "*"]
|
||||||
|
|
||||||
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
|
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
|
||||||
syntaxTree.folded()
|
file.foldedSyntaxTree
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
||||||
|
|
Loading…
Reference in New Issue