Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/swift-short-macro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tanstack/highlight": patch
---

Recognize Swift macro declarations with short names while preserving identifiers and member calls.
2 changes: 1 addition & 1 deletion src/languages/swift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const patterns = [
// Contextual words are keywords only before declarations, types, or as accessors.
{
className: 'keyword',
regex: /(?<!`)\b(?:(?<!\.)(?:associatedtype|as|async|await|break|case|catch|class|continue|convenience|default|defer|deinit|didSet|distributed|do|else|enum|extension|fallthrough|fileprivate|for|func|guard|if|import|in|indirect|inout|internal|is|let|mutating|nonisolated|nonmutating|operator|override|precedencegroup|private|protocol|public|repeat|rethrows|return|static|struct|subscript|switch|throw|throws|try|typealias|unowned|var|where|while|willSet)|init|self|Self|super)\b|(?<![\w.`])(?:(?:open|package|optional|dynamic|final|lazy|weak|required|prefix|infix|postfix|actor|macro|some|any|each|isolated|consuming|borrowing|sending)(?=[ \t]+(?:[a-z]{3}|[A-Z([]))|[gs]et(?=[ \t]*[{}]|[ \t]+[a-z]|\(\w+\)[ \t]*\{)|(?<=\()set(?=\)))/g,
regex: /(?<!`)\b(?:(?<!\.)(?:associatedtype|as|async|await|break|case|catch|class|continue|convenience|default|defer|deinit|didSet|distributed|do|else|enum|extension|fallthrough|fileprivate|for|func|guard|if|import|in|indirect|inout|internal|is|let|mutating|nonisolated|nonmutating|operator|override|precedencegroup|private|protocol|public|repeat|rethrows|return|static|struct|subscript|switch|throw|throws|try|typealias|unowned|var|where|while|willSet)|init|self|Self|super)\b|(?<![\w.`])(?:macro(?=[ \t]+[A-Za-z_]\w*[(<])|(?:open|package|optional|dynamic|final|lazy|weak|required|prefix|infix|postfix|actor|macro|some|any|each|isolated|consuming|borrowing|sending)(?=[ \t]+(?:[a-z]{3}|[A-Z([]))|[gs]et(?=[ \t]*[{}]|[ \t]+[a-z]|\(\w+\)[ \t]*\{)|(?<=\()set(?=\)))/g,
},
{ className: 'literal', regex: /(?<![.`])\b(?:true|false|nil)\b/g },
{ className: 'type', regex: /\b(?:class|struct|enum|protocol|extension|actor|typealias|associatedtype)\s+(\w+)/g, group: 1 },
Expand Down
10 changes: 10 additions & 0 deletions test/swift.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,16 @@ let package = Package(
expect(classes(code, '.testTarget(name:')).toEqual([undefined, 'function', undefined, 'property', undefined])
})

it('recognizes macro declarations with short names', () => {
for (const name of ['m', 'm<T>']) {
const code = `public macro ${name}() = #externalMacro(module: "M", type: "T")`
expect(classes(code, 'macro')).toEqual(['keyword'])
expect(classes(code, '#externalMacro')).toEqual(['meta'])
}
expect(classes('let macro = value', 'macro')).not.toContain('keyword')
expect(classes('value.macro()', 'macro')).toEqual(['function'])
})

it('registers only the swift name', () => {
expect(highlighter.normalizeLanguage('swift')).toBe('swift')
expect(createHighlighter({ languages: [] }).normalizeLanguage('swift')).toBe('plaintext')
Expand Down
Loading