switched to spaces to better use the alignment thing

This commit is contained in:
roodletoof
2025-01-25 17:14:24 +01:00
parent b36821d364
commit c9bf401cc2
+389 -389
View File
@@ -1,79 +1,78 @@
-- vim:foldmethod=marker -- vim:foldmethod=marker
-- GENERAL SETTINGS {{{1 -- GENERAL SETTINGS {{{1
vim.opt.tabstop = 8
vim.opt.shiftwidth = 0
vim.opt.rnu = true
vim.opt.nu = true
vim.opt.wrap = false
vim.opt.shiftround = true
vim.opt.expandtab = false
vim.opt.hlsearch = false
vim.opt.incsearch = true
vim.opt.scrolloff = 8
vim.o.exrc = true -- Enable local project configuration files vim.o.exrc = true -- Enable local project configuration files
vim.o.secure = true -- Disable potentially unsafe commands in .nvimrc vim.o.secure = true -- Disable potentially unsafe commands in .nvimrc
vim.cmd [[ vim.cmd [[
set clipboard=unnamedplus set clipboard=unnamedplus
set cursorline set cursorline
set tabstop=4
set shiftwidth=0
set rnu
set nu
set nowrap
set shiftround
set expandtab
set nohlsearch
set incsearch
set scrolloff=8
nnoremap ,co :copen<CR> nnoremap ,co :copen<CR>
nnoremap ,cc :cclose<CR> nnoremap ,cc :cclose<CR>
nnoremap ,cq :call setqflist([])<CR>:cclose<CR> nnoremap ,cq :call setqflist([])<CR>:cclose<CR>
nnoremap ,ct :call setqflist([{'filename': expand('%'), 'lnum': line('.'), 'col': col('.'), 'text': 'TODO'}], 'a')<CR> nnoremap ,ct :call setqflist([{'filename': expand('%'), 'lnum': line('.'), 'col': col('.'), 'text': 'TODO'}], 'a')<CR>
nnoremap ,cf :cfirst<CR> nnoremap ,cf :cfirst<CR>
nnoremap ,cl :clast<CR> nnoremap ,cl :clast<CR>
nnoremap <c-n> :cnext<CR> nnoremap <c-n> :cnext<CR>
nnoremap <c-p> :cprevious<CR> nnoremap <c-p> :cprevious<CR>
nnoremap ,cd :cd %:p:h<CR> nnoremap ,cd :cd %:p:h<CR>
nnoremap ,cu :colder<CR> nnoremap ,cu :colder<CR>
nnoremap ,cr :cnewer<CR> nnoremap ,cr :cnewer<CR>
nnoremap ,cD :call setqflist(filter(getqflist(), 'v:val != getqflist()[getqflist({"idx": 0}).idx - 1]'))<CR> nnoremap ,cD :call setqflist(filter(getqflist(), 'v:val != getqflist()[getqflist({"idx": 0}).idx - 1]'))<CR>
tnoremap <esc> <c-\><c-n> tnoremap <esc> <c-\><c-n>
autocmd BufEnter * if &buftype == 'terminal' | :startinsert | endif autocmd BufEnter * if &buftype == 'terminal' | :startinsert | endif
nnoremap ,t <c-w>v<c-w>l:terminal<CR>a nnoremap ,t <c-w>v<c-w>l:terminal<CR>a
autocmd BufWinLeave *.* silent! mkview autocmd BufWinLeave *.* silent! mkview
autocmd BufWinEnter *.* silent! loadview autocmd BufWinEnter *.* silent! loadview
nnoremap <c-h> <c-w>h nnoremap <c-h> <c-w>h
nnoremap <c-j> <c-w>j nnoremap <c-j> <c-w>j
nnoremap <c-k> <c-w>k nnoremap <c-k> <c-w>k
nnoremap <c-l> <c-w>l nnoremap <c-l> <c-w>l
nnoremap ,v <c-w>v nnoremap ,v <c-w>v
tnoremap <c-h> <c-\><c-n><c-w>h tnoremap <c-h> <c-\><c-n><c-w>h
tnoremap <c-j> <c-\><c-n><c-w>j tnoremap <c-j> <c-\><c-n><c-w>j
tnoremap <c-k> <c-\><c-n><c-w>k tnoremap <c-k> <c-\><c-n><c-w>k
tnoremap <c-l> <c-\><c-n><c-w>l tnoremap <c-l> <c-\><c-n><c-w>l
tnoremap <c-w>c <c-\><c-n><c-w>c tnoremap <c-w>c <c-\><c-n><c-w>c
autocmd TextYankPost * silent! lua vim.highlight.on_yank {higroup='Visual', timeout=100} autocmd TextYankPost * silent! lua vim.highlight.on_yank {higroup='Visual', timeout=100}
let g:rustfmt_autosave = 1 let g:rustfmt_autosave = 1
]] ]]
vim.g.c_syntax_for_h = 1 vim.g.c_syntax_for_h = 1
vim.g.python_indent = { -- Fixes retarded default python indentation. vim.g.python_indent = { -- Fixes retarded default python indentation.
open_paren = 'shiftwidth()', open_paren = 'shiftwidth()',
nested_paren = 'shiftwidth()', nested_paren = 'shiftwidth()',
continue = 'shiftwidth()', continue = 'shiftwidth()',
closed_paren_align_last_line = false, closed_paren_align_last_line = false,
searchpair_timeout = 300, searchpair_timeout = 300,
} }
local function file_exists(name) --{{{1 local function file_exists(name) --{{{1
local f = io.open(name,"r") local f = io.open(name,"r")
if f~=nil then if f~=nil then
f:close() f:close()
return true return true
else else
return false return false
end end
end end
@@ -81,376 +80,377 @@ end
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim" local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then if not (vim.uv or vim.loop).fs_stat(lazypath) then
vim.fn.system({ vim.fn.system({
"git", "git",
"clone", "clone",
"--filter=blob:none", "--filter=blob:none",
"https://github.com/folke/lazy.nvim.git", "https://github.com/folke/lazy.nvim.git",
"--branch=stable", -- latest stable release "--branch=stable", -- latest stable release
lazypath, lazypath,
}) })
end end
vim.opt.rtp:prepend(lazypath) vim.opt.rtp:prepend(lazypath)
require'lazy'.setup{ --{{{1 require'lazy'.setup{ --{{{1
{ 'justinmk/vim-sneak', --{{{2 { 'justinmk/vim-sneak', --{{{2
}, },
{ 'michaeljsmith/vim-indent-object', --{{{2 { 'michaeljsmith/vim-indent-object', --{{{2
}, },
{ 'kylechui/nvim-surround', --{{{2 { 'kylechui/nvim-surround', --{{{2
version = '*', -- Use for stability; omit to use `main` branch for the latest features version = '*', -- Use for stability; omit to use `main` branch for the latest features
event = 'VeryLazy', event = 'VeryLazy',
config = function() config = function()
require('nvim-surround').setup{} require('nvim-surround').setup{}
end end
}, },
{ 'echasnovski/mini.align', --{{{2 { 'echasnovski/mini.align', --{{{2
version = false, version = false,
config = function() config = function()
require'mini.align'.setup() require'mini.align'.setup()
end, end,
}, },
{ 'sainnhe/everforest', --{{{2 { 'sainnhe/everforest', --{{{2
lazy = false, lazy = false,
priority = 1000, priority = 1000,
config = function() config = function()
vim.o.termguicolors = true vim.o.termguicolors = true
vim.g.everforest_enable_italic = true vim.g.everforest_enable_italic = true
vim.cmd.colorscheme('everforest') vim.cmd.colorscheme('everforest')
end, end,
}, },
{ 'folke/zen-mode.nvim', --{{{2 { 'folke/zen-mode.nvim', --{{{2
config = function () config = function ()
vim.keymap.set( vim.keymap.set(
'n', 'n',
",z", ",z",
vim.cmd.ZenMode, vim.cmd.ZenMode,
{ silent = true } { silent = true }
) )
end end
}, },
{ 'stevearc/oil.nvim', --{{{2 { 'stevearc/oil.nvim', --{{{2
dependencies = { 'nvim-tree/nvim-web-devicons', }, dependencies = { 'nvim-tree/nvim-web-devicons', },
config = function () config = function ()
local oil = require('oil') local oil = require('oil')
oil.setup{ oil.setup{
default_file_explorer = true, default_file_explorer = true,
columns = {'icon'}, columns = {'icon'},
view_options = { view_options = {
show_hidden = true, show_hidden = true,
}, },
lsp_file_methods = { lsp_file_methods = {
enables = true, enables = true,
timeout_ms = 1000, timeout_ms = 1000,
autosave_changes = true, autosave_changes = true,
}, },
keymaps = { keymaps = {
[",cd"] = { "actions.cd", mode = "n" }, [",cd"] = { "actions.cd", mode = "n" },
}, },
} }
vim.keymap.set("n", "-", vim.cmd.Oil, { desc = "Open parent directory" }) vim.keymap.set("n", "-", vim.cmd.Oil, { desc = "Open parent directory" })
end, end,
}, },
{ 'neovim/nvim-lspconfig', --{{{2 { 'neovim/nvim-lspconfig', --{{{2
config = function() config = function()
require'lspconfig'.gopls.setup{} require'lspconfig'.gopls.setup{}
require'lspconfig'.rust_analyzer.setup{} require'lspconfig'.rust_analyzer.setup{}
require'lspconfig'.gdscript.setup{} require'lspconfig'.gdscript.setup{}
require'lspconfig'.clangd.setup{} require'lspconfig'.clangd.setup{}
require'lspconfig'.pyright.setup{} require'lspconfig'.pyright.setup{}
require'lspconfig'.ts_ls.setup{} require'lspconfig'.ts_ls.setup{}
require'lspconfig'.jdtls.setup{} require'lspconfig'.jdtls.setup{}
vim.cmd [[ vim.cmd [[
noremap ,rn :lua vim.lsp.buf.rename()<CR> noremap ,rn :lua vim.lsp.buf.rename()<CR>
noremap ,fd :lua vim.lsp.buf.definition()<CR> noremap ,fd :lua vim.lsp.buf.definition()<CR>
noremap ,ft :lua vim.lsp.buf.type_definition()<CR> noremap ,ft :lua vim.lsp.buf.type_definition()<CR>
noremap ,fr :lua vim.lsp.buf.references()<CR> noremap ,fr :lua vim.lsp.buf.references()<CR>
noremap ,ca :lua vim.lsp.buf.code_action()<CR> noremap ,ca :lua vim.lsp.buf.code_action()<CR>
noremap ,oe :lua vim.diagnostic.open_float()<CR> noremap ,oe :lua vim.diagnostic.open_float()<CR>
noremap ,ea :lua vim.diagnostic.setqflist()<CR> noremap ,ea :lua vim.diagnostic.setqflist()<CR>
noremap ,ee :lua vim.diagnostic.setqflist{severity="ERROR"}<CR> noremap ,ee :lua vim.diagnostic.setqflist{severity="ERROR"}<CR>
noremap ,ew :lua vim.diagnostic.setqflist{severity="WARN"}<CR> noremap ,ew :lua vim.diagnostic.setqflist{severity="WARN"}<CR>
noremap ,ei :lua vim.diagnostic.setqflist{severity="INFO"}<CR> noremap ,ei :lua vim.diagnostic.setqflist{severity="INFO"}<CR>
noremap ,eh :lua vim.diagnostic.setqflist{severity="HINT"}<CR> noremap ,eh :lua vim.diagnostic.setqflist{severity="HINT"}<CR>
]] ]]
end end
}, },
{ 'mfussenegger/nvim-dap', --{{{2 { 'mfussenegger/nvim-dap', --{{{2
dependencies = { dependencies = {
'nvim-treesitter/nvim-treesitter', 'nvim-treesitter/nvim-treesitter',
'theHamsta/nvim-dap-virtual-text', 'theHamsta/nvim-dap-virtual-text',
'leoluz/nvim-dap-go', 'leoluz/nvim-dap-go',
'mfussenegger/nvim-dap-python', 'mfussenegger/nvim-dap-python',
}, },
config = function() config = function()
require'nvim-dap-virtual-text'.setup{ commented = true, } require'nvim-dap-virtual-text'.setup{ commented = true, }
require'dap-go'.setup() require'dap-go'.setup()
require'dap-python'.setup(vim.fn.stdpath('config') .. '/.venv/bin/python') require'dap-python'.setup(vim.fn.stdpath('config') .. '/.venv/bin/python')
local dap = require'dap' local dap = require'dap'
dap.adapters.godot = { type = 'server', host = '127.0.0.1', port = 6006, } dap.adapters.godot = { type = 'server', host = '127.0.0.1', port = 6006, }
dap.configurations.gdscript = { {type = 'godot', request = 'launch', name = 'Launch scene', project = "${workspaceFolder}",} } dap.configurations.gdscript = { {type = 'godot', request = 'launch', name = 'Launch scene', project = "${workspaceFolder}",} }
vim.cmd [[ vim.cmd [[
nnoremap ,b :DapToggleBreakpoint<CR> nnoremap ,b :DapToggleBreakpoint<CR>
nnoremap ,B :DapClearBreakpoints<CR> nnoremap ,B :DapClearBreakpoints<CR>
nnoremap <B :DapClearBreakpoints<CR> nnoremap <B :DapClearBreakpoints<CR>
nnoremap ,db :DapContinue<CR> nnoremap ,db :DapContinue<CR>
nnoremap <Down> :DapStepInto<CR> nnoremap <Down> :DapStepInto<CR>
nnoremap <UP> :DapStepOut<CR> nnoremap <UP> :DapStepOut<CR>
nnoremap <Right> :DapStepOver<CR> nnoremap <Right> :DapStepOver<CR>
]] ]]
end end
}, },
{ 'dcampos/nvim-snippy', --{{{2 { 'dcampos/nvim-snippy', --{{{2
config = function() config = function()
require'snippy'.setup{ enable_auto = true, } require'snippy'.setup{ enable_auto = true, }
vim.cmd [[ vim.cmd [[
imap <expr> <c-l> '<Plug>(snippy-next)' imap <expr> <c-l> '<Plug>(snippy-next)'
imap <expr> <c-k> '<Plug>(snippy-previous)' imap <expr> <c-k> '<Plug>(snippy-previous)'
smap <expr> <c-l> '<Plug>(snippy-next)' smap <expr> <c-l> '<Plug>(snippy-next)'
smap <expr> <c-k> '<Plug>(snippy-previous)' smap <expr> <c-k> '<Plug>(snippy-previous)'
xmap <Tab> <Plug>(snippy-cut-text) xmap <Tab> <Plug>(snippy-cut-text)
]] ]]
vim.api.nvim_create_user_command( vim.api.nvim_create_user_command(
'S', 'S',
function () function ()
---@type string ---@type string
local snippets_path = vim.fn.stdpath('config') .. '/snippets/' .. vim.api.nvim_buf_get_option(0, "filetype") .. '.snippets' local snippets_path = vim.fn.stdpath('config') .. '/snippets/' .. vim.api.nvim_buf_get_option(0, "filetype") .. '.snippets'
if not file_exists(snippets_path) then if not file_exists(snippets_path) then
local file = io.open( snippets_path, 'w' ) local file = io.open( snippets_path, 'w' )
assert( assert(
file ~= nil, file ~= nil,
("io.open('%s', 'w') returned nil.\n"):format(snippets_path) .. ("io.open('%s', 'w') returned nil.\n"):format(snippets_path) ..
"Make sure the snippets folder in the above path exists." "Make sure the snippets folder in the above path exists."
) )
file:close() file:close()
print('created file: ', snippets_path) print('created file: ', snippets_path)
end end
vim.api.nvim_command(('SnippyEdit %s'):format(snippets_path)) vim.api.nvim_command(('SnippyEdit %s'):format(snippets_path))
end, end,
{ nargs = 0 } { nargs = 0 }
) )
end end
}, },
{ 'hrsh7th/nvim-cmp', --{{{2 { 'hrsh7th/nvim-cmp', --{{{2
dependencies = { dependencies = {
'hrsh7th/cmp-nvim-lsp', 'hrsh7th/cmp-nvim-lsp',
'hrsh7th/cmp-buffer', 'hrsh7th/cmp-buffer',
'hrsh7th/cmp-path', 'hrsh7th/cmp-path',
'dcampos/nvim-snippy', 'dcampos/nvim-snippy',
'dcampos/cmp-snippy', 'dcampos/cmp-snippy',
}, },
config = function() config = function()
local cmp = require'cmp' local cmp = require'cmp'
cmp.setup{ cmp.setup{
snippet = { snippet = {
expand = function(args) expand = function(args)
require'snippy'.expand_snippet(args.body) require'snippy'.expand_snippet(args.body)
end, end,
}, },
mapping = { mapping = {
['<C-y>'] = cmp.mapping.confirm{ select = true }, ['<C-y>'] = cmp.mapping.confirm{ select = true },
['<C-n>'] = cmp.mapping.select_next_item(), ['<C-n>'] = cmp.mapping.select_next_item(),
['<C-p>'] = cmp.mapping.select_prev_item(), ['<C-p>'] = cmp.mapping.select_prev_item(),
}, },
sources = cmp.config.sources( sources = cmp.config.sources(
{ {
{ name = 'snippy', priority = 100000000000000000000 }, { name = 'snippy', priority = 100000000000000000000 },
{ name = 'nvim_lsp', priority = 100}, { name = 'nvim_lsp', priority = 100},
{ name = 'path', priority = 1}, { name = 'path', priority = 1},
} }
), ),
preselect = cmp.PreselectMode.None, preselect = cmp.PreselectMode.None,
} }
end, end,
}, },
{ 'nvim-telescope/telescope.nvim', --{{{2 { 'nvim-telescope/telescope.nvim', --{{{2
tag = '0.1.8', tag = '0.1.8',
dependencies = { dependencies = {
'nvim-lua/plenary.nvim', 'nvim-lua/plenary.nvim',
'nvim-telescope/telescope-ui-select.nvim', 'nvim-telescope/telescope-ui-select.nvim',
}, },
config = function() config = function()
local a = require'telescope.actions' local a = require'telescope.actions'
require'telescope'.setup{ require'telescope'.setup{
defaults = { defaults = {
mappings = { mappings = {
i = { ["<C-Q>"] = a.smart_send_to_qflist + a.open_qflist, ["<C-j>"] = a.select_default, }, i = { ["<C-Q>"] = a.smart_send_to_qflist + a.open_qflist, ["<C-j>"] = a.select_default, },
n = { ["<C-Q>"] = a.smart_send_to_qflist + a.open_qflist, ["<C-j>"] = a.select_default, }, n = { ["<C-Q>"] = a.smart_send_to_qflist + a.open_qflist, ["<C-j>"] = a.select_default, },
} }
}, },
extensions = { ['ui-select'] = { require'telescope.themes'.get_dropdown{}, }, }, extensions = { ['ui-select'] = { require'telescope.themes'.get_dropdown{}, }, },
} }
vim.cmd [[ vim.cmd [[
noremap ,ff :lua require'telescope.builtin'.find_files()<CR> noremap ,ff :lua require'telescope.builtin'.find_files()<CR>
noremap ,fo :lua require'telescope.builtin'.oldfiles()<CR> noremap ,fo :lua require'telescope.builtin'.oldfiles()<CR>
noremap ,fg :lua require'telescope.builtin'.live_grep()<CR> noremap ,fg :lua require'telescope.builtin'.live_grep()<CR>
noremap ,fz :lua require'telescope.builtin'.current_buffer_fuzzy_find()<CR> noremap ,fz :lua require'telescope.builtin'.current_buffer_fuzzy_find()<CR>
noremap ,fh :lua require'telescope.builtin'.help_tags()<CR> noremap ,fh :lua require'telescope.builtin'.help_tags()<CR>
noremap ,fm :lua require'telescope.builtin'.marks()<CR> noremap ,fm :lua require'telescope.builtin'.marks()<CR>
noremap ,fb :lua require'telescope.builtin'.buffers()<CR> noremap ,fb :lua require'telescope.builtin'.buffers()<CR>
noremap ,fct :lua require'telescope.builtin'.tags()<CR> noremap ,fct :lua require'telescope.builtin'.tags()<CR>
noremap ,fea :lua require'telescope.builtin'.diagnostics()<CR> noremap ,fea :lua require'telescope.builtin'.diagnostics()<CR>
noremap ,fee :lua require'telescope.builtin'.diagnostics{severity="ERROR"}<CR> noremap ,fee :lua require'telescope.builtin'.diagnostics{severity="ERROR"}<CR>
noremap ,few :lua require'telescope.builtin'.diagnostics{severity="WARN"}<CR> noremap ,few :lua require'telescope.builtin'.diagnostics{severity="WARN"}<CR>
noremap ,fei :lua require'telescope.builtin'.diagnostics{severity="INFO"}<CR> noremap ,fei :lua require'telescope.builtin'.diagnostics{severity="INFO"}<CR>
noremap ,feh :lua require'telescope.builtin'.diagnostics{severity="HINT"}<CR> noremap ,feh :lua require'telescope.builtin'.diagnostics{severity="HINT"}<CR>
]] ]]
require'telescope'.load_extension'ui-select' require'telescope'.load_extension'ui-select'
end, end,
}, },
} }
do -- split line {{{1 do -- split line {{{1
local SPLIT_WHITESPACE = ' ' -- TODO make this dependant on my tabbin settings
local SPLIT_DELIMETERS = { -- single characters only local SPLIT_WHITESPACE = ' '
[','] = true, local SPLIT_DELIMETERS = { -- single characters only
[';'] = true, [','] = true,
} [';'] = true,
local SPLIT_BETWEEN = { -- single characters only }
['('] = ')', local SPLIT_BETWEEN = { -- single characters only
['['] = ']', ['('] = ')',
['{'] = '}', ['['] = ']',
['<'] = '>', ['{'] = '}',
} ['<'] = '>',
local SPLIT_IGNORE_BETWEEN = { --single characters only }
['"'] = '"', local SPLIT_IGNORE_BETWEEN = { --single characters only
["'"] = "'", ['"'] = '"',
} ["'"] = "'",
}
local split_line = function() local split_line = function()
local line = vim.api.nvim_get_current_line() local line = vim.api.nvim_get_current_line()
local _, col = unpack(vim.api.nvim_win_get_cursor(0)) local _, col = unpack(vim.api.nvim_win_get_cursor(0))
col = col + 1 -- Doing this to make it 1-indexed col = col + 1 -- Doing this to make it 1-indexed
---@type integer? ---@type integer?
local first_bracket_i = nil local first_bracket_i = nil
for i = col, #line do for i = col, #line do
local char = line:sub(i, i) local char = line:sub(i, i)
if SPLIT_BETWEEN[char] ~= nil then if SPLIT_BETWEEN[char] ~= nil then
first_bracket_i = i first_bracket_i = i
break break
end end
end end
if not first_bracket_i then if not first_bracket_i then
print('No opening brackets found after cursor on this line.') print('No opening brackets found after cursor on this line.')
return return
end end
---@type integer[] ---@type integer[]
local split_indexes = {} -- Populate this array local split_indexes = {} -- Populate this array
---@type integer? ---@type integer?
local last_bracket_i = nil -- And find this index local last_bracket_i = nil -- And find this index
do do
---@type string[] ---@type string[]
local closing_bracket_stack = {} local closing_bracket_stack = {}
local icon_to_close_ignore = '' local icon_to_close_ignore = ''
local in_ignore = false local in_ignore = false
for i = first_bracket_i, #line do for i = first_bracket_i, #line do
local char = line:sub(i,i) local char = line:sub(i,i)
if in_ignore then if in_ignore then
in_ignore = not (char == icon_to_close_ignore) in_ignore = not (char == icon_to_close_ignore)
goto continue goto continue
end end
if SPLIT_IGNORE_BETWEEN[char] ~= nil then if SPLIT_IGNORE_BETWEEN[char] ~= nil then
icon_to_close_ignore = SPLIT_IGNORE_BETWEEN[char] icon_to_close_ignore = SPLIT_IGNORE_BETWEEN[char]
in_ignore = true in_ignore = true
goto continue goto continue
end end
-- string handling complete -- string handling complete
if SPLIT_BETWEEN[char] ~= nil then if SPLIT_BETWEEN[char] ~= nil then
table.insert( table.insert(
closing_bracket_stack, closing_bracket_stack,
SPLIT_BETWEEN[char] SPLIT_BETWEEN[char]
) )
end end
if char == closing_bracket_stack[#closing_bracket_stack] then if char == closing_bracket_stack[#closing_bracket_stack] then
table.remove(closing_bracket_stack) table.remove(closing_bracket_stack)
end end
if #closing_bracket_stack == 1 and SPLIT_DELIMETERS[char] then if #closing_bracket_stack == 1 and SPLIT_DELIMETERS[char] then
table.insert(split_indexes, i) table.insert(split_indexes, i)
end end
if #closing_bracket_stack == 0 then if #closing_bracket_stack == 0 then
last_bracket_i = i last_bracket_i = i
break break
end end
::continue:: ::continue::
end end
end end
if not last_bracket_i then if not last_bracket_i then
print("The first opening bracket found after the cursor was not closed on this line.") print("The first opening bracket found after the cursor was not closed on this line.")
return return
end end
if #split_indexes == 0 then if #split_indexes == 0 then
print('No comma separated items within brackets that were opened and closed after the cursor.') print('No comma separated items within brackets that were opened and closed after the cursor.')
return return
end end
---@type string ---@type string
local leading_whitespace = string.match(line, "^%s*") local leading_whitespace = string.match(line, "^%s*")
local first_line = line:sub(1, first_bracket_i) local first_line = line:sub(1, first_bracket_i)
local last_line = leading_whitespace .. line:sub(last_bracket_i, #line) local last_line = leading_whitespace .. line:sub(last_bracket_i, #line)
local middle_lines = {} local middle_lines = {}
table.insert( table.insert(
middle_lines, middle_lines,
line:sub(first_bracket_i+1, split_indexes[1]) line:sub(first_bracket_i+1, split_indexes[1])
) )
for i = 1, #split_indexes-1 do for i = 1, #split_indexes-1 do
table.insert( table.insert(
middle_lines, middle_lines,
line:sub(split_indexes[i], split_indexes[i+1]) line:sub(split_indexes[i], split_indexes[i+1])
) )
end end
table.insert( table.insert(
middle_lines, middle_lines,
line:sub(split_indexes[#split_indexes], last_bracket_i-1) line:sub(split_indexes[#split_indexes], last_bracket_i-1)
) )
local leading_pattern = "^[%s" local leading_pattern = "^[%s"
for k, _ in pairs(SPLIT_DELIMETERS) do for k, _ in pairs(SPLIT_DELIMETERS) do
leading_pattern = leading_pattern .. k leading_pattern = leading_pattern .. k
end end
leading_pattern = leading_pattern .. "]*" leading_pattern = leading_pattern .. "]*"
-- Cleanup step -- Cleanup step
for i, middle_line in ipairs(middle_lines) do for i, middle_line in ipairs(middle_lines) do
middle_line = middle_line:gsub(leading_pattern, leading_whitespace .. SPLIT_WHITESPACE, 1) middle_line = middle_line:gsub(leading_pattern, leading_whitespace .. SPLIT_WHITESPACE, 1)
middle_line = middle_line:gsub("[%s]*$", '', 1) middle_line = middle_line:gsub("[%s]*$", '', 1)
middle_lines[i] = middle_line middle_lines[i] = middle_line
end end
if middle_lines[#middle_lines]:match("^%s*$") ~= nil then if middle_lines[#middle_lines]:match("^%s*$") ~= nil then
table.remove(middle_lines) table.remove(middle_lines)
end end
local row, _ = unpack(vim.api.nvim_win_get_cursor(0)) local row, _ = unpack(vim.api.nvim_win_get_cursor(0))
vim.api.nvim_buf_set_lines(0, row-1, row, false, {first_line}) vim.api.nvim_buf_set_lines(0, row-1, row, false, {first_line})
vim.api.nvim_buf_set_lines(0, row, row, false, {last_line}) vim.api.nvim_buf_set_lines(0, row, row, false, {last_line})
vim.api.nvim_buf_set_lines(0, row, row, false, middle_lines) vim.api.nvim_buf_set_lines(0, row, row, false, middle_lines)
end end
vim.keymap.set( vim.keymap.set(
'n', 'n',
",s", ",s",
split_line, split_line,
{ silent = true } { silent = true }
) )
end end