Added support for b option. Some snippets can be defined to only be valid at the beginning of the line

This commit is contained in:
Holger Rapp 2009-07-18 00:51:19 +02:00
parent 0b9d1cf6ca
commit 0943961479
8 changed files with 80 additions and 42 deletions

View File

@ -1,4 +1,6 @@
trunk:
- Added support for b option. This only considers a snippet at the beginning
of a line ( *UltiSnips-adding-snippets* )
- Added support for ! option. This overwrites previously defined snippets
with the same tab trigger ( *UltiSnips-adding-snippets* )
- Support for dotted filetype syntax. Now snippets for more than one filetype

View File

@ -4,7 +4,7 @@
##############
# NICE BOXES #
##############
snippet box
snippet box "A nice box with the current comment symbol" b
`!p
c = '#'
cs = vim.eval("&commentstring")
@ -21,7 +21,7 @@ res = ' ' + c + '\n' + (len(t[1])+4)*c`
$0
endsnippet
snippet bbox
snippet bbox "A nice box over the full width" b
`!p
c = '#'
cs = vim.eval("&commentstring")

View File

@ -1,6 +1,6 @@
# Snippets for VIM Help Files
snippet sec
snippet sec "Section marker" b
=============================================================================
${1:SECTION}`!p
file_start = fn.split('.')[0]
@ -11,7 +11,7 @@ res = ("*%s-%s*" % (file_start, sec_name)).rjust(77-len(t[1]))`
$0
endsnippet
snippet ssec
snippet ssec "Sub section marker" b
${1:Subsection}`!p
file_start = fn.split('.')[0]
sec_name = t[1].strip("1234567890. ").lower().replace(' ', '-')
@ -22,7 +22,7 @@ res = sec_title + "\n" + "-"*len(t[1])`
$0
endsnippet
snippet sssec
snippet sssec "Subsub Section marker" b
${1:SubSubsection}:`!p
file_start = fn.split('.')[0]
sec_name = t[1].strip("1234567890. ").lower().replace(' ', '-')

View File

@ -3,13 +3,13 @@
###########################################################################
#! header
snippet #!
snippet #! "Shebang header for python scripts" b
#!/usr/bin/env python
# encoding: utf-8
$0
endsnippet
snippet ifmain "ifmain"
snippet ifmain "ifmain" b
if __name__ == '__main__':
${1:main()}$0
endsnippet
@ -18,7 +18,7 @@ endsnippet
##########
# COMMON #
##########
snippet class
snippet class "smart class" b
class ${1:MyClass}(${2:object}):
"""${3:Docstring for $1}"""
@ -33,20 +33,20 @@ snippet .
self.
endsnippet
snippet from
snippet from "from blah import blah" b
from ${1:module} import ${2:Stuff}
endsnippet
##############
# PROPERTIES #
##############
snippet roprop
snippet roprop "Read Only Property" b
@property
def ${1:property}(self):
${2:return self._$1}$0
endsnippet
snippet rwprop
snippet rwprop "Read write property" b
def ${1:property}(self):
${2/.+/(?0:""")/}${2:The RW property $1}${2/.+/(?0:"""\n )/}def fget(self):
return self._$1$0
@ -59,14 +59,14 @@ endsnippet
##########################
# Try / Except / Finally #
##########################
snippet try "Try / Except"
snippet try "Try / Except" b
try:
${1:pass}
except ${2:Exception}, ${3:e}:
${4:raise $3}
endsnippet
snippet try "Try / Except / Else"
snippet try "Try / Except / Else" b
try:
${1:pass}
except ${2:Exception}, ${3:e}:
@ -75,7 +75,7 @@ else:
${5:pass}
endsnippet
snippet try "Try / Except / Finally"
snippet try "Try / Except / Finally" b
try:
${1:pass}
except ${2:Exception}, ${3:e}:
@ -84,7 +84,7 @@ else:
${5:pass}
endsnippet
snippet try "Try / Except / Else / Finally"
snippet try "Try / Except / Else / Finally" b
try:
${1:pass}
except${2: ${3:Exception}, ${4:e}}:
@ -98,23 +98,23 @@ endsnippet
##############
# Assertions #
##############
snippet ae
snippet ae "Assert equal" b
self.assertEqual(${1:first},${2:second})
endsnippet
snippet at
snippet at "Assert True" b
self.assertTrue(${0:exp})
endsnippet
snippet af
snippet af "Assert False" b
self.assertFalse(${1:expression})
endsnippet
snippet aae
snippet aae "Assert almost equal" b
self.assertAlmostEqual(${1:first},${2:second})
endsnippet
snippet ar
snippet ar "Assert raises" b
self.assertRaises(${1:exception}, ${2:func}${3/.+/, /}${3:arguments})
endsnippet

View File

@ -23,19 +23,19 @@ endsnippet
########################
# ENUM, DESCR, ITEMIZE #
########################
snippet enum
snippet enum "Enumerate" b
\begin{enumerate}
\item $0
\end{enumerate}
endsnippet
snippet item
snippet item "Itemize" b
\begin{itemize}
\item $0
\end{enumerate}
endsnippet
snippet desc
snippet desc "Description" b
\begin{description}
\item[$1] $0
\end{description}
@ -44,35 +44,35 @@ endsnippet
##############
# MATH STUFF #
##############
snippet eq "Equation"
snippet eq "Equation" b
\begin{equation}
$0
\end{equation}
endsnippet
snippet eqnn "Equation without number"
snippet eqnn "Equation without number" b
\begin{equation*}
$0
\end{equation*}
endsnippet
snippet eqa "Equation array"
snippet eqa "Equation array" b
\begin{eqnarray}
$1 & $2 & $0
\end{eqnarray}
endsnippet
snippet eqann "Equation array without numbers"
snippet eqann "Equation array without numbers" b
\begin{eqnarray*}
$1 & $2 & $0
\end{eqnarray*}
endsnippet
snippet frac
snippet frac "Fraction"
\frac{${1:nom}}{${2:denom}}
endsnippet
snippet mat
snippet mat "Smart Matrix"
\begin{${1:p/b/v/V/B/small}matrix}
$0
\end{$1matrix}
@ -83,7 +83,7 @@ endsnippet
# SECTIONS, CHAPTERS AND THERE LIKE #
#####################################
snippet part
snippet part "Part" b
\part{${1:part name}}
\label{prt:${2:${1/(\w+)|\W+/(?1:\L$0\E:_)/g}}}
@ -92,7 +92,7 @@ ${0}
% part $2 (end)
endsnippet
snippet cha
snippet cha "Chapter" b
\chapter{${1:chapter name}}
\label{cha:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/g}}}
@ -101,7 +101,7 @@ ${0}
% chapter $2 (end)
endsnippet
snippet sec
snippet sec "Section" b
\section{${1:section name}}
\label{sec:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/g}}}
@ -111,7 +111,7 @@ ${0}
endsnippet
snippet sub
snippet sub "Subsection" b
\subsection{${1:subsection name}}
\label{sub:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/g}}}
@ -120,7 +120,7 @@ ${0}
% subsection $2 (end)
endsnippet
snippet ssub
snippet ssub "Subsubsection" b
\subsubsection{${1:subsubsection name}}
\label{ssub:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/g}}}
@ -130,7 +130,7 @@ ${0}
endsnippet
snippet par
snippet par "Paragraph" b
\paragraph{${1:paragraph name}}
\label{par:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/g}}}
@ -140,7 +140,7 @@ ${0}
endsnippet
snippet subp
snippet subp "Subparagraph" b
\subparagraph{${1:subparagraph name}}
\label{par:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/g}}}

View File

@ -157,7 +157,11 @@ snippet on. The options currently supported are >
snippets with this tab trigger. Default is to let the user choose at
expansion which snippet to expand. This option is useful to overwrite
bundled tab stops with user defined ones.
b Beginning of line - This snippet will only be expanded if you are at
the beginning of a line (that is if there are only whitespaces in front
of the cursor). Default is to expand snippets at every position, even
mitten in the line. Most of my snippets have this option set, it keeps
UltiSnips out of the way.
4.2 Plaintext snippets *UltiSnips-plaintext-snippets*
----------------------

View File

@ -18,12 +18,16 @@ class Snippet(object):
self._t = trigger
self._v = value
self._d = descr
self._overwrites = "!" in options
self._opts = options
def overwrites_previous(self):
return self._overwrites
return "!" in self._opts
overwrites_previous = property(overwrites_previous)
def needs_ws_in_front(self):
return "b" in self._opts
needs_ws_in_front = property(needs_ws_in_front)
def description(self):
return self._d
description = property(description)
@ -229,6 +233,11 @@ class SnippetManager(object):
snippets = snippets[idx:]
break
# Check if there are any only whitespace in front snippets
text_before = before.rstrip()[:-len(word)]
if text_before.strip(" \t") != '':
snippets = [ s for s in snippets if not s.needs_ws_in_front ]
if not len(snippets):
# No snippet found
return False
@ -259,7 +268,6 @@ class SnippetManager(object):
else:
end = Position(pos.line - p_start.line, pos.col)
start = Position(end.line, end.col - len(snippet.trigger))
text_before = vim.current.line[:pos.col - len(snippet.trigger)]
si = snippet.launch(text_before, self._ctab, start, end)
@ -269,7 +277,6 @@ class SnippetManager(object):
self._csnippets.append(si)
self.jump()
else:
text_before = before.rstrip()[:-len(word)]
self._vb = VimBuffer(text_before, after)
start = Position(lineno-1, len(text_before))

27
test.py
View File

@ -978,7 +978,32 @@ class SnippetOptions_OverwriteThenChoose_ECR(_VimTest):
)
keys = "test" + EX + "1\n\n" + "test" + EX + "2\n"
wanted = "We overwrite\nNo overwrite"
class SnippetOptions_OnlyExpandWhenWSInFront_Expand(_VimTest):
snippets = ("test", "Expand me!", "", "b")
keys = "test" + EX
wanted = "Expand me!"
class SnippetOptions_OnlyExpandWhenWSInFront_Expand2(_VimTest):
snippets = ("test", "Expand me!", "", "b")
keys = " test" + EX
wanted = " Expand me!"
class SnippetOptions_OnlyExpandWhenWSInFront_DontExpand(_VimTest):
snippets = ("test", "Expand me!", "", "b")
keys = "a test" + EX
wanted = "a test"
class SnippetOptions_OnlyExpandWhenWSInFront_OneWithOneWO(_VimTest):
snippets = (
("test", "Expand me!", "", "b"),
("test", "not at beginning", "", ""),
)
keys = "a test" + EX
wanted = "a not at beginning"
class SnippetOptions_OnlyExpandWhenWSInFront_OneWithOneWOChoose(_VimTest):
snippets = (
("test", "Expand me!", "", "b"),
("test", "not at beginning", "", ""),
)
keys = " test" + EX + "1\n"
wanted = " Expand me!"
######################
# SELECTING MULTIPLE #