Skip to content

Commit 672c28a

Browse files
committed
Complete long statements by taking latest substring
1 parent 3e6863e commit 672c28a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

js-comint-test.el

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ reduce((prev, curr) => prev + curr, 0);"
179179
(goto-char (point-min))
180180
(should (equal (js-comint--current-input) nil))))
181181

182+
(ert-deftest js-comint--complete-substring/test ()
183+
"Tests normal behavior."
184+
(should (equal (js-comint--complete-substring "foo; bar")
185+
"bar"))
186+
(should (equal (js-comint--complete-substring "if(tru")
187+
"tru"))
188+
(should (equal (js-comint--complete-substring "for (let i of myObject.pro")
189+
"myObject.pro")))
190+
182191
(ert-deftest js-comint--should-complete/test ()
183192
"Tests default behavior."
184193
(with-new-js-comint-buffer

js-comint.el

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ ARGUMENTS is an optional list of arguments to pass."
335335
(list :arguments arguments)))
336336
js-comint--completion-callbacks))
337337

338+
(defun js-comint--complete-substring (input-string)
339+
"Given a full line in INPUT-STRING return the substring to complete."
340+
(if-let ((match (string-match "[[:space:]({;]+\\([[:word:].]*\\)$" input-string nil t)))
341+
(string-trim (substring-no-properties input-string (1+ match)))
342+
input-string))
343+
338344
(defun js-comint--get-completion-async (input-string callback)
339345
"Complete INPUT-STRING and register CALLBACK to recieve completion output."
340346
(js-comint--set-completion-callback
@@ -423,7 +429,7 @@ Nil if point is before the current prompt."
423429
((prefix)
424430
(when (equal major-mode 'js-comint-mode)
425431
(if (js-comint--should-complete)
426-
(js-comint--current-input)
432+
(js-comint--complete-substring (js-comint--current-input))
427433
'stop)))
428434
((candidates)
429435
(cons :async (apply-partially #'js-comint--get-completion-async arg)))))

0 commit comments

Comments
 (0)