Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top