commit 1fc975a0242734ce4949e07e4f8ebae24cca1b5f
parent ef17edb89f346b02a76ecc4c5a40974170d5655a
Author: breadcat <breadcat@users.noreply.github.com>
Date: Tue, 29 Jul 2025 16:06:10 +0100
Add NixSortIncludes lua script
Diffstat:
1 file changed, 31 insertions(+), 3 deletions(-)
diff --git a/home/neovim.nix b/home/neovim.nix
@@ -1,6 +1,4 @@
-{
- ...
-}: {
+{...}: {
programs.neovim = {
enable = true;
defaultEditor = true;
@@ -37,5 +35,35 @@
autocmd BufWritePre *.nix %s/\s\+$//e | retab
autocmd BufWritePost *.nix silent! execute '!alejandra -qq %' | edit
'';
+ extraLuaConfig = ''
+ -- Define and register :NixSortIncludes to sort selected Nix includes
+ vim.api.nvim_create_user_command('NixSortIncludes', function()
+ -- Get the start and end lines of the visual selection
+ local start_line = vim.fn.line("'<") - 1
+ local end_line = vim.fn.line("'>")
+
+ -- Get the lines from the current buffer
+ local lines = vim.api.nvim_buf_get_lines(0, start_line, end_line, false)
+
+ -- Function to extract path for sorting
+ local function extract_path(line)
+ local cleaned = line
+ cleaned = cleaned:gsub("^%s*%(", "") -- remove leading '('
+ cleaned = cleaned:gsub("^import%s+", "") -- remove 'import'
+ cleaned = cleaned:gsub("%s+%b{}", "") -- remove '{...}'
+ cleaned = cleaned:gsub("^%s+", "") -- trim leading spaces
+ cleaned = cleaned:gsub("%)+%s*$", "") -- remove trailing ')'
+ return cleaned
+ end
+
+ -- Sort lines using the extracted path
+ table.sort(lines, function(a, b)
+ return extract_path(a) < extract_path(b)
+ end)
+
+ -- Replace lines in the buffer
+ vim.api.nvim_buf_set_lines(0, start_line, end_line, false, lines)
+ end, { range = true })
+ '';
};
}