Browse Source

Refactor console message rendering in the REPL

Nicolas Petton 5 years ago
Signed by: nico GPG Key ID: 233587A47C207910
2 changed files with 49 additions and 63 deletions
  1. +22
  2. +27

+ 22
- 11
jade-render.el View File

@ -28,17 +28,30 @@
(declare 'jade-backend-object-reference-p)
(defun jade-render-value (value &optional error)
(defun jade-render-values (values &optional separator)
"Render VALUES separated by SEPARATOR.
If no SEPARATOR is provided, separate VALUES by a space."
(unless separator (setq separator " "))
(let ((length (seq-length values)))
(seq-map-indexed (lambda (value index)
(jade-render-value value)
(unless (<= (1- length) index)
(insert separator)))
(defun jade-render-value (value)
"Render VALUE, based on its object type.
If VALUE represents a reference to a remote object, render it
with a link to an inspector on that object."
(if (jade-backend-object-reference-p value)
(jade-render-object-link value error)
(jade-render-description value error)))
(jade-render-object-link value)
(jade-render-description value)))
(defun jade-render-description (value &optional error)
(let ((description (jade-description-string value))
(face (when error 'jade-repl-error-face)))
(defun jade-render-description (value)
(let ((description (jade-description-string value)))
(propertize description
'font-lock-face (or face 'jade-repl-stdout-face)
'font-lock-face 'jade-repl-stdout-face
'rear-nonsticky '(font-lock-face)))))
(defun jade-render-keyword (string)
@ -70,16 +83,14 @@ definitions."
(defun jade-render-object-link (value error)
(defun jade-render-object-link (value)
(let* ((description (jade-description-string value))
(preview (map-elt value 'preview))
(beg (point))
(end (progn
(insert description)
(face (if error
(face 'jade-link-face))
(set-text-properties beg end
`(font-lock-face ,face
mouse-face highlight

+ 27
- 52
jade-repl.el View File

@ -154,9 +154,7 @@ Getting started:
(jade-backend-evaluate (jade-backend)
(lambda (result error)
(when error
(jade-repl-emit-value result error))
(lambda (result _error)
(jade-inspector-inspect result))))
(defun jade-repl--input-content ()
@ -181,13 +179,14 @@ Getting started:
(defun jade-repl-emit-value (value error)
"Emit a string representation of VALUE.
When ERROR is non-nil, use the error face."
When ERROR is non-nil, display VALUE as an error."
(with-current-buffer (jade-repl-get-buffer)
(goto-char (point-max))
(insert-before-markers "\n")
(set-marker jade-repl-output-start-marker (point))
(jade-render-value value error)
(when error (jade-repl--emit-logging-level "error"))
(jade-render-value value)
(insert "\n")
(set-marker jade-repl-output-end-marker (point)))
@ -208,14 +207,13 @@ MESSAGE must contain `text' or `parameters.'. Other fields are
(with-current-buffer (jade-repl-get-buffer)
(let ((level (or (map-elt message 'level) ""))
(text (map-elt message 'text))
(parameters (map-elt message 'parameters))
(url (map-elt message 'url))
(line (map-elt message 'line)))
(let ((text (map-elt message 'text))
(level (or (map-elt message 'level) "")))
(goto-char jade-repl-output-end-marker)
(set-marker jade-repl-output-start-marker (point))
(jade-repl--emit-values text parameters level url line)
(insert "\n")
(jade-repl--emit-logging-level level)
(jade-repl--emit-message-values message)
(set-marker jade-repl-output-end-marker (point))
(unless (eolp)
(insert "\n"))
@ -225,21 +223,17 @@ optional."
(when (jade-repl--message-level-error-p level)
(message text))))))
(defun jade-repl--emit-values (text values level url line)
"Emit a console message values.
TEXT is the message string to emit. VALUES is a sequence of
values to log. LEVEL is the logging level of the
message (\"log\", \"warning\", \"error\", etc.).
URL and LINE provide context information about the source of the
(pcase (seq-length values)
(0 (jade-repl--emit-single-value-message `((type . "string") (description . ,text))
(1 (jade-repl--emit-single-value-message (seq-elt values 0) level url line))
(_ (jade-repl--emit-multiple-values-message values level url line))))
(defun jade-repl--emit-message-values (message)
"Emit all values of console MESSAGE."
(let ((text (map-elt message 'text))
(values (map-elt message 'parameters))
(url (map-elt message 'url))
(line (map-elt message 'line)))
(when (seq-empty-p values)
(setq values `(((type . "string")
(description . ,text)))))
(jade-render-values values "\n")
(jade-repl--emit-message-url-line url line)))
(defun jade-repl--message-level-error-p (level)
(string= level "error"))
@ -253,34 +247,15 @@ message."
(defun jade-repl--emit-logging-level (level)
(unless (string-empty-p level)
(propertize (format "%s: " level)
(propertize (format "%s:" level)
'font-lock-face (jade-repl-level-face level)
'rear-nonsticky '(font-lock-face))))))
(defun jade-repl--emit-single-value-message (value level url line)
"Emit a single VALUE.
Used when there is only one value in the console message, for
example `console.log(1)'."
(jade-repl--emit-logging-level level)
(jade-render-value value (jade-repl--message-level-error-p level))
(insert (jade-repl--format-url-line "\n" url line)))
(defun jade-repl--emit-multiple-values-message (values level url line)
"Emit values when there is more than one value in the console message.
This is the case for instance with `console.log(1, 2, 3)'."
(jade-repl--emit-logging-level level)
(seq-do (lambda (value)
(insert "\n ")
(jade-render-value value nil))
(insert (jade-repl--format-url-line "\n" url line)))
(defun jade-repl--format-url-line (whitespace url line)
(if (or (null url) (string-empty-p url))
(concat whitespace
'rear-nonsticky '(font-lock-face)))
" ")))
(defun jade-repl--emit-message-url-line (url line)
(unless (seq-empty-p url)
(insert "\nMessage from "
(propertize (format "%s:%s" (file-name-nondirectory url) line)
'font-lock-face 'jade-link-face
'jade-action (lambda ()