Skip to content

Commit

Permalink
Refactor log.fnl to remove aniseed nvim module
Browse files Browse the repository at this point in the history
saccarosium committed Dec 31, 2024

Verified

This commit was signed with the committer’s verified signature.
Heptazhou Heptazhou
1 parent 3208622 commit 1b4b1f6
Showing 2 changed files with 170 additions and 184 deletions.
125 changes: 56 additions & 69 deletions fnl/conjure/log.fnl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(local {: autoload} (require :nfnl.module))
(local a (autoload :conjure.aniseed.core))
(local nvim (autoload :conjure.aniseed.nvim))
(local str (autoload :conjure.aniseed.string))
(local buffer (autoload :conjure.buffer))
(local client (autoload :conjure.client))
@@ -19,7 +18,7 @@
:low-priority-spam {:streak 0
:help-displayed? false}}
:jump-to-latest {:mark nil
:ns (nvim.create_namespace "conjure_log_jump_to_latest")}})
:ns (vim.api.nvim_create_namespace "conjure_log_jump_to_latest")}})

(fn break []
(str.join
@@ -30,14 +29,14 @@
(str.join [(client.get :comment-prefix) "State: " (client.state-key)]))

(fn log-buf-name []
(str.join ["conjure-log-" (nvim.fn.getpid) (client.get :buf-suffix)]))
(str.join ["conjure-log-" (vim.fn.getpid) (client.get :buf-suffix)]))

(fn log-buf? [name]
(text.ends-with name (log-buf-name)))
(vim.endswith name (log-buf-name)))

(fn on-new-log-buf [buf]
(set state.jump-to-latest.mark
(nvim.buf_set_extmark buf state.jump-to-latest.ns 0 0 {}))
(vim.api.nvim_buf_set_extmark buf state.jump-to-latest.ns 0 0 {}))

(when (and vim.diagnostic (= false (config.get-in [:log :diagnostics])))
(if (= 1 (vim.fn.has "nvim-0.10"))
@@ -46,9 +45,9 @@

(when (and vim.treesitter (= false (config.get-in [:log :treesitter])))
(vim.treesitter.stop buf)
(nvim.buf_set_option buf :syntax "on"))
(tset vim.bo buf :syntax "on"))

(nvim.buf_set_lines
(vim.api.nvim_buf_set_lines
buf 0 -1 false
[(str.join [(client.get :comment-prefix)
"Sponsored by @"
@@ -67,7 +66,7 @@
:close-hud
(fn []
(when state.hud.id
(pcall nvim.win_close state.hud.id true)
(pcall vim.api.nvim_win_close state.hud.id true)
(set state.hud.id nil))))

(fn close-hud []
@@ -92,24 +91,20 @@

(fn break-lines [buf]
(let [break-str (break)]
(->> (nvim.buf_get_lines buf 0 -1 false)
(->> (vim.api.nvim_buf_get_lines buf 0 -1 false)
(a.kv-pairs)
(a.filter
(fn [[n s]]
(= s break-str)))
(a.map a.first))))

(fn set-win-opts! [win]
(nvim.win_set_option
win :wrap
(if (config.get-in [:log :wrap])
true
false))
(nvim.win_set_option win :foldmethod :marker)
(nvim.win_set_option win :foldmarker (.. (config.get-in [:log :fold :marker :start])
","
(config.get-in [:log :fold :marker :end])))
(nvim.win_set_option win :foldlevel 0))
(tset vim.wo win :wrap (if (config.get-in [:log :wrap]) true false))
(tset vim.wo win :foldmethod :marker)
(tset vim.wo win :foldmarker (.. (config.get-in [:log :fold :marker :start])
","
(config.get-in [:log :fold :marker :end])))
(tset vim.wo win :foldlevel 0))

(fn in-box? [box pos]
(and (>= pos.x box.x1) (<= pos.x box.x2)
@@ -150,7 +145,8 @@
:box {:y1 north :x1 west
:y2 (+ north size.height) :x2 (+ west size.width)}}
(do
(nvim.err_writeln "g:conjure#log#hud#anchor must be one of: NE, SE, SW, NW")
(vim.notify "g:conjure#log#hud#anchor must be one of: NE, SE, SW, NW"
vim.log.levels.ERROR)
(hud-window-pos :NE size)))
(a.assoc :anchor anchor))]

@@ -168,7 +164,7 @@
pos)))

(fn current-window-floating? []
(= :number (type (a.get (nvim.win_get_config 0) :zindex))))
(= :number (type (a.get (vim.api.nvim_win_get_config 0) :zindex))))

(local low-priority-streak-threshold 5)

@@ -196,7 +192,7 @@
(fn [opts]
(let [buf (upsert-buf)
last-break (a.last (break-lines buf))
line-count (nvim.buf_line_count buf)
line-count (vim.api.nvim_buf_line_count buf)
size {:width (editor.percent-width (config.get-in [:log :hud :width]))
:height (editor.percent-height (config.get-in [:log :hud :height]))}
pos (hud-window-pos (config.get-in [:log :hud :anchor]) size)
@@ -215,29 +211,28 @@
:zindex (config.get-in [:log :hud :zindex])
:border border})]

(when (and state.hud.id (not (nvim.win_is_valid state.hud.id)))
(when (and state.hud.id (not (vim.api.nvim_win_is_valid state.hud.id)))
(close-hud))

(if state.hud.id
(nvim.win_set_buf state.hud.id buf)
(vim.api.nvim_win_set_buf state.hud.id buf)
(do
(handle-low-priority-spam! (a.get opts :low-priority?))
(set state.hud.id (nvim.open_win buf false win-opts))
(set state.hud.id (vim.api.nvim_open_win buf false win-opts))
(set-win-opts! state.hud.id)))

(set state.hud.created-at-ms (vim.loop.now))

(if last-break
(do
(nvim.win_set_cursor state.hud.id [1 0])
(nvim.win_set_cursor
state.hud.id
(vim.api.nvim_win_set_cursor state.hud.id [1 0])
(vim.api.nvim_win_set_cursor state.hud.id
[(math.min
(+ last-break
(a.inc (math.floor (/ win-opts.height 2))))
line-count)
0]))
(nvim.win_set_cursor state.hud.id [line-count 0])))))
(vim.api.nvim_win_set_cursor state.hud.id [line-count 0])))))

(fn display-hud [opts]
(when (and (config.get-in [:log :hud :enabled])
@@ -253,24 +248,24 @@
(hook.exec :display-hud opts)))

(fn win-visible? [win]
(= (nvim.fn.tabpagenr)
(a.first (nvim.fn.win_id2tabwin win))))
(= (vim.fn.tabpagenr)
(a.first (vim.fn.win_id2tabwin win))))

(fn with-buf-wins [buf f]
(a.run!
(fn [win]
(when (= buf (nvim.win_get_buf win))
(when (= buf (vim.api.nvim_win_get_buf win))
(f win)))
(nvim.list_wins)))
(vim.api.nvim_list_wins)))

(fn win-botline [win]
(-> win
(nvim.fn.getwininfo)
(vim.fn.getwininfo)
(a.first)
(a.get :botline)))

(fn trim [buf]
(let [line-count (nvim.buf_line_count buf)]
(let [line-count (vim.api.nvim_buf_line_count buf)]
(when (> line-count (config.get-in [:log :trim :at]))
(let [target-line-count (- line-count (config.get-in [:log :trim :to]))
break-line
@@ -281,24 +276,21 @@
(break-lines buf))]

(when break-line
(nvim.buf_set_lines
buf 0
break-line
false [])
(vim.api.nvim_buf_set_lines buf 0 break-line false [])

;; This hack keeps all log window view ports correct after trim.
;; Without it the text moves off screen in the HUD.
(let [line-count (nvim.buf_line_count buf)]
(let [line-count (vim.api.nvim_buf_line_count buf)]
(with-buf-wins
buf
(fn [win]
(let [[row col] (nvim.win_get_cursor win)]
(nvim.win_set_cursor win [1 0])
(nvim.win_set_cursor win [row col]))))))))))
(let [[row col] (vim.api.nvim_win_get_cursor win)]
(vim.api.nvim_win_set_cursor win [1 0])
(vim.api.nvim_win_set_cursor win [row col]))))))))))

(fn last-line [buf extra-offset]
(a.first
(nvim.buf_get_lines
(vim.api.nvim_buf_get_lines
(or buf (upsert-buf))
(+ -2 (or extra-offset 0)) -1 false)))

@@ -310,19 +302,19 @@

(fn jump-to-latest []
(let [buf (upsert-buf)
last-eval-start (nvim.buf_get_extmark_by_id
last-eval-start (vim.api.nvim_buf_get_extmark_by_id
buf state.jump-to-latest.ns
state.jump-to-latest.mark {})]
(with-buf-wins
buf
(fn [win]
(pcall #(nvim.win_set_cursor win last-eval-start))
(pcall #(vim.api.nvim_win_set_cursor win last-eval-start))

(let [cmd (a.get
cursor-scroll-position->command
(config.get-in [:log :jump_to_latest :cursor_scroll_position]))]
(when cmd
(nvim.win_call win (fn [] (nvim.command cmd)))))))))
(vim.api.nvim_win_call win (fn [] (vim.cmd cmd)))))))))

(fn append [lines opts]
(let [line-count (a.count lines)]
@@ -395,12 +387,12 @@

lines)

old-lines (nvim.buf_line_count buf)]
old-lines (vim.api.nvim_buf_line_count buf)]

(let [(ok? err)
(pcall
(fn []
(nvim.buf_set_lines
(vim.api.nvim_buf_set_lines
buf
(if
(buffer.empty? buf) 0
@@ -414,11 +406,11 @@
(error (.. "Conjure failed to append to log: " err "\n"
"Offending lines: " (a.pr-str lines)))))

(let [new-lines (nvim.buf_line_count buf)
(let [new-lines (vim.api.nvim_buf_line_count buf)
jump-to-latest? (config.get-in [:log :jump_to_latest :enabled])]

;; This mark is used when jumping to the latest log entry.
(nvim.buf_set_extmark
(vim.api.nvim_buf_set_extmark
buf state.jump-to-latest.ns
(if join-first?
old-lines
@@ -438,12 +430,12 @@
(and (not= win state.hud.id)
(win-visible? win)))

(let [[row _] (nvim.win_get_cursor win)]
(let [[row _] (vim.api.nvim_win_get_cursor win)]
(if jump-to-latest?
(jump-to-latest)

(= row old-lines)
(nvim.win_set_cursor win [new-lines 0]))))))
(vim.api.nvim_win_set_cursor win [new-lines 0]))))))

(let [open-when (config.get-in [:log :hud :open_when])]
(if (and (not (a.get opts :suppress-hud?))
@@ -459,14 +451,11 @@
(fn create-win [cmd]
(set state.last-open-cmd cmd)
(let [buf (upsert-buf)]
(nvim.command
(.. "keepalt "
(if (config.get-in [:log :botright])
"botright "
"")
cmd " "
(buffer.resolve (log-buf-name))))
(nvim.win_set_cursor 0 [(nvim.buf_line_count buf) 0])
(vim.cmd (string.format "keepalt %s %s %s"
(if (config.get-in [:log :botright]) "botright" "")
cmd
(buffer.resolve (log-buf-name))))
(vim.api.nvim_win_set_cursor 0 [(vim.api.nvim_buf_line_count buf) 0])
(set-win-opts! 0)
(buffer.unlist buf)))

@@ -485,11 +474,11 @@
(fn find-windows []
(let [buf (upsert-buf)]
(a.filter (fn [win] (and (not= state.hud.id win)
(= buf (nvim.win_get_buf win))))
(nvim.tabpage_list_wins 0))))
(= buf (vim.api.nvim_win_get_buf win))))
(vim.api.nvim_tabpage_list_wins 0))))

(fn close [windows]
(a.run! #(nvim.win_close $1 true) windows))
(a.run! #(vim.api.nvim_win_close $1 true) windows))

(fn close-visible []
(close-hud)
@@ -515,10 +504,9 @@
(on-new-log-buf (upsert-buf)))

(fn reset-hard []
(nvim.ex.bwipeout_ (upsert-buf)))
(vim.api.nvim_buf_delete (upsert-buf) {:force true}))

{
: log-buf?
{: log-buf?
: clear-close-hud-passive-timer
: close-hud
: hud-lifetime-ms
@@ -535,5 +523,4 @@
: toggle
: dbg
: reset-soft
: reset-hard
}
: reset-hard}
229 changes: 114 additions & 115 deletions lua/conjure/log.lua

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

0 comments on commit 1b4b1f6

Please sign in to comment.