From d7caad1bc3549fd9d9201ba336419e7d9ed4c362 Mon Sep 17 00:00:00 2001 From: Ziyang Huang Date: Tue, 8 Aug 2023 08:18:45 +0800 Subject: [PATCH] Rename some `SyntaxClassification` --- .../SwiftIDEUtilsCompatibility.swift | 31 +++++++- .../SwiftIDEUtils/SyntaxClassification.swift | 49 +++++------- .../ClassifiedSyntaxTreePrinter.swift | 8 +- .../ClassificationTests.swift | 76 +++++++++---------- 4 files changed, 86 insertions(+), 78 deletions(-) diff --git a/Sources/SwiftIDEUtils/SwiftIDEUtilsCompatibility.swift b/Sources/SwiftIDEUtils/SwiftIDEUtilsCompatibility.swift index d635efc063a..90a3bcf7fd7 100644 --- a/Sources/SwiftIDEUtils/SwiftIDEUtilsCompatibility.swift +++ b/Sources/SwiftIDEUtils/SwiftIDEUtilsCompatibility.swift @@ -15,9 +15,34 @@ public extension SyntaxClassification { /// A `#` keyword like `#warning`. - @available(*, deprecated, renamed: "poundDirective") - static var poundDirectiveKeyword: SyntaxClassification { - return .poundDirective + @available(*, deprecated, renamed: "ifConfigDirective") + static var poundDirective: SyntaxClassification { + return .ifConfigDirective + } + + @available(*, deprecated, renamed: "ifConfigDirective") + static var buildConfigId: SyntaxClassification { + return .ifConfigDirective + } + + @available(*, deprecated, message: "String interpolation anchors are now classified as .none") + static var stringInterpolationAnchor: SyntaxClassification { + return .none + } + + @available(*, deprecated, renamed: "type") + static var typeIdentifier: SyntaxClassification { + return .type + } + + @available(*, deprecated, renamed: "operator") + static var operatorIdentifier: SyntaxClassification { + return .operator + } + + @available(*, deprecated, renamed: "floatLiteral") + static var floatingLiteral: SyntaxClassification { + return .floatLiteral } } diff --git a/Sources/SwiftIDEUtils/SyntaxClassification.swift b/Sources/SwiftIDEUtils/SyntaxClassification.swift index 5ba88f8b523..a856258b5d4 100644 --- a/Sources/SwiftIDEUtils/SyntaxClassification.swift +++ b/Sources/SwiftIDEUtils/SyntaxClassification.swift @@ -17,8 +17,6 @@ public enum SyntaxClassification { case attribute /// A block comment starting with `/**` and ending with `*/. case blockComment - /// A build configuration directive like `#if`, `#elseif`, `#else`. - case buildConfigId /// A doc block comment starting with `/**` and ending with `*/. case docBlockComment /// A doc line comment starting with `///`. @@ -31,6 +29,8 @@ public enum SyntaxClassification { case floatLiteral /// A generic identifier. case identifier + /// A directive starting with `#if`, `#elseif`or `#else`. + case ifConfigDirective /// An integer literal. case integerLiteral /// A Swift keyword, including contextual keywords. @@ -40,22 +40,13 @@ public enum SyntaxClassification { /// The token should not receive syntax coloring. case none /// An identifier referring to an operator. - case operatorIdentifier - /// A `#` token like `#warning`. - case poundDirective + case `operator` /// A regex literal, including multiline regex literals. case regexLiteral - /// The opening and closing parenthesis of string interpolation. - case stringInterpolationAnchor /// A string literal including multiline string literals. case stringLiteral /// An identifier referring to a type. - case typeIdentifier - - @available(*, deprecated, renamed: "floatLiteral") - public static var floatingLiteral: Self { - return .floatLiteral - } + case type } extension SyntaxClassification { @@ -76,26 +67,22 @@ extension SyntaxClassification { return (.keyword, false) case \DeclModifierSyntax.name: return (.attribute, false) - case \ExpressionSegmentSyntax.leftParen: - return (.stringInterpolationAnchor, true) - case \ExpressionSegmentSyntax.rightParen: - return (.stringInterpolationAnchor, true) case \IfConfigClauseSyntax.poundKeyword: - return (.buildConfigId, false) + return (.ifConfigDirective, false) case \IfConfigClauseSyntax.condition: - return (.buildConfigId, false) + return (.ifConfigDirective, false) case \IfConfigDeclSyntax.poundEndif: - return (.buildConfigId, false) + return (.ifConfigDirective, false) case \MemberTypeIdentifierSyntax.name: - return (.typeIdentifier, false) + return (.type, false) case \OperatorDeclSyntax.name: - return (.operatorIdentifier, false) + return (.operator, false) case \PrecedenceGroupAssociativitySyntax.associativityLabel: return (.keyword, false) case \PrecedenceGroupRelationSyntax.higherThanOrLowerThanLabel: return (.keyword, false) case \SimpleTypeIdentifierSyntax.name: - return (.typeIdentifier, false) + return (.type, false) default: return nil } @@ -114,7 +101,7 @@ extension RawTokenKind { case .backtick: return .none case .binaryOperator: - return .operatorIdentifier + return .operator case .colon: return .none case .comma: @@ -152,7 +139,7 @@ extension RawTokenKind { case .period: return .none case .postfixOperator: - return .operatorIdentifier + return .operator case .postfixQuestionMark: return .none case .pound: @@ -160,21 +147,21 @@ extension RawTokenKind { case .poundAvailable: return .none case .poundElse: - return .poundDirective + return .ifConfigDirective case .poundElseif: - return .poundDirective + return .ifConfigDirective case .poundEndif: - return .poundDirective + return .ifConfigDirective case .poundIf: - return .poundDirective + return .ifConfigDirective case .poundSourceLocation: - return .poundDirective + return .keyword case .poundUnavailable: return .none case .prefixAmpersand: return .none case .prefixOperator: - return .operatorIdentifier + return .operator case .rawStringPoundDelimiter: return .none case .regexLiteralPattern: diff --git a/Sources/lit-test-helper/ClassifiedSyntaxTreePrinter.swift b/Sources/lit-test-helper/ClassifiedSyntaxTreePrinter.swift index e6cba747214..b9eaabc1e01 100644 --- a/Sources/lit-test-helper/ClassifiedSyntaxTreePrinter.swift +++ b/Sources/lit-test-helper/ClassifiedSyntaxTreePrinter.swift @@ -19,16 +19,14 @@ extension SyntaxClassification { case .none: return "" case .keyword: return "kw" case .identifier: return "id" - case .typeIdentifier: return "type" + case .type: return "type" case .dollarIdentifier: return "dollar" - case .operatorIdentifier: return "op" + case .operator: return "op" case .integerLiteral: return "int" case .floatLiteral: return "float" case .stringLiteral: return "str" - case .stringInterpolationAnchor: return "anchor" case .regexLiteral: return "regex" - case .poundDirective: return "#kw" - case .buildConfigId: return "#id" + case .ifConfigDirective: return "#kw" case .attribute: return "attr-builtin" case .editorPlaceholder: return "placeholder" case .lineComment: return "comment-line" diff --git a/Tests/SwiftIDEUtilsTest/ClassificationTests.swift b/Tests/SwiftIDEUtilsTest/ClassificationTests.swift index 490a29b22c6..f39fb6352d5 100644 --- a/Tests/SwiftIDEUtilsTest/ClassificationTests.swift +++ b/Tests/SwiftIDEUtilsTest/ClassificationTests.swift @@ -90,7 +90,7 @@ public class ClassificationTests: XCTestCase { expected: [ ClassificationSpec(source: "let", kind: .keyword), ClassificationSpec(source: "x", kind: .identifier), - ClassificationSpec(source: "Int", kind: .typeIdentifier), + ClassificationSpec(source: "Int", kind: .type), ] ) } @@ -103,11 +103,11 @@ public class ClassificationTests: XCTestCase { expected: [ ClassificationSpec(source: "let", kind: .keyword), ClassificationSpec(source: "x", kind: .identifier), - ClassificationSpec(source: "Int", kind: .typeIdentifier), + ClassificationSpec(source: "Int", kind: .type), ClassificationSpec(source: "4", kind: .integerLiteral), - ClassificationSpec(source: "+", kind: .operatorIdentifier), + ClassificationSpec(source: "+", kind: .operator), ClassificationSpec(source: "5", kind: .integerLiteral), - ClassificationSpec(source: "/", kind: .operatorIdentifier), + ClassificationSpec(source: "/", kind: .operator), ClassificationSpec(source: "6", kind: .integerLiteral), ] ) @@ -117,7 +117,7 @@ public class ClassificationTests: XCTestCase { expected: [ ClassificationSpec(source: "infix", kind: .keyword), ClassificationSpec(source: "operator", kind: .keyword), - ClassificationSpec(source: "*--*", kind: .operatorIdentifier), + ClassificationSpec(source: "*--*", kind: .operator), ] ) } @@ -131,13 +131,13 @@ public class ClassificationTests: XCTestCase { ClassificationSpec(source: "func", kind: .keyword), ClassificationSpec(source: "foo", kind: .identifier), ClassificationSpec(source: "x", kind: .identifier), - ClassificationSpec(source: "Int", kind: .typeIdentifier), + ClassificationSpec(source: "Int", kind: .type), ClassificationSpec(source: "y", kind: .identifier), - ClassificationSpec(source: "Int", kind: .typeIdentifier), - ClassificationSpec(source: "Int", kind: .typeIdentifier), + ClassificationSpec(source: "Int", kind: .type), + ClassificationSpec(source: "Int", kind: .type), ClassificationSpec(source: "return", kind: .keyword), ClassificationSpec(source: "x", kind: .identifier), - ClassificationSpec(source: "+", kind: .operatorIdentifier), + ClassificationSpec(source: "+", kind: .operator), ClassificationSpec(source: "y", kind: .identifier), ] ) @@ -219,18 +219,18 @@ public class ClassificationTests: XCTestCase { #endif """, expected: [ - ClassificationSpec(source: "#if", kind: .poundDirective), - ClassificationSpec(source: "os", kind: .buildConfigId), - ClassificationSpec(source: "macOS", kind: .buildConfigId), + ClassificationSpec(source: "#if", kind: .ifConfigDirective), + ClassificationSpec(source: "os", kind: .ifConfigDirective), + ClassificationSpec(source: "macOS", kind: .ifConfigDirective), ClassificationSpec(source: "var", kind: .keyword), ClassificationSpec(source: "x", kind: .identifier), - ClassificationSpec(source: "Int", kind: .typeIdentifier), - ClassificationSpec(source: "#elseif", kind: .poundDirective), + ClassificationSpec(source: "Int", kind: .type), + ClassificationSpec(source: "#elseif", kind: .ifConfigDirective), ClassificationSpec(source: "var", kind: .keyword), ClassificationSpec(source: "x", kind: .identifier), - ClassificationSpec(source: "Float", kind: .typeIdentifier), - ClassificationSpec(source: "#else", kind: .poundDirective), - ClassificationSpec(source: "#endif", kind: .poundDirective), + ClassificationSpec(source: "Float", kind: .type), + ClassificationSpec(source: "#else", kind: .ifConfigDirective), + ClassificationSpec(source: "#endif", kind: .ifConfigDirective), ] ) } @@ -242,10 +242,10 @@ public class ClassificationTests: XCTestCase { #else """, expected: [ - ClassificationSpec(source: "#if", kind: .poundDirective), - ClassificationSpec(source: "!", kind: .operatorIdentifier), - ClassificationSpec(source: "CONF", kind: .buildConfigId), - ClassificationSpec(source: "#else", kind: .poundDirective), + ClassificationSpec(source: "#if", kind: .ifConfigDirective), + ClassificationSpec(source: "!", kind: .operator), + ClassificationSpec(source: "CONF", kind: .ifConfigDirective), + ClassificationSpec(source: "#else", kind: .ifConfigDirective), ] ) } @@ -269,7 +269,7 @@ public class ClassificationTests: XCTestCase { assertClassification( #"#sourceLocation(file: "x", line: 1)"#, expected: [ - ClassificationSpec(source: "#sourceLocation", kind: .poundDirective), + ClassificationSpec(source: "#sourceLocation", kind: .keyword), ClassificationSpec(source: "file", kind: .keyword), ClassificationSpec(source: #""x""#, kind: .stringLiteral), ClassificationSpec(source: "line", kind: .keyword), @@ -330,7 +330,7 @@ public class ClassificationTests: XCTestCase { ClassificationSpec(source: "OSX", kind: .keyword), ClassificationSpec(source: "10", kind: .integerLiteral), ClassificationSpec(source: "10", kind: .integerLiteral), - ClassificationSpec(source: "*", kind: .operatorIdentifier), + ClassificationSpec(source: "*", kind: .operator), ] ) } @@ -345,7 +345,7 @@ public class ClassificationTests: XCTestCase { ClassificationSpec(source: "@IBOutlet", kind: .attribute), ClassificationSpec(source: "var", kind: .keyword), ClassificationSpec(source: "foo", kind: .identifier), - ClassificationSpec(source: "String", kind: .typeIdentifier), + ClassificationSpec(source: "String", kind: .type), ] ) } @@ -363,8 +363,8 @@ public class ClassificationTests: XCTestCase { ClassificationSpec(source: "indirect", kind: .keyword), ClassificationSpec(source: "case", kind: .keyword), ClassificationSpec(source: "cons", kind: .identifier), - ClassificationSpec(source: "T", kind: .typeIdentifier), - ClassificationSpec(source: "List", kind: .typeIdentifier), + ClassificationSpec(source: "T", kind: .type), + ClassificationSpec(source: "List", kind: .type), ] ) } @@ -410,7 +410,7 @@ public class ClassificationTests: XCTestCase { expected: [ ClassificationSpec(source: "infix", kind: .keyword), ClassificationSpec(source: "operator", kind: .keyword), - ClassificationSpec(source: "*-*", kind: .operatorIdentifier), + ClassificationSpec(source: "*-*", kind: .operator), ClassificationSpec(source: "FunnyPrecedence", kind: .identifier), ] ) @@ -441,7 +441,7 @@ public class ClassificationTests: XCTestCase { expected: [ ClassificationSpec(source: "protocol", kind: .keyword), ClassificationSpec(source: "Prot2", kind: .identifier), - ClassificationSpec(source: "Prot", kind: .typeIdentifier), + ClassificationSpec(source: "Prot", kind: .type), ] ) } @@ -453,13 +453,13 @@ public class ClassificationTests: XCTestCase { ClassificationSpec(source: "func", kind: .keyword), ClassificationSpec(source: "genFn", kind: .identifier), ClassificationSpec(source: "T", kind: .identifier), - ClassificationSpec(source: "Prot", kind: .typeIdentifier), - ClassificationSpec(source: "T", kind: .typeIdentifier), - ClassificationSpec(source: "Int", kind: .typeIdentifier), + ClassificationSpec(source: "Prot", kind: .type), + ClassificationSpec(source: "T", kind: .type), + ClassificationSpec(source: "Int", kind: .type), ClassificationSpec(source: "where", kind: .keyword), - ClassificationSpec(source: "T", kind: .typeIdentifier), - ClassificationSpec(source: "Blarg", kind: .typeIdentifier), - ClassificationSpec(source: "Prot2", kind: .typeIdentifier), + ClassificationSpec(source: "T", kind: .type), + ClassificationSpec(source: "Blarg", kind: .type), + ClassificationSpec(source: "Prot2", kind: .type), ] ) } @@ -469,9 +469,7 @@ public class ClassificationTests: XCTestCase { #""This is string \(1) interpolation""#, expected: [ ClassificationSpec(source: #""This is string "#, kind: .stringLiteral), - ClassificationSpec(source: "(", kind: .stringInterpolationAnchor), ClassificationSpec(source: "1", kind: .integerLiteral), - ClassificationSpec(source: ")", kind: .stringInterpolationAnchor), ClassificationSpec(source: #" interpolation""#, kind: .stringLiteral), ] ) @@ -531,12 +529,12 @@ public class ClassificationTests: XCTestCase { ClassificationSpec(source: "func", kind: .keyword), ClassificationSpec(source: "keywordInCaseAndLocalArgLabel", kind: .identifier), ClassificationSpec(source: "for", kind: .identifier), - ClassificationSpec(source: "Int", kind: .typeIdentifier), + ClassificationSpec(source: "Int", kind: .type), ClassificationSpec(source: "for", kind: .identifier), ClassificationSpec(source: "in", kind: .identifier), - ClassificationSpec(source: "Int", kind: .typeIdentifier), + ClassificationSpec(source: "Int", kind: .type), ClassificationSpec(source: "class", kind: .identifier), - ClassificationSpec(source: "Int", kind: .typeIdentifier), + ClassificationSpec(source: "Int", kind: .type), ] ) }