Skip to content

Commit 28e26e0

Browse files
committed
Fix fontification of generic func names.
Now we fontify func names in generic invocations when there are more than one type params (so we can be sure it is a func instantiation). I also tweaked things to avoid fontifying "bar" in (foo)(bar)(baz) (fixes #385). We also support fontifying method names with type params even though that isn't allowed yet.
1 parent 54c02fd commit 28e26e0

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

go-mode.el

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,9 @@ statements."
486486

487487
(if go-fontify-function-calls
488488
;; Function call/method name
489-
`((,(concat "\\(" go-identifier-regexp "\\)[[:space:]]*(") 1 font-lock-function-name-face)
490-
;; Bracketed function call
491-
(,(concat "[^[:word:][:multibyte:]](\\(" go-identifier-regexp "\\))[[:space:]]*(") 1 font-lock-function-name-face))
489+
`((go--match-func-name
490+
(1 font-lock-function-name-face nil t)
491+
(2 font-lock-function-name-face nil t)))
492492
;; Method name
493493
`((,go-func-meth-regexp 2 font-lock-function-name-face)))
494494

@@ -1730,6 +1730,35 @@ We are looking for the right-hand-side of the type alias"
17301730
found-match))
17311731

17321732

1733+
(defconst go--match-func-name-re
1734+
(concat "\\(?:^\\|[^)]\\)(\\(" go-identifier-regexp "\\))(\\|\\(" go-identifier-regexp "\\)[[(]"))
1735+
1736+
(defun go--match-func-name (end)
1737+
"Search for func names in decls and invocations."
1738+
(let (found-match)
1739+
(while (and
1740+
(not found-match)
1741+
;; Match "(foo)(" or "foo[" or "foo(".
1742+
(re-search-forward go--match-func-name-re end t))
1743+
(if (eq (char-before) ?\()
1744+
;; Followed directly by "(" is a match.
1745+
(setq found-match t)
1746+
;; Followed by "[". We are a match if there is at least one
1747+
;; comma between the "[" and "]", and if "]" is followed by
1748+
;; "(".
1749+
(let ((commas 0))
1750+
(while (go--search-next-comma end ?\])
1751+
(cl-incf commas))
1752+
(forward-char)
1753+
(setq found-match (and
1754+
;; We found a comma (so we are sure these are type
1755+
;; params), or we are at file level (so we are sure
1756+
;; it is a func decl).
1757+
(or (> commas 0) (= 0 (go-paren-level)))
1758+
(eq (char-after) ?\())))))
1759+
found-match))
1760+
1761+
17331762
(defconst go--map-value-re
17341763
(concat "\\_<map\\_>\\[\\(?:\\[[^]]*\\]\\)*[^]]*\\]" go-type-name-regexp))
17351764

test/go-font-lock-test.el

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ QKfuncK (VfV TintT) {}
6161
(go--should-fontify "KvarK VvV TfooT[TbarT[TintT]]")
6262
(go--should-fontify "foo[TintT, KstructK{}, *Tfoo.ZebraT]"))
6363

64+
(ert-deftest go--fontify-func ()
65+
(go--should-fontify "KfuncK FfooF()")
66+
(go--should-fontify "KfuncK FfooF[A TanyT]()")
67+
(go--should-fontify "KfuncK (VfV TfooT) FfooF[A TanyT]()")
68+
(go--should-fontify "FfooF(bar)")
69+
(go--should-fontify "foo.FfooF(bar)")
70+
(go--should-fontify "(FfooF)(foo)(foo)")
71+
(go--should-fontify "{ foo[int](123) }")
72+
(go--should-fontify "FfooF[TintT, TstringT](123)"))
73+
6474
(ert-deftest go--fontify-struct ()
6575
(go--should-fontify "KstructK { i TintT }")
6676
(go--should-fontify "KstructK { a, b TintT }")

0 commit comments

Comments
 (0)