1
0
Fork 0
dotfiles/home/config/nvim/nifoc/statuscolumn.fnl

147 lines
6.4 KiB
Text
Raw Normal View History

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-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-01-22 12:47:36 +00:00
(fn clear-diagnostics-cache! [bufnr diagnostics]
(let [key :diagnostics
namespaces (vim.tbl_map (fn [d]
d.namespace)
diagnostics)
cleared-namespaces {}]
(each [_ ns (pairs namespaces)]
(when (= (. cleared-namespaces ns) nil)
(local current-cache (. cache bufnr key))
(tset cache bufnr key (vim.tbl_filter #(not= $1.ns ns) current-cache))
(tset cleared-namespaces ns true)))))
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)]
(when (or (= current nil) (< diagnostic.severity current.severity))
(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
(set mod.line-number
{:condition #(or (o.number:get) (o.relativenumber:get))
1 statusline.push-right
2 {:provider (fn []
(let [num (o.number:get)
relnum (o.relativenumber:get)]
(if (and num (not relnum)) "%l"
(and relnum (not num)) "%r"
"%{v:relnum?v:relnum:v:lnum}")))}})
;; Signs
(set mod.signs {:provider "%s" :hl {:bold true}})
;; gitsigns
2023-01-12 09:59:15 +00:00
(set mod.gitsigns
{:condition #(= vim.b.nifoc_gitsigns_enabled 1)
:init (fn [self]
(let [bufnr (api.nvim_get_current_buf)
lnum vim.v.lnum
2023-01-14 21:45:23 +00:00
sign (cached-sign bufnr :gitsigns lnum)]
2023-01-12 09:59:15 +00:00
(set self.sign sign)
(set self.has_sign (not= sign nil))))
2023-01-14 16:07:11 +00:00
: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= vim.b.nifoc_gitsigns_enabled
1)
(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-01-14 16:07:11 +00:00
{:condition #(and (= vim.b.nifoc_diagnostics_enabled 1)
(> (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)
lnum vim.v.lnum
2023-01-14 21:45:23 +00:00
sign (cached-sign bufnr :diagnostics 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-01-14 16:07:11 +00:00
100)))}})
2023-01-11 21:45:22 +00:00
mod)