[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/go-mode 57becfb 460/495: Fontify type names in interface d
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/go-mode 57becfb 460/495: Fontify type names in interface declarations |
Date: |
Sat, 7 Aug 2021 09:06:09 -0400 (EDT) |
branch: elpa/go-mode
commit 57becfb6cc30ffe6eb675c0a1acc48ee07174a6b
Author: Muir Manders <muir@mnd.rs>
Commit: Peter Sanford <psanford@sanford.io>
Fontify type names in interface declarations
Now we fontify the function signatures in interface declarations. We
reuse the normal "func" fontification but trigger it for things that
look like function names inside "interface { }" instead of looking for
the "func" keyword.
---
go-mode.el | 58 ++++++++++++++++++++++++++++++++++++++++++++---
test/go-font-lock-test.el | 27 ++++++++++++++++++----
2 files changed, 77 insertions(+), 8 deletions(-)
diff --git a/go-mode.el b/go-mode.el
index 1020812..14fa2d4 100644
--- a/go-mode.el
+++ b/go-mode.el
@@ -421,6 +421,10 @@ For mode=set, all covered lines will have this weight."
,@(mapcar (lambda (x) `(,x font-lock-type-face))
(number-sequence 1 go--font-lock-func-param-num-groups)))
+ (go--match-interface
+ ,@(mapcar (lambda (x) `(,x font-lock-type-face))
+ (number-sequence 1 go--font-lock-func-param-num-groups)))
+
;; Fontify types in e.g. "var foo string".
(go--match-ident-type-pair 1 font-lock-type-face)
@@ -619,7 +623,7 @@ case keyword. It returns nil for the case line itself."
(go--forward-line -1)))
(and
- (looking-at go--case-regexp)
+ (looking-at-p go--case-regexp)
;; we weren't in case list if first line ended in colon
;; and the "case" line ended in colon
(not (and saw-colon (looking-at ".*:[[:space:]]*$"))))))))
@@ -633,8 +637,8 @@ case keyword. It returns nil for the case line itself."
(eq (char-after) ?{)
;; "struct" appears before opening curly
- (backward-word)
- (looking-at "struct[[:space:]]"))))
+ (skip-syntax-backward " ")
+ (looking-back "struct" (- (point) 6)))))
(defun go--in-composite-literal-p ()
"Return non-nil if point is in a composite literal."
@@ -660,6 +664,18 @@ case keyword. It returns nil for the case line itself."
;; literal.
(and (bolp) (go--in-composite-literal-p))))))
+(defun go--in-interface-p ()
+ "Return non-nil if point is inside an interface definition."
+ (save-excursion
+ (and
+ ;; inside curlies
+ (go-goto-opening-parenthesis)
+ (eq (char-after) ?{)
+
+ ;; "interface" appears before opening curly
+ (skip-syntax-backward " ")
+ (looking-back "interface" (- (point) 9)))))
+
(defun go--fill-prefix ()
"Return fill prefix for following comment paragraph."
(save-excursion
@@ -1271,6 +1287,42 @@ of last search. Return t if search succeeded."
(set-match-data (go--make-match-data regions))
t)))))
+(defun go--match-interface (end)
+ "Search for function signatures in interface declarations.
+
+Interface items are function signatures without the \"func\"
+keyword. To find them, we search for lines that look like
+\"\\s*\\w+(\" and are enclosed in an \"interface { }\"
+declaration. Return non-nil if search succeeds."
+ (let (type-names found-match)
+ (while (and
+ (not found-match)
+
+ ;; Search for the beginning of a function name as it shows
+ ;; up in an interface (e.g. "foo(").
+ (re-search-forward (concat "\\_<" go-identifier-regexp "(") end t))
+ (let ((search-end (match-end 0)))
+ ;; Move back to before the "foo(".
+ (goto-char (match-beginning 0))
+
+ ;; Make sure we are in an interface declaration.
+ (when (go--in-interface-p)
+ ;; Invoke the normal "func" signature search.
+ (setq type-names (go--filter-match-data (go--match-func-type-names
end) end))
+
+ (when type-names
+ (set-match-data (go--make-match-data type-names))
+ (setq found-match t)))
+
+ ;; Reset point to the end of the search. If we have no match
+ ;; yet we need to continue to make progress as we search. If
+ ;; we found a match, we need to reset the point since the
+ ;; function signature itself may contain another interface
+ ;; declaration.
+ (goto-char search-end)))
+
+ found-match))
+
(defun go--match-ident-type-pair (end)
"Search for identifier + type-name pairs.
diff --git a/test/go-font-lock-test.el b/test/go-font-lock-test.el
index 96f33ed..1423e65 100644
--- a/test/go-font-lock-test.el
+++ b/test/go-font-lock-test.el
@@ -24,8 +24,8 @@
(ert-deftest go--fontify-decls ()
(should-fontify "KvarK foo TintT")
(should-fontify "KvarK foo *[3]TintT")
- (should-fontify "KvarK foo Tfmt.StringerT")
- (should-fontify "KvarK foo, bar Tfmt.StringerT")
+ (should-fontify "KvarK foo Tfoo.ZebraT")
+ (should-fontify "KvarK foo, bar Tfoo.ZebraT")
(should-fontify "
KvarK (
@@ -47,7 +47,23 @@ KconstK (
(should-fontify "
KstructK {
a TboolT
- c KstructK { f *Tfmt.StringerT }
+ c KstructK { f *Tfoo.ZebraT }
+}"))
+
+(ert-deftest go--fontify-interface ()
+ (should-fontify "
+KinterfaceK {
+ FfooF(a, b TcT) *TstringT
+}")
+
+ (should-fontify "
+KinterfaceK {
+ FfooF(KinterfaceK { FaF() TintT }) (c TdT)
+}")
+
+ (should-fontify "
+KmapK[TstringT]KinterfaceK{}{
+ S`foo`S: foo.FbarF(baz),
}"))
(defun should-fontify (contents)
@@ -67,7 +83,7 @@ expects \"make\" to be a (B)uiltin and \"int\" to be a
(T)type."
;; First pass through buffer looks for the face tags. We delete
;; the tags and record the expected face ranges in `faces'.
(let ((case-fold-search nil) faces start start-pos)
- (while (re-search-forward "[TBKCF]" nil t)
+ (while (re-search-forward "[TBKCFS]" nil t)
(let ((found-char (char-before)))
(backward-delete-char 1)
(if start
@@ -78,7 +94,8 @@ expects \"make\" to be a (B)uiltin and \"int\" to be a
(T)type."
(?B 'font-lock-builtin-face)
(?K 'font-lock-keyword-face)
(?C 'font-lock-constant-face)
- (?F 'font-lock-function-name-face))))
+ (?F 'font-lock-function-name-face)
+ (?S 'font-lock-string-face))))
(setq faces (append faces `((,face ,start-pos ,(point))))))
(setq start nil))
(setq start found-char)
- [nongnu] elpa/go-mode 7f87c32 416/495: Permit empty guru scope, (continued)
- [nongnu] elpa/go-mode 7f87c32 416/495: Permit empty guru scope, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode be9186c 417/495: Support remote files in `gofmt', ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 287595a 420/495: Don't traverse symlinks when recursing directories, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode b990645 425/495: Fix call expr indenting after dangling operator., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode ac6dd8c 430/495: Fix go-remove-unused-imports, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode e8674fe 437/495: Expand electric indent config a bit, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 6a64cbf 439/495: Fix indentation for composite literal keys., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode fb1272c 441/495: indent: fix performance in giant comments, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode c020e2c 456/495: Fontify type names in more places., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 2a1584f 454/495: Readme: add note recommending goimports, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 57becfb 460/495: Fontify type names in interface declarations,
ELPA Syncer <=
- [nongnu] elpa/go-mode cf53daa 459/495: Fix indentation in naked blocks., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode f872781 466/495: Fontify variable names., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode acec0aa 471/495: Fix Content-Type for https://play.golang.org/share, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode cad6d0a 472/495: Fix fontification performance issue, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 1fbe6a8 490/495: Fix comment filling at start of buffer., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 5186c83 491/495: Support basic indenting in multiline comments., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode c576513 494/495: Fix fontification of multiplicand after index expr., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 49a5380 493/495: Trivial fix of docstring and comment spelling typos., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 584628e 148/495: instead of killing lines, delete them, to avoid manipulating the kill ring, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode a82c165 221/495: fix the license, ELPA Syncer, 2021/08/07