0

How is it possible to access the name of the method (and its arguments) when inserting a YASnippet in a Java (or JavaScript) code block?

The goal is to be able to expand

logm

into (for example):

log("notify() called with: " + "context = [" + context + "]");

if inserted in the body of function:

public void notify(final EventHandlerContext context) {
....
}

(Such a live template exists in IntelliJ IDEA.)

user3341592
  • 1,419
  • 1
  • 17
  • 36

1 Answers1

1

You can evaluate arbitrary lisp code in the snippet. So, if you have a function to grab the method name/arguments it can be called from the snippet code. For example, here is a quick attempt to get the method name -- you could probably find a better function somewhere in cc-cmds or eclim libraries.

(defun java-method-name ()
  (save-excursion
    (c-beginning-of-defun)
    (when (re-search-forward "\\([A-Za-z]+\\)(")
      (match-string 1))))

snippet

# -*- mode: snippet -*-
# name: logm
# key: logm
# --
log("`(java-method-name)`() called with: " + ${1:etc:..})
Rorschach
  • 31,301
  • 5
  • 78
  • 129