Skip to content

Commit

Permalink
Add macro declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
taku0 committed Sep 30, 2023
1 parent ef36acf commit a22f053
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 25 deletions.
5 changes: 3 additions & 2 deletions swift-mode-beginning-of-defun.el
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ The cursor must be at the beginning of a statement."
(defun-keywords
'("import" "typealias" "associatedtype"
"enum" "struct" "actor" "protocol" "extension"
"func" "init" "deinit" "subscript" "get" "set" "willSet" "didSet"
"func" "init" "deinit" "subscript" "macro"
"get" "set" "willSet" "didSet"
"prefix" "postfix" "infix" "precedencegroup"
"var" "let"
"case"))
Expand Down Expand Up @@ -1441,7 +1442,7 @@ of ancestors."
name-token
(cond
((member keyword-text
'("typealias" "associatedtype" "precedencegroup" "func"
'("typealias" "associatedtype" "precedencegroup" "func" "macro"
"class" "enum" "struct" "actor" "protocol" "extension"))
(swift-mode:forward-token))

Expand Down
20 changes: 10 additions & 10 deletions swift-mode-font-lock.el
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

(require 'swift-mode-standard-types)
(require 'seq)
(require 'subr-x)

;;; Customizations

Expand Down Expand Up @@ -194,7 +195,12 @@ This function does not search beyond LIMIT."
(and
(< (point) limit)
(looking-at
"\\<\\(func\\|enum\\|struct\\|class\\|protocol\\|extension\\|actor\\)\\>")))
(concat
"\\<\\("
(string-join
'("func" "enum" "struct" "class" "protocol" "extension" "actor" "macro")
"\\|")
"\\)\\>"))))

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

(defconst swift-mode:preprocessor-keywords
'("#available" "#colorLiteral" "#column" "#dsohandle" "#else" "#elseif"
"#endif" "#error" "#file" "#filePath" "#fileLiteral" "#function" "#if"
"#imageLiteral" "#keyPath" "#line" "#selector" "#sourceLocation"
"#unavailable" "#warning")
"Keywords that begin with a number sign (#).")

(defconst swift-mode:declaration-keywords
'("associatedtype" "class" "deinit" "enum" "extension" "fileprivate" "func"
"import" "init" "inout" "internal" "let" "open" "operator" "package"
"private" "protocol" "public" "any" "some" "static" "struct" "subscript"
"typealias" "var" "actor" "nonisolated" "isolated" "distributed"
"borrowing" "consuming")
"borrowing" "consuming" "macro")
"Keywords used in declarations.")

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

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

Expand Down
10 changes: 5 additions & 5 deletions swift-mode-imenu.el
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ Return found declarations in reverse order."
(swift-mode:declaration (intern next-text) name-token nil)
declarations)))

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

(defun swift-mode:scan-function-name-and-parameter-names ()
"Parse function name and parameter names.
"Parse function/macro name and parameter names.
The point is assumed to be before a function name.
The point is assumed to be before a function/macro name.
Return tokens of function names and parameter names.
Return tokens of function/macro names and parameter names.
For example, given the following code, this return tokens \"foo\", \"a\",
and \"c\".
Expand Down
2 changes: 1 addition & 1 deletion swift-mode-indent.el
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,7 @@ It is a Generic parameter list if:
\;
{ } \( \) \[ \]
"true" "false"
"class" "struct" "actor" "enum" "extension" "func" "operator"
"class" "struct" "actor" "enum" "extension" "func" "operator" "macro"
"try" "try?" "try!"
"as" "as?" "as!"
"is"
Expand Down
14 changes: 8 additions & 6 deletions swift-mode-lexer.el
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,8 @@ return non-nil."
;; Suppress implicit semicolon after declaration starters.
((member (swift-mode:token:text previous-token)
'("class" "struct" "actor" "protocol" "enum" "extension" "func"
"typealias" "associatedtype" "precedencegroup" "operator"))
"typealias" "associatedtype" "precedencegroup" "operator"
"macro"))
nil)

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

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

(defun swift-mode:function-parameter-clause-p ()
"Return t if the cursor is before a function parameter clause.
"Return t if the cursor is before a function/macro parameter clause.
Return nil otherwise."
(save-excursion
Expand All @@ -856,8 +857,8 @@ Return nil otherwise."
(progn (swift-mode:try-backward-generic-parameters) (point)))
(swift-mode:function-parameter-clause-p)))
((eq previous-type 'identifier)
(equal (swift-mode:token:text (swift-mode:backward-token-simple))
"func"))
(member (swift-mode:token:text (swift-mode:backward-token-simple))
'("func" "macro")))
(t nil)))))

(defun swift-mode:supertype-colon-p ()
Expand Down Expand Up @@ -948,7 +949,8 @@ Return nil otherwise."
(or (member (swift-mode:token:text (swift-mode:backward-token-simple))
'("init" "subscript"))
(member (swift-mode:token:text (swift-mode:backward-token-simple))
'("typealias" "func" "enum" "struct" "actor" "class" "init")))))
'("typealias" "func" "enum" "struct" "actor" "class" "init"
"macro")))))

(defun swift-mode:fix-operator-type (token)
"Return new operator token with proper token type.
Expand Down
12 changes: 12 additions & 0 deletions test/swift-files/beginning-of-defun/beginning-of-defun.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,18 @@ public
}/*}*/
}/*}*/

// /*[*/Macro declaratoins/*]*/
/*{*/macro foo() = #bar/*}*/

/*{*/@Foo
macro
foo<T>()
=
#bar
where
T: A,
T == B/*}*/

// /*[*/Multiple declaratoins in single line/*]*/

/*{*/func foo(){};/*}*/ /*{*/func foo(){/*{*/func foo(){}/*}*/};/*}*//*{*/func foo(){} ;/*}*/ /*{*/func foo() {} /* */ ;/*}*/ /*{*//* */ func foo() {}/*}*/
Expand Down
3 changes: 2 additions & 1 deletion test/swift-files/imenu/imenu-expected.eld
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
((precedencegroup (identifier "precedenceGroup" 2759 2774) nil)
((macro (identifier "fooMacro(x:)" 2866 2874) nil)
(precedencegroup (identifier "precedenceGroup" 2759 2774) nil)
(operator (identifier "*****" 2719 2724) nil)
(operator (identifier "-----" 2679 2684) nil)
(operator (identifier "+++++" 2640 2645) nil)
Expand Down
2 changes: 2 additions & 0 deletions test/swift-files/imenu/imenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,5 @@ precedencegroup precedenceGroup {
associativity: left
assignment: true
}

macro fooMacro<T>(x: Foo) = #bar
18 changes: 18 additions & 0 deletions test/swift-files/indent/declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -870,3 +870,21 @@ func constrain<each S: Sequence>(
.Element
) == (Int, String) {
}


// Macro declaration

@Foo
@freestanding(expression)
macro
foo<T>(_: T)
->
(T, String)
=
#externalMacro(
module: "A",
type: "B"
)
where
T: AAA,
T == Foo
3 changes: 3 additions & 0 deletions test/swift-files/indent/identifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ func foo() {
foo(
isolated: 1
)
foo(
macro: 1
)

// Keywords used in statements
foo(
Expand Down

0 comments on commit a22f053

Please sign in to comment.