From 715342bba748df1bd70a69f9ad84914baabf22d0 Mon Sep 17 00:00:00 2001 From: max397574 Date: Fri, 12 Nov 2021 17:44:04 +0100 Subject: =?UTF-8?q?refactor(utils/init):=20=F0=9F=94=84use=20more=20local?= =?UTF-8?q?=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/startup/init.lua | 66 +++++++++++++++++++++++---------------------------- lua/startup/utils.lua | 28 +++++++++++++++------- 2 files changed, 50 insertions(+), 44 deletions(-) (limited to 'lua/startup') diff --git a/lua/startup/init.lua b/lua/startup/init.lua index 6925ad5..61d0c26 100644 --- a/lua/startup/init.lua +++ b/lua/startup/init.lua @@ -9,15 +9,22 @@ startup.open_sections = {} startup.good_lines = {} startup.settings = require("startup.config") +local get_cur_line = vim.api.nvim_get_current_line + +---set option in buffer +local set_buf_opt = vim.api.nvim_buf_set_option + local section_alignments = {} +local startup_nvim_displayed +local startup_nvim_loaded + local current_section = "" local opts = { noremap = true, silent = true } local settings = require("startup.config") local utils = require("startup.utils") -local spaces = utils.spaces ---creates a mapping for the current buffer ---@param mapping string the mapping to use @@ -28,9 +35,9 @@ end ---open fold under cursor function startup.open_section() - vim.api.nvim_buf_set_option(0, "modifiable", true) + set_buf_opt(0, "modifiable", true) local line_nr = vim.api.nvim_win_get_cursor(0)[1] - local section_name = vim.trim(vim.api.nvim_get_current_line()) + local section_name = vim.trim(get_cur_line()) local section_align = section_alignments[section_name] local section_highlight = startup.section_highlights[section_name] local section_entries = startup.sections[section_name] @@ -65,7 +72,7 @@ function startup.open_section() table.insert(startup.open_sections, section_name) vim.cmd([[silent! %s/\s\+$//]]) -- clear trailing whitespace vim.api.nvim_win_set_cursor(0, { line_nr, math.floor(vim.o.columns / 2) }) - vim.api.nvim_buf_set_option(0, "modifiable", false) + set_buf_opt(0, "modifiable", false) end local function create_mappings(mappings) @@ -91,13 +98,7 @@ local function create_mappings(mappings) ) if mappings ~= {} then for _, cmd in pairs(mappings) do - vim.api.nvim_buf_set_keymap( - 0, - "n", - cmd[2], - "" .. cmd[1] .. "", - opts - ) + buf_map(cmd[2],"" .. cmd[1] .. "") end end end @@ -112,7 +113,7 @@ local sections_with_mappings = {} ---check if current line is one of the commands function startup.check_line() - local line = vim.api.nvim_get_current_line() + local line = get_cur_line() for _, section in ipairs(sections_with_mappings) do for name, command in pairs(settings[section].content) do if line:match(name) then @@ -124,14 +125,14 @@ end ---open file under cursor function startup.open_file() - local line = vim.api.nvim_get_current_line() + local line = get_cur_line() local filename = line vim.cmd("e " .. filename) end ---open file under cursor in split function startup.open_file_vsplit() - local line = vim.api.nvim_get_current_line() + local line = get_cur_line() local filename = line vim.cmd("vsplit " .. filename) end @@ -147,17 +148,17 @@ function startup.align(dict, alignment) if alignment == "center" then local space_left = vim.o.columns - max_len for _, line in ipairs(dict) do - table.insert(aligned, spaces(space_left / 2) .. line) + table.insert(aligned, utils.spaces(space_left / 2) .. line) end elseif alignment == "left" then for _, line in ipairs(dict) do - table.insert(aligned, spaces(margin_calculated) .. line) + table.insert(aligned, utils.spaces(margin_calculated) .. line) end elseif alignment == "right" then for _, line in ipairs(dict) do table.insert( aligned, - spaces(vim.o.columns - max_len - margin_calculated - 10) .. line + utils.spaces(vim.o.columns - max_len - margin_calculated - 10) .. line ) end end @@ -165,14 +166,6 @@ function startup.align(dict, alignment) return aligned end ----returns table with empty strings ----@param amount number amount of empty strings -local function empty(amount) - for _ = 1, amount, 1 do - table.insert(startup.lines, { " ", "center", false, "normal" }) - end -end - ---creates mapping names from table of mappings ---@param mappings table ---@return table @@ -201,16 +194,16 @@ function startup.mapping_names(mappings) end function startup.display() - if vim.g.startup_nvim_displayed then + if startup_nvim_displayed then return end - vim.g.startup_nvim_displayed = true + startup_nvim_displayed = true local padding_nr = 1 utils.set_buf_options() local parts = settings.parts vim.cmd([[hi link StartupFoldedSection Special]]) for _, part in ipairs(parts) do - empty(settings.options.paddings[padding_nr]) + utils.empty(settings.options.paddings[padding_nr]) padding_nr = padding_nr + 1 current_section = part local options = settings[part] @@ -274,7 +267,7 @@ function startup.display() { line, options.align, true, options.highlight } ) if settings.options.empty_lines_between_mappings then - empty(1) + utils.empty(1) end end end @@ -324,14 +317,14 @@ function startup.display() require("startup").align({ line[1] }, line[2])[1] ) end - vim.api.nvim_buf_set_option(0, "modifiable", true) + set_buf_opt(0, "modifiable", true) vim.api.nvim_buf_set_lines(0, 0, -1, true, {}) vim.api.nvim_buf_set_lines(0, 0, -1, false, startup.formatted_text) vim.cmd([[silent! %s/\s\+$//]]) -- clear trailing whitespace for linenr, line in ipairs(startup.lines) do vim.api.nvim_buf_add_highlight(0, ns, line[4], linenr - 1, 0, -1) end - vim.api.nvim_buf_set_option(0, "modifiable", false) + set_buf_opt(0, "modifiable", false) vim.api.nvim_win_set_cursor(0, { 1, 1 }) vim.api.nvim_win_set_cursor(0, { #settings.header.content + settings.options.paddings[1] + 1, @@ -345,14 +338,15 @@ end ---Create autocmds for startup.nvim and update settings with update ---@param update table the settings to use function startup.setup(update) - if vim.g.startup_nvim_loaded then + if startup_nvim_loaded then return end - vim.g.startup_nvim_loaded = true + startup_nvim_loaded = true settings = vim.tbl_deep_extend("force", settings, update or {}) startup.settings = settings vim.cmd( - [[autocmd VimEnter * lua if vim.fn.argc() == 0 then require("startup").display() end]] + [[autocmd VimEnter * lua if vim.fn.argc() == 0 then require("startup").display() end]], + [[autocmd BufRead * lua if vim.fn.argc() == 0 then require("startup").display() end]] ) vim.cmd( [[autocmd VimResized * lua if vim.bo.ft == "startup" then require"startup".redraw() end]] @@ -368,14 +362,14 @@ function startup.redraw() require("startup").align({ line[1] }, line[2])[1] ) end - vim.api.nvim_buf_set_option(0, "modifiable", true) + set_buf_opt(0, "modifiable", true) vim.api.nvim_buf_set_lines(0, 0, -1, true, {}) vim.api.nvim_buf_set_lines(0, 0, -1, false, startup.formatted_text) vim.cmd([[silent! %s/\s\+$//]]) -- clear trailing whitespace for linenr, line in ipairs(startup.lines) do vim.api.nvim_buf_add_highlight(0, ns, line[4], linenr - 1, 0, -1) end - vim.api.nvim_buf_set_option(0, "modifiable", false) + set_buf_opt(0, "modifiable", false) end return startup diff --git a/lua/startup/utils.lua b/lua/startup/utils.lua index 5c82545..3ce6275 100644 --- a/lua/startup/utils.lua +++ b/lua/startup/utils.lua @@ -2,6 +2,10 @@ local U = {} local flag = false local new_cursor_pos local help_window + +local set_buf_opt = vim.api.nvim_buf_set_option + +local line_count = function() return vim.api.nvim_buf_line_count(0) end -- local startup = require"startup" ---sets cursor to position in current window @@ -28,11 +32,19 @@ function U.spaces(amount) return string.rep(" ", amount) end +---returns table with empty strings +---@param amount number amount of empty strings +function U.empty(amount) + for _ = 1, amount, 1 do + table.insert(require"startup".lines, { " ", "center", false, "normal" }) + end +end + ---open float with all the keybindings function U.key_help() local settings = require("startup").settings local buf = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe") + set_buf_opt(buf, "bufhidden", "wipe") local lines = { " Startup.nvim Mappings ", "", @@ -53,7 +65,7 @@ function U.key_help() style = "minimal", }) vim.api.nvim_win_set_option(help_window, "winblend", 20) - vim.api.nvim_buf_set_option(buf, "modifiable", false) + set_buf_opt(buf, "modifiable", false) vim.cmd( [[autocmd CursorMoved * ++once lua require"startup.utils".close_help()]] ) @@ -191,7 +203,7 @@ local function move_down() flag = true local i if new_cursor_pos[1] > U.cursor_pos[1] then - if new_cursor_pos[1] == vim.api.nvim_buf_line_count(0) then + if new_cursor_pos[1] == line_count() then set_cursor(new_cursor_pos) i = 1 while true do @@ -218,7 +230,7 @@ local function move_down() return end i = i + 1 - if new_cursor_pos[1] + i >= vim.api.nvim_buf_line_count(0) then + if new_cursor_pos[1] + i >= line_count() then set_cursor(U.cursor_pos) flag = false return @@ -266,8 +278,8 @@ end ---set all the options that should be set for the startup buffer function U.set_buf_options() local settings = require("startup").settings - vim.api.nvim_buf_set_option(0, "bufhidden", "wipe") - vim.api.nvim_buf_set_option(0, "buftype", "nofile") + set_buf_opt(0, "bufhidden", "wipe") + set_buf_opt(0, "buftype", "nofile") vim.cmd([[set wrap]]) vim.defer_fn(function() if settings.options.disable_statuslines then @@ -276,8 +288,8 @@ function U.set_buf_options() end end,1 ) - vim.api.nvim_buf_set_option(0, "filetype", "startup") - vim.api.nvim_buf_set_option(0, "swapfile", false) + set_buf_opt(0, "filetype", "startup") + set_buf_opt(0, "swapfile", false) vim.cmd([[setlocal nonu nornu]]) end -- cgit v1.2.3