Skip to content

Commit

Permalink
Add discard self
Browse files Browse the repository at this point in the history
  • Loading branch information
taku0 committed Sep 30, 2023
1 parent 6b19157 commit 1339e24
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion swift-mode-font-lock.el
Original file line number Diff line number Diff line change
Expand Up @@ -535,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" "copy")
"throw" "try" "async" "await" "consume" "copy" "discard")
"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" "copy"
"consume" "copy" "discard"
"in"
"init" "deinit" "get" "set" "willSet" "didSet" "subscript"
"for" "case" "default" "while" "let" "var" "repeat" "if" "else"
Expand Down
14 changes: 8 additions & 6 deletions swift-mode-lexer.el
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ END is the point after the token."

;; Token types is one of the following symbols:
;;
;; - prefix-operator (including try, try?, try!, await, consume, and copy)
;; - prefix-operator (including try, try?, try!, await, consume, copy, and
;; discard)
;; - postfix-operator
;; - binary-operator (including as, as?, as!, is, =, ., and ->)
;; - attribute (e.g. @objc, @abc(def))
Expand Down Expand Up @@ -988,7 +989,8 @@ Other properties are the same as the TOKEN."
(type
(cond
(is-declaration 'identifier)
((member text '("try" "try?" "try!" "await" "consume" "copy"))
((member text '("try" "try?" "try!" "await" "consume" "copy"
"discard"))
'prefix-operator)
((equal text ".") 'binary-operator)
((and has-preceding-space has-following-space) 'binary-operator)
Expand Down Expand Up @@ -1147,7 +1149,7 @@ This function does not return `implicit-;' or `type-:'."
pos-after-comment
(point))))

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

;; Operator (other than as, try, is, await, consume, or copy)
;; Operator (other than as, try, is, await, consume, copy, or discard)
;;
;; Operators which starts with a dot can contain other dots. Other
;; operators cannot contain dots.
Expand Down Expand Up @@ -1501,7 +1503,7 @@ This function does not return `implicit-;' or `type-:'."
text
(point)
(+ (point) (length text))))
((member text '("try" "await" "consume" "copy"))
((member text '("try" "await" "consume" "copy" "discard"))
(swift-mode:token 'prefix-operator
text
(point)
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 @@ -843,3 +843,13 @@ func foo() {
copy
x
}


// discard self
// https://github.com/apple/swift-evolution/blob/main/proposals/0390-noncopyable-structs-and-enums.md

func foo() {
// discard operator cannot be followed by a line break.
discard
self
}
3 changes: 3 additions & 0 deletions test/swift-files/indent/identifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ func foo() {
foo(
copy: 1
)
foo(
discard: 1
)

// Keywords reserved in particular contexts
foo(
Expand Down

0 comments on commit 1339e24

Please sign in to comment.