Skip to content
Browse files

Version 0.5

Add SnippetySnipPrintCurrentSnippetString() to print the inclusion string for the snippet currently under the cursor
Make a best effort to preserve cursor position
  • Loading branch information...
1 parent 57e4d2e commit f3f769de7e81d40923938042a082dce72831f522 @knatten knatten committed with Jun 19, 2012
Showing with 83 additions and 9 deletions.
  1. +16 −1 README
  2. +7 −1 plugin/SnippetySnip/README
  3. +15 −3 plugin/SnippetySnip/SnippetySnip.vim
  4. +24 −4 python/SnippetySnip/snippetysnip.py
  5. +21 −0 python/SnippetySnip/test.py
View
17 README
@@ -2,7 +2,7 @@ This is a mirror of http://www.vim.org/scripts/script.php?script_id=3717
SnippetySnip is a vim tool for automatically inserting parts (snippets) of other files in a file. I wrote it to insert sourcecode examples from source files into html blog-posts, but it can be used for all sorts of text files.
-In the source file, you mark up a region of text (a snippet) and give it a name. In the target file, you mark where you want this particular snippet inserted.
+In the source file, you mark up a region of text (a snippet) and give it a name. In the target file, you mark where you want this particular snippet inserted.
For instance:
@@ -37,6 +37,16 @@ You can have many snippets in a source file, and a target file can reference mul
Paths can be relative or absolute.
+
+HOW TO USE
+In the source files, surround each snippet with snippetysnip_begin:SNIPPET_NAME and snippetysnip_end. These tags need to be on separate lines, but except from that, they can be inside the comment style of your choice, depending on the host language. See the example above.
+In the target file, put on a separate line snippetysnipp:FILE_NAME:SNIPPET_NAME
+Paths can be relative or absolute. See the example above, or python/SnippetySnip/integration_tests/ for a more extensive one.
+To run SnippetySnip and insert all your snippets, do ":call SnippetySnip()" without the quotes. It is suggested you make a map to make this easier, see INSTALLATION
+It can be tedious to type the snippet inclusion strings in the target file. To get this string automatically, go to your source file, place the cursor inside the snippet you want the inclusion string for and do ":call SnippetySnipPrintCurrentSnippetString()" without the quotes. It will print the string for you (with html comments). It is suggested you make a map to make this easier, see INSTALLATION.
+About cursor position: Since the entire buffer is replaced, it is impossible to exactly remember the cursor position. SnippetySnip does however make a best effort, and puts the cursor back at the same line and column.
+
+
ADVANCED USAGE
It is possible to give arguments before="..." and/or after="..." when including a snippet. These arguments will be inserted between the snippet command and the actual snippet. Here is an example, inserting code snippets into Wordpress posts:
@@ -54,3 +64,8 @@ modified target:
(This was motivated by the fact that html-comments do not work well inside [sourcecode] blocks on Wordpress)
+If you want SnippetySnipPrintCurrentSnippetString() to include arguments, define g:SnippetySnipArguments. For instance, you can put this line in ~/.vimrc:
+let g:SnippetySnipArguments = "(before='[sourcecode language=\"cpp\"]', after='}[/sourcecode]')"
+
+SnippetySnip is also available on github: https://github.com/knatten/SnippetySnip
+
View
8 plugin/SnippetySnip/README
@@ -41,16 +41,19 @@ In the source files, surround each snippet with snippetysnip_begin:SNIPPET_NAME
In the target file, put on a separate line snippetysnipp:FILE_NAME:SNIPPET_NAME
Paths can be relative or absolute. See the example above, or python/SnippetySnip/integration_tests/ for a more extensive one.
To run SnippetySnip and insert all your snippets, do ":call SnippetySnip()" without the quotes. It is suggested you make a map to make this easier, see INSTALLATION
+It can be tedious to type the snippet inclusion strings in the target file. To get this string automatically, go to your source file, place the cursor inside the snippet you want the inclusion string for and do ":call SnippetySnipPrintCurrentSnippetString()" without the quotes. It will print the string for you (with html comments). It is suggested you make a map to make this easier, see INSTALLATION.
+About cursor position: Since the entire buffer is replaced, it is impossible to exactly remember the cursor position. SnippetySnip does however make a best effort, and puts the cursor back at the same line and column.
INSTALLATION
Download SnippetySnip-<version>.tgz into your vim directory (usually ~/.vim on Linux systems) and do
tar zxf SnippetySnip-<version>.tgz
-To make SnippetySnip easier to use, I suggest you set up a mapping in ~/.vimrc. One of the following should do nicely:
+To make SnippetySnip easier to use, I suggest you set up a mapping in ~/.vimrc. These should do nicely:
map <Leader>s :call SnippetySnip()<CR> "Type <Leader>s to run SnippetySnip
map <C-s> :call SnippetySnip()<CR> "Press Ctrl+s to run SnippetySnip
+map <Leader>S :call SnippetySnipPrintCurrentSnippetString()<CR> "Type <Leader>S to print the name of the current snippet
@@ -70,3 +73,6 @@ modified target:
<!-- snippetysnip_end:source.cpp:foo -->
(This was motivated by the fact that html-comments do not work well inside [sourcecode] blocks on Wordpress)
+
+If you want SnippetySnipPrintCurrentSnippetString() to include arguments, define g:SnippetySnipArguments. For instance, you can put this line in ~/.vimrc:
+let g:SnippetySnipArguments = "(before='[sourcecode language=\"cpp\"]', after='}[/sourcecode]')"
View
18 plugin/SnippetySnip/SnippetySnip.vim
@@ -1,18 +1,30 @@
" Vim plugin for importing into a file selected lines from other files
-" Version: 0.4
-" Last change: 2011-08-28
+" Version: 0.5
+" Last change: 2012-06-19
" Author: Anders Schau Knatten
" Contact: anders AT knatten DOT org
" License: This file is placed in the public domain.
function! SnippetySnip()
+let saved_pos = getpos(".")
python << endpython
lines = insert_snippets(vim.current.buffer)
vim.current.buffer[:] = None
vim.current.buffer[0] = lines[0] #It seems we cannot get rid of line 0 in previous command
for line in lines[1:]:
vim.current.buffer.append(line)
endpython
+call setpos(".", saved_pos)
+endfunction
+
+function! SnippetySnipPrintCurrentSnippetString()
+ if exists("g:SnippetySnipArguments")
+ let l:arguments = ':' . g:SnippetySnipArguments
+ else
+ let l:arguments = ''
+ endif
+ python vim.command("let l:snippetname='%s'" % get_current_snippet_name(vim.current.buffer, int(vim.eval("line('.')"))))
+ echo '<!-- snippetysnip:' . bufname('%') . ':' . l:snippetname . arguments . ' -->'
endfunction
python << endpython
@@ -21,5 +33,5 @@ import vim
path = os.path.join(os.environ['HOME'], '.vim', 'python')
if not path in sys.path:
sys.path.append(path)
-from SnippetySnip.snippetysnip import insert_snippets
+from SnippetySnip.snippetysnip import insert_snippets, get_current_snippet_name
endpython
View
28 python/SnippetySnip/snippetysnip.py
@@ -1,8 +1,10 @@
import re
+SNIPPET_BEGIN = "snippetysnip_begin"
+SNIPPET_END = "snippetysnip_end"
+
def get_snippet(file_name, snippet_name):
- snippet_begin = "snippetysnip_begin:%s" % snippet_name
- snippet_end = "snippetysnip_end"
+ snippet_begin = "%s:%s" % (SNIPPET_BEGIN, snippet_name)
snippet = ""
in_snippet = False
@@ -16,7 +18,7 @@ def get_snippet(file_name, snippet_name):
in_snippet = True
found_tag = True
continue
- if snippet_end in line:
+ if SNIPPET_END in line:
in_snippet = False
if in_snippet:
snippet += line
@@ -29,7 +31,7 @@ def get_snippet(file_name, snippet_name):
def find_end_line(lines, file_name, snippet_name):
- snippet_end = "snippetysnip_end:%s:%s" % (file_name, snippet_name)
+ snippet_end = "%s:%s:%s" % (SNIPPET_END, file_name, snippet_name)
for line_no in range(0, len(lines)):
if snippet_end in lines[line_no]:
return line_no
@@ -80,3 +82,21 @@ def insert_snippets(old_buffer, snippet_getter=get_snippet):
line_no += end_line
line_no += 1
return new_buffer
+
+def line_is_snippet_end(line):
+ return re.search(SNIPPET_END, line)
+
+def get_snippet_name_if_line_is_snippet_begin(line):
+ match = re.search("%s:(.*)" % SNIPPET_BEGIN, line)
+ if match:
+ return match.group(1)
+ return False
+
+def get_current_snippet_name(buf, current_line):
+ for line in range(current_line, 0, -1):
+ if line_is_snippet_end(buf[line]):
+ raise ValueError('Not in a snippet')
+ snippet_name = get_snippet_name_if_line_is_snippet_begin(buf[line])
+ if snippet_name:
+ return snippet_name
+ raise ValueError('Not in a snippet')
View
21 python/SnippetySnip/test.py
@@ -144,3 +144,24 @@ def test_removes_arguments(self):
actual = "<!-- snippetysnip:snippy.cpp:snippet:(before=\"[sourcecode]\") -->"
expected = "<!-- snippetysnip:snippy.cpp:snippet -->"
self.assertEqual(expected, remove_arguments(actual))
+
+class Test_get_current_snippet_name(unittest.TestCase):
+ buf = ['what', 'ever', 'snippetysnip_begin:foo', 'here', 'snippetysnip_end',
+ 'snippetysnip_begin:bar', 'more', 'snippetysnip_end', 'jubajuba']
+
+ def test_when_in_foo__returns_foo(self):
+ self.assertEqual("foo", get_current_snippet_name(self.buf, self.buf.index('snippetysnip_begin:foo')))
+
+ def test_when_in_bar__returns_bar(self):
+ self.assertEqual("bar", get_current_snippet_name(self.buf, self.buf.index('snippetysnip_begin:bar')))
+
+ def test_when_not_in_snippet__raises_exception(self):
+ with self.assertRaises(ValueError) as error:
+ get_current_snippet_name(self.buf, 0)
+ self.assertEqual('Not in a snippet', error.exception.message)
+
+ def test_when_not_in_snippet__doesnt_accidentally_pick_the_previous_one(self):
+ with self.assertRaises(ValueError) as error:
+ get_current_snippet_name(self.buf, self.buf.index('jubajuba'))
+ self.assertEqual('Not in a snippet', error.exception.message)
+

0 comments on commit f3f769d

Please sign in to comment.
Something went wrong with that request. Please try again.