Dotfiles/site-modules/core/files/vim/UltiSnips/puppet.snippets
Ade Attwood 80239af169 refactor(core): start to make dotfiles public
This is the first commit that brings the privet dotfiles to a public
reop previously this was all one puppet module. Now this has been split
out so I can put all of the private files in a private puppet module
2020-09-20 06:22:17 +01:00

66 lines
1.6 KiB
Text

#
# Python helper code for generating namespaces
#
# See: https://github.com/honza/vim-snippets/blob/master/UltiSnips/puppet.snippets
#
global !p
import vim
import os.path
def get_module_namespace_and_basename():
"""This function will try to guess the current class or define name you are
trying to create. Note that for this to work you should be using the module
structure as per the style guide. Examples inputs and it's output
* /home/nikolavp/puppet/modules/collectd/manifests/init.pp -> collectd
* /home/nikolavp/puppet/modules/collectd/manfistes/mysql.pp -> collectd::mysql
"""
first_time = True
current_file_path_without_ext = vim.eval('expand("%:p:r")') or ""
if not current_file_path_without_ext:
return "name"
parts = os.path.split(current_file_path_without_ext)
namespace = ''
while parts[0] and parts[0] != '/':
if parts[1] == 'init' and first_time and not namespace:
first_time = False
parts = os.path.split(parts[0])
continue
if parts[1] == 'manifests':
return os.path.split(parts[0])[1] + ('::' + namespace).rstrip(':')
else:
namespace = parts[1] + '::' + namespace
parts = os.path.split(parts[0])
return "name"
endglobal
#
# Snippets
#
snippet class "Class declaration" b
class ${1:`!p snip.rv = get_module_namespace_and_basename()`} {
${0:# Puppet code}
}
endsnippet
snippet define "Definition" b
define ${1:`!p snip.rv = get_module_namespace_and_basename()`} {
${0:# Puppet Code}
}
endsnippet
snippet = "" i
=>$0
endsnippet
snippet ai "Array item" i
'${1:key}' => ${0:value}
endsnippet
snippet alert "Alert Function" b
alert("${1:message}")$0
endsnippet
snippet info "Info Function" b
info("${1:message}")$0
endsnippet