Skip to content

Commit 9949dd5

Browse files
committed
Simplify codebase: consolidate duplications, reduce ~115 lines
- LSP: consolidate 5 FileType autocmds into 1 with lookup table - Keymaps: inline gitsigns commands, simplify Snacks fallback (26→6 lines) - Installer: move CONFIG_FILES/BASE_URL to top-level, remove fetch_plugins_list - md-share: consolidate 4 state variables into single table
1 parent 68c16cb commit 9949dd5

4 files changed

Lines changed: 63 additions & 174 deletions

File tree

i

Lines changed: 19 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@
55
set -euo pipefail
66

77
VIMZAP_MARKER="# VimZap aliases"
8+
BASE_URL="https://raw.githubusercontent.com/IFAKA/vimzap/main"
9+
10+
# Single source of truth for config files
11+
CONFIG_FILES=(
12+
"init.lua"
13+
"lua/options.lua"
14+
"lua/plugins.lua"
15+
"lua/lsp.lua"
16+
"lua/debug.lua"
17+
"lua/keymaps.lua"
18+
"lua/benchmark.lua"
19+
"lua/md-share.lua"
20+
"lua/health.lua"
21+
"scripts/md-server.py"
22+
)
823

924
# Single source of truth for VimZap plugins
10-
# Both install and update use this list
1125
VIMZAP_PLUGINS=(
1226
"williamboman/mason.nvim"
1327
"folke/snacks.nvim"
@@ -27,34 +41,6 @@ VIMZAP_PLUGINS=(
2741
"IFAKA/prophet.nvim"
2842
)
2943

30-
# Fetch the PLUGINS array from the latest installer script on GitHub
31-
fetch_plugins_list() {
32-
local temp_script="/tmp/vimzap_installer_$$"
33-
34-
# Download the latest installer script
35-
if ! curl -fsSL "https://raw.githubusercontent.com/IFAKA/vimzap/main/i" -o "$temp_script" 2>/dev/null; then
36-
rm -f "$temp_script"
37-
return 1
38-
fi
39-
40-
# Extract the PLUGINS array from main() function
41-
# Use the "Installing plugins" echo as a marker (only in main(), not update())
42-
local plugins=$(sed -n '/Installing plugins\.\.\./,/^ PLUGIN_DIR=/{
43-
/^ "[^"]*\/[^"]*"/{
44-
s/^[[:space:]]*"\([^"]*\)".*/\1/p
45-
}
46-
}' "$temp_script" | tr '\n' ' ')
47-
48-
rm -f "$temp_script"
49-
50-
if [[ -z "$plugins" ]]; then
51-
return 1
52-
fi
53-
54-
echo "$plugins"
55-
return 0
56-
}
57-
5844
get_shell_rc() {
5945
if [[ -n "${ZSH_VERSION:-}" ]] || [[ "$SHELL" == *"zsh"* ]]; then
6046
echo "$HOME/.zshrc"
@@ -194,21 +180,7 @@ update() {
194180
echo " Updating config..."
195181
mkdir -p ~/.config/nvim/lua
196182
mkdir -p ~/.config/nvim/scripts
197-
BASE_URL="https://raw.githubusercontent.com/IFAKA/vimzap/main"
198-
199-
CONFIG_FILES=(
200-
"init.lua"
201-
"lua/options.lua"
202-
"lua/plugins.lua"
203-
"lua/lsp.lua"
204-
"lua/debug.lua"
205-
"lua/keymaps.lua"
206-
"lua/benchmark.lua"
207-
"lua/md-share.lua"
208-
"lua/health.lua"
209-
"scripts/md-server.py"
210-
)
211-
183+
212184
for file in "${CONFIG_FILES[@]}"; do
213185
local dest="$HOME/.config/nvim/$file"
214186
local temp="/tmp/vimzap_${file//\//_}"
@@ -247,24 +219,11 @@ update() {
247219
echo ""
248220
echo " Updating plugins..."
249221
PLUGIN_DIR="$HOME/.local/share/nvim/site/pack/plugins/opt"
250-
251-
# Try to fetch the latest PLUGINS list from GitHub
252-
local plugins_str=$(fetch_plugins_list)
253-
254-
# Prefer dynamic list, fall back to bundled list
255-
if [[ $? -ne 0 ]] || [[ -z "$plugins_str" ]]; then
256-
# Use the bundled VIMZAP_PLUGINS array (defined at top of script)
257-
PLUGINS=("${VIMZAP_PLUGINS[@]}")
258-
else
259-
# Convert space-separated string to array
260-
read -ra PLUGINS <<< "$plugins_str"
261-
fi
262-
263222
local plugins_installed=0
264223

265224
# First, install any missing plugins
266225
mkdir -p "$PLUGIN_DIR"
267-
for plugin in "${PLUGINS[@]}"; do
226+
for plugin in "${VIMZAP_PLUGINS[@]}"; do
268227
name=$(basename "$plugin")
269228
if [[ ! -d "$PLUGIN_DIR/$name" ]]; then
270229
printf " %s... " "$name"
@@ -291,7 +250,7 @@ update() {
291250

292251
# Skip if we just installed this plugin
293252
local just_installed=false
294-
for plugin in "${PLUGINS[@]}"; do
253+
for plugin in "${VIMZAP_PLUGINS[@]}"; do
295254
if [[ "$(basename "$plugin")" == "$name" ]] && [[ ! -d "$dir/.git" ]] || [[ $plugins_installed -gt 0 ]]; then
296255
just_installed=true
297256
break
@@ -481,19 +440,6 @@ main() {
481440
mkdir -p ~/.local/share/nvim/site/pack/plugins/opt
482441

483442
# Download config files
484-
BASE_URL="https://raw.githubusercontent.com/IFAKA/vimzap/main"
485-
CONFIG_FILES=(
486-
"init.lua"
487-
"lua/options.lua"
488-
"lua/plugins.lua"
489-
"lua/lsp.lua"
490-
"lua/debug.lua"
491-
"lua/keymaps.lua"
492-
"lua/benchmark.lua"
493-
"lua/md-share.lua"
494-
"lua/health.lua"
495-
"scripts/md-server.py"
496-
)
497443
for file in "${CONFIG_FILES[@]}"; do
498444
if ! curl -fsSL "$BASE_URL/$file" -o ~/.config/nvim/"$file"; then
499445
echo "Error: Failed to download $file"
@@ -506,11 +452,8 @@ main() {
506452

507453
# Plugins
508454
echo " [4/6] Installing plugins..."
509-
# Use the shared VIMZAP_PLUGINS array (defined at top of script)
510-
PLUGINS=("${VIMZAP_PLUGINS[@]}")
511-
512455
PLUGIN_DIR="$HOME/.local/share/nvim/site/pack/plugins/opt"
513-
for plugin in "${PLUGINS[@]}"; do
456+
for plugin in "${VIMZAP_PLUGINS[@]}"; do
514457
name=$(basename "$plugin")
515458
if [[ ! -d "$PLUGIN_DIR/$name" ]]; then
516459
printf " %s " "$name"

lua/keymaps.lua

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,9 @@
1-
-- Helper functions
2-
3-
-- Fallback for Snacks when not available
1+
-- Snacks with minimal fallback
42
local snacks_ok, Snacks = pcall(require, "snacks")
53
if not snacks_ok then
6-
-- Create dummy Snacks object with safe fallbacks
7-
Snacks = {
8-
dashboard = function() vim.notify("Dashboard not available", vim.log.levels.WARN) end,
9-
explorer = function() vim.cmd("Explore") end,
10-
picker = {
11-
files = function() vim.cmd("find .") end,
12-
grep = function() vim.cmd("grep") end,
13-
buffers = function() vim.cmd("ls") end,
14-
recent = function() vim.notify("Recent files not available", vim.log.levels.WARN) end,
15-
lsp_symbols = function() vim.notify("LSP symbols not available", vim.log.levels.WARN) end,
16-
git_files = function() vim.notify("Git files picker not available", vim.log.levels.WARN) end,
17-
git_status = function() vim.notify("Git status picker not available", vim.log.levels.WARN) end,
18-
help = function() vim.cmd("help") end,
19-
keymaps = function() vim.cmd("map") end,
20-
commands = function() vim.cmd("command") end,
21-
diagnostics = function() vim.notify("Diagnostics picker not available", vim.log.levels.WARN) end,
22-
},
23-
notifier = {
24-
notify = function(msg, level) vim.notify(msg, level) end,
25-
},
26-
bufdelete = function() vim.cmd("bdelete") end,
27-
terminal = function() vim.cmd("terminal") end,
28-
}
29-
end
30-
31-
local function gitsigns_cmd(cmd)
32-
vim.cmd("Gitsigns " .. cmd)
4+
local warn = function() vim.notify("Snacks.nvim not available", vim.log.levels.WARN) end
5+
local noop = setmetatable({}, { __index = function() return warn end })
6+
Snacks = setmetatable({ picker = noop, notifier = { notify = vim.notify } }, { __index = function() return warn end })
337
end
348

359
-- Insert mode: jj to escape
@@ -127,10 +101,10 @@ if wk_ok then
127101
end, desc = "lazygit" },
128102
{ "<leader>gf", function() Snacks.picker.git_files() end, desc = "git files" },
129103
{ "<leader>gs", function() Snacks.picker.git_status() end, desc = "status" },
130-
{ "<leader>gp", function() gitsigns_cmd("preview_hunk") end, desc = "preview hunk" },
131-
{ "<leader>ga", function() gitsigns_cmd("stage_hunk") end, desc = "stage hunk" },
132-
{ "<leader>gr", function() gitsigns_cmd("reset_hunk") end, desc = "reset hunk" },
133-
{ "<leader>gb", function() gitsigns_cmd("blame_line") end, desc = "blame" },
104+
{ "<leader>gp", "<cmd>Gitsigns preview_hunk<cr>", desc = "preview hunk" },
105+
{ "<leader>ga", "<cmd>Gitsigns stage_hunk<cr>", desc = "stage hunk" },
106+
{ "<leader>gr", "<cmd>Gitsigns reset_hunk<cr>", desc = "reset hunk" },
107+
{ "<leader>gb", "<cmd>Gitsigns blame_line<cr>", desc = "blame" },
134108

135109
-- Search
136110
{ "<leader>s", group = "search" },
@@ -186,8 +160,8 @@ if wk_ok then
186160
{ "]e", function()
187161
vim.diagnostic.goto_next({ severity = vim.diagnostic.severity.ERROR })
188162
end, desc = "next error" },
189-
{ "[h", function() gitsigns_cmd("nav_hunk prev") end, desc = "prev hunk" },
190-
{ "]h", function() gitsigns_cmd("nav_hunk next") end, desc = "next hunk" },
163+
{ "[h", "<cmd>Gitsigns nav_hunk prev<cr>", desc = "prev hunk" },
164+
{ "]h", "<cmd>Gitsigns nav_hunk next<cr>", desc = "next hunk" },
191165

192166
-- Buffer navigation
193167
{ "<S-h>", "<cmd>bprevious<cr>", desc = "prev buffer" },

lua/lsp.lua

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -143,38 +143,19 @@ vim.lsp.config("lua_ls", {
143143
vim.lsp.enable({ "ts_ls", "html", "cssls", "jsonls", "tailwindcss", "eslint", "lua_ls" })
144144

145145
-- Auto-attach LSP to buffers (Neovim 0.11 doesn't auto-attach with just enable())
146-
vim.api.nvim_create_autocmd("FileType", {
147-
pattern = { "typescript", "typescriptreact", "javascript", "javascriptreact" },
148-
callback = function(args)
149-
vim.lsp.start(vim.lsp.config.ts_ls)
150-
end,
151-
})
152-
153-
vim.api.nvim_create_autocmd("FileType", {
154-
pattern = { "html" },
155-
callback = function(args)
156-
vim.lsp.start(vim.lsp.config.html)
157-
end,
158-
})
159-
160-
vim.api.nvim_create_autocmd("FileType", {
161-
pattern = { "css", "scss" },
162-
callback = function(args)
163-
vim.lsp.start(vim.lsp.config.cssls)
164-
end,
165-
})
166-
167-
vim.api.nvim_create_autocmd("FileType", {
168-
pattern = { "json", "jsonc" },
169-
callback = function(args)
170-
vim.lsp.start(vim.lsp.config.jsonls)
171-
end,
172-
})
146+
local filetype_to_lsp = {
147+
typescript = "ts_ls", typescriptreact = "ts_ls", javascript = "ts_ls", javascriptreact = "ts_ls",
148+
html = "html", css = "cssls", scss = "cssls",
149+
json = "jsonls", jsonc = "jsonls", lua = "lua_ls",
150+
}
173151

174152
vim.api.nvim_create_autocmd("FileType", {
175-
pattern = { "lua" },
153+
pattern = vim.tbl_keys(filetype_to_lsp),
176154
callback = function(args)
177-
vim.lsp.start(vim.lsp.config.lua_ls)
155+
local server = filetype_to_lsp[vim.bo[args.buf].filetype]
156+
if server and vim.lsp.config[server] then
157+
vim.lsp.start(vim.lsp.config[server])
158+
end
178159
end,
179160
})
180161

lua/md-share.lua

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
-- Share current markdown file on local network with QR code
33

44
local M = {}
5-
6-
-- State
7-
M.server_pid = nil
8-
M.server_url = nil
9-
M.qr_buf = nil
10-
M.qr_win = nil
5+
local state = { pid = nil, url = nil, buf = nil, win = nil }
116

127
-- Check if qrencode is installed
138
local function check_qrencode()
@@ -17,27 +12,24 @@ end
1712

1813
-- Kill the server process
1914
local function kill_server()
20-
if M.server_pid then
21-
vim.fn.system("kill " .. M.server_pid)
22-
M.server_pid = nil
23-
M.server_url = nil
15+
if state.pid then
16+
vim.fn.system("kill " .. state.pid)
17+
state.pid = nil
18+
state.url = nil
2419
end
2520
end
2621

2722
-- Close QR window and kill server
2823
local function close_qr()
29-
if M.qr_win and vim.api.nvim_win_is_valid(M.qr_win) then
30-
vim.api.nvim_win_close(M.qr_win, true)
24+
if state.win and vim.api.nvim_win_is_valid(state.win) then
25+
vim.api.nvim_win_close(state.win, true)
3126
end
32-
if M.qr_buf and vim.api.nvim_buf_is_valid(M.qr_buf) then
33-
vim.api.nvim_buf_delete(M.qr_buf, { force = true })
27+
if state.buf and vim.api.nvim_buf_is_valid(state.buf) then
28+
vim.api.nvim_buf_delete(state.buf, { force = true })
3429
end
35-
M.qr_win = nil
36-
M.qr_buf = nil
37-
38-
-- Kill server when closing QR
30+
state.win = nil
31+
state.buf = nil
3932
kill_server()
40-
4133
Snacks.notifier.notify("Server stopped", "info")
4234
end
4335

@@ -66,10 +58,10 @@ local function show_qr(url)
6658
table.insert(lines, "")
6759

6860
-- Create buffer
69-
M.qr_buf = vim.api.nvim_create_buf(false, true)
70-
vim.api.nvim_buf_set_lines(M.qr_buf, 0, -1, false, lines)
71-
vim.bo[M.qr_buf].modifiable = false
72-
vim.bo[M.qr_buf].bufhidden = "wipe"
61+
state.buf = vim.api.nvim_create_buf(false, true)
62+
vim.api.nvim_buf_set_lines(state.buf, 0, -1, false, lines)
63+
vim.bo[state.buf].modifiable = false
64+
vim.bo[state.buf].bufhidden = "wipe"
7365

7466
-- Calculate window size
7567
local width = 0
@@ -88,7 +80,7 @@ local function show_qr(url)
8880
local row = math.floor((win_height - height) / 2)
8981

9082
-- Create floating window
91-
M.qr_win = vim.api.nvim_open_win(M.qr_buf, true, {
83+
state.win = vim.api.nvim_open_win(state.buf, true, {
9284
relative = "editor",
9385
width = width + 4,
9486
height = height + 2,
@@ -101,12 +93,11 @@ local function show_qr(url)
10193
})
10294

10395
-- Set window options
104-
vim.wo[M.qr_win].winblend = 0
105-
96+
vim.wo[state.win].winblend = 0
97+
10698
-- Key mappings to close
107-
local close_keys = { "q", "<Esc>", "<CR>" }
108-
for _, key in ipairs(close_keys) do
109-
vim.api.nvim_buf_set_keymap(M.qr_buf, "n", key, "", {
99+
for _, key in ipairs({ "q", "<Esc>", "<CR>" }) do
100+
vim.api.nvim_buf_set_keymap(state.buf, "n", key, "", {
110101
callback = close_qr,
111102
noremap = true,
112103
silent = true,
@@ -151,12 +142,12 @@ local function parse_server_output(output)
151142
return
152143
end
153144

154-
M.server_url = url
155-
145+
state.url = url
146+
156147
-- Get server PID
157148
local pid_output = vim.fn.system("lsof -ti:" .. port)
158149
if vim.v.shell_error == 0 and pid_output ~= "" then
159-
M.server_pid = tonumber(vim.trim(pid_output))
150+
state.pid = tonumber(vim.trim(pid_output))
160151
end
161152

162153
-- Show QR code
@@ -193,7 +184,7 @@ function M.share()
193184
end
194185

195186
-- Close existing session if any
196-
if M.server_pid then
187+
if state.pid then
197188
close_qr()
198189
end
199190

0 commit comments

Comments
 (0)