Skip to content

Commit

Permalink
Add borrowing and consuming modifiers and copy operator
Browse files Browse the repository at this point in the history
  • Loading branch information
taku0 committed Sep 30, 2023
1 parent 221863c commit 6194d95
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
5 changes: 3 additions & 2 deletions swift-mode-font-lock.el
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,8 @@ Return nil otherwise."
'("associatedtype" "class" "deinit" "enum" "extension" "fileprivate" "func"
"import" "init" "inout" "internal" "let" "open" "operator" "private"
"protocol" "public" "any" "some" "static" "struct" "subscript" "typealias"
"var" "actor" "nonisolated" "isolated" "distributed")
"var" "actor" "nonisolated" "isolated" "distributed"
"borrowing" "consuming")
"Keywords used in declarations.")

(defconst swift-mode:statement-keywords
Expand All @@ -534,7 +535,7 @@ Return nil otherwise."

(defconst swift-mode:expression-keywords
'("as" "catch" "dynamicType" "is" "rethrows" "super" "self" "Self" "throws"
"throw" "try" "async" "await" "consume")
"throw" "try" "async" "await" "consume" "copy")
"Keywords used in expressions and types.
Excludes true, false, and keywords begin with a number sign.")
Expand Down
2 changes: 1 addition & 1 deletion swift-mode-indent.el
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,7 @@ It is a Generic parameter list if:
"as" "as?" "as!"
"is"
"await"
"consume"
"consume" "copy"
"in"
"init" "deinit" "get" "set" "willSet" "didSet" "subscript"
"for" "case" "default" "while" "let" "var" "repeat" "if" "else"
Expand Down
19 changes: 10 additions & 9 deletions swift-mode-lexer.el
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ END is the point after the token."

;; Token types is one of the following symbols:
;;
;; - prefix-operator (including try, try?, try!, await, and consume)
;; - prefix-operator (including try, try?, try!, await, consume, and copy)
;; - postfix-operator
;; - binary-operator (including as, as?, as!, is, =, ., and ->)
;; - attribute (e.g. @objc, @abc(def))
Expand Down Expand Up @@ -609,10 +609,11 @@ return non-nil."
;; Suppress implicit semicolon around keywords that cannot start or end
;; statements.
(member (swift-mode:token:text previous-token)
'("any" "some" "inout" "in" "where" "isolated"))
'("any" "some" "inout" "borrowing" "consuming" "in" "where"
"isolated"))
(member (swift-mode:token:text next-token)
'("any" "some" "inout" "throws" "rethrows" "in" "where"
"isolated")))
'("any" "some" "inout" "borrowing" "consuming" "throws"
"rethrows" "in" "where" "isolated")))
nil)

;; Before async
Expand Down Expand Up @@ -987,7 +988,7 @@ Other properties are the same as the TOKEN."
(type
(cond
(is-declaration 'identifier)
((member text '("try" "try?" "try!" "await" "consume"))
((member text '("try" "try?" "try!" "await" "consume" "copy"))
'prefix-operator)
((equal text ".") 'binary-operator)
((and has-preceding-space has-following-space) 'binary-operator)
Expand Down Expand Up @@ -1146,7 +1147,7 @@ This function does not return `implicit-;' or `type-:'."
pos-after-comment
(point))))

;; Operator (other than as, try, is, await, or consume)
;; Operator (other than as, try, is, await, consume, or copy)
;;
;; Operators starts with a dot can contains dots. Other operators cannot
;; contain dots.
Expand Down Expand Up @@ -1245,7 +1246,7 @@ This function does not return `implicit-;' or `type-:'."
text
(- (point) (length text))
(point)))
((member text '("await" "consume"))
((member text '("await" "consume" "copy"))
(swift-mode:token 'prefix-operator
text
(- (point) (length text))
Expand Down Expand Up @@ -1418,7 +1419,7 @@ This function does not return `implicit-;' or `type-:'."
(point)
pos-before-comment)))

;; Operator (other than as, try, is, await, or consume)
;; Operator (other than as, try, is, await, consume, or copy)
;;
;; Operators which starts with a dot can contain other dots. Other
;; operators cannot contain dots.
Expand Down Expand Up @@ -1500,7 +1501,7 @@ This function does not return `implicit-;' or `type-:'."
text
(point)
(+ (point) (length text))))
((member text '("try" "await" "consume"))
((member text '("try" "await" "consume" "copy"))
(swift-mode:token 'prefix-operator
text
(point)
Expand Down
2 changes: 2 additions & 0 deletions test/swift-files/indent/declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,13 @@ private
inout
Int,
y:
borrowing
Int
=
1,
z,
w:
consuming
Int
...
)
Expand Down
10 changes: 10 additions & 0 deletions test/swift-files/indent/expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -833,3 +833,13 @@ func foo() {

return y
}


// copy operator
// https://github.com/apple/swift-evolution/blob/main/proposals/0377-parameter-ownership-modifiers.md

func foo() {
// copy operator cannot be followed by a line break.
copy
x
}
9 changes: 9 additions & 0 deletions test/swift-files/indent/identifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func foo() {
foo(
inout: 1
)
foo(
borrowing: 1
)
foo(
consuming: 1
)
foo(
internal: 1
)
Expand Down Expand Up @@ -215,6 +221,9 @@ func foo() {
foo(
consume: 1
)
foo(
copy: 1
)

// Keywords reserved in particular contexts
foo(
Expand Down

0 comments on commit 6194d95

Please sign in to comment.