Cache folded syntax tree (#4744)

This commit is contained in:
Marcelo Fabri 2023-02-06 10:39:36 -08:00 committed by GitHub
parent eb5712582f
commit 0163ffd328
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 26 additions and 26 deletions

View File

@ -1,10 +0,0 @@
import SwiftOperators
import SwiftSyntax
extension SourceFileSyntax {
func folded() -> SourceFileSyntax? {
OperatorTable.standardOperators
.foldAll(self) { _ in }
.as(SourceFileSyntax.self)
}
}

View File

@ -4,6 +4,7 @@ import Darwin
import Foundation
import IDEUtils
import SourceKittenFramework
import SwiftOperators
import SwiftParser
import SwiftParserDiagnostics
import SwiftSyntax
@ -25,6 +26,11 @@ private let structureDictionaryCache = Cache { file in
private let syntaxTreeCache = Cache { file -> SourceFileSyntax in
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
return SourceLocationConverter(file: file.path ?? "<nopath>", tree: file.syntaxTree)
}
@ -155,6 +161,8 @@ extension SwiftLintFile {
internal var syntaxTree: SourceFileSyntax { syntaxTreeCache.get(self) }
internal var foldedSyntaxTree: SourceFileSyntax? { foldedSyntaxTreeCache.get(self) }
internal var locationConverter: SourceLocationConverter { locationConverterCache.get(self) }
internal var commands: [Command] { commandsCache.get(self) }
@ -192,6 +200,7 @@ extension SwiftLintFile {
syntaxTokensByLinesCache.invalidate(self)
syntaxKindsByLinesCache.invalidate(self)
syntaxTreeCache.invalidate(self)
foldedSyntaxTreeCache.invalidate(self)
locationConverterCache.invalidate(self)
commandsCache.invalidate(self)
linesWithTokensCache.invalidate(self)
@ -207,6 +216,7 @@ extension SwiftLintFile {
syntaxTokensByLinesCache.clear()
syntaxKindsByLinesCache.clear()
syntaxTreeCache.clear()
foldedSyntaxTreeCache.clear()
locationConverterCache.clear()
commandsCache.clear()
linesWithTokensCache.clear()

View File

@ -24,7 +24,7 @@ public protocol SwiftSyntaxRule: SourceKitFreeRule {
/// - 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.
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax?
func preprocess(file: SwiftLintFile) -> SourceFileSyntax?
}
public extension SwiftSyntaxRule where Self: ConfigurationProviderRule,
@ -53,7 +53,7 @@ public extension SwiftSyntaxRule {
}
func validate(file: SwiftLintFile) -> [StyleViolation] {
guard let syntaxTree = preprocess(syntaxTree: file.syntaxTree) else {
guard let syntaxTree = preprocess(file: file) else {
return []
}
@ -79,8 +79,8 @@ public extension SwiftSyntaxRule {
)
}
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
syntaxTree
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
file.syntaxTree
}
}

View File

@ -39,8 +39,8 @@ struct LegacyMultipleRule: OptInRule, ConfigurationProviderRule, SwiftSyntaxRule
]
)
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
syntaxTree.folded()
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
file.foldedSyntaxTree
}
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {

View File

@ -72,8 +72,8 @@ struct IdenticalOperandsRule: ConfigurationProviderRule, SwiftSyntaxRule, OptInR
]
)
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
syntaxTree.folded()
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
file.foldedSyntaxTree
}
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {

View File

@ -34,8 +34,8 @@ struct ContainsOverFirstNotNilRule: SwiftSyntaxRule, OptInRule, ConfigurationPro
Visitor(viewMode: .sourceAccurate)
}
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
syntaxTree.folded()
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
file.foldedSyntaxTree
}
}

View File

@ -23,8 +23,8 @@ struct ContainsOverRangeNilComparisonRule: SwiftSyntaxRule, OptInRule, Configura
}
)
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
syntaxTree.folded()
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
file.foldedSyntaxTree
}
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {

View File

@ -35,8 +35,8 @@ struct EmptyCountRule: ConfigurationProviderRule, OptInRule, SwiftSyntaxRule {
]
)
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
syntaxTree.folded()
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
file.foldedSyntaxTree
}
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {

View File

@ -47,8 +47,8 @@ struct ShorthandOperatorRule: ConfigurationProviderRule, SwiftSyntaxRule {
fileprivate static let allOperators = ["-", "/", "+", "*"]
func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? {
syntaxTree.folded()
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
file.foldedSyntaxTree
}
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {