use of com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression in project intellij-community by JetBrains.
the class CreateFieldOrPropertyFix method generateMembers.
private void generateMembers(final Project project, final Editor editor, final PsiFile file) {
try {
List<? extends GenerationInfo> prototypes = new GenerateFieldOrPropertyHandler(myName, myType, myMemberType, myAnnotations).generateMemberPrototypes(myClass, ClassMember.EMPTY_ARRAY);
prototypes = GenerateMembersUtil.insertMembersAtOffset(myClass, editor.getCaretModel().getOffset(), prototypes);
if (prototypes.isEmpty())
return;
final PsiElement scope = prototypes.get(0).getPsiMember().getContext();
assert scope != null;
final Expression expression = new EmptyExpression() {
@Override
public com.intellij.codeInsight.template.Result calculateResult(final ExpressionContext context) {
return new TextResult(myType.getCanonicalText());
}
};
final TemplateBuilderImpl builder = new TemplateBuilderImpl(scope);
boolean first = true;
@NonNls final String TYPE_NAME_VAR = "TYPE_NAME_VAR";
for (GenerationInfo prototype : prototypes) {
final PsiTypeElement typeElement = PropertyUtil.getPropertyTypeElement(prototype.getPsiMember());
if (first) {
first = false;
builder.replaceElement(typeElement, TYPE_NAME_VAR, expression, true);
} else {
builder.replaceElement(typeElement, TYPE_NAME_VAR, TYPE_NAME_VAR, false);
}
}
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
editor.getCaretModel().moveToOffset(scope.getTextRange().getStartOffset());
TemplateManager.getInstance(project).startTemplate(editor, builder.buildInlineTemplate());
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression in project intellij-community by JetBrains.
the class GroovyCreateFieldFromUsageHelper method setupTemplateImpl.
@Override
public Template setupTemplateImpl(PsiField f, Object expectedTypes, PsiClass targetClass, Editor editor, PsiElement context, boolean createConstantField, PsiSubstitutor substitutor) {
GrVariableDeclaration fieldDecl = (GrVariableDeclaration) f.getParent();
GrField field = (GrField) fieldDecl.getVariables()[0];
TemplateBuilderImpl builder = new TemplateBuilderImpl(fieldDecl);
Project project = context.getProject();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
if (expectedTypes instanceof TypeConstraint[]) {
GrTypeElement typeElement = fieldDecl.getTypeElementGroovy();
assert typeElement != null;
ChooseTypeExpression expr = new ChooseTypeExpression((TypeConstraint[]) expectedTypes, PsiManager.getInstance(project), typeElement.getResolveScope());
builder.replaceElement(typeElement, expr);
} else if (expectedTypes instanceof ExpectedTypeInfo[]) {
new GuessTypeParameters(factory).setupTypeElement(field.getTypeElement(), (ExpectedTypeInfo[]) expectedTypes, substitutor, builder, context, targetClass);
}
if (createConstantField) {
field.setInitializerGroovy(factory.createExpressionFromText("0", null));
builder.replaceElement(field.getInitializerGroovy(), new EmptyExpression());
}
fieldDecl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(fieldDecl);
Template template = builder.buildTemplate();
TextRange range = fieldDecl.getTextRange();
editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
if (expectedTypes instanceof ExpectedTypeInfo[]) {
if (((ExpectedTypeInfo[]) expectedTypes).length > 1)
template.setToShortenLongNames(false);
}
return template;
}
use of com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression in project intellij-community by JetBrains.
the class XmlTagInplaceRenamer method buildTemplate.
private static Template buildTemplate(@NotNull final XmlTag tag, @NotNull final Pair<ASTNode, ASTNode> pair) {
final TemplateBuilderImpl builder = new TemplateBuilderImpl(tag);
final ASTNode selected = pair.first;
final ASTNode other = pair.second;
builder.replaceElement(selected.getPsi(), PRIMARY_VARIABLE_NAME, new EmptyExpression() {
@Override
public Result calculateQuickResult(final ExpressionContext context) {
return new TextResult(selected.getText());
}
@Override
public Result calculateResult(final ExpressionContext context) {
return new TextResult(selected.getText());
}
}, true);
if (other != null) {
builder.replaceElement(other.getPsi(), OTHER_VARIABLE_NAME, PRIMARY_VARIABLE_NAME, false);
}
return builder.buildInlineTemplate();
}
Aggregations