Skip to content

Instantly share code, notes, and snippets.

@kiwanami kiwanami/jsdoc.yasnippet
Last active Dec 17, 2016

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.