wrapped some stuff in do-end blocks

This commit is contained in:
Ivar Fatland
2023-06-30 23:57:41 +02:00
parent 99b8865480
commit ab675f82af
+80 -74
View File
@@ -243,95 +243,101 @@ local function packer_startup(use)
} }
end end
local function file_exists(name) do
local f = io.open(name,"r") local function file_exists(name)
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
-- Opens a default .snippets file for the filetype you are currently editing in a horizontal split pane.
-- If the .snippets file does not exist, it will be created.
-- This requires the snippets folder to exist in the config folder.
-- If the folder does not exist, the command will print out a helpful error message showing what the path
-- should look like.
vim.api.nvim_create_user_command(
'S',
function ()
---@type string
local snippets_path = vim.fn.stdpath('config') .. '/snippets/' ..
vim.api.nvim_buf_get_option(0, "filetype") .. '.snippets'
if not file_exists(snippets_path) then
local file = io.open( snippets_path, 'w' )
assert(
file ~= nil,
("io.open('%s', 'w') returned nil.\n"):format(snippets_path) ..
"Make sure the snippets folder in the above path exists."
)
file:close()
print('created file: ', snippets_path)
end
vim.api.nvim_command(('SnippyEdit %s'):format(snippets_path))
end,
{ nargs = 0 }
)
end end
-- Opens a default .snippets file for the filetype you are currently editing in a horizontal split pane. -- local function split_line()
-- If the .snippets file does not exist, it will be created. -- local line = vim.api.nvim_get_current_line()
-- This requires the snippets folder to exist in the config folder. -- local items = vim.split(line, ", *")
-- If the folder does not exist, the command will print out a helpful error message showing what the path -- local indent = string.match(line, "^%s*")
-- should look like.
vim.api.nvim_create_user_command(
'S',
function ()
---@type string
local snippets_path = vim.fn.stdpath('config') .. '/snippets/' ..
vim.api.nvim_buf_get_option(0, "filetype") .. '.snippets'
if not file_exists(snippets_path) then -- local formatted_items = {items[1] .. ','}
local file = io.open( snippets_path, 'w' ) -- for i = 2, #items -1 do
assert( -- table.insert(formatted_items, indent .. tab_whitespace .. items[i] .. ',')
file ~= nil, -- end
("io.open('%s', 'w') returned nil.\n"):format(snippets_path) .. -- table.insert(formatted_items, indent .. tab_whitespace .. items[#items] )
"Make sure the snippets folder in the above path exists."
)
file:close()
print('created file: ', snippets_path)
end
vim.api.nvim_command(('SnippyEdit %s'):format(snippets_path)) -- local row, _ = unpack(vim.api.nvim_win_get_cursor(0))
end, -- vim.api.nvim_buf_set_lines(0, row-1, row, false, formatted_items)
{ nargs = 0 } -- end
)
local tab_whitespace = '' local split_line
for _ = 1, tab_width do do
tab_whitespace = tab_whitespace .. ' ' local tab_whitespace = ''
end for _ = 1, tab_width do
tab_whitespace = tab_whitespace .. ' '
local function split_line()
local line = vim.api.nvim_get_current_line()
local items = vim.split(line, ", *")
local indent = string.match(line, "^%s*")
local formatted_items = {items[1] .. ','}
for i = 2, #items -1 do
table.insert(formatted_items, indent .. tab_whitespace .. items[i] .. ',')
end end
table.insert(formatted_items, indent .. tab_whitespace .. items[#items] )
local row, _ = unpack(vim.api.nvim_win_get_cursor(0)) local BRACKETS = {
vim.api.nvim_buf_set_lines(0, row-1, row, false, formatted_items) ['{'] ='}',
end ['['] = ']',
['('] = ')'
}
local BRACKETS = { split_line = function()
['{'] ='}', local line = vim.api.nvim_get_current_line()
['['] = ']', local leading_whitespace = string.match(line, "^%s*")
['('] = ')' local leading_indented_whitespace = leading_whitespace .. tab_whitespace
}
local function split_line()
local line = vim.api.nvim_get_current_line()
local leading_whitespace = string.match(line, "^%s*")
local leading_indented_whitespace = leading_whitespace .. tab_whitespace
local first_bracket_i = string.find(line, "[%(%[{]") local first_bracket_i = string.find(line, "[%(%[{]")
local index_ranges = {} -- populate this list with a series of index ranges local index_ranges = {} -- populate this list with a series of index ranges
-- An index range is a list of two numbers: start, end (inclusive) -- An index range is a list of two numbers: start, end (inclusive)
local last_bracket_i = nil -- And find this index local last_bracket_i = nil -- And find this index
local curr_string_symbol = nil local curr_string_symbol = nil
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 (not curr_string_symbol) and (char == '"' or char == "'") then if (not curr_string_symbol) and (char == '"' or char == "'") then
curr_string_symbol = char curr_string_symbol = char
goto continue goto continue
end
if curr_string_symbol == char then
curr_string_symbol = nil
goto continue
end
::continue::
end end
if curr_string_symbol == char then
curr_string_symbol = nil
goto continue
end
::continue::
end end
end end