Skip to content

Commit e8eea7f

Browse files
muirdmpsanford
authored andcommitted
Fix fontification of multiline type switch clauses.
Now we properly fontify "foo" and "bar" in: switch v.(type) { case foo, bar: } I also tried to be careful so comments don't mess things up. Closes: #355 [via git-merge-pr]
1 parent 10d6ab4 commit e8eea7f

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

go-mode.el

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,20 @@ For mode=set, all covered lines will have this weight."
410410
(defvar go--default-face 'default
411411
"A variable to refer to `default' face for use in font lock rules.")
412412

413+
(defun go--fontify-type-switch-case-pre ()
414+
"Move point to line following the end of case statement.
415+
416+
This is used as an anchored font lock keyword PRE-MATCH-FORM. We
417+
expand the font lock region to include multiline type switch case
418+
statements."
419+
(save-excursion
420+
(beginning-of-line)
421+
(while (or (looking-at "[[:space:]]*\\($\\|//\\)") (go--line-suffix-p ","))
422+
(forward-line))
423+
(when (go--line-suffix-p ":")
424+
(forward-line))
425+
(point)))
426+
413427
(defun go--build-font-lock-keywords ()
414428
;; we cannot use 'symbols in regexp-opt because GNU Emacs <24
415429
;; doesn't understand that
@@ -440,7 +454,11 @@ For mode=set, all covered lines will have this weight."
440454
(go--match-ident-type-pair 2 font-lock-type-face)
441455

442456
;; An anchored matcher for type switch case clauses.
443-
(go--match-type-switch-case (go--fontify-type-switch-case nil nil (1 font-lock-type-face)))
457+
(go--match-type-switch-case
458+
(go--fontify-type-switch-case
459+
(go--fontify-type-switch-case-pre)
460+
nil
461+
(1 font-lock-type-face)))
444462

445463
;; Match variable names in var decls, constant names in const
446464
;; decls, and type names in type decls.
@@ -1467,7 +1485,7 @@ comma, it stops at it. Return non-nil if comma was found."
14671485
;; Loop until we find a match because we must skip types we don't
14681486
;; handle, such as "interface { foo() }".
14691487
(while (and (not found-match) (not done))
1470-
(when (looking-at (concat "[[:space:]\n]*" go-type-name-regexp "[[:space:]]*[,:]"))
1488+
(when (looking-at (concat "\\(?:[[:space:]]*\\|//.*\\|\n\\)*" go-type-name-regexp "[[:space:]]*[,:]"))
14711489
(goto-char (match-end 1))
14721490
(unless (member (match-string 1) go-constants)
14731491
(setq found-match t)))

test/go-font-lock-test.el

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ KmapK[TstringT]KinterfaceK{}{
7777
KswitchK foo.(KtypeK) {
7878
KcaseK TstringT, *Tfoo.ZebraT, [2]TbyteT:
7979
KcaseK CnilC:
80+
KcaseK TfooT, TbarT, D// DQhi
81+
Q
82+
D// DQthere
83+
Q TbazT, TquxT:
8084
KdefaultK:
8185
}")
8286

0 commit comments

Comments
 (0)