From 6bc573ca271fe839191092bf4b9d6159f223776c Mon Sep 17 00:00:00 2001 From: Marcus Ortiz <212918+mportiz08@users.noreply.github.com> Date: Wed, 11 Nov 2020 15:25:26 -0800 Subject: [PATCH] (swift) improvements: support `some`, `@main`; Fix `#` keywords . (#2845) * Add support for new Swift 5.1 and 5.3 features. 1. [`some` keyword][1] (Introduced in Swift 5.1) 2. [`@main` attribute][2] (Introduced in Swift 5.3) These language syntax features are utilized heavily in code that depends on the `SwiftUI` framework for user interfaces. [1]: https://github.com/apple/swift-evolution/blob/main/proposals/0244-opaque-result-types.md [2]: https://github.com/apple/swift-evolution/blob/main/proposals/0281-main-attribute.md * Update Swift keyword `$pattern` to include "#". The default of lexeme pattern of `/\w+/` is not sufficient to capture keywords that start with a number sign character (like `#if` as one example). This updates the pattern to also recognize the number sign character as well as other alphanumeric characters that were previously recognized. [1] [1]: https://docs.swift.org/swift-book/ReferenceManual/LexicalStructure.html#ID413 --- AUTHORS.txt | 1 + CHANGES.md | 4 ++++ src/languages/swift.js | 7 +++++-- test/markup/swift/swiftui.expect.txt | 12 ++++++++++++ test/markup/swift/swiftui.txt | 12 ++++++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 test/markup/swift/swiftui.expect.txt create mode 100644 test/markup/swift/swiftui.txt diff --git a/AUTHORS.txt b/AUTHORS.txt index 6c501dc61c..1432d17ca0 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -308,3 +308,4 @@ Contributors: - Marat Nagayev - Patrick Scheibe - Kyle Brown +- Marcus Ortiz diff --git a/CHANGES.md b/CHANGES.md index 9373cee483..564d2874c8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -29,6 +29,9 @@ Language Improvements: - Matching of named-characters aka special symbols like `\[Gamma]` - Updated list of version 12.1 built-in symbols - Matching of patterns, slots, message-names and braces +- fix(swift) Handle keywords that start with `#` [Marcus Ortiz][] +- enh(swift) Match `some` keyword [Marcus Ortiz][] +- enh(swift) Match `@main` attribute [Marcus Ortiz][] Dev Improvements: @@ -54,6 +57,7 @@ New themes: [Michael Rush]: https://github.com/rushimusmaximus [Patrick Scheibe]: https://github.com/halirutan [Kyle Brown]: https://github.com/kylebrown9 +[Marcus Ortiz]: https://github.com/mportiz08 ## Version 10.3.1 diff --git a/src/languages/swift.js b/src/languages/swift.js index 2ee2232fc8..7034f24f45 100644 --- a/src/languages/swift.js +++ b/src/languages/swift.js @@ -10,6 +10,9 @@ Category: common, system export default function(hljs) { var SWIFT_KEYWORDS = { + // override the pattern since the default of of /\w+/ is not sufficient to + // capture the keywords that start with the character "#" + $pattern: /[\w#]+/, keyword: '#available #colorLiteral #column #else #elseif #endif #file ' + '#fileLiteral #function #if #imageLiteral #line #selector #sourceLocation ' + '_ __COLUMN__ __FILE__ __FUNCTION__ __LINE__ Any as as! as? associatedtype ' + @@ -18,7 +21,7 @@ export default function(hljs) { 'get guard if import in indirect infix init inout internal is lazy left let ' + 'mutating nil none nonmutating open operator optional override postfix precedence ' + 'prefix private protocol Protocol public repeat required rethrows return ' + - 'right self Self set static struct subscript super switch throw throws true ' + + 'right self Self set some static struct subscript super switch throw throws true ' + 'try try! try? Type typealias unowned var weak where while willSet', literal: 'true false nil', built_in: 'abs advance alignof alignofValue anyGenerator assert assertionFailure ' + @@ -148,7 +151,7 @@ export default function(hljs) { '@noreturn|@IBAction|@IBDesignable|@IBInspectable|@IBOutlet|' + '@infix|@prefix|@postfix|@autoclosure|@testable|@available|' + '@nonobjc|@NSApplicationMain|@UIApplicationMain|@dynamicMemberLookup|' + - '@propertyWrapper)\\b' + '@propertyWrapper|@main)\\b' }, { diff --git a/test/markup/swift/swiftui.expect.txt b/test/markup/swift/swiftui.expect.txt new file mode 100644 index 0000000000..0d9fae6261 --- /dev/null +++ b/test/markup/swift/swiftui.expect.txt @@ -0,0 +1,12 @@ +@main +struct MyApp: App { + var body: some Scene { + WindowGroup { + #if os(iOS) + Text("Hello, world from iOS!") + #else + Text("Hello, world!") + #endif + } + } +} diff --git a/test/markup/swift/swiftui.txt b/test/markup/swift/swiftui.txt new file mode 100644 index 0000000000..374d45bffe --- /dev/null +++ b/test/markup/swift/swiftui.txt @@ -0,0 +1,12 @@ +@main +struct MyApp: App { + var body: some Scene { + WindowGroup { + #if os(iOS) + Text("Hello, world from iOS!") + #else + Text("Hello, world!") + #endif + } + } +}