From b272949b513b20a4b0d20e2cac06cf18cdaa068b Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Fri, 17 May 2013 06:57:22 +0200 Subject: [PATCH] Improvements to the puppet snippets by Nikola Petrov. --- UltiSnips/puppet.snippets | 43 +++++++++++++++++++++++++++++++++++++-- doc/UltiSnips.txt | 1 + 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/UltiSnips/puppet.snippets b/UltiSnips/puppet.snippets index add906f..cc43ef7 100644 --- a/UltiSnips/puppet.snippets +++ b/UltiSnips/puppet.snippets @@ -1,7 +1,46 @@ # Snippets for Puppet -snippet /^class/ "Class declaration" r -class ${1:name} { +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 + """ + 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] != '/': + # Skip the init.pp files because they don't create a deeper namespace + # and are special. Note this might break things like + # modules/collectd/init/test.pp. But I am not even sure if puppet will + # work for those. + if parts[1] == 'init': + 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]) + # couldn't guess the namespace. The user is editing a raw file in no module like the site.pp file + return "name" +endglobal + +snippet define "Definition" b +define ${1:`!p snip.rv = get_module_namespace_and_basename()`} { + ${0:# body} +} + +endsnippet + +snippet class "Class declaration" b +class ${1:`!p snip.rv = get_module_namespace_and_basename()`} { ${0:# body} } endsnippet diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index 553f563..26af376 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -1369,6 +1369,7 @@ Contributors listed in chronological order: Danilo Bargen (dbrgn) Bernhard Vallant (lazerscience) Von Welch (von) + Nikola Petrov (nikolavp) Thank you for your support.