Skip to content

Commit fef154a

Browse files
committed
Fix fontification of composite literals w/ type params.
Fix "foo.Bar" to be fontified as a type in "foo.Bar[int, string]{}".
1 parent 28e26e0 commit fef154a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

go-mode.el

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ statements."
506506
("\\(!\\)[^=]" 1 font-lock-negation-char-face)
507507

508508
;; Composite literal type
509-
(,(concat go-type-name-regexp "{") 1 font-lock-type-face)
509+
(go--match-composite-literal 1 font-lock-type-face)
510510

511511
;; Map value type
512512
(go--match-map-value 1 font-lock-type-face)
@@ -1758,6 +1758,27 @@ We are looking for the right-hand-side of the type alias"
17581758
(eq (char-after) ?\())))))
17591759
found-match))
17601760

1761+
(defconst go--match-composite-literal-re (concat go-type-name-regexp "[[{]"))
1762+
1763+
(defun go--match-composite-literal (end)
1764+
"Search for composite literals."
1765+
(let (found-match)
1766+
(while (and
1767+
(not found-match)
1768+
;; Match "foo{" or "foo[".
1769+
(re-search-forward go--match-composite-literal-re end t))
1770+
1771+
(setq found-match
1772+
(if (eq (char-before) ?\[)
1773+
;; In "foo[" case, skip to closing "]".
1774+
(progn
1775+
(while (go--search-next-comma end ?\]))
1776+
;; See if closing "]" is followed by "{".
1777+
(eq (char-after (1+ (point))) ?{))
1778+
;; The "foo{" case (definitely composite literal).
1779+
(eq (char-before) ?{))))
1780+
1781+
found-match))
17611782

17621783
(defconst go--map-value-re
17631784
(concat "\\_<map\\_>\\[\\(?:\\[[^]]*\\]\\)*[^]]*\\]" go-type-name-regexp))

test/go-font-lock-test.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ KcaseK string:
124124
(go--should-fontify "TfooT{")
125125
(go--should-fontify "[]TfooT{")
126126
(go--should-fontify "Tfoo.ZarT{")
127+
(go--should-fontify "Tfoo.ZarT[TintT]{")
127128
(go--should-fontify "[]Tfoo.ZarT{")
128129

129130
(go--should-fontify "TfooT{CbarC:baz, CquxC: 123}")

0 commit comments

Comments
 (0)