Skip to content

Commit e515e45

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 824bd27 commit e515e45

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

go-mode.el

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,9 @@ statements."
482482

483483
(if go-fontify-function-calls
484484
;; Function call/method name
485-
`((,(concat "\\(" go-identifier-regexp "\\)[[:space:]]*(") 1 font-lock-function-name-face)
486-
;; Bracketed function call
487-
(,(concat "[^[:word:][:multibyte:]](\\(" go-identifier-regexp "\\))[[:space:]]*(") 1 font-lock-function-name-face))
485+
`((go--match-func-name
486+
(1 font-lock-function-name-face nil t)
487+
(2 font-lock-function-name-face nil t)))
488488
;; Method name
489489
`((,go-func-meth-regexp 2 font-lock-function-name-face)))
490490

@@ -1458,19 +1458,19 @@ the next comma or to the closing paren."
14581458
(setq found-match t)))
14591459

14601460
;; Advance to next comma. We are done if there are no more commas.
1461-
(setq done (not (go--search-next-comma end))))
1461+
(setq done (not (go--search-next-comma end ?\)))))
14621462
found-match))
14631463

1464-
(defun go--search-next-comma (end)
1464+
(defun go--search-next-comma (end closer)
14651465
"Search forward from point for a comma whose nesting level is
14661466
the same as point. If it reaches a closing parenthesis before a
14671467
comma, it stops at it. Return non-nil if comma was found."
14681468
(let ((orig-level (go-paren-level)))
14691469
(while (and (< (point) end)
1470-
(or (looking-at-p "[^,)]")
1470+
(or (not (member (char-after) `(?, ,closer)))
14711471
(> (go-paren-level) orig-level)))
14721472
(forward-char))
1473-
(when (and (looking-at-p ",")
1473+
(when (and (eq (char-after) ?,)
14741474
(< (point) (1- end)))
14751475
(forward-char)
14761476
t)))
@@ -1502,7 +1502,7 @@ comma, it stops at it. Return non-nil if comma was found."
15021502
(goto-char (match-end 1))
15031503
(unless (member (match-string 1) go-constants)
15041504
(setq found-match t)))
1505-
(setq done (not (go--search-next-comma end))))
1505+
(setq done (not (go--search-next-comma end ?\)))))
15061506
found-match))
15071507

15081508
(defun go--containing-decl ()
@@ -1705,6 +1705,35 @@ We are looking for the right-hand-side of the type alias"
17051705
found-match))
17061706

17071707

1708+
(defconst go--match-func-name-re
1709+
(concat "\\(?:^\\|[^)]\\)(\\(" go-identifier-regexp "\\))(\\|\\(" go-identifier-regexp "\\)[[(]"))
1710+
1711+
(defun go--match-func-name (end)
1712+
"Search for func names in decls and invocations."
1713+
(let (found-match)
1714+
(while (and
1715+
(not found-match)
1716+
;; Match "(foo)(" or "foo[" or "foo(".
1717+
(re-search-forward go--match-func-name-re end t))
1718+
(if (eq (char-before) ?\()
1719+
;; Followed directly by "(" is a match.
1720+
(setq found-match t)
1721+
;; Followed by "[". We are a match if there is at least one
1722+
;; comma between the "[" and "]", and if "]" is followed by
1723+
;; "(".
1724+
(let ((commas 0))
1725+
(while (go--search-next-comma end ?\])
1726+
(cl-incf commas))
1727+
(forward-char)
1728+
(setq found-match (and
1729+
;; We found a comma (so we are sure these are type
1730+
;; params), or we are at file level (so we are sure
1731+
;; it is a func decl).
1732+
(or (> commas 0) (= 0 (go-paren-level)))
1733+
(eq (char-after) ?\())))))
1734+
found-match))
1735+
1736+
17081737
(defconst go--map-value-re
17091738
(concat "\\_<map\\_>\\[\\(?:\\[[^]]*\\]\\)*[^]]*\\]" go-type-name-regexp))
17101739

test/go-font-lock-test.el

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ QKfuncK (VfV TintT) {}
5858
(go--should-fontify "foo[int]")
5959
(go--should-fontify "foo[TintT, KstructK{}, *Tfoo.ZebraT]"))
6060

61+
(ert-deftest go--fontify-func ()
62+
(go--should-fontify "KfuncK FfooF()")
63+
(go--should-fontify "KfuncK FfooF[A TanyT]()")
64+
(go--should-fontify "KfuncK (VfV TfooT) FfooF[A TanyT]()")
65+
(go--should-fontify "FfooF(bar)")
66+
(go--should-fontify "foo.FfooF(bar)")
67+
(go--should-fontify "(FfooF)(foo)(foo)")
68+
(go--should-fontify "{ foo[int](123) }")
69+
(go--should-fontify "FfooF[TintT, TstringT](123)"))
70+
6171
(ert-deftest go--fontify-struct ()
6272
(go--should-fontify "KstructK { i TintT }")
6373
(go--should-fontify "KstructK { a, b TintT }")

0 commit comments

Comments
 (0)