Skip to content

Commit

Permalink
extract with_highlight_chores
Browse files Browse the repository at this point in the history
  • Loading branch information
rish987 committed Jun 3, 2023
1 parent 14b5a65 commit 6b1cab6
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 256 deletions.
22 changes: 6 additions & 16 deletions fnl/leap/highlight.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,13 @@
(vim.fn.line "w$"))))


(fn M.apply-backdrop [self backward? ?target-windows]
(fn M.apply-backdrop [self ranges]
(when (pcall api.nvim_get_hl_by_name self.group.backdrop false) ; group exists?
(if ?target-windows
(each [_ winid (ipairs ?target-windows)]
(local wininfo (. (vim.fn.getwininfo winid) 1))
(vim.highlight.range wininfo.bufnr self.ns self.group.backdrop
[(dec wininfo.topline) 0]
[(dec wininfo.botline) -1]
{:priority self.priority.backdrop}))
(let [[curline curcol] (map dec [(vim.fn.line ".") (vim.fn.col ".")])
[win-top win-bot] [(dec (vim.fn.line "w0")) (dec (vim.fn.line "w$"))]
[start finish] (if backward?
[[win-top 0] [curline curcol]]
[[curline (inc curcol)] [win-bot -1]])]
; Expects 0,0-indexed args; `finish` is exclusive.
(vim.highlight.range 0 self.ns self.group.backdrop start finish
{:priority self.priority.backdrop})))))
(each [_ range (ipairs ranges)]
(vim.highlight.range range.bufnr self.ns self.group.backdrop
[range.startrow range.startcol]
[range.endrow range.endcol]
{:priority self.priority.backdrop}))))


(fn M.highlight-cursor [self ?pos]
Expand Down
60 changes: 41 additions & 19 deletions fnl/leap/main.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,17 @@ is either labeled (C) or not (B).
(when aot?
(resolve-conflicts targets)))))


(fn light-up-beacons [targets ?start ?end]
(for [i (or ?start 1) (or ?end (length targets))]
(fn with-highlight-chores [opts task]
(do (local opts (or opts {}))
(hl:cleanup opts.hl-affected-windows)
(hl:apply-backdrop (or opts.backdrop-ranges []))
(task)
(hl:highlight-cursor)
(vim.cmd :redraw)))

(fn light-up-beacons [targets opts]
(local opts (or opts {}))
(for [i (or opts.start 1) (or opts.end (length targets))]
(local target (. targets i))
(case target.beacon
[offset virttext]
Expand Down Expand Up @@ -486,13 +494,29 @@ is either labeled (C) or not (B).
(when vars.errmsg (echo vars.errmsg))
(exit)))

(macro with-highlight-chores [...]
`(do (hl:cleanup hl-affected-windows)
(when-not count
(hl:apply-backdrop backward? ?target-windows))
(do ,...)
(hl:highlight-cursor)
(vim.cmd :redraw)))
(local backdrop-ranges [])
(when (and (pcall api.nvim_get_hl_by_name hl.group.backdrop false) (not count))
(if ?target-windows
(each [_ winid (ipairs ?target-windows)]
(local wininfo (. (vim.fn.getwininfo winid) 1))
(local range {:bufnr wininfo.bufnr
:startrow (dec wininfo.topline)
:startcol 0
:endrow (dec wininfo.botline)
:endcol -1})
(table.insert backdrop-ranges range))
(let [[curline curcol] (map dec [(vim.fn.line ".") (vim.fn.col ".")])
[win-top win-bot] [(dec (vim.fn.line "w0")) (dec (vim.fn.line "w$"))]
[startrow startcol endrow endcol] (if backward?
[win-top 0 curline curcol]
[curline (inc curcol) win-bot -1])]
(local wininfo (. (vim.fn.getwininfo 0) 1))
(local range {:bufnr wininfo.bufnr
:startrow startrow
:startcol startcol
:endrow endrow
:endcol endcol})
(table.insert backdrop-ranges range))))

; Helper functions ///

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

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

(fn get-full-pattern-input []
Expand All @@ -685,9 +709,8 @@ is either labeled (C) or not (B).
(when targets.label-set
(set-label-states targets {: group-offset}))
(set-beacons targets {:aot? vars.aot? : no-labels? : user-given-targets?})
(with-highlight-chores
(local (start end) (get-highlighted-idx-range targets no-labels?))
(light-up-beacons targets start end)))
(local (start end) (get-highlighted-idx-range targets no-labels?))
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (fn [] (light-up-beacons targets {: start : end}))))
; ---
(fn loop [group-offset first-invoc?]
(display group-offset)
Expand Down Expand Up @@ -751,9 +774,8 @@ is either labeled (C) or not (B).
; ---
(fn display []
(set-beacons targets {: no-labels? :aot? vars.aot? : user-given-targets?})
(with-highlight-chores
(local (start end) (get-highlighted-idx-range targets no-labels?))
(light-up-beacons targets start end)))
(local (start end) (get-highlighted-idx-range targets no-labels?))
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (fn [] (light-up-beacons targets {: start : end}))))
; ---
(fn get-new-idx [idx in]
(if (contains? spec-keys.next_target in) (min (inc idx) (length targets))
Expand Down Expand Up @@ -865,7 +887,7 @@ is either labeled (C) or not (B).
targets**
; The action callback should expect a list in this case.
; It might also get user input, so keep the beacons highlighted.
(do (with-highlight-chores (light-up-beacons targets**))
(do (with-highlight-chores {: backdrop-ranges : hl-affected-windows} (fn [] (light-up-beacons targets**)))
(do-action targets**)))
(exit))

Expand Down
69 changes: 24 additions & 45 deletions lua/leap/highlight.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6b1cab6

Please sign in to comment.