Skip to content

Commit

Permalink
Inspector: render Java items using java-mode syntax coloring (#3547)
Browse files Browse the repository at this point in the history
Fixes #3546
  • Loading branch information
vemv authored Oct 23, 2023
1 parent 4c99c02 commit ded24d8
Show file tree
Hide file tree
Showing 4 changed files with 407 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Changes

- [#3546](https://github.com/clojure-emacs/cider/issues/3546): Inspector: render Java items using `java-mode` syntax coloring.

### Bugs fixed

- Inspector: avoid `Symbol's value as variable is void: text-scale-mode-amount` under certain Emacs clients.
Expand Down
10 changes: 1 addition & 9 deletions cider-docstring.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,11 @@
(require 'cl-lib)
(require 'shr)

(defun cider--to-java-string (s)
"Convert string S to a Java-formatted string with syntax highlighting."
(with-temp-buffer
(insert s)
(java-mode)
(font-lock-ensure)
(buffer-string)))

(defsubst cider--render-pre* (dom)
"Render DOM nodes, formatting them them as Java if they are strings."
(dolist (sub (dom-children dom))
(if (stringp sub)
(shr-insert (cider--to-java-string sub))
(shr-insert (cider-font-lock-as 'java-mode sub))
(shr-descend sub))))

(defun cider--render-pre (dom)
Expand Down
36 changes: 29 additions & 7 deletions cider-inspector.el
Original file line number Diff line number Diff line change
Expand Up @@ -427,20 +427,42 @@ MAX-COLL-SIZE if non nil."
(error (insert "\nInspector error for: " str))))
(goto-char (point-min))))

(defvar cider-inspector-looking-at-java-p nil)

(defun cider-inspector-render* (elements)
"Render ELEMENTS."
(setq cider-inspector-looking-at-java-p nil)
(dolist (el elements)
(cider-inspector-render-el* el)))

(defconst cider--inspector-java-headers
'("--- Interfaces:" "--- Constructors:" "--- Fields:" "--- Methods:" "--- Imports:"))

(defun cider-inspector-render-el* (el)
"Render EL."
(cond ((symbolp el) (insert (symbol-name el)))
((stringp el) (insert (propertize el 'font-lock-face 'font-lock-keyword-face)))
((and (consp el) (eq (car el) :newline))
(insert "\n"))
((and (consp el) (eq (car el) :value))
(cider-inspector-render-value (cadr el) (cl-caddr el)))
(t (message "Unrecognized inspector object: %s" el))))
(let ((header-p (or (member el cider--inspector-java-headers)
(and (stringp el)
(string-prefix-p "--- " el)))))
;; Headers reset the Java syntax coloring:
(when header-p
(setq cider-inspector-looking-at-java-p nil))

(cond ((symbolp el) (insert (symbol-name el)))
((stringp el) (insert (if cider-inspector-looking-at-java-p
(cider-font-lock-as 'java-mode el)
(propertize el 'font-lock-face (if header-p
'font-lock-comment-face
'font-lock-keyword-face)))))
((and (consp el) (eq (car el) :newline))
(insert "\n"))
((and (consp el) (eq (car el) :value))
(cider-inspector-render-value (cadr el) (cl-caddr el)))
(t (message "Unrecognized inspector object: %s" el))))

;; Java-related headers indicate that the next elements to be rendered
;; should be syntax-colored as Java:
(when (member el cider--inspector-java-headers)
(setq cider-inspector-looking-at-java-p t)))

(defun cider-inspector-render-value (value idx)
"Render VALUE at IDX."
Expand Down
Loading

0 comments on commit ded24d8

Please sign in to comment.