emacs-elpa-diffs
[Top][All Lists]
Advanced

[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)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]