Skip to content

Commit ce913e8

Browse files
vspinubbatsov
authored andcommitted
Highlight user root namespaces in REPL stacktraces
1 parent 44dc350 commit ce913e8

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

cider-interaction.el

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,7 @@ window."
10811081
(not (string= cur-ns-form
10821082
(buffer-local-value 'cider--last-ns-form connection)))
10831083
(not (cider-ns-form-p form)))
1084+
(cider-repl--cache-ns-roots cur-ns-form connection)
10841085
(when cider-auto-track-ns-form-changes
10851086
;; The first interactive eval on a file can load a lot of libs. This can
10861087
;; easily lead to more than 10 sec.
@@ -1612,6 +1613,7 @@ ClojureScript REPL exists for the project, it is evaluated in both REPLs."
16121613
(lambda (connection)
16131614
(with-current-buffer connection
16141615
(setq cider--last-ns-form ns-form))
1616+
(cider-repl--cache-ns-roots ns-form connection)
16151617
(cider-request:load-file (cider-file-string filename)
16161618
(funcall cider-to-nrepl-filename-function
16171619
(cider--server-filename filename))

cider-repl.el

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -550,32 +550,43 @@ When there is a possible unfinished ansi control sequence,
550550
(insert-before-markers (cadr ansi-color-context))
551551
(setq ansi-color-context nil)))
552552

553-
(defvar cider-repl--root-ns-highlitht-template "\\<%s[^$/: \t\n]+"
553+
(defvar cider-repl--root-ns-highlitht-template "\\<\\(%s\\)[^$/: \t\n]+"
554554
"Regexp used to highlight root ns in REPL buffers.")
555555

556556
(defvar-local cider-repl--root-ns-regexp nil
557557
"Cache of root ns regexp in REPLs.")
558558

559+
(defvar-local cider-repl--ns-roots nil
560+
"List holding all past root namespaces seen during interactive eval.")
561+
562+
(defun cider-repl--cache-ns-roots (ns-form connection)
563+
"Given NS-FORM cache root ns in CONNECTION."
564+
(with-current-buffer connection
565+
(when (string-match "^[ \t\n]*\(ns[ \t\n]+\\([^. \t\n]+\\)" ns-form)
566+
(let ((root (match-string-no-properties 1 ns-form)))
567+
(unless (member root cider-repl--ns-roots)
568+
(push root cider-repl--ns-roots)
569+
(let ((roots (mapconcat
570+
;; Replace _ or - with regexp patter to accommodate "raw" namespaces
571+
(lambda (r) (replace-regexp-in-string "[_-]+" "[_-]+" r))
572+
cider-repl--ns-roots "\\|")))
573+
(setq cider-repl--root-ns-regexp
574+
(format cider-repl--root-ns-highlitht-template roots))))))))
575+
559576
(defun cider-repl--apply-current-project-color (string)
560577
"Fontify project's root namespace to make stacktraces more readable.
561578
Foreground of `cider-stacktrace-ns-face' is used to propertize matched
562579
namespaces. STRING is REPL's output."
563-
(if (null nrepl-project-dir)
564-
string
565-
(unless cider-repl--root-ns-regexp
566-
(let ((root (file-name-nondirectory (directory-file-name nrepl-project-dir))))
567-
(setq cider-repl--root-ns-regexp
568-
;; Replace _ or - with regexp patter to accommodate "raw" namespaces
569-
(format cider-repl--root-ns-highlitht-template
570-
(replace-regexp-in-string "[_-]+" "[_-]+" root)))))
571-
(let ((start 0)
572-
(end 0))
573-
(while (setq start (string-match cider-repl--root-ns-regexp string end))
574-
(setq end (match-end 0))
575-
(let ((face-spec (list (cons 'foreground-color
576-
(face-attribute 'cider-stacktrace-ns-face :foreground nil t)))))
577-
(font-lock-prepend-text-property start end 'face face-spec string)))
578-
string)))
580+
(if cider-repl--root-ns-regexp
581+
(let ((start 0)
582+
(end 0))
583+
(while (setq start (string-match cider-repl--root-ns-regexp string end))
584+
(setq end (match-end 0))
585+
(let ((face-spec (list (cons 'foreground-color
586+
(face-attribute 'cider-stacktrace-ns-face :foreground nil t)))))
587+
(font-lock-prepend-text-property start end 'face face-spec string)))
588+
string)
589+
string))
579590

580591
(defun cider-repl--emit-output-at-pos (buffer string output-face position &optional bol)
581592
"Using BUFFER, insert STRING (applying to it OUTPUT-FACE) at POSITION.
@@ -972,7 +983,7 @@ namespace to switch to."
972983
Each element in the alist has the form (NAME REGEXP HIGHLIGHT VAR FILE
973984
LINE), where NAME is the identifier of the regexp, REGEXP - regexp matching
974985
a location, HIGHLIGHT - sub-expression matching region to highlight on
975-
mouse-over, VAR - sub-expression giving Clojure VAR to look up. FILE is
986+
mouse-over, VAR - sub-expression giving Clojure VAR to look up. FILE is
976987
currently only used when VAR is nil and must be full resource path in that
977988
case."
978989
:type '(alist :key-type sexp)

0 commit comments

Comments
 (0)