A JavaScript development environment for Emacs https://indium.readthedocs.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.9 KiB

;;; jade-render.el --- Helper functions to display JS objects in buffers -*- lexical-binding: t; -*-
;; Copyright (C) 2016 Nicolas Petton
;; Author: Nicolas Petton <nicolas@petton.fr>
;; Keywords:
;; 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
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Helper functions for inserting content in jade buffers.
;;; Code:
(require 'jade-faces)
(declare 'jade-backend-object-reference-p)
(defun jade-render-value (value &optional error)
(if (jade-backend-object-reference-p value)
(jade-render-object-link value error)
(jade-render-description value error)))
(defun jade-render-description (value &optional error)
(let ((description (jade-description-string value))
(face (when error 'jade-repl-error-face)))
(propertize description
'font-lock-face (or face 'jade-repl-stdout-face)
'rear-nonsticky '(font-lock-face)))))
(defun jade-render-keyword (string)
(propertize string
'font-lock-face 'jade-keyword-face
'rear-nonsticky '(font-lock-face))))
(defun jade-render-button (string action)
"Render a button with the label STRING.
When clicked, evaluate ACTION.
ACTION should be a function that takes no argument."
(propertize string
'font-lock-face 'jade-button-face
'jade-action action
'reat-nons '(font-lock-face jade-action))))
(defun jade-description-string (value &optional full)
"Return a short string describing VALUE.
When FULL is non-nil, do not strip long descriptions and function
(let ((description (map-elt value 'description))
(type (map-elt value 'type)))
;; Showing the source code of the function is too verbose
(if (and (not full) (eq type 'function))
(defun jade-render-object-link (value error)
(let* ((description (jade-description-string value))
(preview (map-elt value 'preview))
(beg (point))
(end (progn
(insert description)
(face (if error
(set-text-properties beg end
`(font-lock-face ,face
mouse-face highlight
jade-reference ,value))
(when preview
(insert preview))))
(defun jade-render-properties (properties)
(seq-map #'jade-render-property
(seq-sort (lambda (p1 p2)
(string< (map-elt p1 'name)
(map-elt p2 'name)))
(defun jade-render-property (property)
(insert " " (map-elt property 'name) ": ")
(jade-render-value (map-elt property 'value))
(insert "\n"))
(declare #'jade-inspector-inspect)
(defun jade-follow-link ()
"Follow the link at point."
(let ((reference (get-text-property (point) 'jade-reference))
(action (get-text-property (point) 'jade-action)))
(reference (jade-inspector-inspect reference))
(action (funcall action)))))
(defun jade-perform-action ()
"Evaluate the button action at point."
(let ((function (get-text-property (point) 'jade-action)))
(funcall function)))
(provide 'jade-render)
;;; jade-render.el ends here