Skip to content

Commit

Permalink
(swift) improvements: support some, @main; Fix # keywords . (#2845
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
mportiz08 authored Nov 11, 2020
1 parent 435f6c6 commit 6bc573c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,4 @@ Contributors:
- Marat Nagayev <[email protected]>
- Patrick Scheibe <[email protected]>
- Kyle Brown <kylebrown9@github>
- Marcus Ortiz <[email protected]>
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/languages/swift.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' +
Expand All @@ -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 ' +
Expand Down Expand Up @@ -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'

},
{
Expand Down
12 changes: 12 additions & 0 deletions test/markup/swift/swiftui.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<span class="hljs-meta">@main</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">MyApp</span>: <span class="hljs-title">App</span> </span>{
<span class="hljs-keyword">var</span> body: <span class="hljs-keyword">some</span> <span class="hljs-type">Scene</span> {
<span class="hljs-type">WindowGroup</span> {
<span class="hljs-keyword">#if</span> os(iOS)
<span class="hljs-type">Text</span>(<span class="hljs-string">&quot;Hello, world from iOS!&quot;</span>)
<span class="hljs-keyword">#else</span>
<span class="hljs-type">Text</span>(<span class="hljs-string">&quot;Hello, world!&quot;</span>)
<span class="hljs-keyword">#endif</span>
}
}
}
12 changes: 12 additions & 0 deletions test/markup/swift/swiftui.txt
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 6bc573c

Please sign in to comment.