From 6194d9578d6e02e49343fc02089a093dbcbcdbfc Mon Sep 17 00:00:00 2001 From: taku0 Date: Sun, 24 Sep 2023 16:15:46 +0900 Subject: [PATCH] Add `borrowing` and `consuming` modifiers and `copy` operator https://github.com/apple/swift-evolution/blob/main/proposals/0377-parameter-ownership-modifiers.md --- swift-mode-font-lock.el | 5 +++-- swift-mode-indent.el | 2 +- swift-mode-lexer.el | 19 ++++++++++--------- test/swift-files/indent/declarations.swift | 2 ++ test/swift-files/indent/expressions.swift | 10 ++++++++++ test/swift-files/indent/identifiers.swift | 9 +++++++++ 6 files changed, 35 insertions(+), 12 deletions(-) diff --git a/swift-mode-font-lock.el b/swift-mode-font-lock.el index cd2e030..f37ab79 100644 --- a/swift-mode-font-lock.el +++ b/swift-mode-font-lock.el @@ -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 @@ -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.") diff --git a/swift-mode-indent.el b/swift-mode-indent.el index c1ce923..bf3e283 100644 --- a/swift-mode-indent.el +++ b/swift-mode-indent.el @@ -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" diff --git a/swift-mode-lexer.el b/swift-mode-lexer.el index d6f30bb..2f1d3c3 100644 --- a/swift-mode-lexer.el +++ b/swift-mode-lexer.el @@ -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)) @@ -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 @@ -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) @@ -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. @@ -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)) @@ -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. @@ -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) diff --git a/test/swift-files/indent/declarations.swift b/test/swift-files/indent/declarations.swift index f0dae56..9674369 100644 --- a/test/swift-files/indent/declarations.swift +++ b/test/swift-files/indent/declarations.swift @@ -343,11 +343,13 @@ private inout Int, y: + borrowing Int = 1, z, w: + consuming Int ... ) diff --git a/test/swift-files/indent/expressions.swift b/test/swift-files/indent/expressions.swift index 4b5ff2a..2d8d6ab 100644 --- a/test/swift-files/indent/expressions.swift +++ b/test/swift-files/indent/expressions.swift @@ -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 +} diff --git a/test/swift-files/indent/identifiers.swift b/test/swift-files/indent/identifiers.swift index f57c79d..54fd89d 100644 --- a/test/swift-files/indent/identifiers.swift +++ b/test/swift-files/indent/identifiers.swift @@ -67,6 +67,12 @@ func foo() { foo( inout: 1 ) + foo( + borrowing: 1 + ) + foo( + consuming: 1 + ) foo( internal: 1 ) @@ -215,6 +221,9 @@ func foo() { foo( consume: 1 ) + foo( + copy: 1 + ) // Keywords reserved in particular contexts foo(