From 673af86eaa81955ce9d3619394830fbce0784b7f Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 13 Aug 2022 19:27:37 +0200 Subject: [PATCH] nvim: turn my functions into proper module --- .config/nvim/lua/my/functions.lua | 22 +++++++++++++--------- .config/nvim/lua/my/tools.lua | 6 +++++- .config/nvim/snippets/cpp.lua | 15 +++++++-------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/.config/nvim/lua/my/functions.lua b/.config/nvim/lua/my/functions.lua index ab7a481..b05195b 100644 --- a/.config/nvim/lua/my/functions.lua +++ b/.config/nvim/lua/my/functions.lua @@ -1,4 +1,6 @@ -function MY_remove_trailing_whitespace() +local M = {} + +function M.remove_trailing_whitespace() local curpos = vim.api.nvim_win_get_cursor(0) vim.cmd([[keeppatterns %s/\s\+$//e]]) vim.api.nvim_win_set_cursor(0, curpos) @@ -12,11 +14,11 @@ vim.api.nvim_create_autocmd( '*.lua', '*.cpp', '*.hpp', '*.conf', '*.cfg', '*.ini' }, - callback = MY_remove_trailing_whitespace + callback = M.remove_trailing_whitespace } ) -function MY_insert_modeline() +function M.insert_modeline() local comment_string = vim.o.commentstring local space_maybe = '' if string.match(comment_string, '%%s(.*)') ~= '' then @@ -53,10 +55,10 @@ function MY_insert_modeline() print('modeline inserted') end end -vim.cmd([[command! ModelineInsert lua MY_insert_modeline()]]) +vim.cmd([[command! ModelineInsert lua M.insert_modeline()]]) -- set colorcolumn to textwidth after buffer is displayed or option is changed -function MY_set_colorcolumn() +function M.set_colorcolumn() if vim.o.textwidth > 0 then vim.opt_local.colorcolumn = { vim.o.textwidth } else @@ -68,7 +70,7 @@ vim.api.nvim_create_autocmd( { 'BufEnter' }, { group = 'config_settings', - callback = MY_set_colorcolumn + callback = M.set_colorcolumn } ) vim.api.nvim_create_autocmd( @@ -76,11 +78,11 @@ vim.api.nvim_create_autocmd( { group = 'config_settings', pattern = { 'textwidth' }, - callback = MY_set_colorcolumn + callback = M.set_colorcolumn } ) -function MY_get_project_root() +function M.get_project_root() local path = vim.api.nvim_buf_get_name(0) local root_markers = { '.git', '.hg', '.svn', '.bzr', '_darcs', @@ -99,10 +101,12 @@ function MY_get_project_root() return nil end -function MY_shell_capture(command) +function M.shell_capture(command) local handle = io.popen(command) if not handle then return nil end local result = handle:read() or nil handle:close() return result end + +return M diff --git a/.config/nvim/lua/my/tools.lua b/.config/nvim/lua/my/tools.lua index 4852723..c6c4ee2 100644 --- a/.config/nvim/lua/my/tools.lua +++ b/.config/nvim/lua/my/tools.lua @@ -39,7 +39,11 @@ packer.use { } map('n', 'tb', require('telescope.builtin').buffers) - map('n', 'tf', function() require('telescope.builtin').find_files({ cwd = MY_get_project_root() }) end) + map('n', 'tf', function() + require('telescope.builtin').find_files({ + cwd = require('my.functions').get_project_root() + }) + end) map('n', 'tr', require('telescope.builtin').oldfiles) map('n', 'tg', require('telescope.builtin').live_grep) map('n', 'tm', require('telescope.builtin').man_pages) diff --git a/.config/nvim/snippets/cpp.lua b/.config/nvim/snippets/cpp.lua index 5dfcef7..3856ba2 100644 --- a/.config/nvim/snippets/cpp.lua +++ b/.config/nvim/snippets/cpp.lua @@ -1,5 +1,3 @@ -require('my/functions') - -- local s = require("luasnip.nodes.snippet").S local f = require("luasnip.nodes.functionNode").F @@ -43,6 +41,7 @@ local bsd0 = {cursor}]] +local my = require('my.functions') return { s( { @@ -52,7 +51,7 @@ return { }, fmt(agpl, { project = f(function() - local root = MY_get_project_root() + local root = my.get_project_root() if root then return root:gsub('.*/', '') end return 'INSERT_PROJECT' end), @@ -60,11 +59,11 @@ return { return os.date('%Y') end), name = f(function() - return MY_shell_capture('git config user.name') + return my.shell_capture('git config user.name') or 'INSERT_NAME' end), email = f(function() - return MY_shell_capture('git config user.email') + return my.shell_capture('git config user.email') or 'INSERT_EMAIL' end), cursor = i() @@ -78,7 +77,7 @@ return { }, fmt(bsd0, { project = f(function() - local root = MY_get_project_root() + local root = my.get_project_root() if root then return root:gsub('.*/', '') end return 'INSERT_PROJECT' end), @@ -86,11 +85,11 @@ return { return os.date('%Y') end), name = f(function() - return MY_shell_capture('git config user.name') + return my.shell_capture('git config user.name') or 'INSERT_NAME' end), email = f(function() - return MY_shell_capture('git config user.email') + return my.shell_capture('git config user.email') or 'INSERT_EMAIL' end), cursor = i()