emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 2110973 2/4: Improve checkdoc abbreviation handling


From: Stefan Kangas
Subject: master 2110973 2/4: Improve checkdoc abbreviation handling
Date: Mon, 13 Sep 2021 00:45:30 -0400 (EDT)

branch: master
commit 2110973351f01fb5cdf90b705acb58354b608050
Author: Stefan Kangas <stefan@marxist.se>
Commit: Stefan Kangas <stefan@marxist.se>

    Improve checkdoc abbreviation handling
    
    * lisp/emacs-lisp/checkdoc.el
    (checkdoc-in-abbreviation-p): New helper function.
    (checkdoc-sentencespace-region-engine): Fix handling abbreviations
    after escaped parenthesis.
    
    * test/lisp/emacs-lisp/checkdoc-tests.el
    (checkdoc-tests-in-abbrevation-p)
    (checkdoc-tests-in-abbrevation-p/with-parens)
    (checkdoc-tests-in-abbrevation-p/with-escaped-parens): New tests.
---
 lisp/emacs-lisp/checkdoc.el            | 36 ++++++++++++++++++++++++----------
 test/lisp/emacs-lisp/checkdoc-tests.el | 24 +++++++++++++++++++++++
 2 files changed, 50 insertions(+), 10 deletions(-)

diff --git a/lisp/emacs-lisp/checkdoc.el b/lisp/emacs-lisp/checkdoc.el
index 00cc777..1ab44e0 100644
--- a/lisp/emacs-lisp/checkdoc.el
+++ b/lisp/emacs-lisp/checkdoc.el
@@ -1998,6 +1998,31 @@ The text checked is between START and LIMIT."
          (setq c (1+ c)))
        (and (< 0 c) (= (% c 2) 0))))))
 
+(defun checkdoc-in-abbreviation-p (begin)
+  "Return non-nil if point is at an abbreviation.
+Examples of abbreviations handled: \"e.g.\", \"i.e.\", \"cf.\"."
+  (save-excursion
+    (goto-char begin)
+    (condition-case nil
+        (progn
+          (forward-sexp -1)
+          ;; Piece of an abbreviation.
+          (looking-at
+           (rx (or letter     ; single letter, as in "a."
+                   (seq
+                    ;; There might exist an escaped parenthesis, as
+                    ;; this is often used in docstrings.  In this
+                    ;; case, `forward-sexp' will have skipped over it,
+                    ;; so we need to skip it here too.
+                    (? "\\(")
+                    ;; The abbreviations:
+                    (or (seq (any "iI") "." (any "eE")) ; i.e.
+                        (seq (any "eE") ".g")           ; e.g.
+                        (seq (any "cC") "f")))          ; c.f.
+                   "vs")                                ; vs.
+               ".")))
+      (error t))))
+
 (defun checkdoc-proper-noun-region-engine (begin end)
   "Check all text between BEGIN and END for lower case proper nouns.
 These are Emacs centric proper nouns which should be capitalized for
@@ -2060,16 +2085,7 @@ If the offending word is in a piece of quoted text, then 
it is skipped."
                     (e (match-end 1)))
                 (unless (or (checkdoc-in-sample-code-p begin end)
                             (checkdoc-in-example-string-p begin end)
-                            (save-excursion
-                              (goto-char b)
-                              (condition-case nil
-                                  (progn
-                                    (forward-sexp -1)
-                                    ;; piece of an abbreviation
-                                    ;; FIXME etc
-                                    (looking-at
-                                     
"\\([a-zA-Z]\\|[iI]\\.?e\\|[eE]\\.?g\\|[cC]f\\)\\."))
-                                (error t))))
+                            (checkdoc-in-abbreviation-p b))
                   (if (checkdoc-autofix-ask-replace
                        b e
                        "There should be two spaces after a period.  Fix? "
diff --git a/test/lisp/emacs-lisp/checkdoc-tests.el 
b/test/lisp/emacs-lisp/checkdoc-tests.el
index 2a1d8b2..a4b2520 100644
--- a/test/lisp/emacs-lisp/checkdoc-tests.el
+++ b/test/lisp/emacs-lisp/checkdoc-tests.el
@@ -122,4 +122,28 @@ See the comments in Bug#24998."
     (should (looking-at-p "\"baz\")"))
     (should-not (checkdoc-next-docstring))))
 
+(ert-deftest checkdoc-tests-in-abbrevation-p ()
+  (with-temp-buffer
+    (emacs-lisp-mode)
+    (insert "foo bar e.g. baz")
+    (goto-char (point-min))
+    (re-search-forward "e.g")
+    (should (checkdoc-in-abbreviation-p (point)))))
+
+(ert-deftest checkdoc-tests-in-abbrevation-p/with-parens ()
+  (with-temp-buffer
+    (emacs-lisp-mode)
+    (insert "foo bar (e.g. baz)")
+    (goto-char (point-min))
+    (re-search-forward "e.g")
+    (should (checkdoc-in-abbreviation-p (point)))))
+
+(ert-deftest checkdoc-tests-in-abbrevation-p/with-escaped-parens ()
+  (with-temp-buffer
+    (emacs-lisp-mode)
+    (insert "foo\n\\(e.g. baz)")
+    (goto-char (point-min))
+    (re-search-forward "e.g")
+    (should (checkdoc-in-abbreviation-p (point)))))
+
 ;;; checkdoc-tests.el ends here



reply via email to

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