branch: elpa/typescript-mode
commit 7172a23d86d7b40c1b850429d805f3a39bdf3baa
Author: Louis-Dominique Dubeau <[email protected]>
Commit: Louis-Dominique Dubeau <[email protected]>
Add a custom predicate for flyspell-prog-mode.
---
typescript-mode-tests.el | 41 +++++++++++++++++++++++++++++++++++++++++
typescript-mode.el | 33 +++++++++++++++++++++++++++++++++
2 files changed, 74 insertions(+)
diff --git a/typescript-mode-tests.el b/typescript-mode-tests.el
index cbc25dccdf..9a4725afc5 100644
--- a/typescript-mode-tests.el
+++ b/typescript-mode-tests.el
@@ -236,6 +236,47 @@ fontified as documentation."
(should (eq (text-property-not-all loc (point-max) 'face
font-lock-string-face)
(1- (point-max)))))))
+(defun flyspell-predicate-test (search-for)
+ "This function runs a test on
+`typescript--flyspell-mode-predicate'. `SEARCH-FOR' is a string
+to search for in the current buffer before running
+`typescript--flyspell-mode-predicate'. This test checks that the
+point has not moved. It returns the value of returned by the
+invocation of `typescript--flyspell-mode-predicate'."
+ (search-forward search-for)
+ (let ((point-before (point)))
+ (prog1
+ (typescript--flyspell-mode-predicate)
+ ;; We should not have moved.
+ (should (eq (point) point-before)))
+ ))
+
+(ert-deftest flyspell-mode-predicate-skips-what-it-should ()
+ "Check that the custom flyspell predicate filters strings in
+import... from...."
+ (let (flyspell-generic-progmode-verify)
+ (fset 'flyspell-generic-progmode-verify (lambda () t))
+ ;; In the following searches we search for the starting quote of the
strings
+ ;; to avoid hitting keywords. Moreover, the end position of the search is
important.
+ ;; Flyspell puts point at the end of the word before calling the
predicate. We must
+ ;; replicate that behavior here.
+ (test-with-temp-buffer
+ "import 'a';\nimport { x } from 'b';\nconst foo = 'c';import { x }\nfrom
'd';"
+ (should (not (flyspell-predicate-test "'a")))
+ (should (not (flyspell-predicate-test "'b")))
+ (should (flyspell-predicate-test "'c"))
+ (should (not (flyspell-predicate-test "'d"))))
+ (test-with-temp-buffer
+ ;; This is valid TypeScript.
+ "const from = 'a';"
+ (should (flyspell-predicate-test "'a")))
+ (test-with-temp-buffer
+ ;; TypeScript does not allow a function named "import" but object
+ ;; members may be named "import". So this *can* be valid
+ ;; TypeScript.
+ "x.import('a');"
+ (should (flyspell-predicate-test "'a")))))
+
(provide 'typescript-mode-tests)
;;; typescript-mode-tests.el ends here
diff --git a/typescript-mode.el b/typescript-mode.el
index 7f5f4c4375..0379c011c5 100644
--- a/typescript-mode.el
+++ b/typescript-mode.el
@@ -1739,6 +1739,36 @@ and searches for the next token to be highlighted."
'(0 font-lock-variable-name-face))))
"Level three font lock for `typescript-mode'.")
+(defun typescript--flyspell-mode-predicate ()
+ "A custom predicate to help `flyspell-prog-mode' determine whether a word
should be checked."
+ ;; We depend on fontification for our results. font-lock-ensure is defined on
+ ;; Emacs 25 and over. Earlier versions use font-lock-fontify-buffer.
+ (if (fboundp 'font-lock-ensure)
+ (font-lock-ensure)
+ (font-lock-fontify-buffer))
+ (and
+ ;; Check with the default method that flyspell provides.
+ (flyspell-generic-progmode-verify)
+
+ ;;
+ ;; And eliminate cases specific to our mode we don't want to have
+ ;; spell-checked.
+ ;;
+
+ ;; Don't check the module names in import statements.
+ (save-excursion
+ (not (let* ((parse (syntax-ppss (1- (point))))
+ (string-start-pos (and (nth 3 parse)
+ (nth 8 parse))))
+ (and string-start-pos
+ (save-match-data
+ ;; Move to back to the start of the string, then past any ws
+ ;; and then past any non-ws to see if we have "from" or
"import".
+ (goto-char string-start-pos)
+ (typescript--backward-syntactic-ws)
+ (skip-syntax-backward "^-" (point-at-bol))
+ (looking-at "from\\|import\\s-"))))))))
+
(defun typescript--inside-pitem-p (pitem)
"Return whether point is inside the given pitem's header or body."
(typescript--ensure-cache)
@@ -2475,6 +2505,9 @@ Key bindings:
(with-no-warnings
(font-lock-fontify-buffer))))
+;; Set our custom predicate for flyspell prog mode
+(put 'typescript-mode 'flyspell-mode-predicate
+ 'typescript--flyspell-mode-predicate)
;;;###autoload
(eval-after-load 'folding