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

The following examples show how to use com.intellij.codeInsight.template.Template#addTextSegment() . 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: 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 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: RulesTemplates.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static void addAttributeToTemplate(Template template, AttributeDefinition attribute) {
  String name = attribute.getName();
  Wrap wrap = TYPE_TO_WRAP.getOrDefault(attribute.getType(), Wrap.EMPTY);
  template.addTextSegment("\n    " + name + " = " + wrap.left);
  addVariableToTemplate(template, name);
  template.addTextSegment(wrap.right + ",");
}
 
Example 4
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 5
Source File: CreateUnresolvedConstructorFix.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 ");
	template.addTextSegment(myReferenceName);
	buildParameterList(context, file, template);
	template.addTextSegment("{\n");
	template.addEndVariable();
	template.addTextSegment("}");
}
 
Example 6
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 7
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 8
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 9
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("}");
	}
}