Skip to content

Commit 824bd27

Browse files
committed
Fontify types in generic instantiation.
If there are more than one type params (e.g. "foo[one, two]") we can be certain it is a generic instantiation and not another kind of index expression.
1 parent 7c7d44c commit 824bd27

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

go-mode.el

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,12 @@ statements."
453453
(1 font-lock-type-face nil t)
454454
(2 font-lock-type-face nil t))
455455

456+
;; Match multi type param instantiation such as "foo[int, string]".
457+
;; Does not match on "foo[int]" since that is ambiguous.
458+
(go--match-type-instantiation
459+
(1 font-lock-type-face nil t)
460+
(2 font-lock-type-face nil t))
461+
456462
;; An anchored matcher for type switch case clauses.
457463
(go--match-type-switch-case
458464
(go--fontify-type-switch-case
@@ -1633,6 +1639,35 @@ succeeds."
16331639
(go--in-type-params-p))))))
16341640
found-match))
16351641

1642+
(defconst go--type-instantiation-re (concat go-type-name-regexp "\\s-*,\\|,\\s-*" go-type-name-regexp))
1643+
1644+
(defun go--match-type-instantiation (end)
1645+
"Search for type params in type instantiations.
1646+
1647+
We look for comma separated names within square brackets. That
1648+
means this only matches for instantiations with more than one
1649+
param."
1650+
(let (found-match)
1651+
(while (and
1652+
(not found-match)
1653+
;; Look for "foo ," or ", foo" (i.e. a type name before or
1654+
;; after a comma).
1655+
(re-search-forward go--type-instantiation-re end t))
1656+
1657+
(let ((match (match-string 1)))
1658+
;; If we matched "foo ,", move back one char so we can see the
1659+
;; comma again on the next iteration.
1660+
(if match
1661+
(backward-char)
1662+
(setq match (match-string 2)))
1663+
1664+
(setq found-match (and
1665+
(not (member match go-mode-keywords))
1666+
(save-excursion
1667+
(go-goto-opening-parenthesis)
1668+
(eq (char-after) ?\[))))))
1669+
found-match))
1670+
16361671
(defconst go--single-func-result-re (concat ")[[:space:]]+" go-type-name-regexp "\\(?:$\\|[[:space:]),]\\)"))
16371672

16381673
(defun go--match-single-func-result (end)

test/go-font-lock-test.el

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ QKfuncK (VfV TintT) {}
5353
(go--should-fontify "KfuncK FfooF[a TintT | TstringT | KstructK{} | *Tfoo.ZebraT](TintT) { }")
5454
(go--should-fontify "KinterfaceK { TintT | Tfloat64T }"))
5555

56+
(ert-deftest go--fontify-type-instantiation ()
57+
;; ambiguous - leave it unfontified
58+
(go--should-fontify "foo[int]")
59+
(go--should-fontify "foo[TintT, KstructK{}, *Tfoo.ZebraT]"))
60+
5661
(ert-deftest go--fontify-struct ()
5762
(go--should-fontify "KstructK { i TintT }")
5863
(go--should-fontify "KstructK { a, b TintT }")

0 commit comments

Comments
 (0)