Skip to content

Commit

Permalink
Fix indentation for keywords used as parameter names
Browse files Browse the repository at this point in the history
```
foo(
  in: 1
)

foo(
  where: 1
)
```
  • Loading branch information
taku0 committed Jan 29, 2017
1 parent 6cd2948 commit 42669da
Show file tree
Hide file tree
Showing 3 changed files with 308 additions and 54 deletions.
1 change: 0 additions & 1 deletion swift-mode-font-lock.el
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ Return nil otherwise."
"\\<catch\\>"
"\\<dynamicType\\>"
"\\<is\\>"
"\\<nil\\>"
"\\<rethrows\\>"
"\\<super\\>"
"\\<self\\>"
Expand Down
106 changes: 53 additions & 53 deletions swift-mode-indent.el
Original file line number Diff line number Diff line change
Expand Up @@ -217,46 +217,6 @@ declaration and its offset is `swift-mode:basic-offset'."
(goto-char (swift-mode:token:start previous-token))
(swift-mode:calculate-indent-after-comma))

;; Before "in" on the same line
((and next-is-on-same-line (equal next-text "in"))
;; When it is for-in statement, align with the token after "for":
;;
;; for
;; x
;; in
;; foo
;;
;; for x
;; in
;; foo
;;
;; When it is anonymous function, align with the token after {:
;;
;; foo {
;; x
;; in
;; ...
;; }
;;
;;
;; foo { x
;; in
;; ...
;; }
;;
;; foo { [
;; weak self
;; ]
;; (
;; x,
;; y
;; )
;; -> Void
;; in
;; a
;; }
(swift-mode:find-and-align-with-parents '("for" {)))

;; Before "case" or "default" on the same line, for switch statement
((and
next-is-on-same-line
Expand Down Expand Up @@ -340,6 +300,59 @@ declaration and its offset is `swift-mode:basic-offset'."
swift-mode:statement-parent-tokens
swift-mode:multiline-statement-offset))

;; After {
((eq previous-type '{)
(goto-char (swift-mode:token:start previous-token))
(swift-mode:calculate-indent-after-open-curly-brace
swift-mode:basic-offset))

;; After ( or [
((memq previous-type '(\( \[))
(goto-char (swift-mode:token:start previous-token))
(swift-mode:calculate-indent-of-expression
swift-mode:parenthesized-expression-offset
swift-mode:parenthesized-expression-offset))

;; Before "in" on the same line
((and next-is-on-same-line (equal next-text "in"))
;; When it is for-in statement, align with the token after "for":
;;
;; for
;; x
;; in
;; foo
;;
;; for x
;; in
;; foo
;;
;; When it is anonymous function, align with the token after {:
;;
;; foo {
;; x
;; in
;; ...
;; }
;;
;;
;; foo { x
;; in
;; ...
;; }
;;
;; foo { [
;; weak self
;; ]
;; (
;; x,
;; y
;; )
;; -> Void
;; in
;; a
;; }
(swift-mode:find-and-align-with-parents '("for" {)))

;; Before "where" on the same line
((and next-is-on-same-line (equal next-text "where"))
;; switch {
Expand Down Expand Up @@ -411,19 +424,6 @@ declaration and its offset is `swift-mode:basic-offset'."
'(<))
swift-mode:multiline-statement-offset)))))

;; After {
((eq previous-type '{)
(goto-char (swift-mode:token:start previous-token))
(swift-mode:calculate-indent-after-open-curly-brace
swift-mode:basic-offset))

;; After ( or [
((memq previous-type '(\( \[))
(goto-char (swift-mode:token:start previous-token))
(swift-mode:calculate-indent-of-expression
swift-mode:parenthesized-expression-offset
swift-mode:parenthesized-expression-offset))

;; After "where"
((equal previous-text "where")
;; switch {
Expand Down
255 changes: 255 additions & 0 deletions test/swift-files/identifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,261 @@ let foo = foo.var
let x = foo.where +
a

// keywords as parameter names

func foo() {
// Keywords used in declarations
foo(
associatedtype: 1
)
foo(
class: 1
)
foo(
deinit: 1
)
foo(
enum: 1
)
foo(
extension: 1
)
foo(
fileprivate: 1
)
foo(
func: 1
)
foo(
import: 1
)
foo(
init: 1
)
foo(
inout: 1
)
foo(
internal: 1
)
foo(
let: 1
)
foo(
open: 1
)
foo(
operator: 1
)
foo(
private: 1
)
foo(
protocol: 1
)
foo(
public: 1
)
foo(
static: 1
)
foo(
struct: 1
)
foo(
subscript: 1
)
foo(
typealias: 1
)
foo(
var: 1
)

// Keywords used in statements
foo(
break: 1
)
foo(
case: 1
)
foo(
continue: 1
)
foo(
default: 1
)
foo(
defer: 1
)
foo(
do: 1
)
foo(
else: 1
)
foo(
fallthrough: 1
)
foo(
for: 1
)
foo(
guard: 1
)
foo(
if: 1
)
foo(
in: 1
)
foo(
repeat: 1
)
foo(
return: 1
)
foo(
switch: 1
)
foo(
where: 1
)
foo(
while: 1
)

// Keywords used in expressions and types (without true, false, and keywords begin with a number sign)
foo(
as: 1
)
foo(
catch: 1
)
foo(
dynamicType: 1
)
foo(
is: 1
)
foo(
rethrows: 1
)
foo(
super: 1
)
foo(
self: 1
)
foo(
Self: 1
)
foo(
throws: 1
)
foo(
throw: 1
)
foo(
try: 1
)

// Keywords reserved in particular contexts
foo(
Protocol: 1
)
foo(
Type: 1
)
foo(
and: 1
)
foo(
assignment: 1
)
foo(
associativity: 1
)
foo(
convenience: 1
)
foo(
didSet: 1
)
foo(
dynamic: 1
)
foo(
final: 1
)
foo(
get: 1
)
foo(
higherThan: 1
)
foo(
indirect: 1
)
foo(
infix: 1
)
foo(
lazy: 1
)
foo(
left: 1
)
foo(
lowerThan: 1
)
foo(
mutating: 1
)
foo(
none: 1
)
foo(
nonmutating: 1
)
foo(
optional: 1
)
foo(
override: 1
)
foo(
postfix: 1
)
foo(
precedence: 1
)
foo(
precedencegroup: 1
)
foo(
prefix: 1
)
foo(
required: 1
)
foo(
right: 1
)
foo(
set: 1
)
foo(
unowned: 1
)
foo(
weak: 1
)
foo(
willSet: 1
)
}

// Unicode identifiers

let こんにちは = 你好 +
Expand Down

0 comments on commit 42669da

Please sign in to comment.