Browse Source

Add support for JS evaluation in all JS buffers

The new minor mode jade-interaction-mode enables JS evaluation and
inspection in all js2-mode buffers.
Nicolas Petton 6 years ago
4 changed files with 52 additions and 21 deletions
  1. +1
  2. +43
  3. +1
  4. +7

+ 1
- 1
jade-backend.el View File

@ -43,7 +43,7 @@ by backends.")
(defmacro jade-with-connection (connection &rest body)
"Set the value of `jade-connection' to CONNECTION and evaluate BODY."
(declare (debug t))
(declare (debug t) (indent 1))
`(let ((jade-connection ,connection))

+ 43
- 3
jade-interaction.el View File

@ -21,6 +21,8 @@
;;; Code:
(require 'js2-mode)
(require 'map)
(require 'seq)
(require 'jade-backend)
(require 'jade-inspector)
(require 'jade-render)
@ -28,7 +30,8 @@
(defun jade-eval-buffer ()
"Evaluate the accessible portion of current buffer."
(jade-backend-evaluate jade-backend (buffer-string)))
(jade-backend-evaluate (jade-backend) (buffer-string)))
(defun jade-eval-last-node (arg)
"Evaluate the node before point; print in the echo area.
@ -37,7 +40,8 @@ This is similar to `eval-last-sexp', but for JavaScript buffers.
Interactively, with a prefix argument ARG, print output into
current buffer."
(interactive "P")
(jade-backend-evaluate jade-backend
(jade-backend-evaluate (jade-backend)
(js2-node-string (jade-interaction-node-before-point))
(lambda (value _error)
(let ((description (jade-description-string value)))
@ -49,7 +53,8 @@ current buffer."
(defun jade-inspect-last-node ()
"Evaluate and inspect the node before point."
(jade-backend-evaluate jade-backend
(jade-backend-evaluate (jade-backend)
(js2-node-string (jade-interaction-node-before-point))
(lambda (result error)
(jade-inspector-inspect result))))
@ -79,5 +84,40 @@ current buffer."
(setq node parent))
(defun jade-interaction--ensure-connection ()
"Set a connection if no connection is set for the current buffer.
If the current buffer has no associated `jade-connection', prompt
the user for one of the open connections if many of them are
open, and set it in the current buffer."
(unless jade-connection
(setq-local jade-connection
(if (= 1 (seq-length jade-connections))
(seq-elt jade-connections 0)
(defun jade-interaction--read-connection ()
"Read a connection from the minibuffer, with completion."
(let ((url (completing-read "Choose a connection: "
(seq-map (lambda (conn)
(map-elt conn 'url))
(seq-find (lambda (conn)
(string= (map-elt conn 'url)
(defvar jade-interaction-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-x C-e") #'jade-eval-last-node)
(define-key map (kbd "C-c M-i") #'jade-inspect-last-node)
(define-minor-mode jade-interaction-mode
"Mode for JavaScript evalution.
:lighter " js-interaction"
:keymap jade-interaction-mode-map)
(provide 'jade-interaction)
;;; jade-interaction.el ends here

+ 1
- 17
jade-scratch.el View File

@ -26,7 +26,6 @@
(require 'jade-interaction)
(require 'jade-repl)
(require 'company)
(defun jade-scratch ()
"Pop to the scratch buffer.
@ -59,25 +58,10 @@ If there is no current connection, throw an error."
(with-current-buffer buffer
;; TODO: enable completion like in the REPL
(setq-local jade-connection connection)
(defvar jade-scratch-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-x C-e") #'jade-eval-last-node)
(define-key map (kbd "C-c M-i") #'jade-inspect-last-node)
(define-minor-mode jade-scratch-mode
"Mode for JS evalution.
:lighter " js-scratch"
:keymap jade-scratch-mode-map
(when jade-scratch-mode
(defun jade-scratch-insert-welcome-message ()
"Insert a welcome message to help use the scratch buffer."
(insert "// This buffer is for JavaScript evaluation.

+ 7
- 0 View File

@ -7,6 +7,7 @@ JavaScript development, including:
- a REPL (with auto completion) & object inspection;
- an inspector, with history and navigation;
- a scratch buffer (`M-x jade-scratch`);
- JavaScript evaluation in JS buffers with `jade-interaction-mode`;
- a stepping Debugger, similar to `edebug`, or `cider`.
## Screenshots
@ -53,6 +54,12 @@ Start a node process with the `--listen` flag:
Evaluate `M-x jade-connect-to-nodejs`.
### JavaScript evaluation in JS buffers
Add the following to enable evaluation in all JS buffers:
(add-hook 'js2-mode-hook #'jade-interaction-mode)
## Missing features
Jade is young, here's a list of missing/wanted features