Skip to content

Instantly share code, notes, and snippets.

@kiwanami
Last active December 17, 2016 04:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiwanami/af52d0ca067ebde2b9f5 to your computer and use it in GitHub Desktop.
Save kiwanami/af52d0ca067ebde2b9f5 to your computer and use it in GitHub Desktop.
JS の function の定義から、ある程度自動的に内容を拾ってjsdoc用のコメントをつくる yasnippet
# -*- mode: snippet -*-
# name: jsdoc comment
# key: doc
# --
/**
* $0
`(save-excursion
(let* ((pos-fun (re-search-forward "\\<function\\s-*(\\([^)]*\\))"))
(argstr (and pos-fun (match-string 1)))
(args (and pos-fun (split-string argstr ",\\s-*"))))
(loop with params = nil
for arg in args
for nump = (string-match "s$" arg)
for param = (if nump (format " * @param {[]} %s - " arg)
(format " * @param {} %s - " arg))
do
(if (< 0 (length arg))
(push param params))
finally return
(mapconcat 'identity (reverse (cons " * @return " params)) "\n"))))`
*/

Before

<|>
var connect = function(port, methods, host) {

After

/**
 * <|>
 * @param {} port - 
 * @param {[]} methods - 
 * @param {} host - 
 * @return 
 */
var connect = function(port, methods, host) {

引数名がsで終わってたら、勝手に配列だと解釈する.

@agzam
Copy link

agzam commented Sep 23, 2015

regex should be more like this:
\\<function\\s-*\.*(\\([^)]*\\))
so it can match strings like
function foo(a, b, c)
otherwise it throws an error: [yas] elisp error: Search failed: "\\<function\\s-*(\\([^)]*\\))"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment