Custom SimpleMacro and incrementing a number.

Hi,

 

I'm trying to create a custom expression for a live template that adds 1 to a value of index and then puts that into a variable to be used in the template.

 

I have: 

import com.intellij.codeInsight.template.*;
import com.intellij.codeInsight.template.macro.SimpleMacro;

public class AutoIncrement extends SimpleMacro {

private int currentIndex = 0;

protected AutoIncrement() {
super("autoIndex");
}

@Override
protected String evaluateSimpleMacro(Expression[] expressions, ExpressionContext expressionContext) {
String index = String.valueOf(currentIndex);
currentIndex += 1;
return index;
}
}

However, when using the macro in an expression it seems to increment the value by 13 the first time and 15 for the rest. Am I missing something, Is this just not possible or Is the plugin SDK doing something weird??

 

Thanks.

0

Currently the platform calls `Macro#calculateResult` (which calls `evaluateSimpleMacro`) several times. This doesn't cause problem for other macros because they return the same result.

Why do you need to have such a macro?

0
Avatar
Permanently deleted user

I need an expression that can fill an incrementing value into a variable name. I.e increasing it by 100 each time a new variable is made “var100” -> “var200” and so on

0

But why do you need such variables? Are you going to generate code which have many variables with distinct names? If so there may be a more convenient way to do this.

0
Avatar
Permanently deleted user

It’s not needed for variables that was purely an example. I have a file that has incrementing steps actual example @Step(index = the value, id = 123, description = “Lorem”) creating these 30 times a file can take time so automating it via the function

0

You can create an action 'Generate Step' (like Code | Generate | Test we have now) instead, which will create a new method annotated with this annotation and automatically set `id` to value which is greater by one than the value in the previous method.

0

请先登录再写评论。