1

I wrote this snippet to insert a new variable into Java source:

# -*- mode: snippet -*-
# name: variable
# key: v
# --
$1 ${1:$(java-default-variable-name text)} = new $1($2);
$0

It declares and creates a variable based on the type, e.g.

AtomicInteger-> AtomicInteger ai = new AtomicInteger();

What is missing is that I can't give another variable name inside the snippet, if I don't like the default one. I've tried to write:

$1 ${2:$(java-default-variable-name $1)}

or

$1 ${2:`(java-default-variable-name $1)`}

But it doesn't work. Is there a way to make a transformation of field $1 into field $2?

abo-abo
  • 20,038
  • 3
  • 50
  • 71

1 Answers1

2

I don't know the exact context of java-default-variable-name - I'm assuming it takes a string and returns another string?

If so, I think this is roughly what you're after:

$1 ${2:varname$(let* ((end (- (point) 1))
           (start (save-excursion (backward-word) (point)))
           (type (buffer-substring start end)))
         (if (equal yas/text "varname")
            (java-default-variable-name type)
            yas/text))} = new $1($3);

$0

David Miller
  • 2,189
  • 2
  • 15
  • 17
  • I tried adding four space characters before the final `$0` tab stop for you, but edits must be something other than whitespace. – yurisich Oct 30 '12 at 02:58