Java Code Examples for com.intellij.codeInsight.template.Template#addVariable()

The following examples show how to use com.intellij.codeInsight.template.Template#addVariable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AliasPostfixTemplate.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@Override
public void expand(@NotNull PsiElement context, @NotNull Editor editor) {
    FluidInlineStatement expression = (FluidInlineStatement) PsiTreeUtil.findFirstParent(context, psiElement -> psiElement instanceof FluidInlineStatement);
    if (expression == null) {
        return;
    }

    Template tagTemplate = LiveTemplateFactory.createTagModeAliasTemplate(expression);
    tagTemplate.addVariable("ALIAS", new TextExpression("myVar"), true);
    tagTemplate.addVariable("EXPR", new TextExpression("'" + expression.getText() + "'"), true);

    int textOffset = expression.getTextOffset() + expression.getTextLength();
    editor.getCaretModel().moveToOffset(textOffset);

    TemplateManager.getInstance(context.getProject()).startTemplate(editor, tagTemplate);
    PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument());

    expression.delete();
}
 
Example 2
Source File: CreateUnresolvedMethodByLambdaTypeFix.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
protected void buildParameterList(@Nonnull CreateUnresolvedElementFixContext context, @Nonnull PsiFile file, @Nonnull Template template)
{
	template.addTextSegment("(");

	CSharpSimpleParameterInfo[] parameterInfos = myLikeMethod.getParameterInfos();

	for(int i = 0; i < parameterInfos.length; i++)
	{
		if(i != 0)
		{
			template.addTextSegment(", ");
		}

		CSharpSimpleParameterInfo parameterInfo = parameterInfos[i];

		template.addVariable(new ConstantNode(CSharpTypeRefPresentationUtil.buildShortText(parameterInfo.getTypeRef(), context.getExpression())), true);
		template.addTextSegment(" ");
		template.addVariable(new ConstantNode(parameterInfo.getNotNullName()), true);

	}
	template.addTextSegment(")");
}
 
Example 3
Source File: CreateUnresolvedMethodByLambdaTypeFix.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
@Override
public void buildTemplate(@Nonnull CreateUnresolvedElementFixContext context, CSharpContextUtil.ContextType contextType, @Nonnull PsiFile file, @Nonnull Template template)
{
	template.addTextSegment(CreateUnresolvedMethodFix.calcModifier(context).getPresentableText());
	template.addTextSegment(" ");
	if(contextType == CSharpContextUtil.ContextType.STATIC)
	{
		template.addTextSegment("static ");
	}
	template.addVariable(new TypeRefExpression(myLikeMethod.getReturnTypeRef(), file), true);
	template.addTextSegment(" ");
	template.addTextSegment(myReferenceName);

	buildParameterList(context, file, template);

	template.addTextSegment("{\n");

	template.addVariable("$RETURN_STATEMENT$", new ReturnStatementExpression(), false);
	template.addEndVariable();

	template.addTextSegment("}");
}
 
Example 4
Source File: SnackbarTemplate.java    From android-postfix-plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected void addExprVariable(@NotNull PsiElement expr, Template template) {
    ToStringIfNeedMacro toStringIfNeedMacro = new ToStringIfNeedMacro();
    MacroCallNode macroCallNode = new MacroCallNode(toStringIfNeedMacro);
    macroCallNode.addParameter(new ConstantNode(expr.getText()));

    MacroCallNode node = new MacroCallNode(new VariableOfTypeMacro());
    node.addParameter(new ConstantNode(VIEW.toString()));
    template.addVariable("view", node, new EmptyNode(), false);

    template.addVariable("expr", macroCallNode, false);

    if (mWithAction) {
        template.addVariable("actionText", new EmptyNode(), false);
        template.addVariable("action", new EmptyNode(), false);
    }
}
 
Example 5
Source File: ForEachPostfixTemplate.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@Override
public void expand(@NotNull PsiElement context, @NotNull Editor editor) {
    FluidInlineStatement expression = (FluidInlineStatement) PsiTreeUtil.findFirstParent(context, psiElement -> psiElement instanceof FluidInlineStatement);
    if (expression == null) {
        return;
    }

    Template tagTemplate = LiveTemplateFactory.createTagModeForLoopTemplate(expression);
    tagTemplate.addVariable("EACH", new TextExpression(expression.getText()), true);
    tagTemplate.addVariable("AS", new TextExpression("myVar"), true);

    int textOffset = expression.getTextOffset();
    editor.getCaretModel().moveToOffset(textOffset);

    TemplateManager.getInstance(context.getProject()).startTemplate(editor, tagTemplate);
    PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument());

    expression.delete();
}
 
Example 6
Source File: CreateUnresolvedPropertyFix.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Override
public void buildTemplate(@Nonnull CreateUnresolvedElementFixContext context,
		CSharpContextUtil.ContextType contextType,
		@Nonnull PsiFile file,
		@Nonnull Template template)
{
	template.addTextSegment("public ");

	if(contextType == CSharpContextUtil.ContextType.STATIC)
	{
		template.addTextSegment("static ");
	}

	// get expected from method call expression not reference
	List<ExpectedTypeInfo> expectedTypeRefs = ExpectedTypeVisitor.findExpectedTypeRefs(context.getExpression());

	if(!expectedTypeRefs.isEmpty())
	{
		template.addVariable(new TypeRefExpression(expectedTypeRefs, file), true);
	}
	else
	{
		template.addVariable(new TypeRefExpression(new CSharpTypeRefByQName(file, DotNetTypes.System.Object), file), true);
	}

	template.addTextSegment(" ");
	template.addTextSegment(myReferenceName);
	template.addTextSegment("\n{\n");
	template.addTextSegment("get;set;\n");
	template.addTextSegment("}");
	template.addEndVariable();
}
 
Example 7
Source File: CreateUnresolvedEventFix.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Override
public void buildTemplate(@Nonnull CreateUnresolvedElementFixContext context,
		CSharpContextUtil.ContextType contextType,
		@Nonnull PsiFile file,
		@Nonnull Template template)
{
	template.addTextSegment("public ");

	if(contextType == CSharpContextUtil.ContextType.STATIC)
	{
		template.addTextSegment("static ");
	}

	template.addTextSegment("event ");

	// get expected from method call expression not reference
	List<ExpectedTypeInfo> expectedTypeRefs = ExpectedTypeVisitor.findExpectedTypeRefs(context.getExpression());

	if(!expectedTypeRefs.isEmpty())
	{
		template.addVariable(new TypeRefExpression(expectedTypeRefs, file), true);
	}
	else
	{
		template.addVariable(new TypeRefExpression(new CSharpTypeRefByQName(file, DotNetTypes.System.Object), file), true);
	}

	template.addTextSegment(" ");
	template.addTextSegment(myReferenceName);
	template.addTextSegment("\n{\n");
	template.addTextSegment("add;remove;\n");
	template.addTextSegment("}");
	template.addEndVariable();
}
 
Example 8
Source File: CreateUnresolvedFieldFix.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Override
public void buildTemplate(@Nonnull CreateUnresolvedElementFixContext context, CSharpContextUtil.ContextType contextType, @Nonnull PsiFile file, @Nonnull Template template)
{
	template.addTextSegment("public ");

	if(contextType == CSharpContextUtil.ContextType.STATIC)
	{
		template.addTextSegment("static ");
	}

	// get expected from method call expression not reference
	List<ExpectedTypeInfo> expectedTypeRefs = ExpectedTypeVisitor.findExpectedTypeRefs(context.getExpression());

	if(!expectedTypeRefs.isEmpty())
	{
		template.addVariable(new TypeRefExpression(expectedTypeRefs, file), true);
	}
	else
	{
		template.addVariable(new TypeRefExpression(new CSharpTypeRefByQName(file, DotNetTypes.System.Object), file), true);
	}

	template.addTextSegment(" ");
	template.addTextSegment(myReferenceName);
	template.addTextSegment(";");
	template.addEndVariable();
}
 
Example 9
Source File: FindViewByIdTemplate.java    From android-postfix-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected void addExprVariable(@NotNull PsiElement expr, Template template) {
    final FindViewByIdMacro toStringIfNeedMacro = new FindViewByIdMacro();
    MacroCallNode macroCallNode = new MacroCallNode(toStringIfNeedMacro);
    macroCallNode.addParameter(new ConstantNode(expr.getText()));
    template.addVariable("expr", macroCallNode, false);
}
 
Example 10
Source File: ToastTemplate.java    From android-postfix-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected void setVariables(@NotNull Template template, @NotNull PsiElement element) {
    MacroCallNode node = new MacroCallNode(new VariableOfTypeMacro());
    node.addParameter(new ConstantNode(CONTEXT.toString()));
    template.addVariable("context", node, new ConstantNode(""), false);

}
 
Example 11
Source File: ToastTemplate.java    From android-postfix-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected void addExprVariable(@NotNull PsiElement expr, Template template) {
    final ToStringIfNeedMacro toStringIfNeedMacro = new ToStringIfNeedMacro();
    MacroCallNode macroCallNode = new MacroCallNode(toStringIfNeedMacro);
    macroCallNode.addParameter(new ConstantNode(expr.getText()));
    template.addVariable("expr", macroCallNode, false);
}
 
Example 12
Source File: RulesTemplates.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static void addVariableToTemplate(Template template, String variableName) {
  template.addVariable(
      variableName,
      /* expression= */ null,
      /* defaultValueExpression= */ null,
      /* isAlwaysStopAt= */ true,
      /* skipOnStart= */ false);
}
 
Example 13
Source File: CreateRuleFix.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
	String ruleName = editor.getDocument().getText(textRange);

	prepareEditor(project, editor, file);

	Template template = TemplateManager.getInstance(project).createTemplate("", "");
	template.addTextSegment(ruleName + ": ");
	template.addVariable("CONTENT", new TextExpression("' '"), true);
	template.addTextSegment(";");

	TemplateManager.getInstance(project).startTemplate(editor, template);
}
 
Example 14
Source File: TextUtilsIsEmptyTemplate.java    From android-postfix-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected void addExprVariable(@NotNull PsiElement expr, Template template) {
    template.addVariable("expr", new TextExpression(expr.getText()), false);
}
 
Example 15
Source File: LogTemplate.java    From android-postfix-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected void setVariables(@NotNull Template template, @NotNull PsiElement element) {
    MacroCallNode node = new MacroCallNode(new TagMacro());
    template.addVariable("TAG", node, new ConstantNode(""), false);
}
 
Example 16
Source File: InvisibleTemplate.java    From android-postfix-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected void addExprVariable(@NotNull PsiElement expr, Template template) {
    template.addVariable("view", new TextExpression(expr.getText()), false);
}
 
Example 17
Source File: GoneTemplate.java    From android-postfix-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected void addExprVariable(@NotNull PsiElement expr, Template template) {
    template.addVariable("view", new TextExpression(expr.getText()), false);
}
 
Example 18
Source File: VisibleTemplate.java    From android-postfix-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected void addExprVariable(@NotNull PsiElement expr, Template template) {
    template.addVariable("view", new TextExpression(expr.getText()), false);
}
 
Example 19
Source File: CreateUnresolvedMethodFix.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@RequiredReadAction
@Override
public void buildTemplate(@Nonnull CreateUnresolvedElementFixContext context, CSharpContextUtil.ContextType contextType, @Nonnull PsiFile file, @Nonnull Template template)
{
	boolean forInterface = context.getTargetForGenerate() instanceof CSharpTypeDeclaration && ((CSharpTypeDeclaration) context.getTargetForGenerate()).isInterface();

	if(!forInterface)
	{
		template.addTextSegment(calcModifier(context).getPresentableText());
		template.addTextSegment(" ");
		if(contextType == CSharpContextUtil.ContextType.STATIC)
		{
			template.addTextSegment("static ");
		}
	}

	// get expected from method call expression not reference
	List<ExpectedTypeInfo> expectedTypeRefs = ExpectedTypeVisitor.findExpectedTypeRefs(context.getExpression().getParent());

	if(!expectedTypeRefs.isEmpty())
	{
		template.addVariable(new TypeRefExpression(expectedTypeRefs, file), true);
	}
	else
	{
		template.addVariable(new TypeRefExpression(new CSharpTypeRefByQName(file, DotNetTypes.System.Void), file), true);
	}

	template.addTextSegment(" ");
	template.addTextSegment(myReferenceName);

	buildParameterList(context, file, template);

	if(forInterface)
	{
		template.addTextSegment(";");
	}
	else
	{
		template.addTextSegment("{\n");

		template.addVariable("$RETURN_STATEMENT$", new ReturnStatementExpression(), false);
		template.addEndVariable();

		template.addTextSegment("}");
	}
}
 
Example 20
Source File: AbstractRichStringBasedPostfixTemplate.java    From HakunaMatataIntelliJPlugin with Apache License 2.0 4 votes vote down vote up
protected void addExprVariable(@NotNull PsiElement expr, Template template) {
    template.addVariable("expr", new TextExpression(expr.getText()), false);
}