Skip to content

Commit 2ba7a0d

Browse files
rfkmcap10morgan
authored andcommitted
Use text properties instead of overlays for ANSI coloring
1 parent 194813e commit 2ba7a0d

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Changelog
22

3-
## master (unreleased)
4-
53
## 0.10.1 / (unreleased)
64

5+
### Changes
6+
7+
* [#1500](https://github.com/clojure-emacs/cider/pull/1500): Improve the performance of REPL buffers by using text properties instead of overlays for ANSI coloring.
8+
79
### Bugs fixed
810

911
* [#1480](https://github.com/clojure-emacs/cider/issues/1480): Fix an error in `cider-restart` caused by not using the REPL's buffer as the current connection when calling `cider-current-connection` from REPL buffer.
@@ -12,9 +14,8 @@
1214
* [#1466](https://github.com/clojure-emacs/cider/issues/1466): Correctly font-lock pretty-printed results in the REPL.
1315
* [#1475](https://github.com/clojure-emacs/cider/pull/1475): Fix `args-out-of-range` error in `cider--get-symbol-indent`.
1416
* [#1479](https://github.com/clojure-emacs/cider/pull/1479): Make paredit and `cider-repl-mode` play nice.
15-
* [#1452](https://github.com/clojure-emacs/cider/issues/1452): Prevent ANSI color overlays in the REPL buffer from being extended.
17+
* [#1452](https://github.com/clojure-emacs/cider/issues/1452): Fix wrong ANSI coloring in the REPL buffer.
1618
* [#1486](https://github.com/clojure-emacs/cider/issues/1486): Complete a partial fix in stacktrace font-locking.
17-
* [#1488](https://github.com/clojure-emacs/cider/pull/1488): Delete zombie overlays in the REPL buffer.
1819
* [#1482](https://github.com/clojure-emacs/cider/issues/1482): Clear nREPL sessions when a connection is closed.
1920
* [#1435](https://github.com/clojure-emacs/cider/issues/1435): Improve error display in cider-test.
2021
* [#1379](https://github.com/clojure-emacs/cider/issues/1379): Fix test highlighting at start of line.

cider-repl.el

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -469,34 +469,21 @@ If BOL is non-nil insert at the beginning of line."
469469
(goto-char position)
470470
;; TODO: Review the need for bol
471471
(when (and bol (not (bolp))) (insert-before-markers "\n"))
472-
(cider-propertize-region `(font-lock-face ,output-face
473-
rear-nonsticky (font-lock-face))
474-
(insert-before-markers string)
475-
(when (and (= (point) cider-repl-prompt-start-mark)
476-
(not (bolp)))
477-
(insert-before-markers "\n")
478-
(set-marker cider-repl-output-end (1- (point))))))))
472+
(insert-before-markers (ansi-color-apply (propertize string
473+
'font-lock-face output-face
474+
'rear-nonsticky '(font-lock-face))))
475+
(when (and (= (point) cider-repl-prompt-start-mark)
476+
(not (bolp)))
477+
(insert-before-markers "\n")
478+
(set-marker cider-repl-output-end (1- (point)))))))
479479
(cider-repl--show-maximum-output)))
480480

481481
(defun cider-repl--emit-interactive-output (string face)
482482
"Emit STRING as interactive output using FACE."
483483
(with-current-buffer (cider-current-repl-buffer)
484484
(let ((pos (cider-repl--end-of-line-before-input-start))
485485
(string (replace-regexp-in-string "\n\\'" "" string)))
486-
(cider-repl--emit-output-at-pos (current-buffer) string face pos t)
487-
(ansi-color-apply-on-region pos (point-max))
488-
489-
;; If the output has a trailing overlay created by ansi-color, it would be
490-
;; extended by the following outputs. To avoid this, ansi-color adds
491-
;; `ansi-color-freeze-overlay' to the `modification-hooks', but it never
492-
;; seems to be called. By using `insert-behind-hooks' instead, we can make
493-
;; it work. For more details, please see
494-
;; https://github.com/clojure-emacs/cider/issues/1452
495-
(dolist (ov (overlays-at (1- (cider-repl--end-of-line-before-input-start))))
496-
;; Ensure ov is created by ansi-color
497-
(when (and (member #'ansi-color-freeze-overlay (overlay-get ov 'modification-hooks))
498-
(not (member #'ansi-color-freeze-overlay (overlay-get ov 'insert-behind-hooks))))
499-
(push #'ansi-color-freeze-overlay (overlay-get ov 'insert-behind-hooks)))))))
486+
(cider-repl--emit-output-at-pos (current-buffer) string face pos t))))
500487

501488
(defun cider-repl-emit-interactive-stdout (string)
502489
"Emit STRING as interactive output."
@@ -521,9 +508,7 @@ FORMAT is a format string to compile with ARGS and display on the REPL."
521508
"Using BUFFER, emit STRING font-locked with FACE.
522509
If BOL is non-nil, emit at the beginning of the line."
523510
(with-current-buffer buffer
524-
(let ((pos (cider-repl--end-of-line-before-input-start)))
525-
(cider-repl--emit-output-at-pos buffer string face cider-repl-input-start-mark bol)
526-
(ansi-color-apply-on-region pos (point-max)))))
511+
(cider-repl--emit-output-at-pos buffer string face cider-repl-input-start-mark bol)))
527512

528513
(defun cider-repl-emit-stdout (buffer string)
529514
"Using BUFFER, emit STRING as standard output."

test/cider-tests.el

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,23 @@
517517
(should (equal (cider-repl-prompt-abbreviated "some.pretty.long.namespace.name")
518518
"s.p.l.n.name> ")))
519519

520+
(ert-deftest test-cider-repl--emit-output-at-pos-with-ansi-code ()
521+
(with-temp-buffer
522+
(let* ((ansi-color-names-vector ["black" "red3" "green3" "yellow3" "blue2" "magenta3" "cyan3" "gray90"])
523+
(ansi-color-map (ansi-color-make-color-map)))
524+
(cider-repl-reset-markers)
525+
526+
(cider-repl--emit-output-at-pos (current-buffer) "a" 'cider-repl-stdout-face (point))
527+
(cider-repl--emit-output-at-pos (current-buffer) "b" 'cider-repl-stdout-face (point))
528+
(cider-repl--emit-output-at-pos (current-buffer) "c" 'cider-repl-stdout-face (point))
529+
(cider-repl--emit-output-at-pos (current-buffer) "d" 'cider-repl-stdout-face (point))
530+
531+
(should (string= (buffer-string) "a\nb\nc\nd\n"))
532+
(should (equal (get-text-property 1 'font-lock-face) '(foreground-color . "black")))
533+
(should (equal (get-text-property 3 'font-lock-face) 'cider-repl-stdout-face))
534+
(should (equal (get-text-property 5 'font-lock-face) '(foreground-color . "red3")))
535+
(should (equal (get-text-property 7 'font-lock-face) '(foreground-color . "red3"))))))
536+
520537
(ert-deftest test-cider--url-to-file ()
521538
(should (equal "/space test" (cider--url-to-file "file:/space%20test")))
522539
(should (equal "C:/space test" (cider--url-to-file "file:/C:/space%20test"))))

0 commit comments

Comments
 (0)