From 43b3b350d53aff50434ffdd92c22bc4ed9e3234f Mon Sep 17 00:00:00 2001 From: York Zhao Date: Thu, 17 Nov 2016 13:03:54 -0500 Subject: [PATCH 01/52] Always display inferior process buffer after evaluation Previously, if the inferior process buffer is not being shown in any window, one would have to manually bring it up after evaluating an expression in order to see the results. With this change, the inferior process buffer will always be displayed after evaluating an expression so that one can alway see the results without having to bring up the inferior process buffer. --- js-comint.el | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/js-comint.el b/js-comint.el index b1917ce..e189204 100644 --- a/js-comint.el +++ b/js-comint.el @@ -193,8 +193,9 @@ is run). nil inferior-js-program-arguments) (inferior-js-mode))) (setq inferior-js-buffer "*js*") - (if (not dont-switch-p) - (pop-to-buffer "*js*"))) + (if dont-switch-p + (display-buffer "*js*") + (pop-to-buffer "*js*"))) ;;;###autoload (defun js-send-region (start end) From 986c7795139a759b3571b437346aa0f4d075d9d1 Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Tue, 13 Dec 2016 12:36:20 +1100 Subject: [PATCH 02/52] add js-clear --- js-comint.el | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/js-comint.el b/js-comint.el index e189204..995cff3 100644 --- a/js-comint.el +++ b/js-comint.el @@ -155,8 +155,23 @@ Return a string representing the node version." (let ((cmd (concat "require(\"" filename "\")\n"))) (when (not (js--is-nodejs)) (setq cmd (concat "load(\"" filename "\")\n"))) - cmd - )) + cmd)) + +;;;###autoload +(defun js-clear () + "Clear the *js* buffer." + (interactive) + (let* ((buf (get-buffer inferior-js-buffer)) + (old-buf (current-buffer))) + (save-excursion + (cond + ((buffer-live-p buf) + (switch-to-buffer buf) + (erase-buffer) + (switch-to-buffer old-buf) + (message "*js* buffer cleared.")) + (t + (error "*js* buffer doesn't exist!")))))) ;;;###autoload (defun run-js (cmd &optional dont-switch-p) From b5d6cb831fd2eb92a147ad0f7e17523343c3974a Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Tue, 13 Dec 2016 12:46:27 +1100 Subject: [PATCH 03/52] ignore shebang (first line) of node js script --- js-comint.el | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/js-comint.el b/js-comint.el index 995cff3..813d06e 100644 --- a/js-comint.el +++ b/js-comint.el @@ -251,18 +251,27 @@ is run). (point)) (point))) +(defun js-get-buffer-point-min () + (let* ((rlt (point-min))) + (save-excursion + (goto-char rlt) + (when (= (following-char) ?#) + (forward-line) + (setq rlt (line-beginning-position)))) + rlt)) + ;;;###autoload (defun js-send-buffer () "Send the buffer to the inferior Javascript process." (interactive) - (js-send-region (point-min) (point-max))) + (js-send-region (js-get-buffer-point-min) (point-max))) ;;;###autoload (defun js-send-buffer-and-go () "Send the buffer to the inferior Javascript process." (interactive) - (js-send-region-and-go (point-min) (point-max))) + (js-send-region-and-go (js-get-buffer-point-min) (point-max))) ;;;###autoload (defun js-load-file (filename) From 20e69455fbff6d896b6ce29152be6a750f710bc9 Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Tue, 13 Dec 2016 13:50:22 +1100 Subject: [PATCH 04/52] could hide `undefined` output --- js-comint.el | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/js-comint.el b/js-comint.el index 813d06e..4e0ff71 100644 --- a/js-comint.el +++ b/js-comint.el @@ -2,6 +2,7 @@ ;;; Copyright (C) 2008 Paul Huff ;;; Copyright (C) 2015 Stefano Mazzucco +;;; Copyright (C) 2016 Chen Bin ;;; Author: Paul Huff , Stefano Mazzucco ;;; Maintainer: Chen Bin @@ -112,7 +113,12 @@ (defvar js-prompt-regexp "^\\(?:> \\)" "Prompt for `run-js'.") -(defvar js-nvm-current-version nil "Current version of node.") +(defvar js-comint-drop-regexp + "^[ \t]*undefined[\r\n]+" + "Regexp that matches text to silently drop.") + +(defvar js-nvm-current-version nil + "Current version of node.") (defun js-list-nvm-versions (prompt) "List all available node versions from nvm prompting the user with PROMPT. @@ -290,6 +296,18 @@ is run). (comint-send-string inferior-js-buffer (js--guess-load-file-cmd filename)) (switch-to-js inferior-js-buffer))) +;;;###autoload +(defun js-comint-process-output (string) + "Cleaner output." + (let* ((start-marker (or comint-last-output-start + (point-min-marker))) + (end-marker (process-mark (get-buffer-process (current-buffer))))) + ;; shamelessly copied from `ansi-color-process-output' + (save-excursion + (goto-char start-marker) + (while (re-search-forward js-comint-drop-regexp end-marker t) + (replace-match ""))))) + ;;;###autoload (defun switch-to-js (eob-p) "Switch to the javascript process buffer. From 2e94173c081690ebe3233b877738be8a6e98d6de Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Tue, 13 Dec 2016 16:00:48 +1100 Subject: [PATCH 05/52] could setup $NODE_PATH automatically --- js-comint.el | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/js-comint.el b/js-comint.el index 4e0ff71..554cbf2 100644 --- a/js-comint.el +++ b/js-comint.el @@ -208,6 +208,25 @@ is run). (setq inferior-js-program-command (pop inferior-js-program-arguments))))) (setenv "NODE_NO_READLINE" "1") + + ;; add "node_modules/" into $NODE_PATH + (let* ((node-modules (locate-dominating-file default-directory "node_modules")) + (node-path (getenv "NODE_PATH"))) + (cond + (node-modules + (setq node-modules (concat (file-name-as-directory node-modules) "node_modules")) + (cond + ((or (not node-path) + (string= "" node-path)) + ;; set + (setenv "NODE_PATH" node-modules)) + ((not (string-match-p node-modules node-path)) + ;; append + (setenv "NODE_PATH" (concat node-path ":" node-modules)))) + (message "%s added into \$NODE_PATH" node-modules)) + (t + (message "Can't find node_modules/")))) + (if (not (comint-check-proc "*js*")) (with-current-buffer (apply 'make-comint "js" inferior-js-program-command From 067d52cd5f1f30634b7f332b33d0ee181594508f Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Tue, 13 Dec 2016 16:00:57 +1100 Subject: [PATCH 06/52] clean code and README --- README.org | 16 +++++++++++++++- js-comint.el | 22 +++++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/README.org b/README.org index e016ff2..825c1df 100644 --- a/README.org +++ b/README.org @@ -1,4 +1,4 @@ -* js-comint.el (v0.0.5) +* js-comint.el (v1.0.0) Run a JavaScript interpreter in an inferior process window The first release, [[http://js-comint-el.sourceforge.net/][js-comint 0.0.1, is hosted on sourceforge]] but it has not been updated for five years. @@ -39,11 +39,25 @@ Add js-comint to your Cask file: * Usage After installation, do `M-x run-js` to create a comint buffer with the JavaScript interpreter. +Please note =node_modules== is *automatically* searched and appended into environment variable `NODE_PATH'. So you can use third party javascript without setup. For example, after =npm install --save moment=, run below command in js-comint shell, +#+begin_src javascript +require('moment')().format('YYYY-MM-DD'); +#+end_src + +You can =M-x js-clear= before =M-x js-send-buffer= to get clean output. + +In order to get clearner output when using NodeJS, I suggest add below setup into =.emacs=, +#+begin_src elisp +(defun inferior-js-mode-hook-setup () + (add-hook 'comint-output-filter-functions 'js-comint-process-output)) +(add-hook 'inferior-js-mode-hook 'inferior-js-mode-hook-setup t) +#+end_src * Customization You can set the `inferior-js-program-command' string and the `inferior-js-program-arguments' list to the executable that runs the JS interpreter and the arguments to pass to it respectively. E.g., the default is: #+BEGIN_SRC elisp +;; You can also customize `js-comint-drop-regexp' to filter output (setq inferior-js-program-command "node") (setq inferior-js-program-arguments '("--interactive")) #+END_SRC diff --git a/js-comint.el b/js-comint.el index 554cbf2..865f946 100644 --- a/js-comint.el +++ b/js-comint.el @@ -7,7 +7,7 @@ ;;; Author: Paul Huff , Stefano Mazzucco ;;; Maintainer: Chen Bin ;;; Created: 15 Feb 2014 -;;; Version: 0.0.5 +;;; Version: 1.0.0 ;;; URL: https://github.com/redguardtoo/js-comint ;;; Package-Requires: () ;;; Keywords: javascript, node, inferior-mode, convenience @@ -60,6 +60,8 @@ ;; Do: `M-x run-js' ;; Away you go. +;; `node_modules' is *automatically* searched and appended into environment +;; variable `NODE_PATH'. So 3rd party javascript is usable out of box. ;; If you have nvm, you can select the versions of node.js installed and run ;; them. This is done thanks to nvm.el. @@ -68,8 +70,19 @@ ;; The first time you start the JS interpreter with run-js, you will be asked ;; to select a version of node.js ;; If you want to change version of node js, run `js-select-node-version' - -;; You can add the following couple of lines to your .emacs to take advantage of +;; +;; `js-clear' clears the content of REPL. +;; +;; You may get cleaner output by following setup (highly recommended): +;; +;; (defun inferior-js-mode-hook-setup () +;; (add-hook 'comint-output-filter-functions 'js-comint-process-output)) +;; (add-hook 'inferior-js-mode-hook 'inferior-js-mode-hook-setup t) +;; +;; `js-comint-process-output' uses `js-comint-drop-regexp' which could be +;; customized by users. +;; +;; You can add the following lines to your .emacs to take advantage of ;; cool keybindings for sending things to the javascript interpreter inside ;; of Steve Yegge's most excellent js2-mode. ;; @@ -154,8 +167,7 @@ Return a string representing the node version." "/node")))) (defun js--is-nodejs () - (string= "node" - (substring-no-properties inferior-js-program-command -4 nil))) + (string-match-p "node$" inferior-js-program-command)) (defun js--guess-load-file-cmd (filename) (let ((cmd (concat "require(\"" filename "\")\n"))) From 2f42249c0a18ad2e17cfb272d75aa9fd3ec5e4ec Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Thu, 22 Dec 2016 10:39:22 +1100 Subject: [PATCH 07/52] fixed typo --- README.org | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.org b/README.org index 825c1df..2d338b4 100644 --- a/README.org +++ b/README.org @@ -39,14 +39,14 @@ Add js-comint to your Cask file: * Usage After installation, do `M-x run-js` to create a comint buffer with the JavaScript interpreter. -Please note =node_modules== is *automatically* searched and appended into environment variable `NODE_PATH'. So you can use third party javascript without setup. For example, after =npm install --save moment=, run below command in js-comint shell, +Please note the directory =node_modules= is *automatically* searched and appended into environment variable `NODE_PATH'. So you can use third party javascript without setup. For example, after =npm install --save moment=, run below command in js-comint shell, #+begin_src javascript require('moment')().format('YYYY-MM-DD'); #+end_src You can =M-x js-clear= before =M-x js-send-buffer= to get clean output. -In order to get clearner output when using NodeJS, I suggest add below setup into =.emacs=, +In order to get cleaner output when using NodeJS, I suggest add below setup into =.emacs=, #+begin_src elisp (defun inferior-js-mode-hook-setup () (add-hook 'comint-output-filter-functions 'js-comint-process-output)) From 048fa08f02006dad47591851f59f2c67e5f5e766 Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Fri, 16 Jun 2017 23:46:18 +1000 Subject: [PATCH 08/52] Learn from nodejs-repl --- js-comint.el | 272 ++++++++++++++++++++++----------------------------- 1 file changed, 115 insertions(+), 157 deletions(-) diff --git a/js-comint.el b/js-comint.el index 865f946..5819725 100644 --- a/js-comint.el +++ b/js-comint.el @@ -2,7 +2,7 @@ ;;; Copyright (C) 2008 Paul Huff ;;; Copyright (C) 2015 Stefano Mazzucco -;;; Copyright (C) 2016 Chen Bin +;;; Copyright (C) 2016-2017 Chen Bin ;;; Author: Paul Huff , Stefano Mazzucco ;;; Maintainer: Chen Bin @@ -16,7 +16,7 @@ ;;; License: -;; js-comint.el is free software; you can redistribute it and/or +;; This program is free software; you can redistribute it and/or ;; modify it under the terms of the GNU General Public License as ;; published by the Free Software Foundation; either version 2, or ;; at your option any later version. @@ -46,19 +46,10 @@ ;; Put js-comint.el in your load path ;; Add (require 'js-comint) to your .emacs or ~/.emacs.d/init.el ;; -;; Optionally, set the `inferior-js-program-command' string -;; and the `inferior-js-program-arguments' list to the executable that runs +;; Optionally, set the `js-comint-program-command' string +;; and the `js-comint-program-arguments' list to the executable that runs ;; the JS interpreter and the arguments to pass to it respectively. -;; E.g., the default is: -;; (setq inferior-js-program-command "node") -;; (setq inferior-js-program-arguments '("--interactive")) - -;; E.g. Set up the Rhino JAR downloaded from -;; https://github.com/mozilla/rhino -;; (setq inferior-js-program-command "java") -;; (setq inferior-js-program-arguments '("-jar" "/absolute/path/to/rhino/js.jar")) - -;; Do: `M-x run-js' +;; Do: `M-x js-comint-repl' ;; Away you go. ;; `node_modules' is *automatically* searched and appended into environment ;; variable `NODE_PATH'. So 3rd party javascript is usable out of box. @@ -71,16 +62,11 @@ ;; to select a version of node.js ;; If you want to change version of node js, run `js-select-node-version' ;; -;; `js-clear' clears the content of REPL. +;; `js-comint-clear' clears the content of REPL. ;; ;; You may get cleaner output by following setup (highly recommended): ;; -;; (defun inferior-js-mode-hook-setup () -;; (add-hook 'comint-output-filter-functions 'js-comint-process-output)) -;; (add-hook 'inferior-js-mode-hook 'inferior-js-mode-hook-setup t) -;; -;; `js-comint-process-output' uses `js-comint-drop-regexp' which could be -;; customized by users. +;; Output matching `js-comint-drop-regexp' will be dropped silently ;; ;; You can add the following lines to your .emacs to take advantage of ;; cool keybindings for sending things to the javascript interpreter inside @@ -89,47 +75,62 @@ ;; (add-hook 'js2-mode-hook ;; (lambda () ;; (local-set-key (kbd "C-x C-e") 'js-send-last-sexp) -;; (local-set-key (kbd "C-M-x") 'js-send-last-sexp-and-go) ;; (local-set-key (kbd "C-c b") 'js-send-buffer) -;; (local-set-key (kbd "C-c C-b") 'js-send-buffer-and-go) -;; (local-set-key (kbd "C-c l") 'js-load-file-and-go))) +;; )) ;;; Code: +(require 'js) (require 'comint) +(require 'ansi-color) -(defgroup inferior-js nil +(defgroup js-comint nil "Run a javascript process in a buffer." - :group 'inferior-js) + :group 'js-comint) -(defcustom inferior-js-program-command "node" +(defcustom js-comint-program-command "node" "JavaScript interpreter." - :group 'inferior-js) + :group 'js-comint) + +(defvar js-comint-drop-regexp + "\\(\x1b\\[[0-9]+[GJK]\\|^[ \t]*undefined[\r\n]+\\)" + "Regex to silence matching output.") -(defcustom inferior-js-program-arguments '("--interactive") - "List of command line arguments to pass to the JavaScript interpreter." - :group 'inferior-js) +(defcustom js-comint-program-arguments '() + "List of command line arguments passed to the JavaScript interpreter." + :group 'js-comint) -(defcustom inferior-js-mode-hook nil - "*Hook for customizing inferior-js mode." +(defcustom js-comint-prompt "> " + "Prompt used in `js-comint-mode'." + :group 'js-comint + :type 'string) + +(defcustom js-comint-mode-hook nil + "*Hook for customizing js-comint mode." :type 'hook - :group 'inferior-js) + :group 'js-comint) (defcustom js-use-nvm nil "When t, use NVM. Requires nvm.el." :type 'boolean - :group 'inferior-js) + :group 'js-comint) -(defvar inferior-js-buffer nil +(defvar js-comint-buffer "Javascript REPL" "Name of the inferior JavaScript buffer.") +;; process.stdout.columns should be set. +;; but process.stdout.columns in Emacs is infinity because Emacs returns 0 as winsize.ws_col. +;; The completion candidates won't be displayed if process.stdout.columns is infinity. +;; see also `handleGroup` function in readline.js +(defvar js-comint-code-format + (concat + "process.stdout.columns = %d;" + "require('repl').start('%s', null, null, true, false, " + "require('repl')['REPL_MODE_' + '%s'.toUpperCase()])")) + (defvar js-prompt-regexp "^\\(?:> \\)" "Prompt for `run-js'.") -(defvar js-comint-drop-regexp - "^[ \t]*undefined[\r\n]+" - "Regexp that matches text to silently drop.") - (defvar js-nvm-current-version nil "Current version of node.") @@ -160,14 +161,14 @@ Return a string representing the node version." (format "Node version (current %s): " (car old-js-nvm)) "Node version: ")))))) (progn - (setq inferior-js-program-command + (setq js-comint-program-command (concat (car (last js-nvm-current-version)) "/bin" "/node")))) (defun js--is-nodejs () - (string-match-p "node$" inferior-js-program-command)) + (string-match-p "node$" js-comint-program-command)) (defun js--guess-load-file-cmd (filename) (let ((cmd (concat "require(\"" filename "\")\n"))) @@ -175,11 +176,27 @@ Return a string representing the node version." (setq cmd (concat "load(\"" filename "\")\n"))) cmd)) +(defun js-comint-quit-or-cancel () + "Send ^C to Node.js process." + (interactive) + (process-send-string (get-process js-comint-buffer) "\x03")) + +(defun js-cominit-filter-output (string) + "Filter extra escape sequences from output." + (let ((beg (or comint-last-output-start + (point-min-marker))) + (end (process-mark (get-buffer-process (current-buffer))))) + (save-excursion + (goto-char beg) + ;; Remove ansi escape sequences used in readline.js + (while (re-search-forward js-comint-drop-regexp end t) + (replace-match ""))))) + ;;;###autoload -(defun js-clear () - "Clear the *js* buffer." +(defun js-comint-clear () + "Clear the Javascript REPL." (interactive) - (let* ((buf (get-buffer inferior-js-buffer)) + (let* ((buf (get-buffer js-comint-buffer)) (old-buf (current-buffer))) (save-excursion (cond @@ -187,19 +204,14 @@ Return a string representing the node version." (switch-to-buffer buf) (erase-buffer) (switch-to-buffer old-buf) - (message "*js* buffer cleared.")) + (message "Javascript REPL cleared.")) (t - (error "*js* buffer doesn't exist!")))))) + (error "Javascript REPL buffer doesn't exist!")))))) +(defalias 'js-clear 'js-comint-clear) ;;;###autoload -(defun run-js (cmd &optional dont-switch-p) - "Run an inferior Javascript process, input and output via buffer `*js*'. -If there is a process already running in `*js*', switch to that buffer. -With argument, allows you to edit the command line (default is value -of `inferior-js-program-command'). -Runs the hook `inferior-js-mode-hook' \(after the `comint-mode-hook' -is run). -\(Type \\[describe-mode] in the process buffer for a list of commands.)" +(defun js-comint-repl (cmd) + "Run an Javascript process." (interactive (list (when current-prefix-arg @@ -208,16 +220,16 @@ is run). (mapconcat 'identity (cons - inferior-js-program-command - inferior-js-program-arguments) + js-comint-program-command + js-comint-program-arguments) " "))) (when js-use-nvm (unless (featurep 'nvm) (require 'nvm)) (unless js-nvm-current-version (js-select-node-version))) - (setq inferior-js-program-arguments (split-string cmd)) - (setq inferior-js-program-command (pop inferior-js-program-arguments))))) + (setq js-comint-program-arguments (split-string cmd)) + (setq js-comint-program-command (pop js-comint-program-arguments))))) (setenv "NODE_NO_READLINE" "1") @@ -239,46 +251,28 @@ is run). (t (message "Can't find node_modules/")))) - (if (not (comint-check-proc "*js*")) - (with-current-buffer - (apply 'make-comint "js" inferior-js-program-command - nil inferior-js-program-arguments) - (inferior-js-mode))) - (setq inferior-js-buffer "*js*") - (if dont-switch-p - (display-buffer "*js*") - (pop-to-buffer "*js*"))) + (let* ((repl-mode (or (getenv "NODE_REPL_MODE") "magic")) + (js-comint-code (format js-comint-code-format + (window-width) js-comint-prompt repl-mode))) + (pop-to-buffer + (apply 'make-comint js-comint-buffer js-comint-program-command nil + `(,@js-comint-program-arguments "-e" ,js-comint-code))) + (js-comint-mode))) +(defalias 'run-js 'js-comint-repl) ;;;###autoload -(defun js-send-region (start end) +(defun js-comint-send-region (start end) "Send the current region to the inferior Javascript process." (interactive "r") - (run-js inferior-js-program-command t) - (comint-send-region inferior-js-buffer start end) - (comint-send-string inferior-js-buffer "\n")) + (run-js js-comint-program-command t) + (comint-send-region js-comint-buffer start end) + (comint-send-string js-comint-buffer "\n")) ;;;###autoload -(defun js-send-region-and-go (start end) - "Send the current region to the inferior Javascript process." - (interactive "r") - (run-js inferior-js-program-command t) - (comint-send-region inferior-js-buffer start end) - ;; (comint-send-string inferior-js-buffer "\n") - (switch-to-js inferior-js-buffer)) - -;;;###autoload -(defun js-send-last-sexp-and-go () - "Send the previous sexp to the inferior Js process." - (interactive) - (js-send-region-and-go - (save-excursion - (backward-sexp) - (move-beginning-of-line nil) - (point)) - (point))) +(defalias 'js-comint-send-region 'js-send-region) ;;;###autoload -(defun js-send-last-sexp () +(defun js-comint-send-last-sexp () "Send the previous sexp to the inferior Javascript process." (interactive) (js-send-region @@ -288,94 +282,58 @@ is run). (point)) (point))) -(defun js-get-buffer-point-min () - (let* ((rlt (point-min))) - (save-excursion - (goto-char rlt) - (when (= (following-char) ?#) - (forward-line) - (setq rlt (line-beginning-position)))) - rlt)) - ;;;###autoload -(defun js-send-buffer () - "Send the buffer to the inferior Javascript process." - (interactive) - (js-send-region (js-get-buffer-point-min) (point-max))) - +(defalias 'js-send-last-sexp 'js-comint-send-region) ;;;###autoload -(defun js-send-buffer-and-go () +(defun js-comint-send-buffer () "Send the buffer to the inferior Javascript process." (interactive) - (js-send-region-and-go (js-get-buffer-point-min) (point-max))) + (js-send-region (point-min) (point-max))) ;;;###autoload -(defun js-load-file (filename) - "Load a file in the javascript interpreter." - (interactive "f") - (let ((filename (expand-file-name filename))) - (run-js inferior-js-program-command t) - (comint-send-string inferior-js-buffer (js--guess-load-file-cmd filename)))) +(defalias 'js-send-buffer 'js-comint-send-buffer) ;;;###autoload -(defun js-load-file-and-go (filename) +(defun js-comint-load-file (filename) "Load a file in the javascript interpreter." (interactive "f") (let ((filename (expand-file-name filename))) - (run-js inferior-js-program-command t) - (comint-send-string inferior-js-buffer (js--guess-load-file-cmd filename)) - (switch-to-js inferior-js-buffer))) + (run-js js-comint-program-command t) + (comint-send-string js-comint-buffer (js--guess-load-file-cmd filename)))) ;;;###autoload -(defun js-comint-process-output (string) - "Cleaner output." - (let* ((start-marker (or comint-last-output-start - (point-min-marker))) - (end-marker (process-mark (get-buffer-process (current-buffer))))) - ;; shamelessly copied from `ansi-color-process-output' - (save-excursion - (goto-char start-marker) - (while (re-search-forward js-comint-drop-regexp end-marker t) - (replace-match ""))))) +(defalias 'js-load-file 'js-comint-load-file) ;;;###autoload -(defun switch-to-js (eob-p) +(defun js-comint-switch-to-repl (eob-p) "Switch to the javascript process buffer. With argument, position cursor at end of buffer." (interactive "P") - (if (and inferior-js-buffer (get-buffer inferior-js-buffer)) - (pop-to-buffer inferior-js-buffer) - (error "No current process buffer. See variable `inferior-js-buffer'")) - (when eob-p - (push-mark) - (goto-char (point-max)))) - -(defvar inferior-js-mode-map - (let ((m (make-sparse-keymap))) - (define-key m "\C-x\C-e" 'js-send-last-sexp) - (define-key m "\C-cl" 'js-load-file) - m)) + (if (and js-comint-buffer (get-buffer js-comint-buffer)) + (pop-to-buffer js-comint-buffer) + (error "No current process buffer. See variable `js-comint-buffer'"))) +(defalias 'switch-to-js 'js-comint-switch-repl) -;;;###autoload -(define-derived-mode inferior-js-mode comint-mode "Inferior Javascript" - "Major mode for interacting with an inferior javascript process. - -The following commands are available: -\\{inferior-js-mode-map} +(defvar js-comint-mode-map + (let ((map (make-sparse-keymap))) + (define-key map (kbd "C-c l") 'js-comint-load-file) + (define-key map (kbd "C-c C-c") 'js-comint-quit-or-cancel) + map)) -A javascript process can be fired up with M-x run-js. -Customization: Entry to this mode runs the hooks on comint-mode-hook and -inferior-js-mode-hook (in that order). - -You can send text to the inferior Javascript process from other buffers containing -Javascript source. - switch-to-js switches the current buffer to the Javascript process buffer. - js-send-region sends the current region to the Javascript process. -" - :group 'inferior-js - (use-local-map inferior-js-mode-map)) +;;;###autoload +(define-derived-mode js-comint-mode comint-mode "Javascript REPL" + :group 'js-comint + :syntax-table js-mode-syntax-table + (setq-local font-lock-defaults (list js--font-lock-keywords)) + ;; No echo + (setq comint-process-echoes t) + ;; Ignore duplicates + (setq comint-input-ignoredups t) + (add-hook 'comint-output-filter-functions 'js-cominit-filter-output nil t) + (use-local-map js-comint-mode-map) + (ansi-color-for-comint-mode-on)) (provide 'js-comint) ;;; js-comint.el ends here From 9113cb538c9b9e1a1ccf0b6a4e8197b5f03faa51 Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Sat, 17 Jun 2017 23:28:44 +1000 Subject: [PATCH 09/52] use js-comint-module-paths to search for node module --- js-comint.el | 177 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 119 insertions(+), 58 deletions(-) diff --git a/js-comint.el b/js-comint.el index 5819725..b0b369a 100644 --- a/js-comint.el +++ b/js-comint.el @@ -58,9 +58,9 @@ ;; them. This is done thanks to nvm.el. ;; Please note nvm.el is optional. So you need *manually* install it. ;; To enable nvm support, run `js-do-use-nvm'. -;; The first time you start the JS interpreter with run-js, you will be asked -;; to select a version of node.js -;; If you want to change version of node js, run `js-select-node-version' +;; The first time you start the JS interpreter with `js-comint-repl', you will +;;be asked to select a version of Node.js +;; If you want to change version of node js, run `js-comint-select-node-version' ;; ;; `js-comint-clear' clears the content of REPL. ;; @@ -92,6 +92,10 @@ "JavaScript interpreter." :group 'js-comint) +(defvar js-comint-module-paths '() + "List of modules paths which could be used by NodeJS +to search modules.") + (defvar js-comint-drop-regexp "\\(\x1b\\[[0-9]+[GJK]\\|^[ \t]*undefined[\r\n]+\\)" "Regex to silence matching output.") @@ -128,13 +132,10 @@ "require('repl').start('%s', null, null, true, false, " "require('repl')['REPL_MODE_' + '%s'.toUpperCase()])")) -(defvar js-prompt-regexp "^\\(?:> \\)" - "Prompt for `run-js'.") - (defvar js-nvm-current-version nil "Current version of node.") -(defun js-list-nvm-versions (prompt) +(defun js-comint-list-nvm-versions (prompt) "List all available node versions from nvm prompting the user with PROMPT. Return a string representing the node version." (let ((candidates (sort (mapcar 'car (nvm--installed-versions)) 'string<))) @@ -142,13 +143,14 @@ Return a string representing the node version." candidates nil t nil nil (car candidates)))) + ;;;###autoload (defun js-do-use-nvm () "Enable nvm." (setq js-use-nvm t)) ;;;###autoload -(defun js-select-node-version (&optional version) +(defun js-comint-select-node-version (&optional version) "Use a given VERSION of node from nvm." (interactive) (if version @@ -156,7 +158,7 @@ Return a string representing the node version." (let ((old-js-nvm js-nvm-current-version)) (setq js-nvm-current-version (nvm--find-exact-version-for - (js-list-nvm-versions + (js-comint-list-nvm-versions (if old-js-nvm (format "Node version (current %s): " (car old-js-nvm)) "Node version: ")))))) @@ -167,20 +169,95 @@ Return a string representing the node version." "/bin" "/node")))) -(defun js--is-nodejs () - (string-match-p "node$" js-comint-program-command)) - -(defun js--guess-load-file-cmd (filename) - (let ((cmd (concat "require(\"" filename "\")\n"))) - (when (not (js--is-nodejs)) - (setq cmd (concat "load(\"" filename "\")\n"))) - cmd)) +(defun js-comint-guess-load-file-cmd (filename) + (concat "require(\"" filename "\")\n")) (defun js-comint-quit-or-cancel () - "Send ^C to Node.js process." + "Send ^C to Javascript REPL." (interactive) (process-send-string (get-process js-comint-buffer) "\x03")) +(defun js-comint--path-sep () + (if (eq system-type 'windows-nt) ";" ":")) + +(defun js-comint--suggest-module-path () + (let* ((dir (locate-dominating-file default-directory + "node_modules"))) + (if dir (concat (file-name-as-directory dir) + "node_modules") + default-directory))) + +;;;###autoload +(defun js-comint-add-module-path () + "Add a directory to `js-comint-module-paths'." + (interactive) + (let* ((dir (read-directory-name "Module path:" + (js-comint--suggest-module-path)))) + (when dir + (add-to-list 'js-comint-module-paths (file-truename dir)) + (message "\"%s\" added to `js-comint-module-paths'" dir)))) + +;;;###autoload +(defun js-comint-delete-module-path () + "Delete a directory from `js-comint-module-paths'." + (interactive) + (cond + ((not js-comint-module-paths) + (message "`js-comint-module-paths' is empty.")) + (t + (let* ((dir (ido-completing-read "Directory to delete: " + js-comint-module-paths))) + (when dir + (setq js-comint-module-paths + (delete dir js-comint-module-paths)) + (message "\"%s\" delete from `js-comint-module-paths'" dir)))))) + +;;;###autoload +(defun js-comint-save-setup () + "Save current setup to `.dir-locals.el'" + (interactive) + (let* (sexp + (root (read-directory-name "Where to create .dir-locals.el: " + default-directory)) + (file (concat (file-name-as-directory root) + ".dir-locals.el"))) + (cond + (js-comint-module-paths + (setq sexp (list (list nil (cons 'js-comint-module-paths js-comint-module-paths)))) + (with-temp-buffer + (insert (format "%S" sexp)) + (write-file file) + (message "%s created." file))) + (t + (message "Nothing to save. `js-comint-module-paths' is empty."))))) + +(defun js-comint-setup-module-paths () + (let* ((paths (mapconcat 'identity + js-comint-module-paths + (js-comint--path-sep))) + (node-path (getenv "NODE_PATH"))) + (cond + ((or (not node-path) + (string= "" node-path)) + ;; set + (setenv "NODE_PATH" paths)) + ((not (string= "" paths)) + ;; append + (setenv "NODE_PATH" (concat node-path (js-comint--path-sep) paths)) + (message "%s added into \$NODE_PATH" paths))))) + +;;;###autoload +(defun js-comint-reset-repl () + "Kill existing REPL process if possible. Create a new +Javascript REPL process. The environment variable `NODE_PATH' +is setup by `js-comint-module-paths' before the process starts." + (interactive) + (when (get-process js-comint-buffer) + (process-send-string (get-process js-comint-buffer) ".exit\n") + ;; wait the process to be killed + (sit-for 1)) + (js-comint-start-or-switch-to-repl)) + (defun js-cominit-filter-output (string) "Filter extra escape sequences from output." (let ((beg (or comint-last-output-start @@ -209,62 +286,47 @@ Return a string representing the node version." (error "Javascript REPL buffer doesn't exist!")))))) (defalias 'js-clear 'js-comint-clear) + +(defun js-comint-start-or-switch-to-repl () + (setenv "NODE_NO_READLINE" "1") + (js-comint-setup-module-paths) + (let* ((repl-mode (or (getenv "NODE_REPL_MODE") "magic")) + (js-comint-code (format js-comint-code-format + (window-width) js-comint-prompt repl-mode))) + (pop-to-buffer + (apply 'make-comint js-comint-buffer js-comint-program-command nil + `(,@js-comint-program-arguments "-e" ,js-comint-code))) + (js-comint-mode))) + ;;;###autoload (defun js-comint-repl (cmd) - "Run an Javascript process." + "Run an Javascript process. The environment variable `NODE_PATH' +is setup by `js-comint-module-paths' before the process +starts." (interactive (list + ;; You can select node version here (when current-prefix-arg (setq cmd (read-string "Run js: " - (mapconcat - 'identity - (cons - js-comint-program-command - js-comint-program-arguments) - " "))) + (format "%s %s" + js-comint-program-command + js-comint-program-arguments))) (when js-use-nvm (unless (featurep 'nvm) (require 'nvm)) (unless js-nvm-current-version - (js-select-node-version))) + (js-comint-select-node-version))) (setq js-comint-program-arguments (split-string cmd)) (setq js-comint-program-command (pop js-comint-program-arguments))))) - - (setenv "NODE_NO_READLINE" "1") - - ;; add "node_modules/" into $NODE_PATH - (let* ((node-modules (locate-dominating-file default-directory "node_modules")) - (node-path (getenv "NODE_PATH"))) - (cond - (node-modules - (setq node-modules (concat (file-name-as-directory node-modules) "node_modules")) - (cond - ((or (not node-path) - (string= "" node-path)) - ;; set - (setenv "NODE_PATH" node-modules)) - ((not (string-match-p node-modules node-path)) - ;; append - (setenv "NODE_PATH" (concat node-path ":" node-modules)))) - (message "%s added into \$NODE_PATH" node-modules)) - (t - (message "Can't find node_modules/")))) - - (let* ((repl-mode (or (getenv "NODE_REPL_MODE") "magic")) - (js-comint-code (format js-comint-code-format - (window-width) js-comint-prompt repl-mode))) - (pop-to-buffer - (apply 'make-comint js-comint-buffer js-comint-program-command nil - `(,@js-comint-program-arguments "-e" ,js-comint-code))) - (js-comint-mode))) + (js-comint-start-or-switch-to-repl)) (defalias 'run-js 'js-comint-repl) ;;;###autoload (defun js-comint-send-region (start end) "Send the current region to the inferior Javascript process." (interactive "r") - (run-js js-comint-program-command t) + (js-comint-repl js-comint-program-command) (comint-send-region js-comint-buffer start end) (comint-send-string js-comint-buffer "\n")) @@ -299,8 +361,8 @@ Return a string representing the node version." "Load a file in the javascript interpreter." (interactive "f") (let ((filename (expand-file-name filename))) - (run-js js-comint-program-command t) - (comint-send-string js-comint-buffer (js--guess-load-file-cmd filename)))) + (js-comint-repl js-comint-program-command) + (comint-send-string js-comint-buffer (js-comint-guess-load-file-cmd filename)))) ;;;###autoload (defalias 'js-load-file 'js-comint-load-file) @@ -317,7 +379,6 @@ With argument, position cursor at end of buffer." (defvar js-comint-mode-map (let ((map (make-sparse-keymap))) - (define-key map (kbd "C-c l") 'js-comint-load-file) (define-key map (kbd "C-c C-c") 'js-comint-quit-or-cancel) map)) From 603aab6a5012e8c7d4db8de00659a6f2c98fbcaa Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Tue, 27 Jun 2017 16:49:32 +1000 Subject: [PATCH 10/52] fixed typo --- js-comint.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js-comint.el b/js-comint.el index b0b369a..a456f09 100644 --- a/js-comint.el +++ b/js-comint.el @@ -331,7 +331,7 @@ starts." (comint-send-string js-comint-buffer "\n")) ;;;###autoload -(defalias 'js-comint-send-region 'js-send-region) +(defalias 'js-send-region 'js-comint-send-region) ;;;###autoload (defun js-comint-send-last-sexp () @@ -345,7 +345,7 @@ starts." (point))) ;;;###autoload -(defalias 'js-send-last-sexp 'js-comint-send-region) +(defalias 'js-send-last-sexp 'js-comint-send-last-sexp) ;;;###autoload (defun js-comint-send-buffer () From 2c19fafed953ea0972ff086614f86614f4d5dc13 Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Wed, 28 Jun 2017 12:40:07 +1000 Subject: [PATCH 11/52] fixed bugs. evil-mode friendly --- README.org | 4 +-- js-comint.el | 70 +++++++++++++++++++++++++++++++++++----------------- 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/README.org b/README.org index 2d338b4..6873345 100644 --- a/README.org +++ b/README.org @@ -1,4 +1,4 @@ -* js-comint.el (v1.0.0) +* js-comint.el (v1.1.0) Run a JavaScript interpreter in an inferior process window The first release, [[http://js-comint-el.sourceforge.net/][js-comint 0.0.1, is hosted on sourceforge]] but it has not been updated for five years. @@ -91,4 +91,4 @@ You can add the following couple of lines to your .emacs to take advantage of co (local-set-key (kbd "C-c b") 'js-send-buffer) (local-set-key (kbd "C-c C-b") 'js-send-buffer-and-go) (local-set-key (kbd "C-c l") 'js-load-file-and-go))) -#+END_SRC \ No newline at end of file +#+END_SRC diff --git a/js-comint.el b/js-comint.el index a456f09..2bff17d 100644 --- a/js-comint.el +++ b/js-comint.el @@ -7,7 +7,7 @@ ;;; Author: Paul Huff , Stefano Mazzucco ;;; Maintainer: Chen Bin ;;; Created: 15 Feb 2014 -;;; Version: 1.0.0 +;;; Version: 1.1.0 ;;; URL: https://github.com/redguardtoo/js-comint ;;; Package-Requires: () ;;; Keywords: javascript, node, inferior-mode, convenience @@ -175,7 +175,7 @@ Return a string representing the node version." (defun js-comint-quit-or-cancel () "Send ^C to Javascript REPL." (interactive) - (process-send-string (get-process js-comint-buffer) "\x03")) + (process-send-string (js-comint-get-process) "\x03")) (defun js-comint--path-sep () (if (eq system-type 'windows-nt) ";" ":")) @@ -187,6 +187,10 @@ Return a string representing the node version." "node_modules") default-directory))) +(defun js-comint-get-process () + (and js-comint-buffer + (get-process js-comint-buffer))) + ;;;###autoload (defun js-comint-add-module-path () "Add a directory to `js-comint-module-paths'." @@ -252,8 +256,8 @@ Return a string representing the node version." Javascript REPL process. The environment variable `NODE_PATH' is setup by `js-comint-module-paths' before the process starts." (interactive) - (when (get-process js-comint-buffer) - (process-send-string (get-process js-comint-buffer) ".exit\n") + (when (js-comint-get-process) + (process-send-string (js-comint-get-process) ".exit\n") ;; wait the process to be killed (sit-for 1)) (js-comint-start-or-switch-to-repl)) @@ -269,11 +273,18 @@ is setup by `js-comint-module-paths' before the process starts." (while (re-search-forward js-comint-drop-regexp end t) (replace-match ""))))) +(defun js-comint-get-buffer-name () + (format "*%s*" js-comint-buffer)) + +(defun js-comint-get-buffer () + (and js-comint-buffer + (get-buffer (js-comint-get-buffer-name)))) + ;;;###autoload (defun js-comint-clear () "Clear the Javascript REPL." (interactive) - (let* ((buf (get-buffer js-comint-buffer)) + (let* ((buf (js-comint-get-buffer) ) (old-buf (current-buffer))) (save-excursion (cond @@ -323,26 +334,39 @@ starts." (defalias 'run-js 'js-comint-repl) ;;;###autoload -(defun js-comint-send-region (start end) - "Send the current region to the inferior Javascript process." - (interactive "r") - (js-comint-repl js-comint-program-command) - (comint-send-region js-comint-buffer start end) - (comint-send-string js-comint-buffer "\n")) +(defun js-comint-send-string (str) + (comint-send-string (js-comint-get-process) + (concat str "\n"))) + +;;;###autoload +(defun js-comint-send-region () + "Send the current region to the inferior Javascript process. +If no region selected, you could manually input javascript expression." + (interactive) + (let* ((str (if (region-active-p) + (buffer-substring-no-properties (region-beginning) (region-end)) + (read-string "input js expression: ")))) + (message "str=%s" str) + (js-comint-send-string str))) ;;;###autoload (defalias 'js-send-region 'js-comint-send-region) ;;;###autoload (defun js-comint-send-last-sexp () - "Send the previous sexp to the inferior Javascript process." + "Send the previous sexp to the inferior Javascript process. `evil-mode' friendly." (interactive) - (js-send-region - (save-excursion - (backward-sexp) - (move-beginning-of-line nil) - (point)) - (point))) + (let* ((b (save-excursion + (backward-sexp) + (move-beginning-of-line nil) + (point))) + (e (if (and (boundp 'evil-mode) + evil-mode + (eq evil-state 'normal)) + (+ 1 (point)) + (point))) + (str (buffer-substring-no-properties b e))) + (js-comint-send-string str))) ;;;###autoload (defalias 'js-send-last-sexp 'js-comint-send-last-sexp) @@ -351,7 +375,9 @@ starts." (defun js-comint-send-buffer () "Send the buffer to the inferior Javascript process." (interactive) - (js-send-region (point-min) (point-max))) + (js-comint-send-string + (buffer-substring-no-properties (point-min) + (point-max)))) ;;;###autoload (defalias 'js-send-buffer 'js-comint-send-buffer) @@ -362,7 +388,7 @@ starts." (interactive "f") (let ((filename (expand-file-name filename))) (js-comint-repl js-comint-program-command) - (comint-send-string js-comint-buffer (js-comint-guess-load-file-cmd filename)))) + (comint-send-string (js-comint-get-process) (js-comint-guess-load-file-cmd filename)))) ;;;###autoload (defalias 'js-load-file 'js-comint-load-file) @@ -372,8 +398,8 @@ starts." "Switch to the javascript process buffer. With argument, position cursor at end of buffer." (interactive "P") - (if (and js-comint-buffer (get-buffer js-comint-buffer)) - (pop-to-buffer js-comint-buffer) + (if (js-comint-get-buffer) + (pop-to-buffer (js-comint-get-buffer)) (error "No current process buffer. See variable `js-comint-buffer'"))) (defalias 'switch-to-js 'js-comint-switch-repl) From eb4744122724b24e492c2171fff438e3ee2045a8 Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Tue, 8 Aug 2017 22:27:17 +1000 Subject: [PATCH 12/52] upgrade license --- js-comint.el | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/js-comint.el b/js-comint.el index 2bff17d..6defc84 100644 --- a/js-comint.el +++ b/js-comint.el @@ -8,36 +8,26 @@ ;;; Maintainer: Chen Bin ;;; Created: 15 Feb 2014 ;;; Version: 1.1.0 -;;; URL: https://github.com/redguardtoo/js-comint +;;; URL: https://github.com/js-comint/js-comint ;;; Package-Requires: () ;;; Keywords: javascript, node, inferior-mode, convenience -;; This file is NOT part of GNU Emacs. +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. -;;; License: - -;; This program is free software; you can redistribute it and/or -;; modify it under the terms of the GNU General Public License as -;; published by the Free Software Foundation; either version 2, or -;; at your option any later version. - -;; js-comint.el is distributed in the hope that it will be useful, but -;; WITHOUT ANY WARRANTY; without even the implied warranty of -;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -;; General Public License for more details. +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License -;; along with GNU Emacs; see the file COPYING, or type `C-h C-c'. If -;; not, write to the Free Software Foundation at this address: - -;; Free Software Foundation -;; 51 Franklin Street, Fifth Floor -;; Boston, MA 02110-1301 -;; USA +;; along with this program. If not, see . ;;; Commentary: -;; js-comint.el is a comint mode for Emacs which allows you to run a +;; This program is a comint mode for Emacs which allows you to run a ;; compatible javascript repl like Node.js/Spidermonkey/Rhino inside Emacs. ;; It also defines a few functions for sending javascript input to it ;; quickly. From 83e932e4a83d1a69098ee87e0ab911d299368e60 Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Thu, 30 Nov 2017 15:49:52 +1100 Subject: [PATCH 13/52] doc and bugs --- README.org | 7 +++- js-comint.el | 103 +++++++++++++++++++++++++++++---------------------- 2 files changed, 64 insertions(+), 46 deletions(-) diff --git a/README.org b/README.org index 6873345..1f49525 100644 --- a/README.org +++ b/README.org @@ -1,4 +1,4 @@ -* js-comint.el (v1.1.0) +* js-comint.el (v1.1.1) Run a JavaScript interpreter in an inferior process window The first release, [[http://js-comint-el.sourceforge.net/][js-comint 0.0.1, is hosted on sourceforge]] but it has not been updated for five years. @@ -36,6 +36,11 @@ Add js-comint to your Cask file: (depends-on "js-comint") #+END_SRC +** Windows +On window, you may need below setup: +#+begin_src elisp +(setq inferior-js-program-command "C:/Program Files/nodejs/node.exe") +#+end_src * Usage After installation, do `M-x run-js` to create a comint buffer with the JavaScript interpreter. diff --git a/js-comint.el b/js-comint.el index 6defc84..d1cca3b 100644 --- a/js-comint.el +++ b/js-comint.el @@ -1,4 +1,4 @@ -;;; js-comint.el --- Run a JavaScript interpreter in an inferior process window. +;;; js-comint.el --- JavaScript interpreter in window. ;;; Copyright (C) 2008 Paul Huff ;;; Copyright (C) 2015 Stefano Mazzucco @@ -7,23 +7,33 @@ ;;; Author: Paul Huff , Stefano Mazzucco ;;; Maintainer: Chen Bin ;;; Created: 15 Feb 2014 -;;; Version: 1.1.0 -;;; URL: https://github.com/js-comint/js-comint -;;; Package-Requires: () +;;; Version: 1.1.1 +;;; URL: https://github.com/redguardtoo/js-comint +;;; Package-Requires: ((emacs "24.3")) ;;; Keywords: javascript, node, inferior-mode, convenience -;; This program is free software; you can redistribute it and/or modify -;; it under the terms of the GNU General Public License as published by -;; the Free Software Foundation, either version 3 of the License, or -;; (at your option) any later version. +;; This file is NOT part of GNU Emacs. -;; This program is distributed in the hope that it will be useful, -;; but WITHOUT ANY WARRANTY; without even the implied warranty of -;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -;; GNU General Public License for more details. +;;; License: + +;; This program is free software; you can redistribute it and/or +;; modify it under the terms of the GNU General Public License as +;; published by the Free Software Foundation; either version 3, or +;; at your option any later version. + +;; js-comint.el is distributed in the hope that it will be useful, but +;; WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; General Public License for more details. ;; You should have received a copy of the GNU General Public License -;; along with this program. If not, see . +;; along with GNU Emacs; see the file COPYING, or type `C-h C-c'. If +;; not, write to the Free Software Foundation at this address: + +;; Free Software Foundation +;; 51 Franklin Street, Fifth Floor +;; Boston, MA 02110-1301 +;; USA ;;; Commentary: @@ -39,14 +49,17 @@ ;; Optionally, set the `js-comint-program-command' string ;; and the `js-comint-program-arguments' list to the executable that runs ;; the JS interpreter and the arguments to pass to it respectively. -;; Do: `M-x js-comint-repl' +;; For example, on windows you might need below setup: +;; (setq inferior-js-program-command "C:/Program Files/nodejs/node.exe") +;; +;; After setup, do: `M-x js-comint-repl' ;; Away you go. ;; `node_modules' is *automatically* searched and appended into environment -;; variable `NODE_PATH'. So 3rd party javascript is usable out of box. +;; variable `NODE_PATH'. So 3rd party javascript is usable out of box. ;; If you have nvm, you can select the versions of node.js installed and run ;; them. This is done thanks to nvm.el. -;; Please note nvm.el is optional. So you need *manually* install it. +;; Please note nvm.el is optional. So you need *manually* install it. ;; To enable nvm support, run `js-do-use-nvm'. ;; The first time you start the JS interpreter with `js-comint-repl', you will ;;be asked to select a version of Node.js @@ -65,8 +78,7 @@ ;; (add-hook 'js2-mode-hook ;; (lambda () ;; (local-set-key (kbd "C-x C-e") 'js-send-last-sexp) -;; (local-set-key (kbd "C-c b") 'js-send-buffer) -;; )) +;; (local-set-key (kbd "C-c b") 'js-send-buffer))) ;;; Code: @@ -83,8 +95,7 @@ :group 'js-comint) (defvar js-comint-module-paths '() - "List of modules paths which could be used by NodeJS -to search modules.") + "List of modules paths which could be used by NodeJS to search modules.") (defvar js-comint-drop-regexp "\\(\x1b\\[[0-9]+[GJK]\\|^[ \t]*undefined[\r\n]+\\)" @@ -160,6 +171,7 @@ Return a string representing the node version." "/node")))) (defun js-comint-guess-load-file-cmd (filename) + "Create Node file loading command for FILENAME." (concat "require(\"" filename "\")\n")) (defun js-comint-quit-or-cancel () @@ -168,9 +180,11 @@ Return a string representing the node version." (process-send-string (js-comint-get-process) "\x03")) (defun js-comint--path-sep () + "Separator of file path." (if (eq system-type 'windows-nt) ";" ":")) (defun js-comint--suggest-module-path () + "Find node_modules." (let* ((dir (locate-dominating-file default-directory "node_modules"))) (if dir (concat (file-name-as-directory dir) @@ -178,6 +192,7 @@ Return a string representing the node version." default-directory))) (defun js-comint-get-process () + "Get repl process." (and js-comint-buffer (get-process js-comint-buffer))) @@ -208,7 +223,7 @@ Return a string representing the node version." ;;;###autoload (defun js-comint-save-setup () - "Save current setup to `.dir-locals.el'" + "Save current setup to \".dir-locals.el\"." (interactive) (let* (sexp (root (read-directory-name "Where to create .dir-locals.el: " @@ -226,6 +241,7 @@ Return a string representing the node version." (message "Nothing to save. `js-comint-module-paths' is empty."))))) (defun js-comint-setup-module-paths () + "Setup node_modules path." (let* ((paths (mapconcat 'identity js-comint-module-paths (js-comint--path-sep))) @@ -242,9 +258,10 @@ Return a string representing the node version." ;;;###autoload (defun js-comint-reset-repl () - "Kill existing REPL process if possible. Create a new -Javascript REPL process. The environment variable `NODE_PATH' -is setup by `js-comint-module-paths' before the process starts." + "Kill existing REPL process if possible. +Create a new Javascript REPL process. +The environment variable `NODE_PATH' is setup by `js-comint-module-paths' +before the process starts." (interactive) (when (js-comint-get-process) (process-send-string (js-comint-get-process) ".exit\n") @@ -252,8 +269,8 @@ is setup by `js-comint-module-paths' before the process starts." (sit-for 1)) (js-comint-start-or-switch-to-repl)) -(defun js-cominit-filter-output (string) - "Filter extra escape sequences from output." +(defun js-comint-filter-output (string) + "Filter extra escape sequences from STRING." (let ((beg (or comint-last-output-start (point-min-marker))) (end (process-mark (get-buffer-process (current-buffer))))) @@ -264,9 +281,11 @@ is setup by `js-comint-module-paths' before the process starts." (replace-match ""))))) (defun js-comint-get-buffer-name () + "Get repl buffer name." (format "*%s*" js-comint-buffer)) (defun js-comint-get-buffer () + "Get rpl buffer." (and js-comint-buffer (get-buffer (js-comint-get-buffer-name)))) @@ -288,7 +307,10 @@ is setup by `js-comint-module-paths' before the process starts." (defalias 'js-clear 'js-comint-clear) +;;;###autoload (defun js-comint-start-or-switch-to-repl () + "Start a new repl or switch to existing repl." + (interactive) (setenv "NODE_NO_READLINE" "1") (js-comint-setup-module-paths) (let* ((repl-mode (or (getenv "NODE_REPL_MODE") "magic")) @@ -301,9 +323,8 @@ is setup by `js-comint-module-paths' before the process starts." ;;;###autoload (defun js-comint-repl (cmd) - "Run an Javascript process. The environment variable `NODE_PATH' -is setup by `js-comint-module-paths' before the process -starts." + "Start a Javascript process by running CMD. +The environment variable \"NODE_PATH\" is setup by `js-comint-module-paths'." (interactive (list ;; You can select node version here @@ -323,8 +344,8 @@ starts." (js-comint-start-or-switch-to-repl)) (defalias 'run-js 'js-comint-repl) -;;;###autoload (defun js-comint-send-string (str) + "Send STR to repl." (comint-send-string (js-comint-get-process) (concat str "\n"))) @@ -344,7 +365,7 @@ If no region selected, you could manually input javascript expression." ;;;###autoload (defun js-comint-send-last-sexp () - "Send the previous sexp to the inferior Javascript process. `evil-mode' friendly." + "Send the previous sexp to the inferior Javascript process." (interactive) (let* ((b (save-excursion (backward-sexp) @@ -373,25 +394,17 @@ If no region selected, you could manually input javascript expression." (defalias 'js-send-buffer 'js-comint-send-buffer) ;;;###autoload -(defun js-comint-load-file (filename) - "Load a file in the javascript interpreter." +(defun js-comint-load-file (file) + "Load FILE into the javascript interpreter." (interactive "f") - (let ((filename (expand-file-name filename))) + (let ((file (expand-file-name file))) (js-comint-repl js-comint-program-command) - (comint-send-string (js-comint-get-process) (js-comint-guess-load-file-cmd filename)))) + (comint-send-string (js-comint-get-process) (js-comint-guess-load-file-cmd file)))) ;;;###autoload (defalias 'js-load-file 'js-comint-load-file) -;;;###autoload -(defun js-comint-switch-to-repl (eob-p) - "Switch to the javascript process buffer. -With argument, position cursor at end of buffer." - (interactive "P") - (if (js-comint-get-buffer) - (pop-to-buffer (js-comint-get-buffer)) - (error "No current process buffer. See variable `js-comint-buffer'"))) -(defalias 'switch-to-js 'js-comint-switch-repl) +(defalias 'switch-to-js 'js-comint-start-or-switch-to-repl) (defvar js-comint-mode-map (let ((map (make-sparse-keymap))) @@ -408,7 +421,7 @@ With argument, position cursor at end of buffer." (setq comint-process-echoes t) ;; Ignore duplicates (setq comint-input-ignoredups t) - (add-hook 'comint-output-filter-functions 'js-cominit-filter-output nil t) + (add-hook 'comint-output-filter-functions 'js-comint-filter-output nil t) (use-local-map js-comint-mode-map) (ansi-color-for-comint-mode-on)) From d775fd2f4f7866c738ca59a033c5e95d3a0d764f Mon Sep 17 00:00:00 2001 From: re-mx Date: Tue, 6 Nov 2018 18:12:56 +0800 Subject: [PATCH 14/52] Update docs --- README.org | 24 +++++++++++------------- js-comint.el | 4 ++-- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/README.org b/README.org index 1f49525..c2d810b 100644 --- a/README.org +++ b/README.org @@ -39,7 +39,7 @@ Add js-comint to your Cask file: ** Windows On window, you may need below setup: #+begin_src elisp -(setq inferior-js-program-command "C:/Program Files/nodejs/node.exe") +(setq js-comint-program-command "C:/Program Files/nodejs/node.exe") #+end_src * Usage After installation, do `M-x run-js` to create a comint buffer with the JavaScript interpreter. @@ -58,13 +58,13 @@ In order to get cleaner output when using NodeJS, I suggest add below setup into (add-hook 'inferior-js-mode-hook 'inferior-js-mode-hook-setup t) #+end_src * Customization -You can set the `inferior-js-program-command' string and the `inferior-js-program-arguments' list to the executable that runs the JS interpreter and the arguments to pass to it respectively. +You can set the `js-comint-program-command' string and the `js-comint-program-arguments' list to the executable that runs the JS interpreter and the arguments to pass to it respectively. E.g., the default is: #+BEGIN_SRC elisp ;; You can also customize `js-comint-drop-regexp' to filter output -(setq inferior-js-program-command "node") -(setq inferior-js-program-arguments '("--interactive")) +(setq js-comint-program-command "node") +(setq js-comint-program-arguments '("--interactive")) #+END_SRC Note that in the example above, the version of node that is picked up will be the first found in `exec-path'. @@ -73,8 +73,8 @@ But you could use Rhino or SpiderMonkey or whatever you want. E.g. to set up the Rhino JAR downloaded from https://github.com/mozilla/rhino, do #+BEGIN_SRC elisp -(setq inferior-js-program-command "java") -(setq inferior-js-program-arguments '("-jar" "/absolute/path/to/rhino/js.jar")) +(setq js-comint-program-command "java") +(setq js-comint-program-arguments '("-jar" "/absolute/path/to/rhino/js.jar")) #+END_SRC If you have nvm, you can select the versions of node.js installed and run them. This is done thanks to =nvm.el=. =nvm.el= is optional. So you need *manually* install it. @@ -89,11 +89,9 @@ The first time you start the JS interpreter with run-js, you will be asked to se You can add the following couple of lines to your .emacs to take advantage of cool key bindings for sending things to the javascript interpreter inside of Steve Yegge's most excellent js2-mode. #+BEGIN_SRC elisp -(add-hook 'js2-mode-hook - (lambda () - (local-set-key (kbd "C-x C-e") 'js-send-last-sexp) - (local-set-key (kbd "C-M-x") 'js-send-last-sexp-and-go) - (local-set-key (kbd "C-c b") 'js-send-buffer) - (local-set-key (kbd "C-c C-b") 'js-send-buffer-and-go) - (local-set-key (kbd "C-c l") 'js-load-file-and-go))) + (add-hook 'js2-mode-hook + (lambda () + (local-set-key (kbd "C-x C-e") 'js-send-last-sexp) + (local-set-key (kbd "C-c b") 'js-send-buffer) + (local-set-key (kbd "C-c C-b") 'js-send-buffer-and-go))) #+END_SRC diff --git a/js-comint.el b/js-comint.el index d1cca3b..cee7ea4 100644 --- a/js-comint.el +++ b/js-comint.el @@ -1,4 +1,4 @@ -;;; js-comint.el --- JavaScript interpreter in window. +nf;;; js-comint.el --- JavaScript interpreter in window. ;;; Copyright (C) 2008 Paul Huff ;;; Copyright (C) 2015 Stefano Mazzucco @@ -50,7 +50,7 @@ ;; and the `js-comint-program-arguments' list to the executable that runs ;; the JS interpreter and the arguments to pass to it respectively. ;; For example, on windows you might need below setup: -;; (setq inferior-js-program-command "C:/Program Files/nodejs/node.exe") +;; (setq js-comint-program-command "C:/Program Files/nodejs/node.exe") ;; ;; After setup, do: `M-x js-comint-repl' ;; Away you go. From 6c33089c850bb8619c9ee4b05adada53b60e8d5b Mon Sep 17 00:00:00 2001 From: MX Reimu Date: Tue, 6 Nov 2018 18:21:00 +0800 Subject: [PATCH 15/52] Fixed typo I am very sorry... I just typed a typo... --- js-comint.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js-comint.el b/js-comint.el index cee7ea4..b22e295 100644 --- a/js-comint.el +++ b/js-comint.el @@ -1,4 +1,4 @@ -nf;;; js-comint.el --- JavaScript interpreter in window. +;;; js-comint.el --- JavaScript interpreter in window. ;;; Copyright (C) 2008 Paul Huff ;;; Copyright (C) 2015 Stefano Mazzucco From e2cf1c735616c862f3532baeeb42ed991b0f674f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jostein=20Kj=C3=B8nigsen?= Date: Fri, 21 Dec 2018 13:20:31 +0100 Subject: [PATCH 16/52] Add GPL3 license This closes https://github.com/redguardtoo/js-comint/issues/24. --- LICENSE | 674 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 674 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f288702 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. From e202057757c19dbbc96bc23bee39ebd0a86a3357 Mon Sep 17 00:00:00 2001 From: ahmed saad Date: Wed, 5 Jun 2019 16:55:52 +0200 Subject: [PATCH 17/52] defining our adjust-window-size-function --- js-comint.el | 2 ++ 1 file changed, 2 insertions(+) diff --git a/js-comint.el b/js-comint.el index b22e295..bee7efe 100644 --- a/js-comint.el +++ b/js-comint.el @@ -422,6 +422,8 @@ If no region selected, you could manually input javascript expression." ;; Ignore duplicates (setq comint-input-ignoredups t) (add-hook 'comint-output-filter-functions 'js-comint-filter-output nil t) + (process-put (js-comint-get-process) + 'adjust-window-size-function (lambda (_process _windows) ())) (use-local-map js-comint-mode-map) (ansi-color-for-comint-mode-on)) From 0dedaf4753fbe8cdbab14aa85f05d7673cbee8b6 Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Fri, 17 Jan 2020 17:15:02 +1100 Subject: [PATCH 18/52] add node_modules into repl automatically --- README.org | 2 +- js-comint.el | 30 +++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/README.org b/README.org index c2d810b..7081b6d 100644 --- a/README.org +++ b/README.org @@ -1,4 +1,4 @@ -* js-comint.el (v1.1.1) +* js-comint.el (v1.2.0) Run a JavaScript interpreter in an inferior process window The first release, [[http://js-comint-el.sourceforge.net/][js-comint 0.0.1, is hosted on sourceforge]] but it has not been updated for five years. diff --git a/js-comint.el b/js-comint.el index bee7efe..9399444 100644 --- a/js-comint.el +++ b/js-comint.el @@ -2,12 +2,12 @@ ;;; Copyright (C) 2008 Paul Huff ;;; Copyright (C) 2015 Stefano Mazzucco -;;; Copyright (C) 2016-2017 Chen Bin +;;; Copyright (C) 2016-2020 Chen Bin ;;; Author: Paul Huff , Stefano Mazzucco ;;; Maintainer: Chen Bin ;;; Created: 15 Feb 2014 -;;; Version: 1.1.1 +;;; Version: 1.2.0 ;;; URL: https://github.com/redguardtoo/js-comint ;;; Package-Requires: ((emacs "24.3")) ;;; Keywords: javascript, node, inferior-mode, convenience @@ -94,6 +94,10 @@ "JavaScript interpreter." :group 'js-comint) +(defcustom js-comint-set-env-when-startup t + "Set environment variable NODE_PATH automatically during startup." + :group 'js-comint) + (defvar js-comint-module-paths '() "List of modules paths which could be used by NodeJS to search modules.") @@ -335,13 +339,22 @@ The environment variable \"NODE_PATH\" is setup by `js-comint-module-paths'." js-comint-program-command js-comint-program-arguments))) (when js-use-nvm - (unless (featurep 'nvm) - (require 'nvm)) - (unless js-nvm-current-version - (js-comint-select-node-version))) - (setq js-comint-program-arguments (split-string cmd)) + (unless (featurep 'nvm) (require 'nvm)) + (unless js-nvm-current-version (js-comint-select-node-version))) + + (setq js-comint-program-arguments (split-string cmd)) (setq js-comint-program-command (pop js-comint-program-arguments))))) - (js-comint-start-or-switch-to-repl)) + + ;; set NOT_PATH automatically + (cond + ((and js-comint-set-env-when-startup + (file-exists-p (js-comint--suggest-module-path))) + (let* ((js-comint-module-paths (nconc (list (file-truename (js-comint--suggest-module-path))) + js-comint-module-paths))) + (js-comint-start-or-switch-to-repl))) + (t + (js-comint-start-or-switch-to-repl)))) + (defalias 'run-js 'js-comint-repl) (defun js-comint-send-string (str) @@ -357,7 +370,6 @@ If no region selected, you could manually input javascript expression." (let* ((str (if (region-active-p) (buffer-substring-no-properties (region-beginning) (region-end)) (read-string "input js expression: ")))) - (message "str=%s" str) (js-comint-send-string str))) ;;;###autoload From 06b88325af16f27f304970495f06c99e06ca12ee Mon Sep 17 00:00:00 2001 From: Christian Tietze Date: Sun, 19 Dec 2021 20:24:45 +0100 Subject: [PATCH 19/52] Update key bindings --- README.org | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/README.org b/README.org index 7081b6d..df56ca6 100644 --- a/README.org +++ b/README.org @@ -84,14 +84,12 @@ To enable nvm support, run (js-do-use-nvm) #+END_SRC -The first time you start the JS interpreter with run-js, you will be asked to select a version of node.js. If you want to change version of node js, run `(js-select-node-version)` +The first time you start the JS interpreter with run-js, you will be asked to select a version of node.js. If you want to change version of node js, run ~(js-select-node-version)~. -You can add the following couple of lines to your .emacs to take advantage of cool key bindings for sending things to the javascript interpreter inside of Steve Yegge's most excellent js2-mode. +You can add the following couple of lines to your .emacs/init file to take advantage of key bindings for sending things to the JavaScript REPL: #+BEGIN_SRC elisp - (add-hook 'js2-mode-hook - (lambda () - (local-set-key (kbd "C-x C-e") 'js-send-last-sexp) - (local-set-key (kbd "C-c b") 'js-send-buffer) - (local-set-key (kbd "C-c C-b") 'js-send-buffer-and-go))) + ; Remap Elisp's eval-last-sexp (C-x C-e) to eval JavaScript + (define-key js-mode-map [remap eval-last-sexp] #'js-comint-send-last-sexp)) + (define-key js-mode-map (kbd "C-c b") 'js-send-buffer)) #+END_SRC From fe09591f4b1a28f90b83f72b35a51480e2914561 Mon Sep 17 00:00:00 2001 From: Quang-Linh LE Date: Thu, 1 Dec 2022 10:22:36 +0100 Subject: [PATCH 20/52] Allow to send multi-line string --- js-comint.el | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/js-comint.el b/js-comint.el index 9399444..949e5cc 100644 --- a/js-comint.el +++ b/js-comint.el @@ -359,8 +359,13 @@ The environment variable \"NODE_PATH\" is setup by `js-comint-module-paths'." (defun js-comint-send-string (str) "Send STR to repl." - (comint-send-string (js-comint-get-process) - (concat str "\n"))) + (let ((lines (string-lines str))) + (if (length= lines 1) + (comint-send-string (js-comint-get-process) + (concat str "\n")) + (comint-send-string (js-comint-get-process) + (concat ".editor" "\n" str "\n")) + (process-send-eof (js-comint-get-process))))) ;;;###autoload (defun js-comint-send-region () From 4ee2619249117fdf8021277e857d6d74f1b82b28 Mon Sep 17 00:00:00 2001 From: Jean-Marc Saffroy Date: Thu, 9 Mar 2023 21:03:25 +0100 Subject: [PATCH 21/52] Fixes to run under Emacs 27.1 --- js-comint.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js-comint.el b/js-comint.el index 949e5cc..6551b84 100644 --- a/js-comint.el +++ b/js-comint.el @@ -359,8 +359,8 @@ The environment variable \"NODE_PATH\" is setup by `js-comint-module-paths'." (defun js-comint-send-string (str) "Send STR to repl." - (let ((lines (string-lines str))) - (if (length= lines 1) + (let ((lines (split-string str "\r\n"))) + (if (= (length lines) 1) (comint-send-string (js-comint-get-process) (concat str "\n")) (comint-send-string (js-comint-get-process) From 057a8ba153832ce2ec781790f61ac7743532f07b Mon Sep 17 00:00:00 2001 From: Jean-Marc Saffroy Date: Thu, 9 Nov 2023 20:40:53 +0100 Subject: [PATCH 22/52] Fix regression when sending multi-line string --- js-comint.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js-comint.el b/js-comint.el index 6551b84..6b3d845 100644 --- a/js-comint.el +++ b/js-comint.el @@ -359,7 +359,7 @@ The environment variable \"NODE_PATH\" is setup by `js-comint-module-paths'." (defun js-comint-send-string (str) "Send STR to repl." - (let ((lines (split-string str "\r\n"))) + (let ((lines (split-string str "\n"))) (if (= (length lines) 1) (comint-send-string (js-comint-get-process) (concat str "\n")) From 24da61e82df7108b8bec67fa39ef24184167ccee Mon Sep 17 00:00:00 2001 From: Jean-Marc Saffroy Date: Sat, 25 Nov 2023 18:32:09 +0100 Subject: [PATCH 23/52] Add tests using Emacs ERT for multiline JS statements --- test-js-comint.el | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 test-js-comint.el diff --git a/test-js-comint.el b/test-js-comint.el new file mode 100644 index 0000000..e89adf9 --- /dev/null +++ b/test-js-comint.el @@ -0,0 +1,44 @@ +(defun js-comint-test-buffer-matches (regex) + "Search the js-comint buffer for the given regular expression. +Return 't if a match is found, nil otherwise." + (with-current-buffer (js-comint-get-buffer) + (save-excursion + (goto-char (point-min)) + (if (re-search-forward regex nil t) t nil)))) + +(defun js-comint-test-output-matches (input regex) + "Verify that sending INPUT yields output that matches REGEX." + + ;; Start an instance to run tests on. + (js-comint-reset-repl) + + (sit-for 1) + + (js-comint-send-string input) + + (sit-for 1) + + (js-comint-test-buffer-matches regex)) + +(ert-deftest js-comint-test-multiline-dotchain-line-start () + "Test multiline statement with dots at beginning of lines." + (should (js-comint-test-output-matches "[1, 2, 3] + .map((it) => it + 1) + .filter((it) => it > 0) + .reduce((prev, curr) => prev + curr, 0);" "^9$"))) + +(ert-deftest js-comint-test-multiline-dotchain-line-start-dos () + "Test multiline statement with dots at beginning of lines, with +DOS line separators." + (should (js-comint-test-output-matches "[1, 2, 3]\r + .map((it) => it + 1)\r + .filter((it) => it > 0)\r + .reduce((prev, curr) => prev + curr, 0);\r +" "^9$"))) + +(ert-deftest js-comint-test-multiline-dotchain-line-end () + "Test multiline statement with dots at end of lines." + (should (js-comint-test-output-matches "[1, 2, 3]. +map((it) => it + 1). +filter((it) => it > 0). +reduce((prev, curr) => prev + curr, 0);" "^9$"))) From fd08ea4db60fe1e9a75fdeb67c18b37d6d917c92 Mon Sep 17 00:00:00 2001 From: Jean-Marc Saffroy Date: Sat, 25 Nov 2023 18:34:20 +0100 Subject: [PATCH 24/52] Add github workflow that runs tests across multiple Emacs versions --- .github/workflows/actions.yml | 16 ++++++++++++++++ install-deps.sh | 6 ++++++ runtests.sh | 9 +++++++++ 3 files changed, 31 insertions(+) create mode 100644 .github/workflows/actions.yml create mode 100755 install-deps.sh create mode 100755 runtests.sh diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml new file mode 100644 index 0000000..28a94b3 --- /dev/null +++ b/.github/workflows/actions.yml @@ -0,0 +1,16 @@ +name: Run tests across emacs versions +on: [push, pull_request] +jobs: + test-emacs-versions: + strategy: + matrix: + emacs-version: [26, 27, 28, 29] + runs-on: ubuntu-latest + container: silex/emacs:${{matrix.emacs-version}} + steps: + - name: Check out repository code + uses: actions/checkout@v4 + - name: Install test dependencies + run: ./install-deps.sh + - name: Run tests + run: ./runtests.sh diff --git a/install-deps.sh b/install-deps.sh new file mode 100755 index 0000000..7d8faf7 --- /dev/null +++ b/install-deps.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -xeu + +apt-get update +apt-get install -y nodejs +apt-get clean diff --git a/runtests.sh b/runtests.sh new file mode 100755 index 0000000..58e9b85 --- /dev/null +++ b/runtests.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -xeu + +cd "$(dirname $0)" +emacs -Q --batch \ + -l ert \ + -l js-comint.el \ + -l test-js-comint.el \ + -f ert-run-tests-batch-and-exit From e01b75330c562d945a765528a847584d96439431 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Tue, 15 Oct 2024 13:51:02 -0700 Subject: [PATCH 25/52] Enable lexical binding --- js-comint.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js-comint.el b/js-comint.el index 6b3d845..1a56bed 100644 --- a/js-comint.el +++ b/js-comint.el @@ -1,4 +1,4 @@ -;;; js-comint.el --- JavaScript interpreter in window. +;;; js-comint.el --- JavaScript interpreter in window. -*- lexical-binding: t -*- ;;; Copyright (C) 2008 Paul Huff ;;; Copyright (C) 2015 Stefano Mazzucco From cc6a360415c85df02a49a1ef9058ba28e018ad6d Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Wed, 16 Oct 2024 20:23:10 -0700 Subject: [PATCH 26/52] Ignore string arg to js-comint-filter-output It just changes the output in the comint buffer. --- js-comint.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js-comint.el b/js-comint.el index 1a56bed..563d74b 100644 --- a/js-comint.el +++ b/js-comint.el @@ -273,8 +273,8 @@ before the process starts." (sit-for 1)) (js-comint-start-or-switch-to-repl)) -(defun js-comint-filter-output (string) - "Filter extra escape sequences from STRING." +(defun js-comint-filter-output (_string) + "Filter extra escape sequences from last output." (let ((beg (or comint-last-output-start (point-min-marker))) (end (process-mark (get-buffer-process (current-buffer))))) From ed7e77a3795fd1c5b5d45cbaef2a4a80a1570c34 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Wed, 16 Oct 2024 20:54:12 -0700 Subject: [PATCH 27/52] Lint and simplify js-comint-repl require does not load if already present. Convert cond to equivalent if --- js-comint.el | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/js-comint.el b/js-comint.el index 563d74b..cec442d 100644 --- a/js-comint.el +++ b/js-comint.el @@ -319,7 +319,7 @@ before the process starts." (js-comint-setup-module-paths) (let* ((repl-mode (or (getenv "NODE_REPL_MODE") "magic")) (js-comint-code (format js-comint-code-format - (window-width) js-comint-prompt repl-mode))) + (window-width) js-comint-prompt repl-mode))) (pop-to-buffer (apply 'make-comint js-comint-buffer js-comint-program-command nil `(,@js-comint-program-arguments "-e" ,js-comint-code))) @@ -339,21 +339,20 @@ The environment variable \"NODE_PATH\" is setup by `js-comint-module-paths'." js-comint-program-command js-comint-program-arguments))) (when js-use-nvm - (unless (featurep 'nvm) (require 'nvm)) + (require 'nvm) (unless js-nvm-current-version (js-comint-select-node-version))) - (setq js-comint-program-arguments (split-string cmd)) + (setq js-comint-program-arguments (split-string cmd)) (setq js-comint-program-command (pop js-comint-program-arguments))))) - ;; set NOT_PATH automatically - (cond - ((and js-comint-set-env-when-startup - (file-exists-p (js-comint--suggest-module-path))) - (let* ((js-comint-module-paths (nconc (list (file-truename (js-comint--suggest-module-path))) - js-comint-module-paths))) - (js-comint-start-or-switch-to-repl))) - (t - (js-comint-start-or-switch-to-repl)))) + ;; set NODE_PATH automatically + (if (and js-comint-set-env-when-startup + (file-exists-p (js-comint--suggest-module-path))) + (let ((js-comint-module-paths (cons (file-truename (js-comint--suggest-module-path)) + js-comint-module-paths))) + (js-comint-start-or-switch-to-repl)) + ;; else + (js-comint-start-or-switch-to-repl))) (defalias 'run-js 'js-comint-repl) From d9088c32a4a06fbd8428d64b331488a01cddd230 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Wed, 16 Oct 2024 14:09:48 -0700 Subject: [PATCH 28/52] Add lexical-binding and requires to test --- test-js-comint.el | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test-js-comint.el b/test-js-comint.el index e89adf9..b3e0236 100644 --- a/test-js-comint.el +++ b/test-js-comint.el @@ -1,3 +1,8 @@ +;; -*- lexical-binding: t -*- + +(require 'js-comint) +(require 'ert) + (defun js-comint-test-buffer-matches (regex) "Search the js-comint buffer for the given regular expression. Return 't if a match is found, nil otherwise." From ceeb9ebafb1dbe8eeb50fda92e2d0cf1a8a9d083 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Thu, 17 Oct 2024 12:09:41 -0700 Subject: [PATCH 29/52] Bug: js-comint--suggest-module-path should be nil if no node_modules This means that default-directory is always added to NODE_PATH when calling js-comint-repl --- js-comint.el | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/js-comint.el b/js-comint.el index cec442d..7d4734f 100644 --- a/js-comint.el +++ b/js-comint.el @@ -188,12 +188,9 @@ Return a string representing the node version." (if (eq system-type 'windows-nt) ";" ":")) (defun js-comint--suggest-module-path () - "Find node_modules." - (let* ((dir (locate-dominating-file default-directory - "node_modules"))) - (if dir (concat (file-name-as-directory dir) - "node_modules") - default-directory))) + "Path to node_modules in parent dirs, or nil if none exists." + (when-let ((dir (locate-dominating-file default-directory "node_modules"))) + (concat (file-name-as-directory dir) "node_modules"))) (defun js-comint-get-process () "Get repl process." @@ -204,8 +201,9 @@ Return a string representing the node version." (defun js-comint-add-module-path () "Add a directory to `js-comint-module-paths'." (interactive) - (let* ((dir (read-directory-name "Module path:" - (js-comint--suggest-module-path)))) + (let ((dir (read-directory-name "Module path:" + (or (js-comint--suggest-module-path) + default-directory)))) (when dir (add-to-list 'js-comint-module-paths (file-truename dir)) (message "\"%s\" added to `js-comint-module-paths'" dir)))) @@ -347,7 +345,7 @@ The environment variable \"NODE_PATH\" is setup by `js-comint-module-paths'." ;; set NODE_PATH automatically (if (and js-comint-set-env-when-startup - (file-exists-p (js-comint--suggest-module-path))) + (js-comint--suggest-module-path)) (let ((js-comint-module-paths (cons (file-truename (js-comint--suggest-module-path)) js-comint-module-paths))) (js-comint-start-or-switch-to-repl)) From addff71a7ac958ea535a19f12dacb1442a15ba82 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Thu, 17 Oct 2024 14:15:35 -0700 Subject: [PATCH 30/52] Use temporary local env vars when calling comint This avoids modifying NODE_PATH for later calls --- js-comint.el | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/js-comint.el b/js-comint.el index 7d4734f..d0ec399 100644 --- a/js-comint.el +++ b/js-comint.el @@ -310,17 +310,24 @@ before the process starts." ;;;###autoload -(defun js-comint-start-or-switch-to-repl () - "Start a new repl or switch to existing repl." +(defun js-comint-start-or-switch-to-repl (&optional node-modules-path) + "Start a new repl or switch to existing repl. +Optional NODE-MODULES-PATH is the path to a local node_modules to use." (interactive) - (setenv "NODE_NO_READLINE" "1") - (js-comint-setup-module-paths) - (let* ((repl-mode (or (getenv "NODE_REPL_MODE") "magic")) + (let* ((node-path (getenv "NODE_PATH")) + (all-paths-list (flatten-list (list node-path + node-modules-path + js-comint-module-paths))) + (local-node-path (string-join all-paths-list (js-comint--path-sep))) + (repl-mode (or (getenv "NODE_REPL_MODE") "magic")) (js-comint-code (format js-comint-code-format (window-width) js-comint-prompt repl-mode))) - (pop-to-buffer - (apply 'make-comint js-comint-buffer js-comint-program-command nil - `(,@js-comint-program-arguments "-e" ,js-comint-code))) + ;; TODO what happens if you switch into this from a different project? + (with-environment-variables (("NODE_NO_READLINE" "1") + ("NODE_PATH" local-node-path)) + (pop-to-buffer + (apply 'make-comint js-comint-buffer js-comint-program-command nil + `(,@js-comint-program-arguments "-e" ,js-comint-code)))) (js-comint-mode))) ;;;###autoload @@ -344,11 +351,9 @@ The environment variable \"NODE_PATH\" is setup by `js-comint-module-paths'." (setq js-comint-program-command (pop js-comint-program-arguments))))) ;; set NODE_PATH automatically - (if (and js-comint-set-env-when-startup - (js-comint--suggest-module-path)) - (let ((js-comint-module-paths (cons (file-truename (js-comint--suggest-module-path)) - js-comint-module-paths))) - (js-comint-start-or-switch-to-repl)) + (if-let ((module-path (and js-comint-set-env-when-startup + (js-comint--suggest-module-path)))) + (js-comint-start-or-switch-to-repl (file-truename module-path)) ;; else (js-comint-start-or-switch-to-repl))) From 3d879f595714b31fa47c5861a299a81a9d6ee51e Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Thu, 17 Oct 2024 14:18:11 -0700 Subject: [PATCH 31/52] Remove unused js-comint-setup-module-paths --- js-comint.el | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/js-comint.el b/js-comint.el index d0ec399..b93f20f 100644 --- a/js-comint.el +++ b/js-comint.el @@ -242,28 +242,10 @@ Return a string representing the node version." (t (message "Nothing to save. `js-comint-module-paths' is empty."))))) -(defun js-comint-setup-module-paths () - "Setup node_modules path." - (let* ((paths (mapconcat 'identity - js-comint-module-paths - (js-comint--path-sep))) - (node-path (getenv "NODE_PATH"))) - (cond - ((or (not node-path) - (string= "" node-path)) - ;; set - (setenv "NODE_PATH" paths)) - ((not (string= "" paths)) - ;; append - (setenv "NODE_PATH" (concat node-path (js-comint--path-sep) paths)) - (message "%s added into \$NODE_PATH" paths))))) - ;;;###autoload (defun js-comint-reset-repl () "Kill existing REPL process if possible. -Create a new Javascript REPL process. -The environment variable `NODE_PATH' is setup by `js-comint-module-paths' -before the process starts." +Create a new Javascript REPL process." (interactive) (when (js-comint-get-process) (process-send-string (js-comint-get-process) ".exit\n") @@ -332,8 +314,7 @@ Optional NODE-MODULES-PATH is the path to a local node_modules to use." ;;;###autoload (defun js-comint-repl (cmd) - "Start a Javascript process by running CMD. -The environment variable \"NODE_PATH\" is setup by `js-comint-module-paths'." + "Start a Javascript process by running CMD." (interactive (list ;; You can select node version here From 2eceaf37926919497941eb7a710894e272c44ecd Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Thu, 17 Oct 2024 14:21:26 -0700 Subject: [PATCH 32/52] Depend on emacs 28.1 to use with-environment-variables --- .github/workflows/actions.yml | 2 +- js-comint.el | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index 28a94b3..306c453 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -4,7 +4,7 @@ jobs: test-emacs-versions: strategy: matrix: - emacs-version: [26, 27, 28, 29] + emacs-version: [28, 29] runs-on: ubuntu-latest container: silex/emacs:${{matrix.emacs-version}} steps: diff --git a/js-comint.el b/js-comint.el index b93f20f..610f980 100644 --- a/js-comint.el +++ b/js-comint.el @@ -9,7 +9,7 @@ ;;; Created: 15 Feb 2014 ;;; Version: 1.2.0 ;;; URL: https://github.com/redguardtoo/js-comint -;;; Package-Requires: ((emacs "24.3")) +;;; Package-Requires: ((emacs "28.1")) ;;; Keywords: javascript, node, inferior-mode, convenience ;; This file is NOT part of GNU Emacs. From 18ab6737f706db09199dd9d987d1a8e2509b28e1 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Thu, 17 Oct 2024 14:57:50 -0700 Subject: [PATCH 33/52] Tidy argument handling for js-comint-repl Made cmd optional and documented behaviour. --- js-comint.el | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/js-comint.el b/js-comint.el index 610f980..57ef4d6 100644 --- a/js-comint.el +++ b/js-comint.el @@ -313,23 +313,30 @@ Optional NODE-MODULES-PATH is the path to a local node_modules to use." (js-comint-mode))) ;;;###autoload -(defun js-comint-repl (cmd) - "Start a Javascript process by running CMD." +(defun js-comint-repl (&optional cmd) + "Start a NodeJS REPL process. +Optional CMD will override `js-comint-program-command' and +`js-comint-program-arguments', as well as any nvm setting. + +When called interactively use a universal prefix to +set CMD." (interactive - (list - ;; You can select node version here - (when current-prefix-arg - (setq cmd - (read-string "Run js: " - (format "%s %s" - js-comint-program-command - js-comint-program-arguments))) - (when js-use-nvm - (require 'nvm) - (unless js-nvm-current-version (js-comint-select-node-version))) - - (setq js-comint-program-arguments (split-string cmd)) - (setq js-comint-program-command (pop js-comint-program-arguments))))) + (when current-prefix-arg + (list + (read-string "Run js: " + (string-join + (cons js-comint-program-command + js-comint-program-arguments) + " "))))) + + (when (and (not cmd) + js-use-nvm) + (require 'nvm) + (unless js-nvm-current-version (js-comint-select-node-version))) + + (when cmd + (setq js-comint-program-arguments (split-string cmd)) + (setq js-comint-program-command (pop js-comint-program-arguments))) ;; set NODE_PATH automatically (if-let ((module-path (and js-comint-set-env-when-startup @@ -398,7 +405,7 @@ If no region selected, you could manually input javascript expression." "Load FILE into the javascript interpreter." (interactive "f") (let ((file (expand-file-name file))) - (js-comint-repl js-comint-program-command) + (js-comint-repl) (comint-send-string (js-comint-get-process) (js-comint-guess-load-file-cmd file)))) ;;;###autoload From e83eb2703c9bbbfc32092b155151c71ebdb8ffda Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Thu, 17 Oct 2024 15:07:14 -0700 Subject: [PATCH 34/52] Add missing customization types to fix compiler warnings --- js-comint.el | 3 +++ 1 file changed, 3 insertions(+) diff --git a/js-comint.el b/js-comint.el index 57ef4d6..6384c63 100644 --- a/js-comint.el +++ b/js-comint.el @@ -92,10 +92,12 @@ (defcustom js-comint-program-command "node" "JavaScript interpreter." + :type 'string :group 'js-comint) (defcustom js-comint-set-env-when-startup t "Set environment variable NODE_PATH automatically during startup." + :type 'boolean :group 'js-comint) (defvar js-comint-module-paths '() @@ -107,6 +109,7 @@ (defcustom js-comint-program-arguments '() "List of command line arguments passed to the JavaScript interpreter." + :type '(list string) :group 'js-comint) (defcustom js-comint-prompt "> " From 2058be8736163c14abbc0edb23c9a341127a86eb Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Thu, 17 Oct 2024 15:23:02 -0700 Subject: [PATCH 35/52] Add unit tests for js-comint-start-or-switch-to-repl --- test-js-comint.el | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test-js-comint.el b/test-js-comint.el index b3e0236..86a740e 100644 --- a/test-js-comint.el +++ b/test-js-comint.el @@ -47,3 +47,48 @@ DOS line separators." map((it) => it + 1). filter((it) => it > 0). reduce((prev, curr) => prev + curr, 0);" "^9$"))) + +(ert-deftest js-comint-start-or-switch-to-repl/test-no-modules () + "Should preserve node_path when nothing is set." + (let ((original js-comint-module-paths) + (original-env (getenv "NODE_PATH"))) + (unwind-protect + (progn + (setq js-comint-module-paths '()) + (setenv "NODE_PATH" "/foo/bar") + (js-comint-start-or-switch-to-repl) + (sit-for 1) + (js-comint-send-string "process.env['NODE_PATH'];") + (js-comint-test-buffer-matches "/foo/bar")) + (setq js-comint-module-paths original) + (setenv "NODE_PATH" original-env)))) + +(ert-deftest js-comint-start-or-switch-to-repl/test-global-set () + "Should include the value of `js-comint-node-modules' if set." + (let ((original js-comint-module-paths) + (original-env (getenv "NODE_PATH"))) + (unwind-protect + (progn + (setq js-comint-module-paths '("/baz/xyz")) + (setenv "NODE_PATH" "/foo/bar") + (js-comint-start-or-switch-to-repl) + (sit-for 1) + (js-comint-send-string "process.env['NODE_PATH'];") + (js-comint-test-buffer-matches (concat "/foo/bar" (js-comint--path-sep) "/baz/xyz"))) + (setq js-comint-module-paths original) + (setenv "NODE_PATH" original-env)))) + +(ert-deftest js-comint-start-or-switch-to-repl/test-local () + "Should include the optional node-modules-path." + (let ((original js-comint-module-paths) + (original-env (getenv "NODE_PATH"))) + (unwind-protect + (progn + (setq js-comint-module-paths '()) + (setenv "NODE_PATH" "/foo/bar") + (js-comint-start-or-switch-to-repl "/baz/xyz") + (sit-for 1) + (js-comint-send-string "process.env['NODE_PATH'];") + (js-comint-test-buffer-matches (concat "/foo/bar" (js-comint--path-sep) "/baz/xyz"))) + (setq js-comint-module-paths original) + (setenv "NODE_PATH" original-env)))) From dd6c11718310a84ec965c79b73f3f7e90013133d Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Thu, 17 Oct 2024 15:39:34 -0700 Subject: [PATCH 36/52] Simplify js-comint--suggest-module-path read-directory-name defaults to default-directory if nil and expand-file-name produces output equivalent to file-truename. --- js-comint.el | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/js-comint.el b/js-comint.el index 6384c63..d5a6708 100644 --- a/js-comint.el +++ b/js-comint.el @@ -193,7 +193,7 @@ Return a string representing the node version." (defun js-comint--suggest-module-path () "Path to node_modules in parent dirs, or nil if none exists." (when-let ((dir (locate-dominating-file default-directory "node_modules"))) - (concat (file-name-as-directory dir) "node_modules"))) + (expand-file-name "node_modules" dir))) (defun js-comint-get-process () "Get repl process." @@ -205,8 +205,7 @@ Return a string representing the node version." "Add a directory to `js-comint-module-paths'." (interactive) (let ((dir (read-directory-name "Module path:" - (or (js-comint--suggest-module-path) - default-directory)))) + (js-comint--suggest-module-path)))) (when dir (add-to-list 'js-comint-module-paths (file-truename dir)) (message "\"%s\" added to `js-comint-module-paths'" dir)))) @@ -341,12 +340,9 @@ set CMD." (setq js-comint-program-arguments (split-string cmd)) (setq js-comint-program-command (pop js-comint-program-arguments))) - ;; set NODE_PATH automatically - (if-let ((module-path (and js-comint-set-env-when-startup - (js-comint--suggest-module-path)))) - (js-comint-start-or-switch-to-repl (file-truename module-path)) - ;; else - (js-comint-start-or-switch-to-repl))) + ;; set NODE_PATH if js-comint-set-env-when-startup is true + (js-comint-start-or-switch-to-repl (and js-comint-set-env-when-startup + (js-comint--suggest-module-path)))) (defalias 'run-js 'js-comint-repl) From 951131ceb542f983f533b7c6086bb7e08f66ed0b Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Thu, 17 Oct 2024 16:01:07 -0700 Subject: [PATCH 37/52] Have js-comint-start-or-switch-to-repl check local node_modules This way js-rest-repl does not "forget" the local node_modules. --- js-comint.el | 12 ++++++------ test-js-comint.el | 46 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/js-comint.el b/js-comint.el index d5a6708..8d6128c 100644 --- a/js-comint.el +++ b/js-comint.el @@ -294,11 +294,13 @@ Create a new Javascript REPL process." ;;;###autoload -(defun js-comint-start-or-switch-to-repl (&optional node-modules-path) - "Start a new repl or switch to existing repl. -Optional NODE-MODULES-PATH is the path to a local node_modules to use." +(defun js-comint-start-or-switch-to-repl () + "Start a new repl or switch to existing repl." (interactive) (let* ((node-path (getenv "NODE_PATH")) + ;; The path to a local node_modules + (node-modules-path (and js-comint-set-env-when-startup + (js-comint--suggest-module-path))) (all-paths-list (flatten-list (list node-path node-modules-path js-comint-module-paths))) @@ -340,9 +342,7 @@ set CMD." (setq js-comint-program-arguments (split-string cmd)) (setq js-comint-program-command (pop js-comint-program-arguments))) - ;; set NODE_PATH if js-comint-set-env-when-startup is true - (js-comint-start-or-switch-to-repl (and js-comint-set-env-when-startup - (js-comint--suggest-module-path)))) + (js-comint-start-or-switch-to-repl)) (defalias 'run-js 'js-comint-repl) diff --git a/test-js-comint.el b/test-js-comint.el index 86a740e..d92739d 100644 --- a/test-js-comint.el +++ b/test-js-comint.el @@ -51,44 +51,68 @@ reduce((prev, curr) => prev + curr, 0);" "^9$"))) (ert-deftest js-comint-start-or-switch-to-repl/test-no-modules () "Should preserve node_path when nothing is set." (let ((original js-comint-module-paths) + (original-set-env js-comint-set-env-when-startup) (original-env (getenv "NODE_PATH"))) (unwind-protect (progn - (setq js-comint-module-paths '()) + (setq js-comint-module-paths nil + js-comint-set-env-when-startup nil) (setenv "NODE_PATH" "/foo/bar") + (when (js-comint-get-process) + (process-send-string (js-comint-get-process) ".exit\n") + (sit-for 1)) (js-comint-start-or-switch-to-repl) (sit-for 1) (js-comint-send-string "process.env['NODE_PATH'];") (js-comint-test-buffer-matches "/foo/bar")) - (setq js-comint-module-paths original) - (setenv "NODE_PATH" original-env)))) + (setq js-comint-module-paths original + js-comint-set-env-when-startup original-set-env) + (setenv "NODE_PATH" original-env) + (process-send-string (js-comint-get-process) ".exit\n")))) (ert-deftest js-comint-start-or-switch-to-repl/test-global-set () "Should include the value of `js-comint-node-modules' if set." (let ((original js-comint-module-paths) + (original-set-env js-comint-set-env-when-startup) (original-env (getenv "NODE_PATH"))) (unwind-protect (progn - (setq js-comint-module-paths '("/baz/xyz")) + (setq js-comint-module-paths '("/baz/xyz") + js-comint-set-env-when-startup nil) (setenv "NODE_PATH" "/foo/bar") + (when (js-comint-get-process) + (process-send-string (js-comint-get-process) ".exit\n") + (sit-for 1)) (js-comint-start-or-switch-to-repl) (sit-for 1) (js-comint-send-string "process.env['NODE_PATH'];") (js-comint-test-buffer-matches (concat "/foo/bar" (js-comint--path-sep) "/baz/xyz"))) - (setq js-comint-module-paths original) - (setenv "NODE_PATH" original-env)))) + (setq js-comint-module-paths original + js-comint-set-env-when-startup original-set-env) + (setenv "NODE_PATH" original-env) + (process-send-string (js-comint-get-process) ".exit\n")))) (ert-deftest js-comint-start-or-switch-to-repl/test-local () "Should include the optional node-modules-path." (let ((original js-comint-module-paths) - (original-env (getenv "NODE_PATH"))) + (original-set-env js-comint-set-env-when-startup) + (original-env (getenv "NODE_PATH")) + (original-suggest js-comint--suggest-module-path)) (unwind-protect (progn - (setq js-comint-module-paths '()) + (fset 'js-comint--suggest-module-path (lambda () "/baz/xyz")) + (setq js-comint-module-paths '() + js-comint-set-env-when-startup 't) (setenv "NODE_PATH" "/foo/bar") - (js-comint-start-or-switch-to-repl "/baz/xyz") + (when (js-comint-get-process) + (process-send-string (js-comint-get-process) ".exit\n") + (sit-for 1)) + (js-comint-start-or-switch-to-repl) (sit-for 1) (js-comint-send-string "process.env['NODE_PATH'];") (js-comint-test-buffer-matches (concat "/foo/bar" (js-comint--path-sep) "/baz/xyz"))) - (setq js-comint-module-paths original) - (setenv "NODE_PATH" original-env)))) + (setq js-comint-module-paths original + js-comint-set-env-when-startup original-set-env) + (setenv "NODE_PATH" original-env) + (fset 'js-comint--suggest-module-path original-suggest) + (process-send-string (js-comint-get-process) ".exit\n")))) From be24e8e381cdae9c9bb08a7440878f9ea02f0a4a Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Fri, 18 Oct 2024 08:53:22 -0700 Subject: [PATCH 38/52] Fix failing test --- test-js-comint.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-js-comint.el b/test-js-comint.el index d92739d..7ca7a87 100644 --- a/test-js-comint.el +++ b/test-js-comint.el @@ -97,7 +97,7 @@ reduce((prev, curr) => prev + curr, 0);" "^9$"))) (let ((original js-comint-module-paths) (original-set-env js-comint-set-env-when-startup) (original-env (getenv "NODE_PATH")) - (original-suggest js-comint--suggest-module-path)) + (original-suggest (symbol-function 'js-comint--suggest-module-path))) (unwind-protect (progn (fset 'js-comint--suggest-module-path (lambda () "/baz/xyz")) From 7ceec3d0c7b1f1e9a150bd40e7a828c231267060 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Fri, 18 Oct 2024 08:56:30 -0700 Subject: [PATCH 39/52] Factor out process exit logic in tests --- test-js-comint.el | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test-js-comint.el b/test-js-comint.el index 7ca7a87..fff330c 100644 --- a/test-js-comint.el +++ b/test-js-comint.el @@ -25,6 +25,12 @@ Return 't if a match is found, nil otherwise." (js-comint-test-buffer-matches regex)) +(defun js-comint-test-exit-comint () + "Finish process." + (when (js-comint-get-process) + (process-send-string (js-comint-get-process) ".exit\n") + (sit-for 1))) + (ert-deftest js-comint-test-multiline-dotchain-line-start () "Test multiline statement with dots at beginning of lines." (should (js-comint-test-output-matches "[1, 2, 3] @@ -58,9 +64,7 @@ reduce((prev, curr) => prev + curr, 0);" "^9$"))) (setq js-comint-module-paths nil js-comint-set-env-when-startup nil) (setenv "NODE_PATH" "/foo/bar") - (when (js-comint-get-process) - (process-send-string (js-comint-get-process) ".exit\n") - (sit-for 1)) + (js-comint-test-exit-comint) (js-comint-start-or-switch-to-repl) (sit-for 1) (js-comint-send-string "process.env['NODE_PATH'];") @@ -68,7 +72,7 @@ reduce((prev, curr) => prev + curr, 0);" "^9$"))) (setq js-comint-module-paths original js-comint-set-env-when-startup original-set-env) (setenv "NODE_PATH" original-env) - (process-send-string (js-comint-get-process) ".exit\n")))) + (js-comint-test-exit-comint)))) (ert-deftest js-comint-start-or-switch-to-repl/test-global-set () "Should include the value of `js-comint-node-modules' if set." @@ -80,9 +84,7 @@ reduce((prev, curr) => prev + curr, 0);" "^9$"))) (setq js-comint-module-paths '("/baz/xyz") js-comint-set-env-when-startup nil) (setenv "NODE_PATH" "/foo/bar") - (when (js-comint-get-process) - (process-send-string (js-comint-get-process) ".exit\n") - (sit-for 1)) + (js-comint-test-exit-comint) (js-comint-start-or-switch-to-repl) (sit-for 1) (js-comint-send-string "process.env['NODE_PATH'];") @@ -90,7 +92,7 @@ reduce((prev, curr) => prev + curr, 0);" "^9$"))) (setq js-comint-module-paths original js-comint-set-env-when-startup original-set-env) (setenv "NODE_PATH" original-env) - (process-send-string (js-comint-get-process) ".exit\n")))) + (js-comint-test-exit-comint)))) (ert-deftest js-comint-start-or-switch-to-repl/test-local () "Should include the optional node-modules-path." @@ -104,9 +106,7 @@ reduce((prev, curr) => prev + curr, 0);" "^9$"))) (setq js-comint-module-paths '() js-comint-set-env-when-startup 't) (setenv "NODE_PATH" "/foo/bar") - (when (js-comint-get-process) - (process-send-string (js-comint-get-process) ".exit\n") - (sit-for 1)) + (js-comint-test-exit-comint) (js-comint-start-or-switch-to-repl) (sit-for 1) (js-comint-send-string "process.env['NODE_PATH'];") @@ -115,4 +115,4 @@ reduce((prev, curr) => prev + curr, 0);" "^9$"))) js-comint-set-env-when-startup original-set-env) (setenv "NODE_PATH" original-env) (fset 'js-comint--suggest-module-path original-suggest) - (process-send-string (js-comint-get-process) ".exit\n")))) + (js-comint-test-exit-comint)))) From 8f05c1b402efdca7b082cc20a515781ba42af7c2 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Fri, 18 Oct 2024 11:23:26 -0700 Subject: [PATCH 40/52] Add declares for nvm functions. This fixes byte-compiler warnings, but keeps nvm optional --- js-comint.el | 3 +++ 1 file changed, 3 insertions(+) diff --git a/js-comint.el b/js-comint.el index 8d6128c..f872b5c 100644 --- a/js-comint.el +++ b/js-comint.el @@ -143,6 +143,9 @@ (defvar js-nvm-current-version nil "Current version of node.") +(declare-function nvm--installed-versions "nvm.el" ()) +(declare-function nvm--find-exact-version-for "nvm.el" (short)) + (defun js-comint-list-nvm-versions (prompt) "List all available node versions from nvm prompting the user with PROMPT. Return a string representing the node version." From 7fbcb4b18a95ed9becb9856117f00a1473e4aa48 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Fri, 18 Oct 2024 11:32:32 -0700 Subject: [PATCH 41/52] Move (require 'nvm) into select-node-version, tidy logic js-comint-select-node-version is the common entry point when using nvm functions. --- js-comint.el | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/js-comint.el b/js-comint.el index f872b5c..40db58f 100644 --- a/js-comint.el +++ b/js-comint.el @@ -141,7 +141,8 @@ "require('repl')['REPL_MODE_' + '%s'.toUpperCase()])")) (defvar js-nvm-current-version nil - "Current version of node.") + "Current version of node for js-comint. +Either nil or a list (VERSION-STRING PATH).") (declare-function nvm--installed-versions "nvm.el" ()) (declare-function nvm--find-exact-version-for "nvm.el" (short)) @@ -164,21 +165,17 @@ Return a string representing the node version." (defun js-comint-select-node-version (&optional version) "Use a given VERSION of node from nvm." (interactive) - (if version - (setq js-nvm-current-version (nvm--find-exact-version-for version)) - (let ((old-js-nvm js-nvm-current-version)) - (setq js-nvm-current-version - (nvm--find-exact-version-for - (js-comint-list-nvm-versions - (if old-js-nvm - (format "Node version (current %s): " (car old-js-nvm)) - "Node version: ")))))) - (progn - (setq js-comint-program-command - (concat - (car (last js-nvm-current-version)) - "/bin" - "/node")))) + (require 'nvm) + (setq js-use-nvm t) ;; NOTE: js-use-nvm could probably be deprecated + (unless version + (let* ((old-js-nvm (car js-nvm-current-version)) + (prompt (if old-js-nvm + (format "Node version (current %s): " old-js-nvm) + "Node version: "))) + (setq version (js-comint-list-nvm-versions prompt)))) + + (setq js-nvm-current-version (nvm--find-exact-version-for version) + js-comint-program-command (format "%s/bin/node" (cadr js-nvm-current-version)))) (defun js-comint-guess-load-file-cmd (filename) "Create Node file loading command for FILENAME." @@ -335,15 +332,13 @@ set CMD." (cons js-comint-program-command js-comint-program-arguments) " "))))) - - (when (and (not cmd) - js-use-nvm) - (require 'nvm) - (unless js-nvm-current-version (js-comint-select-node-version))) - - (when cmd - (setq js-comint-program-arguments (split-string cmd)) - (setq js-comint-program-command (pop js-comint-program-arguments))) + (if cmd + (let ((cmd-parts (split-string cmd))) + (setq js-comint-program-arguments (cdr cmd-parts) + js-comint-program-command (car cmd-parts))) + (when (and js-use-nvm + (not js-nvm-current-version)) + (js-comint-select-node-version))) (js-comint-start-or-switch-to-repl)) From 1c3b1ab62227458b5245874bfe7e1b8b9ca9a7f7 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Fri, 18 Oct 2024 11:43:22 -0700 Subject: [PATCH 42/52] Message when changing cmd while nvm is set --- js-comint.el | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/js-comint.el b/js-comint.el index 40db58f..ddc5a10 100644 --- a/js-comint.el +++ b/js-comint.el @@ -335,7 +335,10 @@ set CMD." (if cmd (let ((cmd-parts (split-string cmd))) (setq js-comint-program-arguments (cdr cmd-parts) - js-comint-program-command (car cmd-parts))) + js-comint-program-command (car cmd-parts)) + (when js-nvm-current-version + (message "nvm node version overridden, reset with M-x js-comint-select-node-version") + (setq js-use-nvm nil))) (when (and js-use-nvm (not js-nvm-current-version)) (js-comint-select-node-version))) From 6eaa359a8761266d17ae46df1f5e6e65d7f29df7 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Fri, 18 Oct 2024 12:22:02 -0700 Subject: [PATCH 43/52] Do not include empty strings in path --- js-comint.el | 1 + 1 file changed, 1 insertion(+) diff --git a/js-comint.el b/js-comint.el index ddc5a10..25a8dfa 100644 --- a/js-comint.el +++ b/js-comint.el @@ -304,6 +304,7 @@ Create a new Javascript REPL process." (all-paths-list (flatten-list (list node-path node-modules-path js-comint-module-paths))) + (all-paths-list (seq-remove 'string-empty-p all-paths-list)) (local-node-path (string-join all-paths-list (js-comint--path-sep))) (repl-mode (or (getenv "NODE_REPL_MODE") "magic")) (js-comint-code (format js-comint-code-format From 4353fe2fa6f0bea8c005ae2ee9d5c1e6597da81d Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Fri, 18 Oct 2024 12:24:53 -0700 Subject: [PATCH 44/52] Switch buffers in start-or-switch-to-buffer --- js-comint.el | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/js-comint.el b/js-comint.el index 25a8dfa..22bf916 100644 --- a/js-comint.el +++ b/js-comint.el @@ -297,25 +297,26 @@ Create a new Javascript REPL process." (defun js-comint-start-or-switch-to-repl () "Start a new repl or switch to existing repl." (interactive) - (let* ((node-path (getenv "NODE_PATH")) - ;; The path to a local node_modules - (node-modules-path (and js-comint-set-env-when-startup - (js-comint--suggest-module-path))) - (all-paths-list (flatten-list (list node-path - node-modules-path - js-comint-module-paths))) - (all-paths-list (seq-remove 'string-empty-p all-paths-list)) - (local-node-path (string-join all-paths-list (js-comint--path-sep))) - (repl-mode (or (getenv "NODE_REPL_MODE") "magic")) - (js-comint-code (format js-comint-code-format - (window-width) js-comint-prompt repl-mode))) - ;; TODO what happens if you switch into this from a different project? - (with-environment-variables (("NODE_NO_READLINE" "1") - ("NODE_PATH" local-node-path)) - (pop-to-buffer - (apply 'make-comint js-comint-buffer js-comint-program-command nil - `(,@js-comint-program-arguments "-e" ,js-comint-code)))) - (js-comint-mode))) + (if (js-comint-get-process) + (pop-to-buffer (js-comint-get-buffer)) + (let* ((node-path (getenv "NODE_PATH")) + ;; The path to a local node_modules + (node-modules-path (and js-comint-set-env-when-startup + (js-comint--suggest-module-path))) + (all-paths-list (flatten-list (list node-path + node-modules-path + js-comint-module-paths))) + (all-paths-list (seq-remove 'string-empty-p all-paths-list)) + (local-node-path (string-join all-paths-list (js-comint--path-sep))) + (repl-mode (or (getenv "NODE_REPL_MODE") "magic")) + (js-comint-code (format js-comint-code-format + (window-width) js-comint-prompt repl-mode))) + (with-environment-variables (("NODE_NO_READLINE" "1") + ("NODE_PATH" local-node-path)) + (pop-to-buffer + (apply 'make-comint js-comint-buffer js-comint-program-command nil + `(,@js-comint-program-arguments "-e" ,js-comint-code)))) + (js-comint-mode)))) ;;;###autoload (defun js-comint-repl (&optional cmd) From e1347ba425972de18246456a590aaa345da09189 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Tue, 22 Oct 2024 22:50:07 -0700 Subject: [PATCH 45/52] repl.start() takes a single object arg --- js-comint.el | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/js-comint.el b/js-comint.el index 22bf916..2114812 100644 --- a/js-comint.el +++ b/js-comint.el @@ -137,8 +137,12 @@ (defvar js-comint-code-format (concat "process.stdout.columns = %d;" - "require('repl').start('%s', null, null, true, false, " - "require('repl')['REPL_MODE_' + '%s'.toUpperCase()])")) + "require('repl').start({ +\"prompt\": '%s', +\"ignoreUndefined\": true, +\"preview\": true, +\"replMode\": require('repl')['REPL_MODE_' + '%s'.toUpperCase()] +})")) (defvar js-nvm-current-version nil "Current version of node for js-comint. From b99d8059b6a5b964674a78c245378c680f0d836c Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Mon, 28 Oct 2024 12:55:33 -0700 Subject: [PATCH 46/52] NODE_REPL_MODE does not need to be set manually --- js-comint.el | 6 ++---- test-js-comint.el | 6 ++++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/js-comint.el b/js-comint.el index 2114812..8c6a93e 100644 --- a/js-comint.el +++ b/js-comint.el @@ -140,8 +140,7 @@ "require('repl').start({ \"prompt\": '%s', \"ignoreUndefined\": true, -\"preview\": true, -\"replMode\": require('repl')['REPL_MODE_' + '%s'.toUpperCase()] +\"preview\": true })")) (defvar js-nvm-current-version nil @@ -312,9 +311,8 @@ Create a new Javascript REPL process." js-comint-module-paths))) (all-paths-list (seq-remove 'string-empty-p all-paths-list)) (local-node-path (string-join all-paths-list (js-comint--path-sep))) - (repl-mode (or (getenv "NODE_REPL_MODE") "magic")) (js-comint-code (format js-comint-code-format - (window-width) js-comint-prompt repl-mode))) + (window-width) js-comint-prompt))) (with-environment-variables (("NODE_NO_READLINE" "1") ("NODE_PATH" local-node-path)) (pop-to-buffer diff --git a/test-js-comint.el b/test-js-comint.el index fff330c..e3cfb04 100644 --- a/test-js-comint.el +++ b/test-js-comint.el @@ -116,3 +116,9 @@ reduce((prev, curr) => prev + curr, 0);" "^9$"))) (setenv "NODE_PATH" original-env) (fset 'js-comint--suggest-module-path original-suggest) (js-comint-test-exit-comint)))) + +(ert-deftest js-comint/test-strict-mode () + "When NODE_REPL_MODE=strict should use strict mode." + (with-environment-variables (("NODE_REPL_MODE" "strict")) + ;; global variables are not allowed in strict mode + (js-comint-test-output-matches "foo = 5;" "Uncaught ReferenceError.*"))) From 446cb64c4ee6dc0f993fd7202d4f327ba7ff97ee Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Tue, 22 Oct 2024 12:05:56 -0700 Subject: [PATCH 47/52] Add Eask file Add gitignore --- .gitignore | 2 ++ Eask | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 .gitignore create mode 100644 Eask diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3dadea7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.elc +.eask diff --git a/Eask b/Eask new file mode 100644 index 0000000..1053277 --- /dev/null +++ b/Eask @@ -0,0 +1,18 @@ +(package "js-comint" + "1.2.0" + "JavaScript interpreter in window.") + +(website-url "https://github.com/redguardtoo/js-comint") +(keywords "javascript" "node" "inferior-mode" "convenience") + +(package-file "js-comint.el") + +(script "test" "eask test ert test-js-comint.el") + +(source "gnu") +(source "melpa") + +(depends-on "emacs" "28.1") + +(development + (depends-on "el-mock")) From dd9c0ed2c8f47e0c4e7394b071cfc04527ae8cc4 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Tue, 22 Oct 2024 12:37:46 -0700 Subject: [PATCH 48/52] Update github action to use eask Remove unused script files --- .github/workflows/actions.yml | 9 ++++++--- install-deps.sh | 6 ------ runtests.sh | 9 --------- 3 files changed, 6 insertions(+), 18 deletions(-) delete mode 100755 install-deps.sh delete mode 100755 runtests.sh diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index 306c453..755ffda 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -6,11 +6,14 @@ jobs: matrix: emacs-version: [28, 29] runs-on: ubuntu-latest - container: silex/emacs:${{matrix.emacs-version}} + container: silex/emacs:${{matrix.emacs-version}}-ci-eask steps: - name: Check out repository code uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20 - name: Install test dependencies - run: ./install-deps.sh + run: eask install-deps --dev - name: Run tests - run: ./runtests.sh + run: eask run script test diff --git a/install-deps.sh b/install-deps.sh deleted file mode 100755 index 7d8faf7..0000000 --- a/install-deps.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -set -xeu - -apt-get update -apt-get install -y nodejs -apt-get clean diff --git a/runtests.sh b/runtests.sh deleted file mode 100755 index 58e9b85..0000000 --- a/runtests.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -set -xeu - -cd "$(dirname $0)" -emacs -Q --batch \ - -l ert \ - -l js-comint.el \ - -l test-js-comint.el \ - -f ert-run-tests-batch-and-exit From 3e00e754edbf21bb48a1c657cefe2fcc6f8b0128 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Tue, 29 Oct 2024 14:25:44 -0700 Subject: [PATCH 49/52] Add lint job to catch nvm.el signature changes --- .github/workflows/actions.yml | 10 ++++++++++ Eask | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index 755ffda..ef34f90 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -17,3 +17,13 @@ jobs: run: eask install-deps --dev - name: Run tests run: eask run script test + lint: + runs-on: ubuntu-latest + container: silex/emacs:29-ci-eask + steps: + - name: Check out repository code + uses: actions/checkout@v4 + - name: Install test dependencies + run: eask install-deps --dev + - name: Run linters + run: eask lint declare --strict diff --git a/Eask b/Eask index 1053277..7de41f2 100644 --- a/Eask +++ b/Eask @@ -15,4 +15,5 @@ (depends-on "emacs" "28.1") (development - (depends-on "el-mock")) + (depends-on "el-mock") + (depends-on "nvm")) From 61d2dbf826127a6cf1e6c7ac644105871bd2eaf8 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Tue, 29 Oct 2024 14:30:14 -0700 Subject: [PATCH 50/52] Add unit tests for js-comint-select-node-version using el-mock --- test-js-comint.el | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/test-js-comint.el b/test-js-comint.el index e3cfb04..91b9066 100644 --- a/test-js-comint.el +++ b/test-js-comint.el @@ -2,6 +2,7 @@ (require 'js-comint) (require 'ert) +(require 'el-mock) (defun js-comint-test-buffer-matches (regex) "Search the js-comint buffer for the given regular expression. @@ -122,3 +123,49 @@ reduce((prev, curr) => prev + curr, 0);" "^9$"))) (with-environment-variables (("NODE_REPL_MODE" "strict")) ;; global variables are not allowed in strict mode (js-comint-test-output-matches "foo = 5;" "Uncaught ReferenceError.*"))) + +(ert-deftest js-comint-select-node-version/test-no-nvm () + "Should error if nvm is missing." + (let ((original-command-value js-comint-program-command)) + (with-mock + (mock (require 'nvm) => (error "Cannot open nvm")) + (should-error (js-comint-select-node-version)) + (should-not js-use-nvm) + (should (equal js-comint-program-command + original-command-value))))) + +(ert-deftest js-comint-select-node-version/test-with-arg () + "Should set program-command when called non-interactively." + (let ((original-command-value js-comint-program-command) + (original-use-jvm-value js-use-nvm) + (original-nvm-version js-nvm-current-version)) + (unwind-protect + (with-mock + (mock (require 'nvm)) + (mock (nvm--find-exact-version-for "foo") => '("foo-1.2" "some_path")) + (js-comint-select-node-version "foo") + (should js-use-nvm) + (should (equal js-comint-program-command + "some_path/bin/node")) + (should (equal js-nvm-current-version + '("foo-1.2" "some_path")))) + (setq js-comint-program-command original-command-value + js-use-nvm original-use-jvm-value + js-nvm-current-version original-nvm-version)))) + +(ert-deftest js-comint-select-node-version/test-optional-arg () + "Should set program-command when called with no arg." + (let ((original-command-value js-comint-program-command) + (original-use-jvm-value js-use-nvm) + (original-nvm-version js-nvm-current-version)) + (unwind-protect + (with-mock + (mock (require 'nvm)) + (mock (js-comint-list-nvm-versions *) => "foo") + (mock (nvm--find-exact-version-for "foo") => '("foo-1.2" "some_path")) + (js-comint-select-node-version) + (should (equal js-comint-program-command + "some_path/bin/node"))) + (setq js-comint-program-command original-command-value + js-use-nvm original-use-jvm-value + js-nvm-current-version original-nvm-version)))) From 5a69cdd32a4487b2a281154bbafcb792bbdc2d7b Mon Sep 17 00:00:00 2001 From: Lin Jian Date: Tue, 12 Nov 2024 12:03:04 +0800 Subject: [PATCH 51/52] Exclude tests from MELPA packages MELPA excludes[1] files named as *-test.el. [1]: https://github.com/melpa/melpa/blob/f1945a7004f8bbe4043a8579b16e1147f19d7623/README.md?plain=1#L171 --- Eask | 2 +- test-js-comint.el => js-comint-test.el | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename test-js-comint.el => js-comint-test.el (100%) diff --git a/Eask b/Eask index 7de41f2..86d8442 100644 --- a/Eask +++ b/Eask @@ -7,7 +7,7 @@ (package-file "js-comint.el") -(script "test" "eask test ert test-js-comint.el") +(script "test" "eask test ert js-comint-test.el") (source "gnu") (source "melpa") diff --git a/test-js-comint.el b/js-comint-test.el similarity index 100% rename from test-js-comint.el rename to js-comint-test.el From 9da6cfc68f0f677fc3caccc16ced44a93af86d04 Mon Sep 17 00:00:00 2001 From: Josh Bax Date: Thu, 5 Dec 2024 11:21:21 -0800 Subject: [PATCH 52/52] Add completion feature Squashed commit of the following: commit 479eb4664d9f7325980bd61e344bd7e631aa57fd Author: Josh Bax Date: Tue Dec 3 16:41:38 2024 -0800 Fix: use emacs 28 compat completion fns in test commit 5870d05b9417e3a46f3806cc76089a051842fe6a Author: Josh Bax Date: Tue Dec 3 16:17:33 2024 -0800 Fix: avoid Eask issue to unblock tests commit 19446fd9f146e92500ed511ddd57d42219091f8f Author: Josh Bax Date: Tue Dec 3 10:51:17 2024 -0800 Tidy comments and docstrings commit af87be00c890c506b562eee473d97d71eca1cae8 Author: Josh Bax Date: Tue Dec 3 10:41:40 2024 -0800 Fix do not pass arguments to callback commit ee0824b7ebeb3f7bef8830e368de3b670c2e34fa Author: Josh Bax Date: Tue Dec 3 15:23:16 2024 -0800 Add Capf tests * test/capf-tests.el: C commit 9fcb825af2fe384b822361d1d15ca6f142c7a627 Author: Josh Bax Date: Mon Dec 2 17:50:10 2024 -0800 Move tests into own folder and separate files This allows separate loading of company and CAPE commit bd2616b48575fcb66fb5e438b273f23486ced759 Author: Josh Bax Date: Tue Dec 3 15:17:17 2024 -0800 Native capf commit 5d67b6ba8594214ec963ef5f93cd94892eb8e6c7 Author: Josh Bax Date: Wed Nov 27 11:22:20 2024 -0800 Use string-match-p to support 28 commit 9f1241f22e0eee9f3cbdf52ea391ceee2696b33e Author: Josh Bax Date: Wed Nov 27 11:03:56 2024 -0800 Cleanup hung callbacks when completing props of null object commit 89375779aa38aae4990267187bd54243fd35755f Author: Josh Bax Date: Tue Nov 26 12:45:12 2024 -0800 Quote string when used as regexp in callback commit e276f26555b458790d865205b8ec88fd86de4433 Author: Josh Bax Date: Tue Nov 26 11:46:38 2024 -0800 Address fast-typing bug: extra input when callbacks overlap commit 4353c89f0e263f664c09d531dab1e38ba30aa6f5 Author: Josh Bax Date: Tue Nov 26 10:28:00 2024 -0800 Use ert-async for tests with checks in callbacks Update test-company-complete-long-line commit 44f2eacd1e030d372ef5dfa3c9e1e36e03bec98b Author: Josh Bax Date: Mon Nov 25 12:32:37 2024 -0800 Fix matching bug in complete-substring commit bb479f5f3663a33a59fea97e91bcfcc9e8302bf6 Author: Josh Bax Date: Mon Nov 25 11:34:27 2024 -0800 Remove unused completion string property commit ea7a9fd68810059b4e9e0b51a5885dad8bf23d62 Author: Josh Bax Date: Mon Nov 25 10:28:29 2024 -0800 Fix: minor formatting change commit 2ae491d162e49b6109ec74351f432fa50a873e22 Author: Josh Bax Date: Mon Oct 28 22:35:16 2024 -0700 Cleanup completion buffers commit e07de7bb02114529b5abf962a50431b70002fa80 Author: Josh Bax Date: Mon Oct 28 22:22:11 2024 -0700 Pass NODE_REPL_MODE as argument due to custom env Env var has no effect for some reason commit b649559fb0055a29b5e0b093b0420a21bd618608 Author: Josh Bax Date: Mon Oct 28 22:19:17 2024 -0700 Wiggle the cursor to produce a prompt and prevent hanging This makes most cases resolve with similar output too commit 123c87dec4434a9a6e3aa87f2b26306367325c12 Author: Josh Bax Date: Mon Oct 28 21:23:52 2024 -0700 Add Company integration tests commit 83f8a31b31fbb63780618ce48f20bc81eb79cf6f Author: Josh Bax Date: Mon Oct 28 19:37:40 2024 -0700 Simplify process-completion-output commit f80ca422a6991690489554a710128fa99c0016d1 Author: Josh Bax Date: Mon Oct 28 19:24:50 2024 -0700 Proactively clear partial completions commit 672c28afe896cf8cc60f1c641dbc7b8cae9465e5 Author: Josh Bax Date: Mon Oct 28 16:08:57 2024 -0700 Complete long statements by taking latest substring commit 3e6863ef2f0f9064bdbdae72ad349416a4c984ee Author: Josh Bax Date: Mon Oct 28 15:48:04 2024 -0700 Don't complete at whitespace commit 51ebba7051e852e1e3e7d36e0bdc92d81fa3fd42 Author: Josh Bax Date: Fri Oct 25 14:48:24 2024 -0700 Add unit tests for listener callback code commit 41577969df45572ef4e6d7cc1b9e9a734f117f57 Author: Josh Bax Date: Fri Oct 25 14:42:12 2024 -0700 Recover from errors in company callbacks after completion commit 0ed6d0be08ef334c710a2dcd0ab67d5d95e91098 Author: Josh Bax Date: Fri Oct 25 14:12:50 2024 -0700 Set TERM using env Remove with-environment-variables because it must be set within the comint call. Move other vars into the same location. commit aa3821050361f608469924f6144b33e2f186244c Author: Josh Bax Date: Fri Oct 25 10:22:44 2024 -0700 Failed callbacks should be removed commit 5b3efeaf6bde14e3729e80bea24bfc48e67a367e Author: Josh Bax Date: Fri Oct 25 09:45:53 2024 -0700 Factor out callback active test commit 67208a6cf769d4250342d46e61b6298999f9dbd4 Author: Josh Bax Date: Thu Oct 24 23:11:50 2024 -0700 Add retry counter for method completion commit 9433d55aea03aeea9a9167451425d7a1e4bbcc42 Author: Josh Bax Date: Thu Oct 24 23:11:38 2024 -0700 Factor out callback creation commit dd0a5e5deac9608c692f356b7be9767df2650adc Author: Josh Bax Date: Thu Oct 24 13:08:07 2024 -0700 Rewrite completions to use listener pattern callback commit 83e3509a5d472d3b7b90c42daf394eb218a20813 Author: Josh Bax Date: Thu Oct 24 13:02:45 2024 -0700 Rename company-js-comint-backend => company-js-comint Matches other backend names commit 74592d57755a51e88508f41707cc636bf09a475a Author: Josh Bax Date: Tue Oct 22 23:56:41 2024 -0700 Don't use comint-send-string for invisible input commit 4816ab3dce13bfc40fcf1dc0a880b883d9c247a0 Author: Josh Bax Date: Tue Oct 22 23:50:09 2024 -0700 Inline company definition in should-complete commit c6cbbda5b43028aa6aa995b736dcda93429494ee Author: Josh Bax Date: Tue Oct 22 12:48:22 2024 -0700 Use 28.1 compatible kill-matching-buffers in test commit bb64f539258354b2365a965470a9ca1475e79749 Author: Josh Bax Date: Tue Oct 22 12:36:52 2024 -0700 Add should-complete unit tests Add Eask file commit 0dc206a7be64f61ba8098f22a962be317d115df6 Author: Josh Bax Date: Tue Oct 22 11:23:47 2024 -0700 Add test macro with-new-js-comint-buffer and simplify tests Kills comint buffer between runs for safety commit 054c09781d9c2c1711b4e56b822650cb44169944 Author: Josh Bax Date: Sun Oct 20 11:42:37 2024 -0700 Add unload function to remove company backend commit 96cf5a7d7f45402d67523e52872c5ffed9099f9c Author: Josh Bax Date: Wed Oct 16 16:32:28 2024 -0700 Unit tests for normal completion commit 2721fd884b06eeb06e456bfb78a9c52c5c33d704 Author: Josh Bax Date: Wed Oct 16 16:24:30 2024 -0700 Use js-comint-get-process commit f2f3d65c8729acbf585c3494ab0768d526161276 Author: Josh Bax Date: Wed Oct 16 15:50:56 2024 -0700 Do not take arguments for js-comint--discard-output when a callback commit 7829facfcbf47b90714391df3bc5748ead32e438 Author: Josh Bax Date: Wed Oct 16 15:41:30 2024 -0700 Discard should apply to all output until control char commit 6e274c8fe3c50247a6481b1d6b943e1fd1b8bbc6 Author: Josh Bax Date: Wed Oct 16 15:35:53 2024 -0700 Add unit tests for js-comint--process-completion-output commit 5b50adad45d65b73b5480b8d0b589b965cac068c Author: Josh Bax Date: Wed Oct 16 13:32:57 2024 -0700 Properly complete method names commit 7f57d7618e43ed43e1d7a47b46cac22ed4825b59 Author: Josh Bax Date: Wed Oct 16 11:12:29 2024 -0700 Factor out completion processing logic commit a98dc6e4947a085eacc82f68539ad72638e3f4a3 Author: Josh Bax Date: Wed Oct 16 09:15:20 2024 -0700 Simplify handler logic commit f182a523725c6e62a5c1808c2141c191d7c31acf Author: Josh Bax Date: Tue Oct 15 15:30:25 2024 -0700 Completion handler with debug messages commit 18ff4f64a0426885b6e79cb6a130284a2082cdde Author: Josh Bax Date: Tue Oct 15 13:43:59 2024 -0700 Filter completion output when a flag is set commit 1cfedc9854d4ce6f99a3a14132764ef720737955 Author: Josh Bax Date: Tue Oct 15 10:13:52 2024 -0700 Get input for completion commit 1352dbf197c4cc27ffdd82e57e01cc02171bf367 Author: Josh Bax Date: Tue Oct 15 09:01:49 2024 -0700 Add stub for company-js-comint-backend and init code --- Eask | 6 +- js-comint-test.el | 171 ---------------- js-comint.el | 303 +++++++++++++++++++++++++++- test/capf-tests.el | 77 ++++++++ test/common.el | 33 ++++ test/company-tests.el | 80 ++++++++ test/unit-tests.el | 449 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 939 insertions(+), 180 deletions(-) delete mode 100644 js-comint-test.el create mode 100644 test/capf-tests.el create mode 100644 test/common.el create mode 100644 test/company-tests.el create mode 100644 test/unit-tests.el diff --git a/Eask b/Eask index 86d8442..54dad19 100644 --- a/Eask +++ b/Eask @@ -7,7 +7,7 @@ (package-file "js-comint.el") -(script "test" "eask test ert js-comint-test.el") +(script "test" "eask test ert test/*tests.el") (source "gnu") (source "melpa") @@ -16,4 +16,6 @@ (development (depends-on "el-mock") - (depends-on "nvm")) + (depends-on "nvm") + (depends-on "company") + (depends-on "ert-async")) diff --git a/js-comint-test.el b/js-comint-test.el deleted file mode 100644 index 91b9066..0000000 --- a/js-comint-test.el +++ /dev/null @@ -1,171 +0,0 @@ -;; -*- lexical-binding: t -*- - -(require 'js-comint) -(require 'ert) -(require 'el-mock) - -(defun js-comint-test-buffer-matches (regex) - "Search the js-comint buffer for the given regular expression. -Return 't if a match is found, nil otherwise." - (with-current-buffer (js-comint-get-buffer) - (save-excursion - (goto-char (point-min)) - (if (re-search-forward regex nil t) t nil)))) - -(defun js-comint-test-output-matches (input regex) - "Verify that sending INPUT yields output that matches REGEX." - - ;; Start an instance to run tests on. - (js-comint-reset-repl) - - (sit-for 1) - - (js-comint-send-string input) - - (sit-for 1) - - (js-comint-test-buffer-matches regex)) - -(defun js-comint-test-exit-comint () - "Finish process." - (when (js-comint-get-process) - (process-send-string (js-comint-get-process) ".exit\n") - (sit-for 1))) - -(ert-deftest js-comint-test-multiline-dotchain-line-start () - "Test multiline statement with dots at beginning of lines." - (should (js-comint-test-output-matches "[1, 2, 3] - .map((it) => it + 1) - .filter((it) => it > 0) - .reduce((prev, curr) => prev + curr, 0);" "^9$"))) - -(ert-deftest js-comint-test-multiline-dotchain-line-start-dos () - "Test multiline statement with dots at beginning of lines, with -DOS line separators." - (should (js-comint-test-output-matches "[1, 2, 3]\r - .map((it) => it + 1)\r - .filter((it) => it > 0)\r - .reduce((prev, curr) => prev + curr, 0);\r -" "^9$"))) - -(ert-deftest js-comint-test-multiline-dotchain-line-end () - "Test multiline statement with dots at end of lines." - (should (js-comint-test-output-matches "[1, 2, 3]. -map((it) => it + 1). -filter((it) => it > 0). -reduce((prev, curr) => prev + curr, 0);" "^9$"))) - -(ert-deftest js-comint-start-or-switch-to-repl/test-no-modules () - "Should preserve node_path when nothing is set." - (let ((original js-comint-module-paths) - (original-set-env js-comint-set-env-when-startup) - (original-env (getenv "NODE_PATH"))) - (unwind-protect - (progn - (setq js-comint-module-paths nil - js-comint-set-env-when-startup nil) - (setenv "NODE_PATH" "/foo/bar") - (js-comint-test-exit-comint) - (js-comint-start-or-switch-to-repl) - (sit-for 1) - (js-comint-send-string "process.env['NODE_PATH'];") - (js-comint-test-buffer-matches "/foo/bar")) - (setq js-comint-module-paths original - js-comint-set-env-when-startup original-set-env) - (setenv "NODE_PATH" original-env) - (js-comint-test-exit-comint)))) - -(ert-deftest js-comint-start-or-switch-to-repl/test-global-set () - "Should include the value of `js-comint-node-modules' if set." - (let ((original js-comint-module-paths) - (original-set-env js-comint-set-env-when-startup) - (original-env (getenv "NODE_PATH"))) - (unwind-protect - (progn - (setq js-comint-module-paths '("/baz/xyz") - js-comint-set-env-when-startup nil) - (setenv "NODE_PATH" "/foo/bar") - (js-comint-test-exit-comint) - (js-comint-start-or-switch-to-repl) - (sit-for 1) - (js-comint-send-string "process.env['NODE_PATH'];") - (js-comint-test-buffer-matches (concat "/foo/bar" (js-comint--path-sep) "/baz/xyz"))) - (setq js-comint-module-paths original - js-comint-set-env-when-startup original-set-env) - (setenv "NODE_PATH" original-env) - (js-comint-test-exit-comint)))) - -(ert-deftest js-comint-start-or-switch-to-repl/test-local () - "Should include the optional node-modules-path." - (let ((original js-comint-module-paths) - (original-set-env js-comint-set-env-when-startup) - (original-env (getenv "NODE_PATH")) - (original-suggest (symbol-function 'js-comint--suggest-module-path))) - (unwind-protect - (progn - (fset 'js-comint--suggest-module-path (lambda () "/baz/xyz")) - (setq js-comint-module-paths '() - js-comint-set-env-when-startup 't) - (setenv "NODE_PATH" "/foo/bar") - (js-comint-test-exit-comint) - (js-comint-start-or-switch-to-repl) - (sit-for 1) - (js-comint-send-string "process.env['NODE_PATH'];") - (js-comint-test-buffer-matches (concat "/foo/bar" (js-comint--path-sep) "/baz/xyz"))) - (setq js-comint-module-paths original - js-comint-set-env-when-startup original-set-env) - (setenv "NODE_PATH" original-env) - (fset 'js-comint--suggest-module-path original-suggest) - (js-comint-test-exit-comint)))) - -(ert-deftest js-comint/test-strict-mode () - "When NODE_REPL_MODE=strict should use strict mode." - (with-environment-variables (("NODE_REPL_MODE" "strict")) - ;; global variables are not allowed in strict mode - (js-comint-test-output-matches "foo = 5;" "Uncaught ReferenceError.*"))) - -(ert-deftest js-comint-select-node-version/test-no-nvm () - "Should error if nvm is missing." - (let ((original-command-value js-comint-program-command)) - (with-mock - (mock (require 'nvm) => (error "Cannot open nvm")) - (should-error (js-comint-select-node-version)) - (should-not js-use-nvm) - (should (equal js-comint-program-command - original-command-value))))) - -(ert-deftest js-comint-select-node-version/test-with-arg () - "Should set program-command when called non-interactively." - (let ((original-command-value js-comint-program-command) - (original-use-jvm-value js-use-nvm) - (original-nvm-version js-nvm-current-version)) - (unwind-protect - (with-mock - (mock (require 'nvm)) - (mock (nvm--find-exact-version-for "foo") => '("foo-1.2" "some_path")) - (js-comint-select-node-version "foo") - (should js-use-nvm) - (should (equal js-comint-program-command - "some_path/bin/node")) - (should (equal js-nvm-current-version - '("foo-1.2" "some_path")))) - (setq js-comint-program-command original-command-value - js-use-nvm original-use-jvm-value - js-nvm-current-version original-nvm-version)))) - -(ert-deftest js-comint-select-node-version/test-optional-arg () - "Should set program-command when called with no arg." - (let ((original-command-value js-comint-program-command) - (original-use-jvm-value js-use-nvm) - (original-nvm-version js-nvm-current-version)) - (unwind-protect - (with-mock - (mock (require 'nvm)) - (mock (js-comint-list-nvm-versions *) => "foo") - (mock (nvm--find-exact-version-for "foo") => '("foo-1.2" "some_path")) - (js-comint-select-node-version) - (should (equal js-comint-program-command - "some_path/bin/node"))) - (setq js-comint-program-command original-command-value - js-use-nvm original-use-jvm-value - js-nvm-current-version original-nvm-version)))) diff --git a/js-comint.el b/js-comint.el index 8c6a93e..5905bfa 100644 --- a/js-comint.el +++ b/js-comint.el @@ -85,6 +85,7 @@ (require 'js) (require 'comint) (require 'ansi-color) +(require 'cl-lib) (defgroup js-comint nil "Run a javascript process in a buffer." @@ -140,7 +141,8 @@ "require('repl').start({ \"prompt\": '%s', \"ignoreUndefined\": true, -\"preview\": true +\"preview\": true, +\"replMode\": require('repl')['REPL_MODE_' + '%s'.toUpperCase()] })")) (defvar js-nvm-current-version nil @@ -150,6 +152,11 @@ Either nil or a list (VERSION-STRING PATH).") (declare-function nvm--installed-versions "nvm.el" ()) (declare-function nvm--find-exact-version-for "nvm.el" (short)) +;; company.el declarations +(defvar company-backends) +(declare-function company-begin-backend "company.el" (backend &optional callback)) +(declare-function company-in-string-or-comment "company.el" nil) + (defun js-comint-list-nvm-versions (prompt) "List all available node versions from nvm prompting the user with PROMPT. Return a string representing the node version." @@ -228,6 +235,270 @@ Return a string representing the node version." (delete dir js-comint-module-paths)) (message "\"%s\" delete from `js-comint-module-paths'" dir)))))) +;;;; Completions: +(defun js-comint--process-completion-output (completion prefix) + "Format COMPLETION string as a list of candidates. +PREFIX is the original completion prefix string." + (let* ((completion (replace-regexp-in-string "\\[[[:digit:]]+[[:alpha:]]+" "" completion)) + (completion-lines (split-string completion " \n" 't)) + ;; in a single completion the node REPL optionally prints type information in a comment + (completion-lines (seq-remove (apply-partially #'string-prefix-p "//") + completion-lines)) + (completion-tokens (seq-mapcat (lambda (x) (split-string x nil 't)) completion-lines)) + (trimmed-prompt (string-trim js-comint-prompt)) + (completion-res (seq-remove (lambda (x) (or (equal x prefix) + (equal x trimmed-prompt) + (equal x "..."))) + completion-tokens))) + completion-res)) + +(defvar-local js-comint--completion-callbacks nil + "List of pending callbacks. +Each should be a plist with last-prompt-start, input-string, type, +function. See `js-comint--set-completion-callback'.") + +(defvar-local js-comint--completion-buffer nil + "Buffer for completion output.") + +(defun js-comint--clear-completion-state () + "Clear stored partial completions." + (when js-comint--completion-buffer + (with-current-buffer js-comint--completion-buffer + (erase-buffer)))) + +(defun js-comint--callback-active-p (callback) + "Non-nil if CALLBACK should be used given current prompt location and input." + (and + (equal + ;; marker for the prompt active when callback was created + (plist-get callback :last-prompt-start) + ;; start prompt marker, must be in comint buffer + (car comint-last-prompt)) + ;; check input-string + (or + ;; "clear" callbacks always fire + (equal 'clear (plist-get callback :type)) + ;; otherwise, if inputs are different, then not active + (equal (js-comint--current-input) (plist-get callback :input-string))))) + +(defun js-comint--async-output-filter (output) + "Dispatches callbacks listening for comint OUTPUT." + (cond + ((null js-comint--completion-callbacks) + output) + ;; Assuming most recent callbacks are at head of the list + ;; If the head is not active, then discard all in the list + ((not (js-comint--callback-active-p (car js-comint--completion-callbacks))) + (prog1 output + (js-comint--clear-completion-state) + (setq js-comint--completion-callbacks nil))) + (t + (prog1 "" + ;; first write output to completion-buffer + (unless (bufferp js-comint--completion-buffer) + (setq js-comint--completion-buffer (generate-new-buffer " *js-comint completion*" t))) + (with-current-buffer js-comint--completion-buffer + (goto-char (point-max)) + (insert output)) + ;; call only the active ones, discard others + (let ((active-callbacks (seq-filter + #'js-comint--callback-active-p + js-comint--completion-callbacks))) + ;; Some callbacks may add further callbacks during their execution. + ;; Re-add any pending callbacks to avoid overwriting new callbacks. + (setq js-comint--completion-callbacks nil) + (dolist (cb active-callbacks) + ;; if the callback exits with non-nil or signals, remove it + (unless (condition-case err + (funcall (plist-get cb :function)) + (t (prog1 t + (message "Error in callback %s" (cdr err))))) + (push cb js-comint--completion-callbacks)))) + (if js-comint--completion-callbacks + (accept-process-output (js-comint-get-process) 0.4) + ;; otherwise reset + (js-comint--clear-completion-state)))))) + +(defun js-comint--completion-looking-back-p (regexp) + "Call `looking-back' with REGEXP on `js-comint--completion-buffer'." + (with-current-buffer js-comint--completion-buffer + (goto-char (point-max)) + (looking-back regexp (line-beginning-position)))) + +(defun js-comint--set-completion-callback (callback type) + "Add CALLBACK to listen for completion output. +TYPE is a symbol describing the callback, either \"clear\" or \"completion\"." + (push `(:last-prompt-start + ,(car comint-last-prompt) + :input-string + ,(js-comint--current-input) + :type + ,type + :function + ,callback) + js-comint--completion-callbacks)) + +(defun js-comint--complete-substring (input-string) + "Given a full line in INPUT-STRING return the substring to complete." + (if-let ((match (string-match-p "[[:space:](\\[{;]\\([[:word:].]*\\)$" input-string))) + (string-trim (substring-no-properties input-string (1+ match))) + input-string)) + +(defun js-comint--get-completion-async (input-string callback) + "Complete INPUT-STRING and register CALLBACK to recieve completion output." + (js-comint--clear-completion-state) + (js-comint--set-completion-callback + ;; callback closure + (let (tab-sent ;; flag tracking repeat tabs: Array\t => \t + check-sent ;; flag tracking whether check has been sent + finished) ;; flag tracking if 'callback' has been called, referred to by inner closures + (lambda () + (cond + ;; case: exact match to input-string in output + ((and (not (string-empty-p input-string)) + (js-comint--completion-looking-back-p (regexp-quote input-string))) + ;; Completions like "Array." need a second tab after the response + (cond + ((and (string-suffix-p "." input-string) + (not tab-sent)) + (prog1 nil ;; do not remove callback + (setq tab-sent 't) + ;; When completing "Array." this will get a list of props + (process-send-string (js-comint-get-process) "\t") + ;; When the completion does not exist, e.g. "foo." there is no response to this tab + ;; Instead, schedule a callback that produces a prompt, e.g. "> foo." + ;; This might happen after completion is finished, either by other input or clearing, + ;; so check to avoid adding garbage output. + (run-at-time 1 nil (lambda () (unless (or finished (not js-comint--completion-callbacks)) + (process-send-string (js-comint-get-process) " \b")))))) + ;; Output may sometimes be staggered "Arr|ay" so the completion appears stepwise + ((not check-sent) + (prog1 nil ;; do not remove callback + (setq check-sent 't) + ;; This "wiggles" the cursor making node repeat the prompt with current input + (process-send-string (js-comint-get-process) " \b"))) + ;; Otherwise there was no match (after retry). + ;; The wiggle should cause a prompt to be echoed, so this case likely does not occur. + ;; Probably useful to keep for edge cases though. + ('t + (prog1 't ;; remove callback + (unwind-protect + (funcall callback nil) + (js-comint--clear-input-async)))))) + ;; case: found a control character (usually part of a prompt) + ((or check-sent + (js-comint--completion-looking-back-p "\\[[[:digit:]]+[AJG]$")) + (setq finished 't) + (let* ((completion-output (with-current-buffer js-comint--completion-buffer + (buffer-string))) + (completion-res (js-comint--process-completion-output + completion-output + input-string))) + (unwind-protect + (funcall callback completion-res) + (js-comint--clear-input-async)) + 't)) + ;; all other cases + ('t + ;; expect that the callback will be removed + nil)))) + 'completion) + + ;; Need to send 2x tabs to trigger completion when there is no input + ;; 1st tab usually does common prefix + (when (string-empty-p input-string) + (process-send-string + (js-comint-get-process) + "\t")) + + (process-send-string + (js-comint-get-process) + (format "%s\t" input-string))) + +(defun js-comint--clear-input-async () + "Clear input already sent to the REPL. +This is used specifically to remove input used to trigger completion." + (js-comint--set-completion-callback + (lambda () + ;; mask output until the REPL echoes a prompt + (js-comint--completion-looking-back-p (concat js-comint-prompt "\\[[[:digit:]]+[AG]$"))) + 'clear) + + (process-send-string + (js-comint-get-process) + ;; this is the symbol for "cut" + "")) + +(defun js-comint--current-input () + "Return current comint input relative to point. +Nil if point is before the current prompt." + (let ((pmark (process-mark (js-comint-get-process)))) + (when (>= (point) (marker-position pmark)) + (buffer-substring pmark (point))))) + +(defun js-comint--should-complete () + "Non-nil if completion should be attempted on text before point." + (let* ((parse (syntax-ppss)) + (string-or-comment (or (nth 3 parse) (nth 4 parse) (nth 7 parse)))) + (cond + (string-or-comment + nil) + ((looking-back "\\." (line-beginning-position)) + 't) + ((looking-back "[[:punct:][:space:]]" (line-beginning-position)) + nil) + (t + 't)))) + +;;;###autoload +(defun company-js-comint (command &optional arg &rest _ignored) + "Wraps node REPL completion for company." + (interactive (list 'interactive)) + (cl-case command + ((interactive) + (company-begin-backend 'company-js-comint)) + ((prefix) + (when (equal major-mode 'js-comint-mode) + (if (js-comint--should-complete) + (js-comint--complete-substring (js-comint--current-input)) + 'stop))) + ((candidates) + (cons :async (apply-partially #'js-comint--get-completion-async arg))))) + +(with-eval-after-load 'company + (cl-pushnew #'company-js-comint company-backends)) + +;; loosely follows cape-company-to-capf +(defun js-comint--capf () + "Convert `company-js-comint' function to a completion-at-point-function." + (when-let ((_ (js-comint--should-complete)) + (initial-input (js-comint--complete-substring (js-comint--current-input)))) + (let* ((end (point)) + (beg (- end (length initial-input))) + restore-props) + (list beg end + (completion-table-dynamic + (lambda (input) + (let ((cands 'js-comint--waiting)) + (js-comint--get-completion-async input (lambda (arg) (setq cands arg))) + ;; Force synchronization, not interruptible! We use polling + ;; here and ignore pending input since we don't use + ;; `sit-for'. This is the same method used by Company itself. + (while (eq cands 'js-comint--waiting) + (sleep-for 0.01)) + ;; The candidate string including text properties should be + ;; restored in the :exit-function + (setq restore-props cands) + (cons (apply-partially #'string-prefix-p input) cands))) + t) + :exclusive 'no + :exit-function (lambda (x _status) + ;; Restore the candidate string including + ;; properties if restore-props is non-nil. See + ;; the comment above. + (setq x (or (car (member x restore-props)) x)) + nil))))) + ;;;###autoload (defun js-comint-save-setup () "Save current setup to \".dir-locals.el\"." @@ -312,12 +583,16 @@ Create a new Javascript REPL process." (all-paths-list (seq-remove 'string-empty-p all-paths-list)) (local-node-path (string-join all-paths-list (js-comint--path-sep))) (js-comint-code (format js-comint-code-format - (window-width) js-comint-prompt))) - (with-environment-variables (("NODE_NO_READLINE" "1") - ("NODE_PATH" local-node-path)) - (pop-to-buffer - (apply 'make-comint js-comint-buffer js-comint-program-command nil - `(,@js-comint-program-arguments "-e" ,js-comint-code)))) + (window-width) + js-comint-prompt + (or (getenv "NODE_REPL_MODE") "sloppy"))) + ;; NOTE: it's recommended not to use NODE_PATH + (environment `("TERM=emacs" + "NODE_NO_READLINE=1" + ,(format "NODE_PATH=%s" local-node-path)))) + (pop-to-buffer + (apply 'make-comint js-comint-buffer "env" nil + `(,@environment ,js-comint-program-command ,@js-comint-program-arguments "-e" ,js-comint-code))) (js-comint-mode)))) ;;;###autoload @@ -422,6 +697,16 @@ If no region selected, you could manually input javascript expression." (define-key map (kbd "C-c C-c") 'js-comint-quit-or-cancel) map)) +(defun js-comint-unload-function () + "Cleanup mode settings." + (when company-backends + (setq company-backends + (delete #'company-js-comint company-backends)))) + +(defun js-comint--cleanup () + "Runs after comint buffer is killed." + (when js-comint--completion-buffer + (kill-buffer js-comint--completion-buffer))) ;;;###autoload (define-derived-mode js-comint-mode comint-mode "Javascript REPL" @@ -433,6 +718,10 @@ If no region selected, you could manually input javascript expression." ;; Ignore duplicates (setq comint-input-ignoredups t) (add-hook 'comint-output-filter-functions 'js-comint-filter-output nil t) + (add-hook 'comint-preoutput-filter-functions #'js-comint--async-output-filter nil t) + (add-hook 'kill-buffer-hook #'js-comint--cleanup nil t) + (unless (featurep 'company) + (add-hook 'completion-at-point-functions #'js-comint--capf nil t)) (process-put (js-comint-get-process) 'adjust-window-size-function (lambda (_process _windows) ())) (use-local-map js-comint-mode-map) diff --git a/test/capf-tests.el b/test/capf-tests.el new file mode 100644 index 0000000..7c9c70c --- /dev/null +++ b/test/capf-tests.el @@ -0,0 +1,77 @@ +;; -*- lexical-binding: t -*- + +(require 'js-comint) +(require 'ert) +(require 'ert-async) + +(load-file "./test/common.el") + +;;; Integration tests for native completion + +;; quick fix for eask bug that improperly fails tests which use minibuffer +;; see https://github.com/emacs-eask/cli/pull/286 +;; can be removed once fix is available in CI images +(when (advice-member-p 'eask-test-ert--message 'message) + (message "override advice") + (define-advice message (:before-until (&rest args)) + (not (car args)))) + +(ert-deftest js-comint/test-capf-init () + "When company is not loaded should use CAPF." + (when (featurep 'company) + (unload-feature 'company 't)) + (with-new-js-comint-buffer + (should-not (equal completion-at-point-functions + (default-value 'completion-at-point-functions))))) + +(ert-deftest js-comint/test-capf-unique-result () + "Completing with a unique result. +E.g. Arr => Array, or conso => console." + (when (featurep 'company) + (unload-feature 'company 't)) + (with-new-js-comint-buffer + (sit-for 1) + (insert "Arra") + (completion-at-point) + (should (looking-back "Array")))) + +;; note that completion-at-point does not seem to work for an empty prompt + +(ert-deftest js-comint/test-capf-complete-props () + "Completing props of an object. +E.g. should complete \"Array.\" to all properties." + (when (featurep 'company) + (unload-feature 'company 't)) + (with-new-js-comint-buffer + (sit-for 1) + (insert "Array.") + (completion-at-point) + (with-selected-window (get-buffer-window "*Completions*") + (next-completion 1) + (choose-completion)) + (should (looking-back "Array.__proto__")))) + +(ert-deftest js-comint/test-capf-complete-long-line () + "Completing part of a line. +E.g. 'if (true) { console.'" + (when (featurep 'company) + (unload-feature 'company 't)) + (with-new-js-comint-buffer + (sit-for 1) + (insert "if (true) { console.") + (completion-at-point) + (with-selected-window (get-buffer-window "*Completions*") + (next-completion 1) + (choose-completion)) + (should (looking-back "console.__proto__")))) + +(ert-deftest-async js-comint/test-capf-no-props (done) + "Completing a string with trailing dot should not hang." + (when (featurep 'company) + (unload-feature 'company 't)) + (with-new-js-comint-buffer + (sit-for 1) + (insert "foo.") + (should-not (completion-at-point)) + ;; detects if completion-at-point hangs + (funcall done))) diff --git a/test/common.el b/test/common.el new file mode 100644 index 0000000..16d8a6b --- /dev/null +++ b/test/common.el @@ -0,0 +1,33 @@ +;; -*- lexical-binding: t -*- + +(require 'js-comint) +(require 'ert) + +(defmacro with-new-js-comint-buffer (&rest body) + "Run BODY with a fresh js-comint as current buffer and exit after." + (declare (indent 0) (debug t)) + `(progn + (when (js-comint-get-process) + (kill-process (js-comint-get-process))) + (sleep-for 0.2) + (kill-matching-buffers (js-comint-get-buffer-name) nil t) + (run-js) + (unwind-protect + (with-current-buffer (js-comint-get-buffer) + (font-lock-mode -1) + (sit-for 1) ;; prevent race condition on start + ,@body) + (when (js-comint-get-process) + (kill-process (js-comint-get-process))) + (sleep-for 0.2) + (kill-matching-buffers (js-comint-get-buffer-name) nil t)))) + +(defun js-comint-test-output-matches (input regex) + "Verify that sending INPUT yields output that matches REGEX." + (with-new-js-comint-buffer + (js-comint-send-string input) + (sit-for 1) + (let ((output (buffer-substring-no-properties + comint-last-input-end + (car comint-last-prompt)))) + (should (string-match-p regex output))))) diff --git a/test/company-tests.el b/test/company-tests.el new file mode 100644 index 0000000..8f403af --- /dev/null +++ b/test/company-tests.el @@ -0,0 +1,80 @@ +;; -*- lexical-binding: t -*- + +(require 'js-comint) +(require 'ert) +(require 'company) + +(load-file "./test/common.el") + +;;; Company Integration Tests + +;; sanity check: node should interpret ^U +(ert-deftest js-comint--clear-input-async/test-integration () + "Tests whether clear works in a live comint." + (with-new-js-comint-buffer + (process-send-string (get-buffer-process (current-buffer)) "5") + (js-comint--clear-input-async) + (comint-send-input) + (let ((output (buffer-substring-no-properties + comint-last-input-end + (car comint-last-prompt)))) + ;; if it fails, node will see 5^U and fail, or see just 5 and echo it + (should (string-empty-p output))))) + +(ert-deftest js-comint/test-dumb-term () + "TERM env var should not be dumb." + (js-comint-test-output-matches "process.env['TERM']" "emacs")) + +(ert-deftest js-comint/test-company-global () + "Tests completion with an empty prompt." + (with-new-js-comint-buffer + (company-mode) + (sit-for 1) + (company-manual-begin) + ;; register callback to see globals + (company-complete-selection) + (should (looking-back "AbortController")))) + +(ert-deftest js-comint/test-company-unique-result () + "Completing with a unique result. +E.g. Arr => Array, or conso => console." + (with-new-js-comint-buffer + (company-mode) + (setq company-async-timeout 5) + (sit-for 1) + (insert "Arra") + (company-complete) + (should (looking-back "Array")))) + +(ert-deftest js-comint/test-company-complete-props () + "Completing props of an object. +E.g. should complete \"Array.\" to all properties." + (with-new-js-comint-buffer + (company-mode) + (sit-for 1) + (insert "Array.") + (company-manual-begin) + (company-complete-selection) + (should (looking-back "Array.__proto__")))) + +(ert-deftest js-comint/test-company-complete-long-line () + "Completing part of a line. +E.g. 'if (true) { console.'" + (with-new-js-comint-buffer + (company-mode) + (sit-for 1) + (insert "if (true) { console.") + (company-manual-begin) + (company-complete-selection) + (should (looking-back "console.__proto__")))) + +(ert-deftest js-comint/test-company-quick-typing () + "When completion is triggered while one is already running." + (with-new-js-comint-buffer + (company-mode) + (sit-for 1) + (insert "scrog.") + (company-manual-begin) + (insert "foo") + (company-manual-begin) + (should (looking-back "scrog.foo")))) diff --git a/test/unit-tests.el b/test/unit-tests.el new file mode 100644 index 0000000..6ac2623 --- /dev/null +++ b/test/unit-tests.el @@ -0,0 +1,449 @@ +;; -*- lexical-binding: t -*- + +(require 'js-comint) +(require 'ert) +(require 'el-mock) +(require 'ert-async) + +(load-file "./test/common.el") + +(ert-deftest js-comint-test-multiline-dotchain-line-start-dos () + "Test multiline statement with dots at beginning of lines, with +DOS line separators." + (js-comint-test-output-matches + "[1, 2, 3]\r + .map((it) => it + 1)\r + .filter((it) => it > 0)\r + .reduce((prev, curr) => prev + curr, 0);\r +" + ;; output + "^9$")) + +(ert-deftest js-comint-test-multiline-dotchain-line-end () + "Test multiline statement with dots at end of lines." + (js-comint-test-output-matches + "[1, 2, 3]. +map((it) => it + 1). +filter((it) => it > 0). +reduce((prev, curr) => prev + curr, 0);" + ;; output + "^9$")) + +(ert-deftest js-comint-start-or-switch-to-repl/test-no-modules () + "Should preserve node_path when nothing is set." + (let ((original js-comint-module-paths) + (original-set-env js-comint-set-env-when-startup) + (original-env (getenv "NODE_PATH"))) + (unwind-protect + (progn + (setq js-comint-module-paths nil + js-comint-set-env-when-startup nil) + (setenv "NODE_PATH" "/foo/bar") + (js-comint-test-output-matches "process.env['NODE_PATH'];" + "/foo/bar")) + (setq js-comint-module-paths original + js-comint-set-env-when-startup original-set-env) + (setenv "NODE_PATH" original-env)))) + +(ert-deftest js-comint-start-or-switch-to-repl/test-global-set () + "Should include the value of `js-comint-node-modules' if set." + (let ((original js-comint-module-paths) + (original-set-env js-comint-set-env-when-startup) + (original-env (getenv "NODE_PATH"))) + (unwind-protect + (progn + (setq js-comint-module-paths '("/baz/xyz") + js-comint-set-env-when-startup nil) + (setenv "NODE_PATH" "/foo/bar") + (js-comint-test-output-matches "process.env['NODE_PATH'];" + (concat "/foo/bar" (js-comint--path-sep) "/baz/xyz"))) + (setq js-comint-module-paths original + js-comint-set-env-when-startup original-set-env) + (setenv "NODE_PATH" original-env)))) + +(ert-deftest js-comint-start-or-switch-to-repl/test-local () + "Should include the optional node-modules-path." + (let ((original js-comint-module-paths) + (original-set-env js-comint-set-env-when-startup) + (original-env (getenv "NODE_PATH")) + (original-suggest (symbol-function 'js-comint--suggest-module-path))) + (unwind-protect + (progn + (fset 'js-comint--suggest-module-path (lambda () "/baz/xyz")) + (setq js-comint-module-paths '() + js-comint-set-env-when-startup 't) + (setenv "NODE_PATH" "/foo/bar") + (js-comint-test-output-matches "process.env['NODE_PATH'];" + (concat "/foo/bar" (js-comint--path-sep) "/baz/xyz"))) + (setq js-comint-module-paths original + js-comint-set-env-when-startup original-set-env) + (setenv "NODE_PATH" original-env) + (fset 'js-comint--suggest-module-path original-suggest)))) + +(ert-deftest js-comint/test-strict-mode () + "When NODE_REPL_MODE=strict should use strict mode." + (with-environment-variables (("NODE_REPL_MODE" "strict")) + ;; global variables are not allowed in strict mode + (js-comint-test-output-matches "foo = 5;" "Uncaught ReferenceError.*"))) + +(ert-deftest js-comint-select-node-version/test-no-nvm () + "Should error if nvm is missing." + (let ((original-command-value js-comint-program-command)) + (with-mock + (mock (require 'nvm) => (error "Cannot open nvm")) + (should-error (js-comint-select-node-version)) + (should-not js-use-nvm) + (should (equal js-comint-program-command + original-command-value))))) + +(ert-deftest js-comint-select-node-version/test-with-arg () + "Should set program-command when called non-interactively." + (let ((original-command-value js-comint-program-command) + (original-use-jvm-value js-use-nvm) + (original-nvm-version js-nvm-current-version)) + (unwind-protect + (with-mock + (mock (require 'nvm)) + (mock (nvm--find-exact-version-for "foo") => '("foo-1.2" "some_path")) + (js-comint-select-node-version "foo") + (should js-use-nvm) + (should (equal js-comint-program-command + "some_path/bin/node")) + (should (equal js-nvm-current-version + '("foo-1.2" "some_path")))) + (setq js-comint-program-command original-command-value + js-use-nvm original-use-jvm-value + js-nvm-current-version original-nvm-version)))) + +(ert-deftest js-comint-select-node-version/test-optional-arg () + "Should set program-command when called with no arg." + (let ((original-command-value js-comint-program-command) + (original-use-jvm-value js-use-nvm) + (original-nvm-version js-nvm-current-version)) + (unwind-protect + (with-mock + (mock (require 'nvm)) + (mock (js-comint-list-nvm-versions *) => "foo") + (mock (nvm--find-exact-version-for "foo") => '("foo-1.2" "some_path")) + (js-comint-select-node-version) + (should (equal js-comint-program-command + "some_path/bin/node"))) + (setq js-comint-program-command original-command-value + js-use-nvm original-use-jvm-value + js-nvm-current-version original-nvm-version)))) + +(ert-deftest js-comint--current-input/test () + "Tests default behavior." + (with-new-js-comint-buffer + (insert "Array") + (should (equal (js-comint--current-input) "Array")) + (comint-send-input) + (should (equal (js-comint--current-input) "")) + (goto-char (point-min)) + (should (equal (js-comint--current-input) nil)))) + +(ert-deftest js-comint--complete-substring/test-semi-colon () + "Should break at semi-colons." + (should (equal (js-comint--complete-substring "foo; bar") + "bar"))) + +(ert-deftest js-comint--complete-substring/test-brackets () + "Should break at opening brackets." + (should (equal (js-comint--complete-substring "if(tru") + "tru"))) + +(ert-deftest js-comint--complete-substring/test-words () + "Should break at words." + (should (equal (js-comint--complete-substring "for (let i of myObject.pro") + "myObject.pro"))) + +(ert-deftest js-comint--complete-substring/test-braces () + "Should break at opening braces." + (should (equal (js-comint--complete-substring "if (true) { cons") + "cons"))) + +(ert-deftest js-comint--should-complete/test () + "Tests default behavior." + (with-new-js-comint-buffer + (insert "Arr") + (should (js-comint--should-complete)) + (comint-kill-input) + + (insert "Array.") + (should (js-comint--should-complete)) + (comint-kill-input) + + ;; empty line + (should (js-comint--should-complete)) + + (insert "// a comment") + (should-not (js-comint--should-complete)) + (comint-kill-input) + + (insert "\"foo") + (should-not (js-comint--should-complete)) + (comint-kill-input) + + (insert "[1,2,") + (should-not (js-comint--should-complete)) + (comint-kill-input) + + (insert "foo() ") + (should-not (js-comint--should-complete)) + (comint-kill-input))) + +(ert-deftest js-comint--process-completion-output/test-globals () + "Completing an empty string." + (should + (equal + (js-comint--process-completion-output + " +AbortController AbortSignal AggregateError Array + +constructor + +> " + "") + '("AbortController" + "AbortSignal" + "AggregateError" + "Array" + "constructor")))) + +(ert-deftest js-comint--process-completion-output/test-single-completion () + "Completion of \"Arr\" yields a single result and type info." + (should (equal (js-comint--process-completion-output + "Array +// [Function: Array]" + "Arr") + '("Array")))) + +(ert-deftest js-comint--process-completion-output/test-method-completion () + "Completion of object properties should give list of properties prefixed with name." + (should + (equal + (js-comint--process-completion-output + "Array. +Array.__proto__ Array.hasOwnProperty Array.isPrototypeOf Array.propertyIsEnumerable Array.toLocaleString +Array.valueOf + +> Array." + "Array.") + '("Array.__proto__" + "Array.hasOwnProperty" + "Array.isPrototypeOf" + "Array.propertyIsEnumerable" + "Array.toLocaleString" + "Array.valueOf")))) + +(ert-deftest js-comint--process-completion-output/test-multiline-prefix () + "Completion when there is a '...' prefix." + (should + (equal + (js-comint--process-completion-output + "Array. +Array.__proto__ Array.hasOwnProperty Array.isPrototypeOf Array.propertyIsEnumerable Array.toLocaleString +Array.valueOf + +... Array." + "Array.") + '("Array.__proto__" + "Array.hasOwnProperty" + "Array.isPrototypeOf" + "Array.propertyIsEnumerable" + "Array.toLocaleString" + "Array.valueOf")))) + +(ert-deftest js-comint--async-output-filter/test-no-callbacks () + "Output should be kept when no callbacks are active." + (with-temp-buffer + (should (equal (js-comint--async-output-filter "foo") + "foo")) + ;; should be the same when old callbacks are used + (with-mock + (mock (js-comint--callback-active-p *) => nil) + (setq js-comint--completion-buffer nil + js-comint--completion-callbacks (list ())) + (should (equal (js-comint--async-output-filter "foo") + "foo")) + ;; callbacks should be cleared too + (should-not js-comint--completion-callbacks)))) + +(ert-deftest js-comint--async-output-filter/test-discard () + "Output should be discarded when completion callback is active." + (with-temp-buffer + ;; function ignore always returns nil, so it is never cleared + (setq js-comint--completion-callbacks (list '(:function ignore))) + (with-mock + (stub js-comint--callback-active-p => 't) + (dolist (output '("foo" "bar" "")) + (should (string-empty-p (js-comint--async-output-filter output))))) + ;; text should be in completion buffer + (should (equal (with-current-buffer js-comint--completion-buffer (buffer-string)) + "foobar")) + (should (equal js-comint--completion-callbacks + (list '(:function ignore)))))) + +(ert-deftest js-comint--async-output-filter/test-callback-error () + "Callback should be removed if it signals an error." + (with-temp-buffer + (setq js-comint--completion-callbacks + (list (list :function (lambda () (error "This should be caught!"))))) + (with-mock + (stub js-comint--callback-active-p => 't) + (dolist (output '("foo" "bar" "")) + (js-comint--async-output-filter output))) + ;; callback should be removed + (should-not js-comint--completion-callbacks))) + +(ert-deftest js-comint--async-output-filter/test-callback-chain () + "Callbacks should be able to add further callbacks." + (with-temp-buffer + (setq js-comint--completion-callbacks + (list (list :function (lambda () + (push '(:function foo) js-comint--completion-callbacks) + 't)))) + (with-mock + (stub js-comint--callback-active-p => 't) + (should (string-empty-p (js-comint--async-output-filter "foo")))) + ;; new callback should be added + (should (equal js-comint--completion-callbacks + (list '(:function foo)))))) + +(ert-deftest js-comint--clear-input-async/test () + "Should send correct clear command and complete on expected response." + (with-temp-buffer + (setq js-comint--completion-callbacks + (list '(:function always))) + (with-mock + (stub js-comint--callback-active-p => 't) + (stub js-comint--current-input => "") + (mock (process-send-string * "")) + (js-comint--async-output-filter "foo") + ;; node input is "foo" + ;; completion buffer is empty as there are no active callbacks + (js-comint--clear-input-async) + ;; term sends a prompt + (should (string-empty-p (js-comint--async-output-filter "> "))) + ;; buffer should be empty and no active callbacks + (should (js-comint--completion-looking-back-p "^$")) + (should-not js-comint--completion-callbacks)))) + +(ert-deftest-async js-comint--async-output-filter/test-no-completion (done) + "Output should be saved until string match, then fail." + (with-mock + (stub js-comint--callback-active-p => 't) + (stub js-comint--current-input => "foo") + ;; 1 - complete foo + ;; 2 - finished test " \b" + ;; 3 - clear "" + (mock (process-send-string * *) :times 3) + (with-temp-buffer + (js-comint--get-completion-async "foo" + (lambda (arg) + ;; callback should be called with nil + (funcall done (when arg + (format "expected %s to be nil" arg))))) + (dolist (output '("f" "oo" ;; output in chunks + " > foo" ;; response to " \b" + )) + (should (string-empty-p (js-comint--async-output-filter output)))) + ;; clear should be called + (should (equal (plist-get (car js-comint--completion-callbacks) :type) + 'clear))))) + +(ert-deftest-async js-comint--async-output-filter/test-stale-callback (done) + "Callback should not be called if input has changed." + (with-new-js-comint-buffer + (with-mock + ;; input is "Array" when callback is set + (mock (js-comint--current-input) => "Array") + (js-comint--get-completion-async "Array" + (lambda (arg) + ;; callback should be called with nil or not called + (funcall done (when arg + (format "expected %s to be nil" arg)))))) + ;; bump output, input is now "" + (js-comint--async-output-filter "") + ;; async-output-filter should discard the previous callback as it doesn't apply + (funcall done (when js-comint--completion-callbacks + (format "expected %s to be nil" js-comint--completion-callbacks))))) + +(ert-deftest-async js-comint--get-completion-async/test-prop-completion-fail (done) + "When completion fails on something that looks like an object don't hang." + (with-mock + (stub js-comint--callback-active-p => 't) + (stub js-comint--current-input => "scrog.") + ;; 1 - complete + ;; 2 - send another \t + ;; 3 - finished test " \b" + ;; 4 - clear + (mock (process-send-string * *) :times 4) + (with-temp-buffer + (js-comint--get-completion-async "scrog." + (lambda (arg) + ;; callback should be called with nil + (funcall done (when arg + (format "expected %s to be nil" arg))))) + (dolist (output '("s" "crog.")) + (should (string-empty-p (js-comint--async-output-filter output)))) + ;; no response to repeat \t + ;; after some delay, send answer to wiggle + (sleep-for 2) + (should (string-empty-p (js-comint--async-output-filter " > scrog.")))))) + +(ert-deftest js-comint--get-completion-async/test-user-callback-error () + "Should clear even if supplied callback errors." + (with-mock + (stub js-comint--callback-active-p => 't) + (stub js-comint--current-input => "foo") + (stub process-send-string) + (with-temp-buffer + ;; callback errors + (js-comint--get-completion-async "foo" + (lambda (arg) (error "Broken user callback"))) + ;; after output the erroring callback is called with nil + (dolist (output '("f" "oo" + " > foo" ;; response to " \b" + )) + (should (string-empty-p (js-comint--async-output-filter output)))) + ;; clear should be called + (should (equal (plist-get (car js-comint--completion-callbacks) :type) + 'clear))))) + +(ert-deftest js-comint--get-completion-async/test-user-callback-error-2 () + "Should clear even if supplied callback errors (multiple completions)." + (with-mock + (stub js-comint--callback-active-p => 't) + (stub js-comint--current-input => "foo") + (stub process-send-string) + (with-temp-buffer + ;; callback errors + (js-comint--get-completion-async "foo" + (lambda (arg) (error "Broken user callback"))) + ;; after output the erroring callback is called with nil + (dolist (output '("foo bar baz \n> foo")) + (should (string-empty-p (js-comint--async-output-filter output)))) + ;; clear should be called + (should (equal (plist-get (car js-comint--completion-callbacks) :type) + 'clear))))) + +(ert-deftest-async js-comint--get-completion-async/test-prop-completion (done) + "When completing object properties, send another tab to get completion." + (with-mock + (stub js-comint--callback-active-p => 't) + (stub js-comint--current-input => "Array.") + (mock (process-send-string * *) :times 3) + (with-temp-buffer + ;; callback should be called with ("foo" "bar" "baz") + (js-comint--get-completion-async "Array." + (lambda (arg) + (if (equal arg '("foo" "bar" "baz")) + (funcall done) + (funcall done (format "error %s should be '(\"foo\" \"bar\" \"baz\")" arg))))) + ;; after the second tab get completion suggestions + (dolist (output '("A" "rray." " foo bar baz \n> Array.")) + (should (string-empty-p (js-comint--async-output-filter output)))) + ;; clear should be called + (should (equal (plist-get (car js-comint--completion-callbacks) :type) + 'clear)))))