branch: elpa/typescript-mode
commit 813cf4fef30d95d055268c06afaf61b4b4f49878
Author: Jostein Kjønigsen <[email protected]>
Commit: Jostein Kjønigsen <[email protected]>
Fix endless loop (hang) in indentation-code.
Tests.
This closes https://github.com/ananthakumaran/typescript.el/issues/20.
---
typescript-mode-tests.el | 35 +++++++++++++++++++++++++++++++++++
typescript-mode.el | 29 ++++++++++++-----------------
2 files changed, 47 insertions(+), 17 deletions(-)
diff --git a/typescript-mode-tests.el b/typescript-mode-tests.el
index 000e9b3484..ad69c1736e 100644
--- a/typescript-mode-tests.el
+++ b/typescript-mode-tests.el
@@ -130,6 +130,41 @@ a severity set to WARNING, no rule name."
(forward-char 1)
(should (= 8 (current-column)))))
+(ert-deftest indentation-does-not-hang-on-multiline-string ()
+ "Testcase for https://github.com/ananthakumaran/typescript.el/issues/20"
+
+ (with-temp-buffer
+ (typescript-mode)
+
+ (insert "let multiLineString = \"line 1")
+ (newline-and-indent)
+ (insert "// and so we continue")
+ (newline-and-indent)
+ ;; completing and not locking up is test-success!
+ ))
+
+(defun test-re-search (searchee contents offset)
+ (with-temp-buffer
+ (typescript-mode)
+
+ (insert contents)
+ (goto-char (- (point-max) offset))
+
+ (should (= 5 (typescript--re-search-backward-inner searchee nil 1)))))
+
+(ert-deftest re-search-backwards-skips-single-line-strings ()
+ (test-re-search "token" "let token = \"token in string-thing\";" 2))
+
+(ert-deftest re-search-backwards-skips-multi-line-strings ()
+ (test-re-search "token" "let token = \"token in\n multi-line token
string\";" 2))
+
+(ert-deftest re-search-backwards-skips-single-line-comments ()
+ (test-re-search "token" "let token; // token in comment" 0))
+
+(ert-deftest re-search-backwards-skips-multi-line-comments ()
+ (test-re-search "token" "let token; /* token in \nmulti-line token comment"
0))
+
+
(provide 'typescript-mode-tests)
;;; typescript-mode-tests.el ends here
diff --git a/typescript-mode.el b/typescript-mode.el
index 7c9676be87..7a7b57fc2c 100644
--- a/typescript-mode.el
+++ b/typescript-mode.el
@@ -691,7 +691,6 @@ macro as normal text."
(defun typescript--re-search-backward-inner (regexp &optional bound count)
"Auxiliary function for `typescript--re-search-backward'."
(let ((parse)
- str-terminator
(orig-macro-start
(save-excursion
(and (typescript--beginning-of-macro)
@@ -702,22 +701,18 @@ macro as normal text."
(save-excursion (backward-char) (looking-at "/[/*]")))
(forward-char))
(setq parse (syntax-ppss))
- (cond ((setq str-terminator (nth 3 parse))
- (when (eq str-terminator t)
- (setq str-terminator ?/))
- (re-search-backward
- (concat "\\([^\\]\\|^\\)" (string str-terminator))
- (save-excursion (beginning-of-line) (point)) t))
- ((nth 7 parse)
- (goto-char (nth 8 parse)))
- ((or (nth 4 parse)
- (and (eq (char-before) ?/) (eq (char-after) ?*)))
- (re-search-backward "/\\*"))
- ((and (not (and orig-macro-start
- (>= (point) orig-macro-start)))
- (typescript--beginning-of-macro)))
- (t
- (setq count (1- count))))))
+ (cond
+ ;; If we are in a comment or a string, jump back to the start
+ ;; of the comment or string.
+ ((nth 8 parse)
+ (goto-char (nth 8 parse)))
+ ((and (eq (char-before) ?/) (eq (char-after) ?*))
+ (re-search-backward "/\\*"))
+ ((and (not (and orig-macro-start
+ (>= (point) orig-macro-start)))
+ (typescript--beginning-of-macro)))
+ (t
+ (setq count (1- count))))))
(point))