2023-01-11 21:45:22 +00:00
|
|
|
(let [mod {}
|
2023-01-14 21:45:23 +00:00
|
|
|
cache {}
|
2023-01-11 21:45:22 +00:00
|
|
|
api vim.api
|
|
|
|
o vim.opt
|
2023-04-03 09:01:19 +00:00
|
|
|
b vim.b
|
|
|
|
v vim.v
|
2023-01-14 16:07:11 +00:00
|
|
|
statusline (require :nifoc.statusline)
|
2023-01-14 21:45:23 +00:00
|
|
|
diagnostic vim.diagnostic
|
|
|
|
gitsigns (require :gitsigns)
|
|
|
|
augroup (vim.api.nvim_create_augroup :NifocStatuscolumn {:clear true})
|
|
|
|
aucmd vim.api.nvim_create_autocmd]
|
|
|
|
;; Cache
|
|
|
|
|
2023-01-22 12:47:36 +00:00
|
|
|
(fn maybe-setup-buffer-cache! [bufnr key]
|
2023-01-14 21:45:23 +00:00
|
|
|
(when (= (. cache bufnr) nil)
|
|
|
|
(tset cache bufnr {}))
|
2023-01-22 12:47:36 +00:00
|
|
|
(when (= (. cache bufnr key) nil)
|
|
|
|
(tset cache bufnr key {})))
|
|
|
|
|
|
|
|
(fn clear-cache! [bufnr key]
|
2023-01-14 21:45:23 +00:00
|
|
|
(tset cache bufnr key {}))
|
|
|
|
|
2023-04-03 09:01:19 +00:00
|
|
|
(fn cache-filter [func t]
|
|
|
|
(let [ret-tab {}]
|
|
|
|
(each [k v (pairs t)] (when (func v k) (tset ret-tab k v)))
|
|
|
|
ret-tab))
|
|
|
|
|
2023-01-22 12:47:36 +00:00
|
|
|
(fn clear-diagnostics-cache! [bufnr diagnostics]
|
|
|
|
(let [key :diagnostics
|
2023-04-03 09:01:19 +00:00
|
|
|
namespaces (vim.tbl_map #$1.namespace diagnostics)
|
|
|
|
current-cache (. cache bufnr key)
|
|
|
|
new-cache (cache-filter #(not (vim.tbl_contains namespaces $1.ns))
|
|
|
|
current-cache)]
|
|
|
|
(tset cache bufnr key new-cache)))
|
2023-01-22 12:47:36 +00:00
|
|
|
|
2023-01-14 21:45:23 +00:00
|
|
|
(fn update-cache-diagnostics [bufnr diagnostics]
|
2023-01-22 12:47:36 +00:00
|
|
|
(maybe-setup-buffer-cache! bufnr :diagnostics)
|
|
|
|
(clear-diagnostics-cache! bufnr diagnostics)
|
2023-01-14 21:45:23 +00:00
|
|
|
(each [_ diagnostic (pairs diagnostics)]
|
|
|
|
(let [lnum (+ diagnostic.lnum 1)
|
|
|
|
current (. cache bufnr :diagnostics lnum)]
|
2023-04-03 09:01:19 +00:00
|
|
|
(when (or (= current nil) (> diagnostic.severity current.severity))
|
2023-01-14 21:45:23 +00:00
|
|
|
(tset cache bufnr :diagnostics lnum
|
2023-01-22 12:47:36 +00:00
|
|
|
{:severity diagnostic.severity
|
|
|
|
:col diagnostic.col
|
|
|
|
:ns diagnostic.namespace})))))
|
2023-01-14 21:45:23 +00:00
|
|
|
|
|
|
|
(fn update-cache-gitsigns [bufnr]
|
2023-01-22 12:47:36 +00:00
|
|
|
(maybe-setup-buffer-cache! bufnr :gitsigns)
|
|
|
|
(clear-cache! bufnr :gitsigns)
|
2023-01-14 21:45:23 +00:00
|
|
|
(let [signs (?. (vim.fn.sign_getplaced bufnr
|
|
|
|
{:group :gitsigns_vimfn_signs_})
|
|
|
|
1 :signs)]
|
|
|
|
(when (not= signs nil)
|
|
|
|
(each [_ sign (pairs signs)]
|
|
|
|
(let [lnum sign.lnum
|
|
|
|
current (. cache bufnr :gitsigns lnum)]
|
|
|
|
(when (= current nil)
|
|
|
|
(tset cache bufnr :gitsigns lnum sign)))))))
|
|
|
|
|
|
|
|
(fn cached-sign [bufnr key lnum]
|
|
|
|
(?. cache bufnr key lnum))
|
|
|
|
|
|
|
|
(aucmd :DiagnosticChanged
|
|
|
|
{:callback #(update-cache-diagnostics $1.buf $1.data.diagnostics)
|
|
|
|
:group augroup
|
|
|
|
:desc "Update cached diagnostic signs"})
|
|
|
|
(aucmd :User {:pattern :GitSignsUpdate
|
|
|
|
:callback #(update-cache-gitsigns $1.buf)
|
|
|
|
:group augroup
|
|
|
|
:desc "Update cached gitsigns signs"})
|
|
|
|
(aucmd :BufWipeout
|
|
|
|
{:callback #(tset cache $1.buf nil)
|
|
|
|
:group augroup
|
|
|
|
:desc "Clear sign cache for current buffer"})
|
2023-01-11 21:45:22 +00:00
|
|
|
;; Line Number
|
2023-04-03 09:01:19 +00:00
|
|
|
(set mod.line-number {:condition #(or (o.number:get) (o.relativenumber:get))
|
|
|
|
1 statusline.push-right
|
|
|
|
2 {:provider (fn []
|
|
|
|
(let [relnum-opt (o.relativenumber:get)
|
|
|
|
relnum v.relnum
|
|
|
|
virtnum v.virtnum]
|
|
|
|
(if (not= virtnum 0) ""
|
|
|
|
(and relnum-opt (= relnum 0)) "%l"
|
|
|
|
relnum-opt (tostring relnum)
|
|
|
|
"%l")))}})
|
2023-01-11 21:45:22 +00:00
|
|
|
;; Signs
|
|
|
|
(set mod.signs {:provider "%s" :hl {:bold true}})
|
|
|
|
;; gitsigns
|
2023-04-03 09:01:19 +00:00
|
|
|
(set mod.gitsigns {:condition #(= b.nifoc_gitsigns_enabled 1)
|
|
|
|
:init (fn [self]
|
|
|
|
(let [bufnr (api.nvim_get_current_buf)
|
|
|
|
sign (cached-sign bufnr :gitsigns v.lnum)]
|
|
|
|
(set self.sign sign)
|
|
|
|
(set self.has_sign (not= sign nil))))
|
|
|
|
:provider " ▏"
|
|
|
|
:hl #(if $1.has_sign $1.sign.name :StatusLineNC)
|
|
|
|
:on_click {:name :heirline_statuscolumn_gitsigns
|
|
|
|
:callback (fn [self]
|
|
|
|
(let [mouse (vim.fn.getmousepos)
|
|
|
|
cursor-pos [mouse.line 0]]
|
|
|
|
(api.nvim_win_set_cursor mouse.winid
|
|
|
|
cursor-pos)
|
|
|
|
(vim.defer_fn #(gitsigns.blame_line {:full true})
|
|
|
|
100)))}})
|
|
|
|
(set mod.gitsigns-or-bar [{:condition #(and (not= b.nifoc_gitsigns_enabled 1)
|
2023-01-14 16:07:11 +00:00
|
|
|
(or (o.number:get)
|
|
|
|
(o.relativenumber:get)))
|
|
|
|
:provider " ▏"
|
2023-01-12 09:59:15 +00:00
|
|
|
:hl :StatusLineNC}
|
|
|
|
mod.gitsigns])
|
2023-01-11 21:45:22 +00:00
|
|
|
;; Diagnostic signs
|
|
|
|
(set mod.diagnostic-signs
|
2023-04-03 09:01:19 +00:00
|
|
|
{:condition #(and (= b.nifoc_diagnostics_enabled 1)
|
2023-01-14 16:07:11 +00:00
|
|
|
(> (length (vim.diagnostic.get 0)) 0))
|
2023-01-14 21:45:23 +00:00
|
|
|
:static {:sign-text {diagnostic.severity.ERROR " "
|
|
|
|
diagnostic.severity.WARN " "
|
|
|
|
diagnostic.severity.INFO " "
|
|
|
|
diagnostic.severity.HINT " "}
|
|
|
|
:sign-hl {diagnostic.severity.ERROR :DiagnosticSignError
|
|
|
|
diagnostic.severity.WARN :DiagnosticSignWarn
|
|
|
|
diagnostic.severity.INFO :DiagnosticSignInfo
|
|
|
|
diagnostic.severity.HINT :DiagnosticSignHint}}
|
2023-01-11 21:45:22 +00:00
|
|
|
:init (fn [self]
|
|
|
|
(let [bufnr (api.nvim_get_current_buf)
|
2023-04-03 09:01:19 +00:00
|
|
|
sign (cached-sign bufnr :diagnostics v.lnum)]
|
2023-01-11 21:45:22 +00:00
|
|
|
(set self.sign sign)
|
|
|
|
(set self.has_sign (not= sign nil))))
|
|
|
|
:provider #(if $1.has_sign
|
2023-01-14 21:45:23 +00:00
|
|
|
(. $1.sign-text $1.sign.severity)
|
2023-01-11 21:45:22 +00:00
|
|
|
" ")
|
|
|
|
:hl #(when $1.has_sign
|
2023-01-14 21:45:23 +00:00
|
|
|
(. $1.sign-hl $1.sign.severity))
|
2023-01-11 21:45:22 +00:00
|
|
|
:on_click {:name :heirline_statuscolumn_diagnostic
|
|
|
|
:callback (fn [self]
|
2023-01-11 22:48:51 +00:00
|
|
|
(let [mouse (vim.fn.getmousepos)
|
|
|
|
line (- mouse.line 1)
|
|
|
|
cursor-pos [mouse.line 0]]
|
|
|
|
(api.nvim_win_set_cursor mouse.winid
|
|
|
|
cursor-pos)
|
|
|
|
(vim.defer_fn #(vim.diagnostic.open_float {:bufnr self.bufnr
|
|
|
|
:pos line
|
|
|
|
:scope :line})
|
2023-04-03 09:01:19 +00:00
|
|
|
100)))}})
|
|
|
|
;; Debug
|
|
|
|
(set mod._debug_cache #cache)
|
2023-01-11 21:45:22 +00:00
|
|
|
mod)
|
|
|
|
|