Skip to content

Commit 3be018b

Browse files
committed
fix: tweaks and maintenance
1 parent 28f3a05 commit 3be018b

12 files changed

Lines changed: 79 additions & 84 deletions

File tree

.luarc.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"workspace.checkThirdParty": false,
77
"diagnostics.globals": [
88
"vim",
9-
"Snacks"
9+
"Snacks",
10+
"vim.g.*"
1011
]
11-
}
12+
}

init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ vim.g.maplocalleader = vim.keycode(",")
1010
-- Load lazy package manager.
1111
-- https://github.com/folke/lazy.nvim
1212
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
13-
if not (vim.uv or vim.loop).fs_stat(lazypath) then
13+
if not vim.uv.fs_stat(lazypath) then
1414
local lazyrepo = "https://github.com/folke/lazy.nvim.git"
1515
local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
1616
if vim.v.shell_error ~= 0 then

lua/lib/fn.lua

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,6 @@ M.is_windows = function()
1616
return vim.fn.has("win32") == 1
1717
end
1818

19-
local spinners = {} -- Store spinner timers by buffer
20-
local icon_spinner = { "", "", "", "", "", "", "", "", "", "" }
21-
22-
M.start_spinner = function(bufnr, msg)
23-
-- Stop any existing spinner for this buffer first to avoid timer leak
24-
if spinners[bufnr] then
25-
M.stop_spinner(bufnr)
26-
end
27-
28-
local spinner_timer = vim.uv.new_timer()
29-
spinners[bufnr] = { idx = 1, timer = spinner_timer }
30-
31-
spinners[bufnr].timer:start(
32-
0,
33-
100,
34-
vim.schedule_wrap(function()
35-
-- Check if the buffer is still valid before updating it
36-
if not vim.api.nvim_buf_is_valid(bufnr) then
37-
M.stop_spinner(bufnr)
38-
return
39-
end
40-
41-
if not spinners[bufnr] then
42-
return
43-
end
44-
45-
spinners[bufnr].idx = (spinners[bufnr].idx % #icon_spinner) + 1
46-
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {
47-
icon_spinner[spinners[bufnr].idx] .. " " .. msg,
48-
"",
49-
})
50-
-- Move cursor to the last line only if we're currently in this buffer
51-
if vim.api.nvim_get_current_buf() == bufnr then
52-
local line_count = vim.api.nvim_buf_line_count(bufnr)
53-
pcall(vim.api.nvim_win_set_cursor, 0, { line_count, 0 })
54-
end
55-
end)
56-
)
57-
end
58-
59-
M.stop_spinner = function(bufnr)
60-
if spinners[bufnr] then
61-
spinners[bufnr].timer:stop()
62-
spinners[bufnr].timer:close()
63-
spinners[bufnr] = nil
64-
end
65-
end
66-
6719
M.fmt_relative_time = function(timestamp)
6820
-- Get current timestamp for relative time calculations
6921
local now = os.time()
@@ -95,15 +47,20 @@ end
9547

9648
-- Returns a list of { key, count } for non-zero diagnostic severities in the given buffer.
9749
-- key is one of "error", "warning", "information", "hint".
98-
local DIAGNOSTIC_KEYS = { "error", "warning", "information", "hint" }
50+
local DIAGNOSTIC_SEVERITIES = {
51+
{ key = "error", severity = vim.diagnostic.severity.ERROR },
52+
{ key = "warning", severity = vim.diagnostic.severity.WARN },
53+
{ key = "information", severity = vim.diagnostic.severity.INFO },
54+
{ key = "hint", severity = vim.diagnostic.severity.HINT },
55+
}
56+
9957
M.diagnostic_counts = function(bufnr)
10058
local results = {}
101-
for i, key in ipairs(DIAGNOSTIC_KEYS) do
102-
local ki = vim.diagnostic.severity[i]
103-
local severity = vim.diagnostic.severity[ki]
104-
local count = vim.diagnostic.count(bufnr, { severity = severity })[severity]
59+
for _, entry in ipairs(DIAGNOSTIC_SEVERITIES) do
60+
local s = entry.severity
61+
local count = vim.diagnostic.count(bufnr, { severity = s })[s]
10562
if count and count > 0 then
106-
table.insert(results, { key = key, count = count })
63+
table.insert(results, { key = entry.key, count = count })
10764
end
10865
end
10966
return results

lua/plugins/treesitter.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ local LANGUAGES = {
2727
"lua",
2828
"luadoc",
2929
"markdown",
30+
"markdown_inline",
3031
"nginx",
3132
"nu",
3233
"powershell",

lua/plugins/treesitter.windows.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ local LANGUAGES = {
2020
"lua",
2121
"luadoc",
2222
"markdown",
23+
"markdown_inline",
2324
"nu",
2425
"powershell",
2526
"regex",

plugin/config/statusline.lua

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,44 @@
22

33
local T = require("lib")
44

5+
local statusline_augroup = vim.api.nvim_create_augroup("ruicsh/config/statusline", { clear = true })
6+
57
-- Hoist requires out of the hot render path
68
local devicons = require("nvim-web-devicons")
79

8-
-- Cache for dynamic file icon highlight group to avoid set_hl on every redraw
9-
local icon_hl_cache = {}
10+
-- Cache for git root to avoid vim.fs.root() on every statusline redraw
1011

11-
-- Cache git root to avoid vim.fs.root() on every statusline redraw
1212
local cached_git_root = vim.fs.root(0, ".git")
1313
vim.api.nvim_create_autocmd({ "BufEnter", "DirChanged" }, {
14-
group = vim.api.nvim_create_augroup("ruicsh/config/statusline", { clear = true }),
14+
group = statusline_augroup,
1515
callback = function()
1616
cached_git_root = vim.fs.root(0, ".git")
1717
end,
1818
})
1919

20+
-- Pre-create icon highlight groups at startup so the hot render path never calls nvim_set_hl
21+
vim.api.nvim_create_autocmd("VimEnter", {
22+
group = statusline_augroup,
23+
callback = function()
24+
local ok, icons = pcall(devicons.get_icons)
25+
if not ok or not icons then
26+
return
27+
end
28+
local seen = {}
29+
for _, icon_data in pairs(icons) do
30+
if icon_data.name and not seen[icon_data.name] then
31+
seen[icon_data.name] = true
32+
local src_hl = "DevIcon" .. icon_data.name
33+
local dst_hl = "StatusLineFileIcon_" .. icon_data.name
34+
-- Only create if the source highlight group exists (defensive)
35+
if vim.fn.hlexists(src_hl) == 1 then
36+
vim.api.nvim_set_hl(0, dst_hl, { link = src_hl })
37+
end
38+
end
39+
end
40+
end,
41+
})
42+
2043
local function sep()
2144
return "%#StatusLineSeparator#|%#StatusLine#"
2245
end
@@ -146,13 +169,11 @@ local function c_filename()
146169
local relpath = vim.fn.fnamemodify(fullpath, ":.:h")
147170
local icon, icon_color = devicons.get_icon(filename, nil, { default = true })
148171

149-
-- Icon with color (only call set_hl when the color changes)
172+
-- Icon with color (highlight group pre-created at VimEnter)
150173
if icon and icon_color then
151-
local hl_group = "StatusLineFileIconDynamic"
152-
if icon_hl_cache[hl_group] ~= icon_color then
153-
vim.api.nvim_set_hl(0, hl_group, { link = icon_color })
154-
icon_hl_cache[hl_group] = icon_color
155-
end
174+
-- icon_color is already a highlight group name like "DevIconLua"
175+
-- Map to our namespaced group: "DevIconFoo" -> "StatusLineFileIcon_Foo"
176+
local hl_group = icon_color:gsub("^DevIcon", "StatusLineFileIcon_")
156177
line = line .. "%#" .. hl_group .. "#" .. icon .. " "
157178
end
158179
line = line .. "%#StatusLineFilename#" .. filename

plugin/custom/lsp-highlight-references.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,26 @@ vim.api.nvim_create_autocmd("LspAttach", {
1717
end
1818

1919
vim.api.nvim_create_autocmd({ "CursorHold", "InsertLeave" }, {
20+
group = augroup,
2021
buffer = bufnr,
2122
callback = vim.lsp.buf.document_highlight,
2223
desc = "Highlight references under the cursor",
2324
})
2425

2526
vim.api.nvim_create_autocmd({ "CursorMoved", "InsertEnter" }, {
27+
group = augroup,
2628
buffer = bufnr,
2729
callback = vim.lsp.buf.clear_references,
2830
desc = "Clear highlight references",
2931
})
3032
end,
3133
})
34+
35+
vim.api.nvim_create_autocmd("LspDetach", {
36+
group = augroup,
37+
callback = function(args)
38+
vim.api.nvim_clear_autocmds({ buffer = args.buf, group = augroup })
39+
end,
40+
desc = "Clean up buffer-local reference highlight autocmds",
41+
})
42+

plugin/custom/vim-messages.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ vim.api.nvim_create_user_command("VimMessages", function()
1717
end
1818
end
1919

20+
-- Reuse existing window if still open
21+
if vim_messages_winnr and vim.api.nvim_win_is_valid(vim_messages_winnr) then
22+
local buf = vim.api.nvim_win_get_buf(vim_messages_winnr)
23+
vim.api.nvim_set_option_value("modifiable", true, { buf = buf })
24+
vim.api.nvim_buf_set_lines(buf, 0, -1, false, filtered_lines)
25+
vim.api.nvim_set_option_value("modifiable", false, { buf = buf })
26+
27+
-- Set cursor to the last line
28+
vim.api.nvim_win_set_cursor(vim_messages_winnr, { vim.api.nvim_buf_line_count(buf), 0 })
29+
return
30+
end
31+
32+
vim_messages_winnr = nil
33+
2034
-- Calculate dimensions for floating window
2135
local width = vim.o.columns
2236
local height = math.floor(vim.o.lines * 0.4)

plugin/keymaps/normal.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ k("J", "mzJ`z:delmarks z<cr>", { desc = "Join lines" })
5050
-- Save file
5151
k("<c-s>", "<cmd>silent! update | redraw<cr>", { desc = "Save" })
5252

53-
-- Don't store on register when changing text or deleting a character.
54-
local black_hole_commands = { "C", "c", "cc", "x", "X" }
53+
-- Don't store on register when deleting a character.
54+
local black_hole_commands = { "x", "X" }
5555
for _, key in pairs(black_hole_commands) do
5656
k(key, '"_' .. key) -- `:h "_`
5757
end

plugin/keymaps/visual.lua

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ k("<c-s>", "<cmd>silent! update | redraw<cr>", { desc = "Save" })
4242
k(">", ">gv") -- Reselect after indent
4343
k("<", "<gv") -- Reselect after dedent
4444

45-
-- http://www.kevinli.co/posts/2017-01-19-multiple-cursors-in-500-bytes-of-vimscript/
46-
local function replace_selection(direction)
47-
vim.g.mc = vim.api.nvim_replace_termcodes("y/\\V<c-r>=escape(@\", '/')<cr><cr>", true, true, true)
48-
return function()
49-
return vim.g.mc .. "``cg" .. direction
50-
end
51-
end
52-
k("cn", replace_selection("n"), { expr = true, desc = "Change selection (forward)" })
53-
k("cN", replace_selection("N"), { expr = true, desc = "Change selection (backward)" })
54-
5545
-- Same behaviour for `I`/`A` as in normal mode
5646
-- https://www.reddit.com/r/neovim/comments/1k4efz8/comment/moelhto/
5747
k("<s-i>", function()

0 commit comments

Comments
 (0)