Skip to content

Commit 359efc0

Browse files
committed
Fix indentation of function parameter clauses
1 parent 25944c2 commit 359efc0

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

swift-mode-lexer.el

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
stop-at-eol-token-types
6969
stop-at-bol-token-types))
7070

71+
(declare-function swift-mode:try-backward-generic-parameters
72+
"swift-mode-indent.el"
73+
())
74+
75+
7176
(defun swift-mode:token (type text start end)
7277
"Construct and return a token.
7378
@@ -571,7 +576,9 @@ Return nil otherwise."
571576
;; ]
572577
((eq (swift-mode:token:type next-token) '\[) t)
573578

574-
;; Inserts implicit semicolon before open parenthesis.
579+
;; Inserts implicit semicolon before open parenthesis, unless it is a
580+
;; function parameter clause. Suppress implicit semicolon before function
581+
;; parameter clause.
575582
;;
576583
;; Open parenthesis for function arguments cannot appear at the start of a
577584
;; line.
@@ -584,7 +591,8 @@ Return nil otherwise."
584591
;; (
585592
;; 1
586593
;; )
587-
((eq (swift-mode:token:type next-token) '\() t)
594+
((eq (swift-mode:token:type next-token) '\()
595+
(not (swift-mode:function-parameter-clause-p)))
588596

589597
;; Suppress implicit semicolon after the beginning of an interpolated
590598
;; expression.
@@ -595,6 +603,26 @@ Return nil otherwise."
595603
;; Otherwise, inserts implicit semicolon.
596604
(t t))))
597605

606+
(defun swift-mode:function-parameter-clause-p ()
607+
"Return t if the cursor is before a function parameter clause.
608+
609+
Return nil otherwise."
610+
(save-excursion
611+
(let* ((previous-token (swift-mode:backward-token-simple))
612+
(previous-type (swift-mode:token:type previous-token)))
613+
(cond
614+
((eq previous-type '>)
615+
(and
616+
(/= (point)
617+
;; FIXME: mutual dependency
618+
(progn (swift-mode:try-backward-generic-parameters) (point)))
619+
(swift-mode:function-parameter-clause-p)))
620+
((or (eq previous-type 'operator)
621+
(eq previous-type 'identifier))
622+
(equal (swift-mode:token:text (swift-mode:backward-token-simple))
623+
"func"))
624+
(t nil)))))
625+
598626
(defun swift-mode:supertype-colon-p ()
599627
"Return t if a colon at the cursor is the colon for supertype.
600628

test/swift-files/indent/declarations.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ class Foo {
223223
private
224224
final
225225
func
226-
foo<A, B>(
226+
foo<A,
227+
B>
228+
(
227229
x:
228230
Int,
229231
y:

0 commit comments

Comments
 (0)