replace Filename by vim_snippets#Filename and document it

It doesn't make sense that all snippet engines have to
keep this function somewhere
This commit is contained in:
Marc Weber 2013-07-08 03:41:11 +02:00
parent 1c74a955b0
commit 80697bea6f
17 changed files with 97 additions and 68 deletions

View File

@ -576,7 +576,7 @@ snippet module "New module (module)" b
// (See accompanying file LICENSE_1_0.txt or copy at // (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
module ${2}.`!v Filename('$1', 'name')`; module ${2}.`!v vim_snippets#Filename('$1', 'name')`;
${3} ${3}

27
autoload/vim_snippets.vim Normal file
View File

@ -0,0 +1,27 @@
" this is well known Filename found in snipmate (and the other engines), but
" rewritten and documented :)
"
" optional arg1: string in which to replace '$1' by filename with extension
" and path dropped. Defaults to $1
" optional arg2: return this value if buffer has no filename
" But why not use the template in this case, too?
" Doesn't make sense to me
fun! vim_snippets#Filename(...)
let template = get(a:000, 0, "$1")
let arg2 = get(a:000, 1, "")
let basename = expand('%:t:r')
if basename == ''
return arg2
else
return substitute(template, '$1', basename, 'g')
endif
endf
" original code:
" fun! Filename(...)
" let filename = expand('%:t:r')
" if filename == '' | return a:0 == 2 ? a:2 : '' | endif
" return !a:0 || a:1 == '' ? filename : substitute(a:1, '$1', filename, 'g')
" endf

View File

@ -20,7 +20,7 @@ snippet inc
#include <${1:stdio}.h>${2} #include <${1:stdio}.h>${2}
# #include "..." # #include "..."
snippet Inc snippet Inc
#include "${1:`Filename("$1.h")`}"${2} #include "${1:`vim_snippets#Filename("$1.h")`}"${2}
# ifndef...define...endif # ifndef...define...endif
snippet ndef snippet ndef
#ifndef $1 #ifndef $1
@ -41,7 +41,7 @@ snippet #if
#endif #endif
# header include guard # header include guard
snippet once snippet once
#ifndef ${1:`toupper(Filename('$1_H', 'UNTITLED_H'))`} #ifndef ${1:`toupper(vim_snippets#Filename('$1_H', 'UNTITLED_H'))`}
#define $1 #define $1
@ -132,14 +132,14 @@ snippet td
typedef ${1:int} ${2:MyCustomType};${3} typedef ${1:int} ${2:MyCustomType};${3}
# struct # struct
snippet st snippet st
struct ${1:`Filename('$1_t', 'name')`} { struct ${1:`vim_snippets#Filename('$1_t', 'name')`} {
${2:/* data */} ${2:/* data */}
}${3: /* optional variable list */};${4} }${3: /* optional variable list */};${4}
# typedef struct # typedef struct
snippet tds snippet tds
typedef struct ${2:_$1 }{ typedef struct ${2:_$1 }{
${3:/* data */} ${3:/* data */}
} ${1:`Filename('$1_t', 'name')`};${4} } ${1:`vim_snippets#Filename('$1_t', 'name')`};${4}
# typedef enum # typedef enum
snippet tde snippet tde
typedef enum { typedef enum {

View File

@ -35,19 +35,19 @@ snippet bfun
${2:# body...} ${2:# body...}
# Class # Class
snippet cla class .. snippet cla class ..
class ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} class ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`}
${2} ${2}
snippet cla class .. constructor: .. snippet cla class .. constructor: ..
class ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} class ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`}
constructor: (${2:args}) -> constructor: (${2:args}) ->
${3} ${3}
${4} ${4}
snippet cla class .. extends .. snippet cla class .. extends ..
class ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} extends ${2:ParentClass} class ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} extends ${2:ParentClass}
${3} ${3}
snippet cla class .. extends .. constructor: .. snippet cla class .. extends .. constructor: ..
class ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} extends ${2:ParentClass} class ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} extends ${2:ParentClass}
constructor: (${3:args}) -> constructor: (${3:args}) ->
${4} ${4}

View File

@ -68,7 +68,7 @@ snippet mu
## Class ## Class
# class # class
snippet cl snippet cl
class ${1:`Filename('$1', 'name')`} class ${1:`vim_snippets#Filename('$1', 'name')`}
{ {
public: public:
$1(${2}); $1(${2});
@ -79,12 +79,12 @@ snippet cl
}; };
# member function implementation # member function implementation
snippet mfun snippet mfun
${4:void} ${1:`Filename('$1', 'ClassName')`}::${2:memberFunction}(${3}) { ${4:void} ${1:`vim_snippets#Filename('$1', 'ClassName')`}::${2:memberFunction}(${3}) {
${5} ${5}
} }
# namespace # namespace
snippet ns snippet ns
namespace ${1:`Filename('', 'my')`} { namespace ${1:`vim_snippets#Filename('', 'my')`} {
${2} ${2}
} /* namespace $1 */ } /* namespace $1 */
## ##

View File

@ -116,49 +116,49 @@ snippet each
} }
# interfaces # interfaces
snippet interface snippet interface
public interface ${1:`Filename()`} { public interface ${1:`vim_snippets#Filename()`} {
${2} ${2}
} }
snippet if+ snippet if+
public interface ${1:`Filename()`} { public interface ${1:`vim_snippets#Filename()`} {
${2} ${2}
} }
# class bodies # class bodies
snippet class snippet class
public class ${1:`Filename()`} { public class ${1:`vim_snippets#Filename()`} {
${2} ${2}
} }
snippet cls snippet cls
${2:public} class ${1:`Filename()`} { ${2:public} class ${1:`vim_snippets#Filename()`} {
${3} ${3}
} }
snippet cls+ snippet cls+
public class ${1:`Filename()`} { public class ${1:`vim_snippets#Filename()`} {
${2} ${2}
} }
snippet cls+^ snippet cls+^
public static class ${1:`Filename()`} { public static class ${1:`vim_snippets#Filename()`} {
${2} ${2}
} }
snippet cls& snippet cls&
internal class ${1:`Filename()`} { internal class ${1:`vim_snippets#Filename()`} {
${2} ${2}
} }
snippet cls&^ snippet cls&^
internal static class ${1:`Filename()`} { internal static class ${1:`vim_snippets#Filename()`} {
${2} ${2}
} }
snippet cls| snippet cls|
protected class ${1:`Filename()`} { protected class ${1:`vim_snippets#Filename()`} {
${2} ${2}
} }
snippet cls|% snippet cls|%
protected abstract class ${1:`Filename()`} { protected abstract class ${1:`vim_snippets#Filename()`} {
${2} ${2}
} }
# constructor # constructor
snippet ctor snippet ctor
public ${1:`Filename()`}() { public ${1:`vim_snippets#Filename()`}() {
${2} ${2}
} }
# properties - auto properties by default. # properties - auto properties by default.
@ -342,7 +342,7 @@ snippet ms-
} }
# structure # structure
snippet struct snippet struct
public struct ${1:`Filename()`} { public struct ${1:`vim_snippets#Filename()`} {
${2} ${2}
} }
# enumeration # enumeration

View File

@ -22,9 +22,9 @@ snippet br
snippet th snippet th
throw ${1} throw ${1}
snippet cl snippet cl
class ${1:`Filename("", "untitled")`} ${2} class ${1:`vim_snippets#Filename("", "untitled")`} ${2}
snippet in snippet in
interface ${1:`Filename("", "untitled")`} ${2} interface ${1:`vim_snippets#Filename("", "untitled")`} ${2}
snippet imp snippet imp
implements ${1} implements ${1}
snippet ext snippet ext

View File

@ -1,6 +1,6 @@
# module and export all # module and export all
snippet mod snippet mod
-module(${1:`Filename('', 'my')`}). -module(${1:`vim_snippets#Filename('', 'my')`}).
-compile([export_all]). -compile([export_all]).
@ -66,7 +66,7 @@ snippet %p
%% @private %% @private
# OTP application # OTP application
snippet application snippet application
-module(${1:`Filename('', 'my')`}). -module(${1:`vim_snippets#Filename('', 'my')`}).
-behaviour(application). -behaviour(application).
@ -84,7 +84,7 @@ snippet application
ok. ok.
# OTP supervisor # OTP supervisor
snippet supervisor snippet supervisor
-module(${1:`Filename('', 'my')`}). -module(${1:`vim_snippets#Filename('', 'my')`}).
-behaviour(supervisor). -behaviour(supervisor).
@ -107,7 +107,7 @@ snippet supervisor
{ok, {RestartStrategy, Children}}. {ok, {RestartStrategy, Children}}.
# OTP gen_server # OTP gen_server
snippet gen_server snippet gen_server
-module(${1:`Filename('', 'my')`}). -module(${1:`vim_snippets#Filename('', 'my')`}).
-behaviour(gen_server). -behaviour(gen_server).

View File

@ -412,7 +412,7 @@ snippet head
<head> <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>${1:`substitute(Filename('', 'Page Title'), '^.', '\u&', '')`}</title> <title>${1:`substitute(vim_snippets#Filename('', 'Page Title'), '^.', '\u&', '')`}</title>
${2} ${2}
</head> </head>
snippet header snippet header
@ -450,7 +450,7 @@ snippet html5
<html> <html>
<head> <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>${1:`substitute(Filename('', 'Page Title'), '^.', '\u&', '')`}</title> <title>${1:`substitute(vim_snippets#Filename('', 'Page Title'), '^.', '\u&', '')`}</title>
${2:meta} ${2:meta}
</head> </head>
<body> <body>
@ -795,7 +795,7 @@ snippet thead
snippet time snippet time
<time datetime="${1}" pubdate="${2:$1}">${3:$1}</time> <time datetime="${1}" pubdate="${2:$1}">${3:$1}</time>
snippet title snippet title
<title>${1:`substitute(Filename('', 'Page Title'), '^.', '\u&', '')`}</title> <title>${1:`substitute(vim_snippets#Filename('', 'Page Title'), '^.', '\u&', '')`}</title>
snippet tr snippet tr
<tr> <tr>
${1} ${1}

View File

@ -39,11 +39,11 @@ snippet j.u
## ##
## Class ## Class
snippet cl snippet cl
class ${1:`Filename("", "untitled")`} ${2} class ${1:`vim_snippets#Filename("", "untitled")`} ${2}
snippet in snippet in
interface ${1:`Filename("", "untitled")`} ${2:extends Parent}${3} interface ${1:`vim_snippets#Filename("", "untitled")`} ${2:extends Parent}${3}
snippet tc snippet tc
public class ${1:`Filename()`} extends ${2:TestCase} public class ${1:`vim_snippets#Filename()`} extends ${2:TestCase}
## ##
## Class Enhancements ## Class Enhancements
snippet ext snippet ext
@ -144,7 +144,7 @@ snippet @au
snippet @br snippet @br
@brief ${1:Description} @brief ${1:Description}
snippet @fi snippet @fi
@file ${1:`Filename()`}.java @file ${1:`vim_snippets#Filename()`}.java
snippet @pa snippet @pa
@param ${1:param} @param ${1:param}
snippet @re snippet @re

View File

@ -3,7 +3,7 @@ snippet Imp
#import <${1:Cocoa/Cocoa.h}>${2} #import <${1:Cocoa/Cocoa.h}>${2}
# #import "..." # #import "..."
snippet imp snippet imp
#import "${1:`Filename()`.h}"${2} #import "${1:`vim_snippets#Filename()`.h}"${2}
# @selector(...) # @selector(...)
snippet sel snippet sel
@selector(${1:method}:)${3} @selector(${1:method}:)${3}
@ -18,7 +18,7 @@ snippet log
NSLog(@"${1:%@}"${2});${3} NSLog(@"${1:%@}"${2});${3}
# Class # Class
snippet objc snippet objc
@interface ${1:`Filename('', 'someClass')`} : ${2:NSObject} @interface ${1:`vim_snippets#Filename('', 'someClass')`} : ${2:NSObject}
{ {
} }
@end @end
@ -28,33 +28,33 @@ snippet objc
@end @end
# Class Interface # Class Interface
snippet int snippet int
@interface ${1:`Filename('', 'someClass')`} : ${2:NSObject} @interface ${1:`vim_snippets#Filename('', 'someClass')`} : ${2:NSObject}
{${3} {${3}
} }
${4} ${4}
@end @end
snippet @interface snippet @interface
@interface ${1:`Filename('', 'someClass')`} : ${2:NSObject} @interface ${1:`vim_snippets#Filename('', 'someClass')`} : ${2:NSObject}
{${3} {${3}
} }
${4} ${4}
@end @end
# Class Implementation # Class Implementation
snippet impl snippet impl
@implementation ${1:`Filename('', 'someClass')`} @implementation ${1:`vim_snippets#Filename('', 'someClass')`}
${2} ${2}
@end @end
snippet @implementation snippet @implementation
@implementation ${1:`Filename('', 'someClass')`} @implementation ${1:`vim_snippets#Filename('', 'someClass')`}
${2} ${2}
@end @end
# Protocol # Protocol
snippet pro snippet pro
@protocol ${1:`Filename('$1Delegate', 'MyProtocol')`} ${2:<NSObject>} @protocol ${1:`vim_snippets#Filename('$1Delegate', 'MyProtocol')`} ${2:<NSObject>}
${3} ${3}
@end @end
snippet @protocol snippet @protocol
@protocol ${1:`Filename('$1Delegate', 'MyProtocol')`} ${2:<NSObject>} @protocol ${1:`vim_snippets#Filename('$1Delegate', 'MyProtocol')`} ${2:<NSObject>}
${3} ${3}
@end @end
# init Definition # init Definition
@ -87,7 +87,7 @@ snippet cat
@end @end
# Category Interface # Category Interface
snippet cath snippet cath
@interface ${1:`Filename('$1', 'NSObject')`} (${2:MyCategory}) @interface ${1:`vim_snippets#Filename('$1', 'NSObject')`} (${2:MyCategory})
${3} ${3}
@end @end
# Method # Method

View File

@ -84,7 +84,7 @@ snippet fore
${1:expression} foreach @${2:array};${3} ${1:expression} foreach @${2:array};${3}
# Package # Package
snippet package snippet package
package ${1:`substitute(Filename('', 'Page Title'), '^.', '\u&', '')`}; package ${1:`substitute(vim_snippets#Filename('', 'Page Title'), '^.', '\u&', '')`};
${2} ${2}
@ -93,7 +93,7 @@ snippet package
__END__ __END__
# Package syntax perl >= 5.14 # Package syntax perl >= 5.14
snippet packagev514 snippet packagev514
package ${1:`substitute(Filename('', 'Page Title'), '^.', '\u&', '')`} ${2:0.99}; package ${1:`substitute(vim_snippets#Filename('', 'Page Title'), '^.', '\u&', '')`} ${2:0.99};
${3} ${3}

View File

@ -28,12 +28,12 @@ snippet use
use ${1:Foo\Bar\Baz}; use ${1:Foo\Bar\Baz};
${2} ${2}
snippet c snippet c
class ${1:`Filename()`} class ${1:`vim_snippets#Filename()`}
{ {
${2} ${2}
} }
snippet i snippet i
interface ${1:`Filename()`} interface ${1:`vim_snippets#Filename()`}
{ {
${2} ${2}
} }
@ -57,7 +57,7 @@ snippet sm
* *
* @param ${2:$1} $$1 ${3:description} * @param ${2:$1} $$1 ${3:description}
* *
* @return ${4:`Filename()`} * @return ${4:`vim_snippets#Filename()`}
*/ */
${5:public} function set${6:$2}(${7:$2 }$$1) ${5:public} function set${6:$2}(${7:$2 }$$1)
{ {
@ -214,7 +214,7 @@ snippet interface
* @package ${3:default} * @package ${3:default}
* @author ${4:`g:snips_author`} * @author ${4:`g:snips_author`}
*/ */
interface ${1:`Filename()`} interface ${1:`vim_snippets#Filename()`}
{ {
${5} ${5}
} }
@ -223,7 +223,7 @@ snippet class
/** /**
* ${1} * ${1}
*/ */
class ${2:`Filename()`} class ${2:`vim_snippets#Filename()`}
{ {
${3} ${3}
/** /**
@ -237,7 +237,7 @@ snippet class
snippet nc snippet nc
namespace ${1:`substitute(substitute(expand("%:h"), '\v^\w+\/(\u)', '\1', ''), '\/', '\\\', 'g')`}; namespace ${1:`substitute(substitute(expand("%:h"), '\v^\w+\/(\u)', '\1', ''), '\/', '\\\', 'g')`};
${2:abstract }class ${3:`Filename()`} ${2:abstract }class ${3:`vim_snippets#Filename()`}
{ {
${4} ${4}
} }
@ -366,7 +366,7 @@ snippet gs
* *
* @param $2 $$1 ${5:description} * @param $2 $$1 ${5:description}
* *
* @return ${6:`Filename()`} * @return ${6:`vim_snippets#Filename()`}
*/ */
public function set$3(${7:$2 }$$1) public function set$3(${7:$2 }$$1)
{ {

View File

@ -5,11 +5,11 @@
# Language Constructs # Language Constructs
snippet class snippet class
class ${1:`Filename('', 'name')`} { class ${1:`vim_snippets#Filename('', 'name')`} {
${2} ${2}
} }
snippet node snippet node
node "${1:`Filename('', 'fqdn')`}" { node "${1:`vim_snippets#Filename('', 'fqdn')`}" {
${2} ${2}
} }
snippet case snippet case

View File

@ -10,7 +10,7 @@ snippet from
# Module Docstring # Module Docstring
snippet docs snippet docs
""" """
File: ${1:`Filename('$1.py', 'foo.py')`} File: ${1:`vim_snippets#Filename('$1.py', 'foo.py')`}
Author: `g:snips_author` Author: `g:snips_author`
Email: `g:snips_email` Email: `g:snips_email`
Github: `g:snips_github` Github: `g:snips_github`

View File

@ -88,23 +88,23 @@ snippet until
${2} ${2}
end end
snippet cla class .. end snippet cla class .. end
class ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} class ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`}
${2} ${2}
end end
snippet cla class .. initialize .. end snippet cla class .. initialize .. end
class ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} class ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`}
def initialize(${2:args}) def initialize(${2:args})
${3} ${3}
end end
end end
snippet cla class .. < ParentClass .. initialize .. end snippet cla class .. < ParentClass .. initialize .. end
class ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} < ${2:ParentClass} class ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} < ${2:ParentClass}
def initialize(${3:args}) def initialize(${3:args})
${4} ${4}
end end
end end
snippet cla ClassName = Struct .. do .. end snippet cla ClassName = Struct .. do .. end
${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} = Struct.new(:${2:attr_names}) do ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} = Struct.new(:${2:attr_names}) do
def ${3:method_name} def ${3:method_name}
${4} ${4}
end end
@ -119,7 +119,7 @@ snippet cla class << self .. end
end end
# class .. < DelegateClass .. initialize .. end # class .. < DelegateClass .. initialize .. end
snippet cla- snippet cla-
class ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} < DelegateClass(${2:ParentClass}) class ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} < DelegateClass(${2:ParentClass})
def initialize(${3:args}) def initialize(${3:args})
super(${4:del_obj}) super(${4:del_obj})
@ -127,17 +127,17 @@ snippet cla-
end end
end end
snippet mod module .. end snippet mod module .. end
module ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} module ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`}
${2} ${2}
end end
snippet mod module .. module_function .. end snippet mod module .. module_function .. end
module ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} module ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`}
module_function module_function
${2} ${2}
end end
snippet mod module .. ClassMethods .. end snippet mod module .. ClassMethods .. end
module ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} module ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`}
module ClassMethods module ClassMethods
${2} ${2}
end end
@ -553,7 +553,7 @@ snippet clafn
snippet sinc snippet sinc
class << self; self end class << self; self end
snippet nam snippet nam
namespace :${1:`Filename()`} do namespace :${1:`vim_snippets#Filename()`} do
${2} ${2}
end end
snippet tas snippet tas
@ -1070,3 +1070,5 @@ snippet isres
it { should respond_to :${1} } it { should respond_to :${1} }
snippet isresw snippet isresw
it { should respond_to(:${1}).with(${2}).arguments } it { should respond_to(:${1}).with(${2}).arguments }
snippet super_call
${1:super_class}.instance_method(:${2:method}).bind(self).call

View File

@ -4,7 +4,7 @@ snippet header
" Description: ${3} " Description: ${3}
${4:" Last Modified: `strftime("%B %d, %Y")`} ${4:" Last Modified: `strftime("%B %d, %Y")`}
snippet guard snippet guard
if exists('${1:did_`Filename()`}') || &cp${2: || version < 700} if exists('${1:did_`vim_snippets#Filename()`}') || &cp${2: || version < 700}
finish finish
endif endif
let $1 = 1${3} let $1 = 1${3}