Skip to content

Commit 69dfe05

Browse files
committed
lispy.el: "xv" will nicely fontify the test
* lispy.el (lispy-test-face): New face for test background. (lispy-cursor-face): New face for the cursor in the test overlay. (lispy--fontify): New function to generate a pretty printed test. (lispy-view-test): Use `lispy--fontify'
1 parent 46eb6f9 commit 69dfe05

File tree

1 file changed

+91
-4
lines changed

1 file changed

+91
-4
lines changed

lispy.el

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,17 @@ The hint will consist of the possible nouns that apply to the verb."
230230
"Face for Elisp commands."
231231
:group 'lispy-faces)
232232

233+
(defface lispy-test-face
234+
'((t (:background "#f8f2ac")))
235+
"Face for `lispy-view-test'."
236+
:group 'lispy-faces)
237+
238+
(defface lispy-cursor-face
239+
'((t
240+
(:background "#000000" :foreground "#ffffff")))
241+
"Face for `lispy-view-test'."
242+
:group 'lispy-faces)
243+
233244
(defface lispy-occur-face
234245
'((t (:background "#CECEFF")))
235246
"Face for `lispy-occur' matches."
@@ -3242,6 +3253,82 @@ If the region is active, replace instead of yanking."
32423253
(looking-at lispy-left))
32433254
(insert " "))))
32443255

3256+
(defun lispy--fontify (str)
3257+
"Return STR fontified in `emacs-lisp-mode'"
3258+
(with-temp-buffer
3259+
(emacs-lisp-mode)
3260+
(show-paren-mode)
3261+
(insert str)
3262+
(font-lock-fontify-buffer)
3263+
(let ((color-paren (face-attribute 'show-paren-match :background))
3264+
(color-cursor-fg (face-attribute 'lispy-cursor-face :foreground))
3265+
(color-cursor-bg (face-attribute 'lispy-cursor-face :background))
3266+
pt mk p1 p2)
3267+
(goto-char (point-min))
3268+
(when (search-forward "|" nil t)
3269+
(backward-delete-char 1)
3270+
(setq pt (point))
3271+
(when (eolp)
3272+
(insert " ")))
3273+
(goto-char (point-min))
3274+
(when (search-forward "~" nil t)
3275+
(backward-delete-char 1)
3276+
(setq mk (point))
3277+
(when (< mk pt)
3278+
(decf pt)))
3279+
(goto-char pt)
3280+
(cond ((looking-back lispy-right)
3281+
(setq p2 (1- (point)))
3282+
(lispy-different)
3283+
(setq p1 (point)))
3284+
((looking-at lispy-left)
3285+
(setq p1 (point))
3286+
(lispy-different)
3287+
(setq p2 (1- (point)))))
3288+
(setq str (buffer-string))
3289+
(add-face-text-property 0 (length str) '(face 'lispy-test-face) t str)
3290+
(when pt
3291+
(when mk
3292+
(if (< mk pt)
3293+
(progn
3294+
(add-text-properties (1- mk) (1- pt) '(face region) str)
3295+
(set-text-properties (1- pt) pt '(face lispy-cursor-face) str))
3296+
(add-text-properties (1- (min pt mk)) (1- (max pt mk)) '(face region) str)
3297+
(set-text-properties (1- pt) pt '(face lispy-cursor-face) str)))
3298+
(when p1
3299+
(add-text-properties
3300+
(1- p1) p1
3301+
`(face (:background
3302+
,color-paren
3303+
:foreground
3304+
,(if (and mk
3305+
(>= p1 (min pt mk))
3306+
(<= p1 (max pt mk)))
3307+
color-cursor-fg
3308+
color-cursor-bg))) str))
3309+
(when p2
3310+
(add-text-properties
3311+
(1- p2) p2
3312+
`(face (:background
3313+
,color-paren
3314+
:foreground
3315+
,(if (and mk
3316+
(>= p2 (min pt mk))
3317+
(<= p2 (max pt mk)))
3318+
color-cursor-fg
3319+
color-cursor-bg)))
3320+
str))
3321+
(add-text-properties
3322+
(1- pt) pt
3323+
`(face (:background
3324+
,color-cursor-bg
3325+
:foreground
3326+
,(if (eq pt p1)
3327+
color-paren
3328+
color-cursor-fg)))
3329+
str)
3330+
str))))
3331+
32453332
(defun lispy-view-test ()
32463333
"View better the test at point."
32473334
(interactive)
@@ -3250,8 +3337,6 @@ If the region is active, replace instead of yanking."
32503337
(delete-overlay lispy-overlay)
32513338
(setq lispy-overlay nil))
32523339

3253-
3254-
32553340
((looking-at "(should (string=")
32563341
(setq lispy-hint-pos (point))
32573342
(let* ((expr (cadr (read (lispy--string-dwim))))
@@ -3261,11 +3346,13 @@ If the region is active, replace instead of yanking."
32613346
(sep (make-string (- (window-width)
32623347
(current-column)) ?-)))
32633348
(lispy--show
3264-
(concat (propertize str1 'face 'lispy-face-hint)
3349+
(concat "\n"
3350+
(lispy--fontify str1)
32653351
"\n" sep "\n"
32663352
(substring (prin1-to-string keys) 1 -1)
32673353
"\n" sep "\n"
3268-
(propertize str2 'face 'lispy-face-hint)))))
3354+
(lispy--fontify str2)
3355+
"\n"))))
32693356

32703357
(t
32713358
(lispy-complain "should position point before (should (string="))))

0 commit comments

Comments
 (0)