From d6140a9f15ed941332df1f404fb4a1a324eac0bb Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Sat, 2 Sep 2023 01:19:43 +1000 Subject: [PATCH] Updated vim with puppet helpers - follow puppet classes, opening in new tab - follow puppet templates, opening in new tab - created a testmode for puppet --- .config/nvim/init.lua | 3 +- .config/nvim/lua/func.lua | 10 +++ .config/nvim/lua/func/puppet.lua | 108 +++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 .config/nvim/lua/func.lua create mode 100644 .config/nvim/lua/func/puppet.lua diff --git a/.config/nvim/init.lua b/.config/nvim/init.lua index e379e8c..12ce31b 100644 --- a/.config/nvim/init.lua +++ b/.config/nvim/init.lua @@ -12,6 +12,7 @@ require('opts') -- Options require('keys') -- Keymaps require('auto') -- Automaps require('plug') -- Plugins +require('func') -- Functions -- nvim-tree require("nvim-tree").setup({ @@ -182,7 +183,7 @@ require("block").setup({ percent = 1.1, depth = 4, colors = nil, - automatic = true, + automatic = false, }) -- require("dracula").setup{ diff --git a/.config/nvim/lua/func.lua b/.config/nvim/lua/func.lua new file mode 100644 index 0000000..8aefdae --- /dev/null +++ b/.config/nvim/lua/func.lua @@ -0,0 +1,10 @@ +-- FUNCTIONS -- + +-- puppet language functions +vim.cmd [[ + augroup lang_puppet + autocmd! + autocmd FileType puppet luafile ~/.config/nvim/lua/func/puppet.lua + augroup END +]] + diff --git a/.config/nvim/lua/func/puppet.lua b/.config/nvim/lua/func/puppet.lua new file mode 100644 index 0000000..ddeb2af --- /dev/null +++ b/.config/nvim/lua/func/puppet.lua @@ -0,0 +1,108 @@ +-- puppet functions +-- +-- + +-- enter testmode +vim.cmd([[ +function! OpenPuppetTestMode() + echo "Test mode active." + echo "Keybindings:" + echo " v - puppet-validate" + echo " l - puppet-lint" + echo " q - quit test mode" + + " Keybinding to run tests on the current buffer + nnoremap v :!/usr/local/bin/puppet-validate % + nnoremap l :!/usr/local/bin/puppet-lint % + + " Keybinding to quit test mode + let keys_to_exit = ['q', ':', 'i', 'a', 'c', 'r', 'w', ''] + for key in keys_to_exit + execute "silent! nnoremap " . key . " :call LeavePuppetTestMode()" + endfor +endfunction + +function! LeavePuppetTestMode() + " Unmap the keybindings we defined for test mode + let keys_to_exit = ['l', 'v', 'q', ':', 'i', 'a', 'c', 'r', 'w', ''] + for key in keys_to_exit + execute "silent! unmap " . key + endfor + + echo "Test Mode Exited!" +endfunction +]]) + +-- follow links to classes +vim.cmd([[ +function! OpenPuppetClass() + + " Get the line under the cursor + let line = getline(".") + + " Use a regex to find the full class name + let classname = tolower(matchstr(line, '\v\w+(\:\:\w+)+')) + + " Initialize an empty variable for the directory + let dirpath = "" + + " Check if the class name starts with 'profiles::' or 'roles::' + if classname =~ '^profiles::' + let dirpath = "site/profiles/manifests/" + let classname = substitute(classname, 'profiles::', '', '') + elseif classname =~ '^roles::' + let dirpath = "site/roles/manifests/" + let classname = substitute(classname, 'roles::', '', '') + else + echo "Unknown class prefix, should be profiles:: or roles::" + return + endif + + " Replace :: with / to convert class name to file path + let filepath = substitute(classname, '::', '/', 'g') + + " Create the full file path + let fullpath = dirpath . filepath . ".pp" + + " Open the file in a new tab + execute "tabedit " . fullpath + +endfunction +]]) + +-- follow links to templates +vim.cmd([[ +function! OpenPuppetTemplate() + + " Get the line under the cursor + let line = getline(".") + + " Use regex to find the full template name + let template = matchstr(line, 'profiles\/.*\.erb') + + " Initialize an empty variable for the new directory path + let newpath = "" + + " Check if the template name starts with 'profiles/' + if template =~ '^profiles\/' + let newpath = substitute(template, '^profiles/', 'site/profiles/templates/', '') + else + echo "Unknown template prefix, should be profiles/, line is: " . template + return + endif + + " Create the full file path + let fullpath = newpath + + " Open the file in a new tab + execute "tabedit " . fullpath + +endfunction +]]) + +-- keybindings +vim.cmd([[ +nnoremap g :call OpenPuppetClass() +nnoremap h :call OpenPuppetTemplate() +nnoremap t :call OpenPuppetTestMode() +]])