Skip to content

Commit 27e959e

Browse files
committed
Handle unmatched parens correctly
1 parent 089853c commit 27e959e

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

swift-mode-beginning-of-defun.el

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ The cursor must be at the beginning of a statement."
172172
"prefix" "postfix" "infix" "precedencegroup"
173173
"var" "let"
174174
"case"))
175-
(stop-tokens '(\; implicit-\; {} } \) \]
175+
(stop-tokens '(\; implicit-\; {} { } \( \) \[ \]
176176
anonymous-function-parameter-in outside-of-buffer))
177177
(class-token nil))
178178
(while (not (or
@@ -641,14 +641,30 @@ Both functions return t if succeeded, return nil otherwise."
641641
region)
642642

643643
(t
644-
(catch 'swift-mode:found-block
645-
(while (funcall move-forward)
646-
(let ((end (point)))
647-
(save-excursion
648-
(funcall move-backward)
649-
(when (<= (point) pos end)
650-
(throw 'swift-mode:found-block (cons (point) end))))))
651-
(cons (point-min) (point-max)))))))
644+
(save-excursion
645+
(catch 'swift-mode:found-block
646+
(let ((start pos)
647+
(end pos))
648+
(while (and (funcall move-forward) (/= end (point)))
649+
(setq end (point))
650+
(save-excursion
651+
(funcall move-backward)
652+
(when (<= (point) pos end)
653+
(throw 'swift-mode:found-block (cons (point) end)))))
654+
(when (= end (point))
655+
;; Got unmatched parens.
656+
;; Scans backward.
657+
(goto-char pos)
658+
(while (and (funcall move-backward) (/= start (point)))
659+
(setq start (point))
660+
(save-excursion
661+
(funcall move-forward)
662+
(when (<= start pos (point))
663+
(throw 'swift-mode:found-block (cons start (point))))
664+
(funcall move-backward)
665+
(when (/= start (point))
666+
(throw 'swift-mode:found-block (cons start end)))))))
667+
(cons (point-min) (point-max))))))))
652668

653669
(defun swift-mode:extend-region-with-spaces (region)
654670
"Return REGION extended with surrounding spaces."

swift-mode-indent.el

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,14 +1320,19 @@ Return the token skipped."
13201320
;; List
13211321
((memq previous-type '(} \) \]))
13221322
(goto-char previous-end)
1323-
(backward-list)
1324-
(swift-mode:token
1325-
(cdr (assoc previous-type '((} . {})
1326-
(\) . \(\))
1327-
(\] . \[\]))))
1328-
(buffer-substring-no-properties (point) previous-end)
1329-
(point)
1330-
previous-end))
1323+
(condition-case nil
1324+
(progn
1325+
(backward-list)
1326+
(swift-mode:token
1327+
(cdr (assoc previous-type '((} . {})
1328+
(\) . \(\))
1329+
(\] . \[\]))))
1330+
(buffer-substring-no-properties (point) previous-end)
1331+
(point)
1332+
previous-end))
1333+
(scan-error
1334+
(goto-char previous-start)
1335+
previous-token)))
13311336

13321337
;; Generic parameter list
13331338
((equal previous-text ">")
@@ -1355,14 +1360,19 @@ Return the token skipped."
13551360
;; List
13561361
((memq next-type '({ \( \[))
13571362
(goto-char next-start)
1358-
(forward-list)
1359-
(swift-mode:token
1360-
(cdr (assoc next-type '(({ . {})
1361-
(\( . \(\))
1362-
(\[ . \[\]))))
1363-
(buffer-substring-no-properties next-start (point))
1364-
next-start
1365-
(point)))
1363+
(condition-case nil
1364+
(progn
1365+
(forward-list)
1366+
(swift-mode:token
1367+
(cdr (assoc next-type '(({ . {})
1368+
(\( . \(\))
1369+
(\[ . \[\]))))
1370+
(buffer-substring-no-properties next-start (point))
1371+
next-start
1372+
(point)))
1373+
(scan-error
1374+
(goto-char next-end)
1375+
next-token)))
13661376

13671377
;; Generic parameter list
13681378
((equal next-text "<")

0 commit comments

Comments
 (0)