├── LICENSE ├── README.md ├── defwithdoc ├── images ├── snippet_parse.gif └── snippet_successive.gif └── parameters /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 marubu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | yasnippet-numpy-style 2 | ===================== 3 | 4 | yasnippet snippets for docstring in [numpy coding style](https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt). 5 | 6 | #Installation 7 | At first, install [yasnippet](https://github.com/capitaomorte/yasnippet). 8 | 9 | Then: 10 | ```shell 11 | $ git clone https://github.com/marubu/yasnippet-numpy-style.git /tmp/python-mode 12 | $ cp /tmp/python-mode/* ~/.emacs.d/snippets/python-mode/ 13 | ``` 14 | 15 | #Demo 16 | ##Parse arguments 17 | Demonstration of snippet `parameters` is shown below. 18 | * This snippet parses dummy argument part. (supports `class` and `def`) 19 | 20 | ![alt text](images/snippet_parse.gif) 21 | 22 | ##Successive expansion 23 | It is possible to combine the above snippet with snippet `defwithdoc` which is for function with docstring. 24 | To use this, you need to write following emacs lisp in `~/.emacs.d/init.el`. 25 | ``` 26 | (setq yas-triggers-in-field t) 27 | ``` 28 | Demonstration of successive expansion is shown below. 29 | * Expand snippet by only tab key. 30 | 31 | ![alt text](images/snippet_successive.gif) 32 | -------------------------------------------------------------------------------- /defwithdoc: -------------------------------------------------------------------------------- 1 | # -*- mode: snippet -*- 2 | #name : defwithdoc 3 | #key : defwithdoc 4 | #contributor : MATSUBARA Naoya 5 | # -- 6 | def ${1:function}(${2:args}): 7 | """${3:short description} 8 | 9 | $0 10 | 11 | ${4:Parameters 12 | ---------- 13 | ${5:Please press the tab key twice.$(replace-regexp-in-string 14 | ".*" "parameters" yas-text)} 15 | }""" -------------------------------------------------------------------------------- /images/snippet_parse.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marubu/yasnippet-numpy-style/b1969a82c6d94d9501201290c13cbfcb8b7fa248/images/snippet_parse.gif -------------------------------------------------------------------------------- /images/snippet_successive.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marubu/yasnippet-numpy-style/b1969a82c6d94d9501201290c13cbfcb8b7fa248/images/snippet_successive.gif -------------------------------------------------------------------------------- /parameters: -------------------------------------------------------------------------------- 1 | # -*- mode: snippet -*- 2 | #name : parameters 3 | #key : parameters 4 | #type : command 5 | #contributor : MATSUBARA Naoya 6 | # -- 7 | 8 | (flet ((parse-args 9 | (count tmp-buf) 10 | (while (and 11 | (< count 5) 12 | (= 1 13 | (call-process-shell-command 14 | (format 15 | (concat "python << ___EOF___\n" 16 | "%s\n" 17 | " pass\n" 18 | "print(%s.__code__.co_varnames)\n" 19 | "___EOF___") 20 | (replace-regexp-in-string 21 | "^ *def" "def" 22 | (buffer-substring-no-properties 23 | (line-beginning-position) 24 | (line-end-position (incf count)))) 25 | (replace-regexp-in-string 26 | "^ *def *\\(.*?\\)(.*$" "\\1" 27 | (buffer-substring-no-properties 28 | (line-beginning-position) (line-end-position))) 29 | ) 30 | nil 31 | (list tmp-buf nil)))))) 32 | (form-template 33 | (arg-list template) 34 | (let ((count 0)) 35 | (dolist (narg arg-list template) 36 | (setq template 37 | (format (concat "%s${%d:%s: ${%d:type}\n" 38 | (make-string python-indent ? ) 39 | "${%d:description}\n}") 40 | template 41 | (incf count) narg (incf count) (incf count))))) 42 | (replace-regexp-in-string "}\\n}$" "}}" template)) 43 | (get-word-from-nline 44 | (n) 45 | (replace-regexp-in-string 46 | "[ \t-]*" "" 47 | (buffer-substring-no-properties 48 | (line-beginning-position n) (line-end-position n))))) 49 | (if (and (equal "" (get-word-from-nline 0)) 50 | (equal "parameters" (downcase (get-word-from-nline -1)))) 51 | (delete-region (line-beginning-position -1) 52 | (line-beginning-position 1))) 53 | (let ((indent (python-indent-calculate-indentation)) 54 | (match) 55 | (template "Parameters\n----------\n") 56 | (tmp-buf "*tmp sni-param*")) 57 | (save-excursion 58 | (while (and (re-search-backward "^[ \t]*\\(def\\|class\\)" nil t) 59 | (setq match (match-string-no-properties 1)) 60 | (re-search-forward "[ \t]*[dc]" nil t) 61 | (not (= (- (point) (line-beginning-position) 1) 62 | (- indent python-indent))))) 63 | (cond 64 | ((equal match "def") 65 | (parse-args 0 tmp-buf) 66 | (setq template 67 | (form-template (split-string 68 | (with-current-buffer tmp-buf (buffer-string)) 69 | "[()', \n]+" t) 70 | template)) 71 | (kill-buffer tmp-buf)) 72 | ((equal match "class") 73 | (while (and (re-search-forward "^[ \t]+def +__init__" nil t) 74 | (not (= (python-indent-calculate-levels) 75 | indent)))) 76 | (parse-args 0 tmp-buf) 77 | (setq template 78 | (form-template (cdr 79 | (split-string 80 | (with-current-buffer tmp-buf (buffer-string)) 81 | "[()', \n]+" t)) 82 | template)) 83 | (kill-buffer tmp-buf)))) 84 | (yas-expand-snippet template nil nil '((yas-indent-line 'fixed))) 85 | )) 86 | --------------------------------------------------------------------------------