Can someone help me with this snippet? I found it here

# -*- mode: snippet -*-
# name: jsdoc comment
# key: doc
# --
* $0
(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))
        (if (< 0 (length arg))
            (push param params))
        finally return
        (mapconcat 'identity (reverse (cons " * @return " params)) "\n"))))`

it creates this:

[yas] elisp error: Search failed: "\\<function\\s-*(\\([^)]*\\))"

I'm suspecting there's something wrong with the regex

What this snippet suppose to do? It creates js-doc comments as function headers in js2-mode. So code like function foo(a, bs, c) should turn into:

* @param {} a - 
* @param {[]} bs - 
* @param {} c - 
* @return 
function foo(port, methods, host) {
  • If you give us some example text of what you expect to find with the proposed search, I bet someone will be able to help. Otherwise, we are all just guessing as to what you want to find with the search -- e.g., you might be mistakenly hoping to search for some text that says "hello-world", or you might be mistakenly searching for your mother's maiden name? The search fails because it finds no match for the regexp going in a forward direction. – lawlist Sep 24 '15 at 2:07
  • 1
    @lawlist this snippet is for creating js-doc comments in js2-mode. it suppose to be looking for string like function foo (a, b, c) {. I'll update the question – iLemming Sep 24 '15 at 2:10
  • There is a full/complete match, so the regexp appears to be good for your situation. Are you behind -- i.e., to the left of -- function foo (a, b, c) { when you execute the snippet? The snippet searches forward. – lawlist Sep 24 '15 at 2:12
  • 1
    @lawlist The cursor is at the line above 'function' – iLemming Sep 24 '15 at 2:16
  • 3
    The regexp matches function (a, b, c), but not function foo (a, b, c). – npostavs Sep 24 '15 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.