Skip to content

Commit 6b1cab6

Browse files
committed
extract with_highlight_chores
1 parent 14b5a65 commit 6b1cab6

File tree

4 files changed

+255
-256
lines changed

4 files changed

+255
-256
lines changed

fnl/leap/highlight.fnl

+6-16
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,13 @@
3434
(vim.fn.line "w$"))))
3535

3636

37-
(fn M.apply-backdrop [self backward? ?target-windows]
37+
(fn M.apply-backdrop [self ranges]
3838
(when (pcall api.nvim_get_hl_by_name self.group.backdrop false) ; group exists?
39-
(if ?target-windows
40-
(each [_ winid (ipairs ?target-windows)]
41-
(local wininfo (. (vim.fn.getwininfo winid) 1))
42-
(vim.highlight.range wininfo.bufnr self.ns self.group.backdrop
43-
[(dec wininfo.topline) 0]
44-
[(dec wininfo.botline) -1]
45-
{:priority self.priority.backdrop}))
46-
(let [[curline curcol] (map dec [(vim.fn.line ".") (vim.fn.col ".")])
47-
[win-top win-bot] [(dec (vim.fn.line "w0")) (dec (vim.fn.line "w$"))]
48-
[start finish] (if backward?
49-
[[win-top 0] [curline curcol]]
50-
[[curline (inc curcol)] [win-bot -1]])]
51-
; Expects 0,0-indexed args; `finish` is exclusive.
52-
(vim.highlight.range 0 self.ns self.group.backdrop start finish
53-
{:priority self.priority.backdrop})))))
39+
(each [_ range (ipairs ranges)]
40+
(vim.highlight.range range.bufnr self.ns self.group.backdrop
41+
[range.startrow range.startcol]
42+
[range.endrow range.endcol]
43+
{:priority self.priority.backdrop}))))
5444

5545

5646
(fn M.highlight-cursor [self ?pos]

fnl/leap/main.fnl

+41-19
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,17 @@ is either labeled (C) or not (B).
357357
(when aot?
358358
(resolve-conflicts targets)))))
359359

360-
361-
(fn light-up-beacons [targets ?start ?end]
362-
(for [i (or ?start 1) (or ?end (length targets))]
360+
(fn with-highlight-chores [opts task]
361+
(do (local opts (or opts {}))
362+
(hl:cleanup opts.hl-affected-windows)
363+
(hl:apply-backdrop (or opts.backdrop-ranges []))
364+
(task)
365+
(hl:highlight-cursor)
366+
(vim.cmd :redraw)))
367+
368+
(fn light-up-beacons [targets opts]
369+
(local opts (or opts {}))
370+
(for [i (or opts.start 1) (or opts.end (length targets))]
363371
(local target (. targets i))
364372
(case target.beacon
365373
[offset virttext]
@@ -486,13 +494,29 @@ is either labeled (C) or not (B).
486494
(when vars.errmsg (echo vars.errmsg))
487495
(exit)))
488496

489-
(macro with-highlight-chores [...]
490-
`(do (hl:cleanup hl-affected-windows)
491-
(when-not count
492-
(hl:apply-backdrop backward? ?target-windows))
493-
(do ,...)
494-
(hl:highlight-cursor)
495-
(vim.cmd :redraw)))
497+
(local backdrop-ranges [])
498+
(when (and (pcall api.nvim_get_hl_by_name hl.group.backdrop false) (not count))
499+
(if ?target-windows
500+
(each [_ winid (ipairs ?target-windows)]
501+
(local wininfo (. (vim.fn.getwininfo winid) 1))
502+
(local range {:bufnr wininfo.bufnr
503+
:startrow (dec wininfo.topline)
504+
:startcol 0
505+
:endrow (dec wininfo.botline)
506+
:endcol -1})
507+
(table.insert backdrop-ranges range))
508+
(let [[curline curcol] (map dec [(vim.fn.line ".") (vim.fn.col ".")])
509+
[win-top win-bot] [(dec (vim.fn.line "w0")) (dec (vim.fn.line "w$"))]
510+
[startrow startcol endrow endcol] (if backward?
511+
[win-top 0 curline curcol]
512+
[curline (inc curcol) win-bot -1])]
513+
(local wininfo (. (vim.fn.getwininfo 0) 1))
514+
(local range {:bufnr wininfo.bufnr
515+
:startrow startrow
516+
:startcol startcol
517+
:endrow endrow
518+
:endcol endcol})
519+
(table.insert backdrop-ranges range))))
496520

497521
; Helper functions ///
498522

@@ -644,7 +668,7 @@ is either labeled (C) or not (B).
644668
(values start end))))
645669

646670
(fn get-first-pattern-input []
647-
(with-highlight-chores (echo "")) ; clean up the command line
671+
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (fn [] (echo ""))) ; clean up the command line
648672
(case (get-input-by-keymap prompt)
649673
; Here we can handle any other modifier key as "zeroth" input,
650674
; if the need arises.
@@ -663,7 +687,7 @@ is either labeled (C) or not (B).
663687
; char<enter> partial input (but it implies not needing
664688
; to show beacons).
665689
(not count))
666-
(with-highlight-chores (light-up-beacons targets)))
690+
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (fn [] (light-up-beacons targets))))
667691
(get-input-by-keymap prompt))
668692

669693
(fn get-full-pattern-input []
@@ -685,9 +709,8 @@ is either labeled (C) or not (B).
685709
(when targets.label-set
686710
(set-label-states targets {: group-offset}))
687711
(set-beacons targets {:aot? vars.aot? : no-labels? : user-given-targets?})
688-
(with-highlight-chores
689-
(local (start end) (get-highlighted-idx-range targets no-labels?))
690-
(light-up-beacons targets start end)))
712+
(local (start end) (get-highlighted-idx-range targets no-labels?))
713+
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (fn [] (light-up-beacons targets {: start : end}))))
691714
; ---
692715
(fn loop [group-offset first-invoc?]
693716
(display group-offset)
@@ -751,9 +774,8 @@ is either labeled (C) or not (B).
751774
; ---
752775
(fn display []
753776
(set-beacons targets {: no-labels? :aot? vars.aot? : user-given-targets?})
754-
(with-highlight-chores
755-
(local (start end) (get-highlighted-idx-range targets no-labels?))
756-
(light-up-beacons targets start end)))
777+
(local (start end) (get-highlighted-idx-range targets no-labels?))
778+
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (fn [] (light-up-beacons targets {: start : end}))))
757779
; ---
758780
(fn get-new-idx [idx in]
759781
(if (contains? spec-keys.next_target in) (min (inc idx) (length targets))
@@ -865,7 +887,7 @@ is either labeled (C) or not (B).
865887
targets**
866888
; The action callback should expect a list in this case.
867889
; It might also get user input, so keep the beacons highlighted.
868-
(do (with-highlight-chores (light-up-beacons targets**))
890+
(do (with-highlight-chores {: backdrop-ranges : hl-affected-windows} (fn [] (light-up-beacons targets**)))
869891
(do-action targets**)))
870892
(exit))
871893

lua/leap/highlight.lua

+24-45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)