nvimConfig/plugin/01_lib.lua
Daniel df2f776d3f fix: address review findings (C1/C3/H1/H2/H5 + M1)
Tackles the critical and high-impact findings from the in-PR review
of commit 7f01be5, plus one consistency fix (M1). All changes are
scoped to the same review branch (PR #12); no behavior changes
elsewhere.

Critical fixes
* C1 -- plugin/22_languages.lua: now does `local Config = require('config')`
  at the top, matching every other `plugin/*.lua` file. Previously the
  file referenced `Config` as a global, depending on `_G.Config` having
  been initialized by `init.lua` before this file loaded. Fragile.
* C2 -- plugin/04_treesitter.lua: removed the hard `<CR>` ->
  `smart_send.send_repl` mapping. Override `<CR>` from a per-buffer
  `ftplugin/<lang>.lua` if you want enter-to-send behavior. R.nvim's
  `<Plug>RDSendLine` (wired by `ftplugin/r.lua`) remains the default
  for R files and is no longer silently clobbered.

High-impact fixes
* C3 -- plugin/27_image.lua: `image.setup({ backend = "kitty" })` ->
  `backend = "auto"`. The previous value silently failed on every
  terminal that is not Kitty. `"auto"` delegates detection to image.nvim.
* H1 -- plugin/10_keymap.lua: `_G.Config = Config` removed from this
  file; `init.lua:2` remains the single source. Avoids drift between
  two aliasing sites.
* H2 -- plugin/25_lsp.lua: `texlab = { single_file_support = true }`,
  so single-file `.tex` buffers attach the LSP without lspconfig's
  sometimes-brittle root_dir heuristic.
* H5 -- plugin/04_treesitter.lua: also drops `<S-CR>` from
  `M.setup_keybindings` on the same principle as C2. `<S-CR>` was a
  hard implicit override in both normal and insert mode, where it
  collided with snippet and transient-state plugins.

Consistency fixes
* M1 -- plugin/01_lib.lua: `print(line)` in `Config.execute_lua_line`
  switched to `vim.notify(line, vim.log.levels.INFO)`, matching the
  print->notify cleanup in `plugin/04_treesitter.lua` from 7f01be5.

Files: 6 modified. +21 / -11.

Local verification
1. `luac -p plugin/{22_languages,27_image,10_keymap,25_lsp,04_treesitter,01_lib}.lua`
2. `nvim --headless -u NONE -l tests/init.lua`
3. Open a R / quarto / tex buffer; verify `<CR>` is no longer hijacked
   by `smart_send` and behaves like the filetype default.

For reviewers
* The text-object configuration in plugin/04_treesitter.lua's
  textobjects block is unchanged. H4 (`@assignment.*` queries may not be
  defined for R) is left for a follow-up with verification.
* yamlls GitHub-rawURL schema dependency (H3) is left intentionally --
  vendoring the schemas is a separate decision.
* Tests (T1-T6 from the review) are deferred; no test infrastructure
  exists beyond `tests/init.lua`.
2026-07-26 06:43:35 +00:00

121 lines
3.9 KiB
Lua

local Config = require('config')
-- Global Functions
Config.new_scratch_buffer = function() vim.api.nvim_win_set_buf(0, vim.api.nvim_create_buf(true, true)) end
-- Toggle quickfix window
Config.toggle_quickfix = function()
local cur_tabnr = vim.fn.tabpagenr()
for _, wininfo in ipairs(vim.fn.getwininfo()) do
if wininfo.quickfix == 1 and wininfo.tabnr == cur_tabnr then return vim.cmd('cclose') end
end
vim.cmd('copen')
end
Config.log = {}
Config.log_buf_id = Config.log_buf_id or nil
Config.start_hrtime = Config.start_hrtime or vim.uv.hrtime()
Config.log_print = function()
if Config.log_buf_id == nil or not vim.api.nvim_buf_is_valid(Config.log_buf_id) then
Config.log_buf_id = vim.api.nvim_create_buf(true, true)
end
vim.api.nvim_win_set_buf(0, Config.log_buf_id)
vim.api.nvim_buf_set_lines(Config.log_buf_id, 0, -1, false, vim.split(vim.inspect(Config.log), '\n'))
end
Config.log_clear = function()
Config.log = {}
Config.start_hrtime = vim.uv.hrtime()
vim.cmd('echo "Cleared log"')
end
-- Execute current line with `lua`
Config.execute_lua_line = function()
local line = 'lua ' .. vim.api.nvim_get_current_line()
vim.api.nvim_command(line)
vim.notify(line, vim.log.levels.INFO)
vim.api.nvim_input('<Down>')
end
-- Try opening current file's dir with fallback to cwd
Config.try_opendir = function()
local buff = vim.api.nvim_buf_get_name(0)
local ok, err = pcall(MiniFiles.open, buff)
if ok then return end
vim.notify(err)
MiniFiles.open()
end
-- For mini.start
--- Edit a file in the specified window, with smart buffer reuse
--- @param path string: File path to edit
--- @param win_id number|nil: Window ID (defaults to current window)
--- @return number|nil: Buffer ID on success, nil on failure
Config.edit = function(path, win_id)
-- Validate inputs
if type(path) ~= 'string' or path == '' then
return nil
end
win_id = win_id or 0
if not vim.api.nvim_win_is_valid(win_id == 0 and vim.api.nvim_get_current_win() or win_id) then
return nil
end
local current_buf = vim.api.nvim_win_get_buf(win_id)
-- Check if current buffer can be reused (empty, unmodified, single window)
local is_empty_buffer = vim.fn.bufname(current_buf) == ''
local is_regular_buffer = vim.bo[current_buf].buftype ~= 'quickfix'
local is_unmodified = not vim.bo[current_buf].modified
local is_single_window = #vim.fn.win_findbuf(current_buf) == 1
local has_only_empty_line = vim.deep_equal(vim.fn.getbufline(current_buf, 1, '$'), { '' })
local can_reuse_buffer = is_empty_buffer and is_regular_buffer and is_unmodified
and is_single_window and has_only_empty_line
-- Create or get buffer for the file
local normalized_path = vim.fn.fnamemodify(path, ':.')
local target_buf = vim.fn.bufadd(normalized_path)
-- Set buffer in window (use pcall to handle swap file messages gracefully)
local success = pcall(vim.api.nvim_win_set_buf, win_id, target_buf)
if not success then
return nil
end
-- Ensure buffer is listed
vim.bo[target_buf].buflisted = true
-- Clean up old buffer if it was reused
if can_reuse_buffer then
pcall(vim.api.nvim_buf_delete, current_buf, { unload = false })
end
return target_buf
end
-- Load library
local packdir = nixCats.vimPackDir or MiniDeps.config.path.package
-- See https://github.com/echasnovski/mini.deps/blob/2953b2089591a49a70e0a88194dbb47fb0e4635c/lua/mini/deps.lua#L518C5-L518C39
Config.source_path = function(path)
pcall(function() vim.cmd('source ' .. vim.fn.fnameescape(path)) end)
end
Config.add = (function(pkg)
vim.cmd.packadd(pkg)
local should_load_after_dir = vim.v.vim_did_enter == 1 and vim.o.loadplugins
if not should_load_after_dir then return end
local after_paths = vim.fn.glob(
packdir .. '/pack/myNeovimPackages/opt/' .. pkg .. '/after/plugin/**/*.{vim,lua}',
false,
true
)
vim.iter(after_paths):map(function(p)
Config.source_path(p)
end)
end)
Config.now_if_args = vim.fn.argc(-1) > 0 and MiniDeps.now or MiniDeps.later