Skip to content

Commit a22f053

Browse files
committed
Add macro declaration
1 parent ef36acf commit a22f053

10 files changed

+64
-25
lines changed

swift-mode-beginning-of-defun.el

+3-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ The cursor must be at the beginning of a statement."
169169
(defun-keywords
170170
'("import" "typealias" "associatedtype"
171171
"enum" "struct" "actor" "protocol" "extension"
172-
"func" "init" "deinit" "subscript" "get" "set" "willSet" "didSet"
172+
"func" "init" "deinit" "subscript" "macro"
173+
"get" "set" "willSet" "didSet"
173174
"prefix" "postfix" "infix" "precedencegroup"
174175
"var" "let"
175176
"case"))
@@ -1441,7 +1442,7 @@ of ancestors."
14411442
name-token
14421443
(cond
14431444
((member keyword-text
1444-
'("typealias" "associatedtype" "precedencegroup" "func"
1445+
'("typealias" "associatedtype" "precedencegroup" "func" "macro"
14451446
"class" "enum" "struct" "actor" "protocol" "extension"))
14461447
(swift-mode:forward-token))
14471448

swift-mode-font-lock.el

+10-10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
(require 'swift-mode-standard-types)
3434
(require 'seq)
35+
(require 'subr-x)
3536

3637
;;; Customizations
3738

@@ -194,7 +195,12 @@ This function does not search beyond LIMIT."
194195
(and
195196
(< (point) limit)
196197
(looking-at
197-
"\\<\\(func\\|enum\\|struct\\|class\\|protocol\\|extension\\|actor\\)\\>")))
198+
(concat
199+
"\\<\\("
200+
(string-join
201+
'("func" "enum" "struct" "class" "protocol" "extension" "actor" "macro")
202+
"\\|")
203+
"\\)\\>"))))
198204

199205
(defun swift-mode:property-access-pos-p (pos limit)
200206
"Return t if POS is just before the property name of a member expression.
@@ -513,19 +519,12 @@ Return nil otherwise."
513519
'("true" "false" "nil")
514520
"Keywords used as constants.")
515521

516-
(defconst swift-mode:preprocessor-keywords
517-
'("#available" "#colorLiteral" "#column" "#dsohandle" "#else" "#elseif"
518-
"#endif" "#error" "#file" "#filePath" "#fileLiteral" "#function" "#if"
519-
"#imageLiteral" "#keyPath" "#line" "#selector" "#sourceLocation"
520-
"#unavailable" "#warning")
521-
"Keywords that begin with a number sign (#).")
522-
523522
(defconst swift-mode:declaration-keywords
524523
'("associatedtype" "class" "deinit" "enum" "extension" "fileprivate" "func"
525524
"import" "init" "inout" "internal" "let" "open" "operator" "package"
526525
"private" "protocol" "public" "any" "some" "static" "struct" "subscript"
527526
"typealias" "var" "actor" "nonisolated" "isolated" "distributed"
528-
"borrowing" "consuming")
527+
"borrowing" "consuming" "macro")
529528
"Keywords used in declarations.")
530529

531530
(defconst swift-mode:statement-keywords
@@ -590,7 +589,8 @@ Excludes true, false, and keywords begin with a number sign.")
590589
.
591590
'swift-mode:constant-keyword-face)
592591

593-
(,(regexp-opt swift-mode:preprocessor-keywords 'symbols)
592+
;; Preprocessor keywords
593+
(,"#\\(\\sw\\|\\s_\\)*"
594594
.
595595
'swift-mode:preprocessor-keyword-face)
596596

swift-mode-imenu.el

+5-5
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ Return found declarations in reverse order."
185185
(swift-mode:declaration (intern next-text) name-token nil)
186186
declarations)))
187187

188-
((member next-text '("func" "init" "subscript"))
188+
((member next-text '("func" "init" "subscript" "macro"))
189189
(setq last-class-token nil)
190-
(unless (equal next-text "func")
190+
(unless (member next-text '("func" "macro"))
191191
(goto-char (swift-mode:token:start next-token)))
192192
(let ((declaration-type (intern next-text))
193193
(names (swift-mode:scan-function-name-and-parameter-names)))
@@ -324,11 +324,11 @@ TYPE is one of `case', `let', or `var'."
324324
items))
325325

326326
(defun swift-mode:scan-function-name-and-parameter-names ()
327-
"Parse function name and parameter names.
327+
"Parse function/macro name and parameter names.
328328
329-
The point is assumed to be before a function name.
329+
The point is assumed to be before a function/macro name.
330330
331-
Return tokens of function names and parameter names.
331+
Return tokens of function/macro names and parameter names.
332332
333333
For example, given the following code, this return tokens \"foo\", \"a\",
334334
and \"c\".

swift-mode-indent.el

+1-1
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,7 @@ It is a Generic parameter list if:
16441644
\;
16451645
{ } \( \) \[ \]
16461646
"true" "false"
1647-
"class" "struct" "actor" "enum" "extension" "func" "operator"
1647+
"class" "struct" "actor" "enum" "extension" "func" "operator" "macro"
16481648
"try" "try?" "try!"
16491649
"as" "as?" "as!"
16501650
"is"

swift-mode-lexer.el

+8-6
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,8 @@ return non-nil."
694694
;; Suppress implicit semicolon after declaration starters.
695695
((member (swift-mode:token:text previous-token)
696696
'("class" "struct" "actor" "protocol" "enum" "extension" "func"
697-
"typealias" "associatedtype" "precedencegroup" "operator"))
697+
"typealias" "associatedtype" "precedencegroup" "operator"
698+
"macro"))
698699
nil)
699700

700701
;; Insert implicit semicolon before modifiers.
@@ -765,7 +766,7 @@ return non-nil."
765766
;; `protocol' is handled by the next rule
766767
((member (swift-mode:token:text next-token)
767768
'("class" "struct" "actor" "enum" "extension" "func" "typealias"
768-
"associatedtype" "precedencegroup"))
769+
"associatedtype" "precedencegroup" "macro"))
769770
t)
770771

771772
;; Inserts implicit semicolon before protocol unless it is followed by <.
@@ -842,7 +843,7 @@ return non-nil."
842843
(t t))))
843844

844845
(defun swift-mode:function-parameter-clause-p ()
845-
"Return t if the cursor is before a function parameter clause.
846+
"Return t if the cursor is before a function/macro parameter clause.
846847
847848
Return nil otherwise."
848849
(save-excursion
@@ -856,8 +857,8 @@ Return nil otherwise."
856857
(progn (swift-mode:try-backward-generic-parameters) (point)))
857858
(swift-mode:function-parameter-clause-p)))
858859
((eq previous-type 'identifier)
859-
(equal (swift-mode:token:text (swift-mode:backward-token-simple))
860-
"func"))
860+
(member (swift-mode:token:text (swift-mode:backward-token-simple))
861+
'("func" "macro")))
861862
(t nil)))))
862863

863864
(defun swift-mode:supertype-colon-p ()
@@ -948,7 +949,8 @@ Return nil otherwise."
948949
(or (member (swift-mode:token:text (swift-mode:backward-token-simple))
949950
'("init" "subscript"))
950951
(member (swift-mode:token:text (swift-mode:backward-token-simple))
951-
'("typealias" "func" "enum" "struct" "actor" "class" "init")))))
952+
'("typealias" "func" "enum" "struct" "actor" "class" "init"
953+
"macro")))))
952954

953955
(defun swift-mode:fix-operator-type (token)
954956
"Return new operator token with proper token type.

test/swift-files/beginning-of-defun/beginning-of-defun.swift

+12
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,18 @@ public
299299
}/*}*/
300300
}/*}*/
301301

302+
// /*[*/Macro declaratoins/*]*/
303+
/*{*/macro foo() = #bar/*}*/
304+
305+
/*{*/@Foo
306+
macro
307+
foo<T>()
308+
=
309+
#bar
310+
where
311+
T: A,
312+
T == B/*}*/
313+
302314
// /*[*/Multiple declaratoins in single line/*]*/
303315

304316
/*{*/func foo(){};/*}*/ /*{*/func foo(){/*{*/func foo(){}/*}*/};/*}*//*{*/func foo(){} ;/*}*/ /*{*/func foo() {} /* */ ;/*}*/ /*{*//* */ func foo() {}/*}*/

test/swift-files/imenu/imenu-expected.eld

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
((precedencegroup (identifier "precedenceGroup" 2759 2774) nil)
1+
((macro (identifier "fooMacro(x:)" 2866 2874) nil)
2+
(precedencegroup (identifier "precedenceGroup" 2759 2774) nil)
23
(operator (identifier "*****" 2719 2724) nil)
34
(operator (identifier "-----" 2679 2684) nil)
45
(operator (identifier "+++++" 2640 2645) nil)

test/swift-files/imenu/imenu.swift

+2
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,5 @@ precedencegroup precedenceGroup {
124124
associativity: left
125125
assignment: true
126126
}
127+
128+
macro fooMacro<T>(x: Foo) = #bar

test/swift-files/indent/declarations.swift

+18
Original file line numberDiff line numberDiff line change
@@ -870,3 +870,21 @@ func constrain<each S: Sequence>(
870870
.Element
871871
) == (Int, String) {
872872
}
873+
874+
875+
// Macro declaration
876+
877+
@Foo
878+
@freestanding(expression)
879+
macro
880+
foo<T>(_: T)
881+
->
882+
(T, String)
883+
=
884+
#externalMacro(
885+
module: "A",
886+
type: "B"
887+
)
888+
where
889+
T: AAA,
890+
T == Foo

test/swift-files/indent/identifiers.swift

+3
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ func foo() {
127127
foo(
128128
isolated: 1
129129
)
130+
foo(
131+
macro: 1
132+
)
130133

131134
// Keywords used in statements
132135
foo(

0 commit comments

Comments
 (0)