Skip to content

Commit 493b49a

Browse files
committed
extract with_highlight_chores
1 parent 6f29127 commit 493b49a

File tree

4 files changed

+247
-261
lines changed

4 files changed

+247
-261
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
@@ -355,9 +355,17 @@ is either labeled (C) or not (B).
355355
(when aot?
356356
(resolve-conflicts targets)))))
357357

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

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

496520
; Helper functions ///
497521

@@ -643,7 +667,7 @@ is either labeled (C) or not (B).
643667
(values start end))))
644668

645669
(fn get-first-pattern-input []
646-
(with-highlight-chores (echo "")) ; clean up the command line
670+
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (echo "")) ; clean up the command line
647671
(case (get-input-by-keymap prompt)
648672
; Here we can handle any other modifier key as "zeroth" input,
649673
; if the need arises.
@@ -657,7 +681,7 @@ is either labeled (C) or not (B).
657681

658682
(fn get-second-pattern-input [targets]
659683
(when (<= (length targets) max-phase-one-targets)
660-
(with-highlight-chores (light-up-beacons targets)))
684+
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (light-up-beacons targets)))
661685
(get-input-by-keymap prompt))
662686

663687
(fn get-full-pattern-input []
@@ -679,9 +703,8 @@ is either labeled (C) or not (B).
679703
(when targets.label-set
680704
(set-label-states targets {: group-offset}))
681705
(set-beacons targets {:aot? vars.aot? : no-labels? : user-given-targets?})
682-
(with-highlight-chores
683-
(local (start end) (get-highlighted-idx-range targets no-labels?))
684-
(light-up-beacons targets start end)))
706+
(local (start end) (get-highlighted-idx-range targets no-labels?))
707+
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (light-up-beacons targets {: start : end})))
685708
; ---
686709
(fn loop [group-offset first-invoc?]
687710
(display group-offset)
@@ -745,9 +768,8 @@ is either labeled (C) or not (B).
745768
; ---
746769
(fn display []
747770
(set-beacons targets {: no-labels? :aot? vars.aot? : user-given-targets?})
748-
(with-highlight-chores
749-
(local (start end) (get-highlighted-idx-range targets no-labels?))
750-
(light-up-beacons targets start end)))
771+
(local (start end) (get-highlighted-idx-range targets no-labels?))
772+
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (light-up-beacons targets {: start : end})))
751773
; ---
752774
(fn get-new-idx [idx in]
753775
(if (contains? spec-keys.next_target in) (min (inc idx) (length targets))
@@ -854,7 +876,7 @@ is either labeled (C) or not (B).
854876
targets**
855877
; The action callback should expect a list in this case.
856878
; It might also get user input, so keep the beacons highlighted.
857-
(do (with-highlight-chores (light-up-beacons targets**))
879+
(do (with-highlight-chores {: backdrop-ranges : hl-affected-windows} (light-up-beacons targets**))
858880
(do-action targets**)))
859881
(exit))
860882

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)