/vim/after/ftplugin/python_snippets.vim

https://bitbucket.org/vertespain/config · Vim Script · 202 lines · 145 code · 21 blank · 36 comment · 20 complexity · 3a56a5e767dd7f11785039a68a9fd526 MD5 · raw file

  1. if !exists('loaded_snippet') || &cp
  2. finish
  3. endif
  4. " Given a string containing a list of arguments (e.g. "one, two = 'test'"),
  5. " this function cleans it up by removing useless whitespace and commas.
  6. function! PyCleanupArgs(text)
  7. if a:text == 'args'
  8. return ''
  9. endif
  10. let text = substitute(a:text, '\(\w\)\s\(\w\)', '\1,\2', 'g')
  11. return join(split(text, '\s*,\s*'), ', ')
  12. endfunction
  13. " Given a string containing a list of arguments (e.g. "one = 'test', *args,
  14. " **kwargs"), this function returns a string containing only the variable
  15. " names, separated by spaces, e.g. "one two".
  16. function! PyGetVarnamesFromArgs(text)
  17. let text = substitute(a:text, 'self,*\s*', '', '')
  18. let text = substitute(text, '\*\*\?\k\+', '', 'g')
  19. let text = substitute(text, '=.\{-},', '', 'g')
  20. let text = substitute(text, '=.\{-}$', '', 'g')
  21. let text = substitute(text, '\s*,\s*', ' ', 'g')
  22. if text == ' '
  23. return ''
  24. endif
  25. return text
  26. endfunction
  27. " Returns the current indent as a string.
  28. function! PyGetIndentString()
  29. if &expandtab
  30. let tabs = indent('.') / &shiftwidth
  31. let tabstr = repeat(' ', &shiftwidth)
  32. else
  33. let tabs = indent('.') / &tabstop
  34. let tabstr = '\t'
  35. endif
  36. return repeat(tabstr, tabs)
  37. endfunction
  38. " Given a string containing a list of arguments (e.g. "one = 'test', *args,
  39. " **kwargs"), this function returns them formatted correctly for the
  40. " docstring.
  41. function! PyGetDocstringFromArgs(text)
  42. let text = PyGetVarnamesFromArgs(a:text)
  43. if a:text == 'args' || text == ''
  44. return ''
  45. endif
  46. let indent = PyGetIndentString()
  47. let st = g:snip_start_tag
  48. let et = g:snip_end_tag
  49. let docvars = map(split(text), 'v:val." -- ".st.et')
  50. return '\n'.indent.join(docvars, '\n'.indent).'\n'.indent
  51. endfunction
  52. " Given a string containing a list of arguments (e.g. "one = 'test', *args,
  53. " **kwargs"), this function returns them formatted as a variable assignment in
  54. " the form "self._ONE = ONE", as used in class constructors.
  55. function! PyGetVariableInitializationFromVars(text)
  56. let text = PyGetVarnamesFromArgs(a:text)
  57. if a:text == 'args' || text == ''
  58. return ''
  59. endif
  60. let indent = PyGetIndentString()
  61. let st = g:snip_start_tag
  62. let et = g:snip_end_tag
  63. let assert_vars = map(split(text), '"assert ".v:val." ".st.et')
  64. let assign_vars = map(split(text), '"self._".v:val." = ".v:val')
  65. let assertions = join(assert_vars, '\n'.indent)
  66. let assignments = join(assign_vars, '\n'.indent)
  67. return assertions.'\n'.indent.assignments.'\n'.indent
  68. endfunction
  69. " Given a string containing a list of arguments (e.g. "one = 'test', *args,
  70. " **kwargs"), this function returns them with the default arguments removed.
  71. function! PyStripDefaultValue(text)
  72. return substitute(a:text, '=.*', '', 'g')
  73. endfunction
  74. " Returns the number of occurences of needle in haystack.
  75. function! Count(haystack, needle)
  76. let counter = 0
  77. let index = match(a:haystack, a:needle)
  78. while index > -1
  79. let counter = counter + 1
  80. let index = match(a:haystack, a:needle, index+1)
  81. endwhile
  82. return counter
  83. endfunction
  84. " Returns replacement if the given subject matches the given match.
  85. " Returns the subject otherwise.
  86. function! PyReplace(subject, match, replacement)
  87. if a:subject == a:match
  88. return a:replacement
  89. endif
  90. return a:subject
  91. endfunction
  92. " Returns the % operator with a tuple containing n elements appended, where n
  93. " is the given number.
  94. function! PyHashArgList(count)
  95. if a:count == 0
  96. return ''
  97. endif
  98. let st = g:snip_start_tag
  99. let et = g:snip_end_tag
  100. return ' % ('.st.et.repeat(', '.st.et, a:count - 1).')'
  101. endfunction
  102. let st = g:snip_start_tag
  103. let et = g:snip_end_tag
  104. let cd = g:snip_elem_delim
  105. " Note to users: The following method of defininf snippets is to allow for
  106. " changes to the default tags.
  107. " Feel free to define your own as so:
  108. " Snippet mysnip This is the expansion text.<{}>
  109. " There is no need to use exec if you are happy to hardcode your own start and
  110. " end tags
  111. " Properties, setters and getters.
  112. exec "Snippet prop ".st."attribute".et." = property(get_".st."attribute".et.", set_".st."attribute".et.st.et.")<CR>".st.et
  113. exec "Snippet get def get_".st."name".et."(self):<CR>return self._".st."name".et."<CR>".st.et
  114. exec "Snippet set def set_".st."name".et."(self, ".st."value".et."):
  115. \<CR>self._".st."name".et." = ".st."value:PyStripDefaultValue(@z)".et."
  116. \<CR>".st.et
  117. " Functions and methods.
  118. exec "Snippet def def ".st."fname".et."(".st."args:PyCleanupArgs(@z)".et."):
  119. \<CR>\"\"\"
  120. \<CR>".st.et."
  121. \<CR>".st."args:PyGetDocstringFromArgs(@z)".et."\"\"\"
  122. \<CR>".st."pass".et."
  123. \<CR>".st.et
  124. exec "Snippet cm ".st."class".et." = classmethod(".st."class".et.")<CR>".st.et
  125. " Class definition.
  126. exec "Snippet cl class ".st."ClassName".et."(".st."object".et."):
  127. \<CR>\"\"\"
  128. \<CR>This class represents ".st.et."
  129. \<CR>\"\"\"
  130. \<CR>
  131. \<CR>def __init__(self, ".st."args:PyCleanupArgs(@z)".et."):
  132. \<CR>\"\"\"
  133. \<CR>Constructor.
  134. \<CR>".st."args:PyGetDocstringFromArgs(@z)".et."\"\"\"
  135. \<CR>".st."args:PyGetVariableInitializationFromVars(@z)".et.st.et
  136. " Keywords
  137. exec "Snippet for for ".st."variable".et." in ".st."ensemble".et.":<CR>".st."pass".et."<CR>".st.et
  138. exec "Snippet pf print '".st."s".et."'".st."s:PyHashArgList(Count(@z, '%[^%]'))".et."<CR>".st.et
  139. exec "Snippet im import ".st."module".et."<CR>".st.et
  140. exec "Snippet from from ".st."module".et." import ".st.'name:PyReplace(@z, "name", "*")'.et."<CR>".st.et
  141. exec "Snippet % '".st."s".et."'".st."s:PyHashArgList(Count(@z, '%[^%]'))".et.st.et
  142. exec "Snippet ass assert ".st."expression".et.st.et
  143. " From Kib2
  144. exec "Snippet bc \"\"\"<CR>".st.et."<CR>\"\"\"<CR>".st.et
  145. " Try, except, finally.
  146. exec "Snippet trye try:
  147. \<CR>".st.et."
  148. \<CR>except Exception, e:
  149. \<CR>".st.et."
  150. \<CR>".st.et
  151. exec "Snippet tryf try:
  152. \<CR>".st.et."
  153. \<CR>finally:
  154. \<CR>".st.et."
  155. \<CR>".st.et
  156. exec "Snippet tryef try:
  157. \<CR>".st.et."
  158. \<CR>except Exception, e:
  159. \<CR>".st.et."
  160. \<CR>finally:
  161. \<CR>".st.et."
  162. \<CR>".st.et
  163. " Other multi statement templates
  164. " From Panos
  165. exec "Snippet ifn if __name__ == '".st."main".et."':<CR>".st.et
  166. exec "Snippet ifmain if __name__ == '__main__':<CR>".st.et
  167. " Shebang
  168. exec "Snippet sb #!/usr/bin/env python<CR># -*- coding: ".st."encoding".et." -*-<CR>".st.et
  169. exec "Snippet sbu #!/usr/bin/env python<CR># -*- coding: UTF-8 -*-<CR>".st.et
  170. " From Kib2
  171. exec "Snippet sbl1 #!/usr/bin/env python<CR># -*- coding: Latin-1 -*-<CR>".st.et
  172. " Unit tests.
  173. exec "Snippet unittest if __name__ == '__main__':
  174. \<CR>import unittest
  175. \<CR>
  176. \<CR>class ".st."ClassName".et."Test(unittest.TestCase):
  177. \<CR>def setUp(self):
  178. \<CR>".st."pass".et."
  179. \<CR>
  180. \<CR>def runTest(self):
  181. \<CR>".st.et