]> git.eshelyaron.com Git - emacs.git/commitdiff
Avoid jumping too far in checkdoc-in-abbreviation-p
authorStefan Kangas <stefan@marxist.se>
Thu, 23 Sep 2021 19:10:08 +0000 (21:10 +0200)
committerStefan Kangas <stefan@marxist.se>
Thu, 23 Sep 2021 20:35:40 +0000 (22:35 +0200)
* lisp/emacs-lisp/checkdoc.el (checkdoc-in-abbreviation-p): Use
'forward-ward' instead of 'forward-sexp' to avoid jumping too far in
some situations.  (Bug#50731)

* test/lisp/emacs-lisp/checkdoc-tests.el
(checkdoc-tests--abbrev-test): New helper function.
(checkdoc-tests-in-abbrevation-p/basic-case): Rename from
'checkdoc-tests-in-abbrevation-p'.
(checkdoc-tests-in-abbrevation-p/with-parens)
(checkdoc-tests-in-abbrevation-p/with-escaped-parens): Use above new
helper function.
(checkdoc-tests-in-abbrevation-p/single-char)
(checkdoc-tests-in-abbrevation-p/with-em-dash)
(checkdoc-tests-in-abbrevation-p/incorrect-abbreviation): New tests.

lisp/emacs-lisp/checkdoc.el
test/lisp/emacs-lisp/checkdoc-tests.el

index 5224a943ac05d6127033857df6e7e7cd64fb2e97..4243e82800169bbdf2d703886f2e76a915a1c942 100644 (file)
@@ -2097,27 +2097,31 @@ Examples of abbreviations handled: \"e.g.\", \"i.e.\", \"cf.\"."
   (save-excursion
     (goto-char begin)
     (condition-case nil
-        (progn
-          (forward-sexp -1)
+        (let ((single-letter t))
+          (forward-word -1)
+          ;; Skip over all dots backwards, as `forward-word' will only
+          ;; go one dot at a time in a string like "e.g.".
+          (while (save-excursion (forward-char -1)
+                                 (looking-at (rx ".")))
+            (setq single-letter nil)
+            (forward-word -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 "cC") "f")              ; cf.
-                        (seq (any "eE") ".g")             ; e.g.
-                        (seq (any "iI") "." (any "eE")))) ; i.e.
-                   "etc"                                  ; etc.
-                   "vs"                                   ; vs.
-                   ;; Some non-standard or less common ones that we
-                   ;; might as well ignore.
-                   "Inc" "Univ" "misc" "resp")
-               ".")))
+           (if single-letter
+               ;; Handle a single letter, as in "a.", as this might be
+               ;; a part of a list.
+               (rx letter ".")
+             (rx (or
+                  ;; The abbreviations:
+                  (seq (or (seq (any "cC") "f")              ; cf.
+                           (seq (any "eE") ".g")             ; e.g.
+                           (seq (any "iI") "." (any "eE")))) ; i.e.
+                  "etc"                                      ; etc.
+                  "vs"                                       ; vs.
+                  ;; Some non-standard or less common ones that we
+                  ;; might as well ignore.
+                  "Inc" "Univ" "misc" "resp")
+                 "."))))
       (error t))))
 
 (defun checkdoc-proper-noun-region-engine (begin end)
index 3eb7da3d4a719b775f9373c332f92245356cd3e8..13b6d134e5c16bacfeb56d93c3e6ce00073e9cca 100644 (file)
@@ -122,29 +122,31 @@ See the comments in Bug#24998."
     (should (looking-at-p "\"baz\")"))
     (should-not (checkdoc-next-docstring))))
 
-(ert-deftest checkdoc-tests-in-abbrevation-p ()
+(defun checkdoc-tests--abbrev-test (buffer-contents goto-string)
   (with-temp-buffer
     (emacs-lisp-mode)
-    (insert "foo bar e.g. baz")
+    (insert buffer-contents)
     (goto-char (point-min))
-    (re-search-forward "e.g")
-    (should (checkdoc-in-abbreviation-p (point)))))
+    (re-search-forward goto-string)
+    (checkdoc-in-abbreviation-p (point))))
+
+(ert-deftest checkdoc-tests-in-abbrevation-p/basic-case ()
+  (should (checkdoc-tests--abbrev-test "foo bar e.g. baz" "e.g")))
 
 (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)))))
+  (should (checkdoc-tests--abbrev-test "foo bar (e.g. baz)" "e.g")))
 
 (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)))))
+  (should (checkdoc-tests--abbrev-test "foo\n\\(e.g. baz)" "e.g")))
+
+(ert-deftest checkdoc-tests-in-abbrevation-p/single-char ()
+  (should (checkdoc-tests--abbrev-test "a. foo bar" "a")))
+
+(ert-deftest checkdoc-tests-in-abbrevation-p/with-em-dash ()
+  (should (checkdoc-tests--abbrev-test "foo bar baz---e.g." "e.g")))
+
+(ert-deftest checkdoc-tests-in-abbrevation-p/incorrect-abbreviation ()
+  (should-not (checkdoc-tests--abbrev-test "foo bar a.b.c." "a.b.c")))
 
 (ert-deftest checkdoc-tests-fix-y-or-n-p ()
   (with-temp-buffer