Skip to content

fix(swift) - add proper syntax highlighting for class func/var declarations #4154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Core Grammars:
- fix(cpp) added flat_set and flat_map as a part of cpp 23 version [Lavan]
- fix(yaml) - Fixed special chars in yaml [Dxuian]
- fix(basic) - Fixed closing quotation marks not required for a PRINT statement [Somya]
- fix(swift) - Fixed syntax highlighting for class func/var declarations [guuido]

New Grammars:

Expand Down Expand Up @@ -58,6 +59,7 @@ CONTRIBUTORS
[Álvaro Mondéjar]: https://github.com/mondeja
[Lavan]: https://github.com/jvlavan
[Somya]: https://github.com/somya-05
[guuido]: https://github.com/guuido


## Version 11.10.0
Expand Down
29 changes: 29 additions & 0 deletions src/languages/swift.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,33 @@ export default function(hljs) {
end: /}/
};

const CLASS_FUNC_DECLARATION = {
match: [
/class\b/,
/\s+/,
/func\b/,
/\s+/,
/\b[A-Za-z_][A-Za-z0-9_]*\b/
],
scope: {
1: "keyword",
3: "keyword",
5: "title.function"
}
};

const CLASS_VAR_DECLARATION = {
match: [
/class\b/,
/\s+/,
/var\b/,
],
scope: {
1: "keyword",
3: "keyword"
}
};

const TYPE_DECLARATION = {
begin: [
/(struct|protocol|class|extension|enum|actor)/,
Expand Down Expand Up @@ -524,6 +551,8 @@ export default function(hljs) {
...COMMENTS,
FUNCTION_OR_MACRO,
INIT_SUBSCRIPT,
CLASS_FUNC_DECLARATION,
CLASS_VAR_DECLARATION,
TYPE_DECLARATION,
OPERATOR_DECLARATION,
PRECEDENCEGROUP,
Expand Down
15 changes: 15 additions & 0 deletions test/markup/swift/class-func-var.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<span class="hljs-keyword">class</span> <span class="hljs-keyword">func</span> <span class="hljs-title function_">f1</span>() -&gt; <span class="hljs-type">Void</span> { }

<span class="hljs-keyword">class</span> <span class="hljs-keyword">func</span> <span class="hljs-title function_">f2</span><span class="hljs-operator">&lt;</span><span class="hljs-type">T</span>: <span class="hljs-type">Comparable</span><span class="hljs-operator">&gt;</span>() -&gt; <span class="hljs-type">T</span> { }

<span class="hljs-keyword">class</span> <span class="hljs-keyword">func</span> <span class="hljs-title function_">f3</span><span class="hljs-operator">&lt;</span><span class="hljs-type">T</span><span class="hljs-operator">&gt;</span>(
<span class="hljs-keyword">_</span> p1: <span class="hljs-type">Int</span>,
p2: <span class="hljs-type">String</span>
) <span class="hljs-keyword">async</span> <span class="hljs-keyword">throws</span> -&gt; [<span class="hljs-type">T</span>] { }

<span class="hljs-keyword">class</span> <span class="hljs-keyword">var</span> v1: <span class="hljs-type">Int</span> { <span class="hljs-keyword">get</span> <span class="hljs-keyword">set</span> }

<span class="hljs-keyword">class</span> <span class="hljs-keyword">var</span> v2: <span class="hljs-type">String</span> {
<span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;test&quot;</span> }
<span class="hljs-keyword">set</span> { }
}
15 changes: 15 additions & 0 deletions test/markup/swift/class-func-var.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class func f1() -> Void { }

class func f2<T: Comparable>() -> T { }

class func f3<T>(
_ p1: Int,
p2: String
) async throws -> [T] { }

class var v1: Int { get set }

class var v2: String {
get { return "test" }
set { }
}