diff --git a/home/config/nvim/nifoc/statuscolumn.fnl b/home/config/nvim/nifoc/statuscolumn.fnl index beac25e..8725a2a 100644 --- a/home/config/nvim/nifoc/statuscolumn.fnl +++ b/home/config/nvim/nifoc/statuscolumn.fnl @@ -2,6 +2,8 @@ cache {} api vim.api o vim.opt + b vim.b + v vim.v statusline (require :nifoc.statusline) diagnostic vim.diagnostic gitsigns (require :gitsigns) @@ -18,17 +20,18 @@ (fn clear-cache! [bufnr key] (tset cache bufnr key {})) + (fn cache-filter [func t] + (let [ret-tab {}] + (each [k v (pairs t)] (when (func v k) (tset ret-tab k v))) + ret-tab)) + (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))))) + 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))) (fn update-cache-diagnostics [bufnr diagnostics] (maybe-setup-buffer-cache! bufnr :diagnostics) @@ -36,7 +39,7 @@ (each [_ diagnostic (pairs diagnostics)] (let [lnum (+ diagnostic.lnum 1) current (. cache bufnr :diagnostics lnum)] - (when (or (= current nil) (< diagnostic.severity current.severity)) + (when (or (= current nil) (> diagnostic.severity current.severity)) (tset cache bufnr :diagnostics lnum {:severity diagnostic.severity :col diagnostic.col @@ -71,38 +74,36 @@ :group augroup :desc "Clear sign cache for current buffer"}) ;; 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}")))}}) + (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")))}}) ;; Signs (set mod.signs {:provider "%s" :hl {:bold true}}) ;; gitsigns - (set mod.gitsigns - {:condition #(= vim.b.nifoc_gitsigns_enabled 1) - :init (fn [self] - (let [bufnr (api.nvim_get_current_buf) - lnum vim.v.lnum - sign (cached-sign bufnr :gitsigns 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= vim.b.nifoc_gitsigns_enabled - 1) + (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) (or (o.number:get) (o.relativenumber:get))) :provider " ▏" @@ -110,7 +111,7 @@ mod.gitsigns]) ;; Diagnostic signs (set mod.diagnostic-signs - {:condition #(and (= vim.b.nifoc_diagnostics_enabled 1) + {:condition #(and (= b.nifoc_diagnostics_enabled 1) (> (length (vim.diagnostic.get 0)) 0)) :static {:sign-text {diagnostic.severity.ERROR " " diagnostic.severity.WARN " " @@ -122,8 +123,7 @@ diagnostic.severity.HINT :DiagnosticSignHint}} :init (fn [self] (let [bufnr (api.nvim_get_current_buf) - lnum vim.v.lnum - sign (cached-sign bufnr :diagnostics lnum)] + sign (cached-sign bufnr :diagnostics v.lnum)] (set self.sign sign) (set self.has_sign (not= sign nil)))) :provider #(if $1.has_sign @@ -141,6 +141,8 @@ (vim.defer_fn #(vim.diagnostic.open_float {:bufnr self.bufnr :pos line :scope :line}) - 100)))}}) + 100)))}}) + ;; Debug + (set mod._debug_cache #cache) mod)