Skip to content

Commit

Permalink
Inspector: render Java items using java-mode syntax coloring
Browse files Browse the repository at this point in the history
Fixes #3546
  • Loading branch information
vemv committed Oct 21, 2023
1 parent 4c99c02 commit df7ca24
Show file tree
Hide file tree
Showing 3 changed files with 415 additions and 7 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
45 changes: 38 additions & 7 deletions cider-inspector.el
Original file line number Diff line number Diff line change
Expand Up @@ -429,21 +429,52 @@ MAX-COLL-SIZE if non nil."

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

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

(defun cider--to-java-string (s)
"Given `S`, returns a propertized string with Java syntax coloring."
(with-temp-buffer
(insert s)
(java-mode)
(font-lock-ensure)
(buffer-string)))

(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--to-java-string 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."
(setq cider-inspector-looking-at-java-p nil)
(cider-propertize-region
(list 'cider-value-idx idx
'mouse-face 'highlight)
Expand Down
Loading

0 comments on commit df7ca24

Please sign in to comment.