use of com.intellij.codeInsight.template.impl.MacroCallNode in project intellij-community by JetBrains.
the class HighlightUtil method showRenameTemplate.
public static void showRenameTemplate(PsiElement context, PsiNameIdentifierOwner element) {
context = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(context);
final Query<PsiReference> query = ReferencesSearch.search(element, element.getUseScope());
final Collection<PsiReference> references = query.findAll();
final Project project = context.getProject();
final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
final Editor editor = fileEditorManager.getSelectedTextEditor();
if (editor == null) {
return;
}
final TemplateBuilderImpl builder = new TemplateBuilderImpl(context);
final Expression macroCallNode = new MacroCallNode(new SuggestVariableNameMacro());
final PsiElement identifier = element.getNameIdentifier();
builder.replaceElement(identifier, "PATTERN", macroCallNode, true);
for (PsiReference reference : references) {
builder.replaceElement(reference, "PATTERN", "PATTERN", false);
}
final Template template = builder.buildInlineTemplate();
final TextRange textRange = context.getTextRange();
final int startOffset = textRange.getStartOffset();
editor.getCaretModel().moveToOffset(startOffset);
final TemplateManager templateManager = TemplateManager.getInstance(project);
templateManager.startTemplate(editor, template);
}
use of com.intellij.codeInsight.template.impl.MacroCallNode in project intellij-community by JetBrains.
the class TryWithResourcesPostfixTemplate method expand.
@Override
public void expand(@NotNull PsiElement context, @NotNull Editor editor) {
PsiExpression expression = JavaPostfixTemplatesUtils.getTopmostExpression(context);
assert expression != null;
Project project = context.getProject();
editor.getDocument().deleteString(expression.getTextRange().getStartOffset(), expression.getTextRange().getEndOffset());
TemplateManager manager = TemplateManager.getInstance(project);
Template template = manager.createTemplate("", "");
template.setToReformat(true);
template.addTextSegment("try (");
MacroCallNode name = new MacroCallNode(new SuggestVariableNameMacro());
template.addVariable("type", new TypeExpression(project, new PsiType[] { expression.getType() }), false);
template.addTextSegment(" ");
template.addVariable("name", name, name, true);
template.addTextSegment(" = ");
template.addVariable("variable", new TextExpression(expression.getText()), false);
template.addTextSegment(") {\n");
template.addEndVariable();
template.addTextSegment("\n}");
Collection<PsiClassType> unhandled = getUnhandled(expression);
for (PsiClassType exception : unhandled) {
MacroCallNode variable = new MacroCallNode(new SuggestVariableNameMacro());
template.addTextSegment("catch(");
template.addVariable("type " + exception.getClassName(), new TypeExpression(project, new PsiType[] { exception }), false);
template.addTextSegment(" ");
template.addVariable("name " + exception.getClassName(), variable, variable, false);
template.addTextSegment(") {}");
}
manager.startTemplate(editor, template);
}
use of com.intellij.codeInsight.template.impl.MacroCallNode in project intellij-community by JetBrains.
the class ForIndexedPostfixTemplate method setVariables.
@Override
public void setVariables(@NotNull Template template, @NotNull PsiElement element) {
MacroCallNode index = new MacroCallNode(new SuggestVariableNameMacro());
template.addVariable("index", index, index, true);
}
use of com.intellij.codeInsight.template.impl.MacroCallNode in project intellij-community by JetBrains.
the class AddWithParamFix method invoke.
public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws IncorrectOperationException {
final RunResult<SmartPsiElementPointer<XmlTag>> result = new WriteAction<SmartPsiElementPointer<XmlTag>>() {
protected void run(@NotNull Result<SmartPsiElementPointer<XmlTag>> result) throws Throwable {
final XmlTag withParamTag = RefactoringUtil.addWithParam(myTag);
withParamTag.setAttribute("name", myName != null ? myName : "dummy");
withParamTag.setAttribute("select", "dummy");
result.setResult(SmartPointerManager.getInstance(project).createSmartPsiElementPointer(withParamTag));
}
}.execute();
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
final Document doc = psiDocumentManager.getDocument(file);
assert doc != null;
psiDocumentManager.doPostponedOperationsAndUnblockDocument(doc);
final XmlTag withParamTag = result.getResultObject().getElement();
assert withParamTag != null;
final TemplateBuilderImpl builder = new TemplateBuilderImpl(withParamTag);
final XmlAttribute selectAttr = withParamTag.getAttribute("select", null);
assert selectAttr != null;
PsiElement dummy = XsltSupport.getAttValueToken(selectAttr);
builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
if (myName == null) {
final XmlAttribute nameAttr = withParamTag.getAttribute("name", null);
assert nameAttr != null;
dummy = XsltSupport.getAttValueToken(nameAttr);
builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
}
moveTo(editor, withParamTag);
new WriteAction() {
@SuppressWarnings({ "RawUseOfParameterizedType" })
protected void run(@NotNull Result result) throws Throwable {
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
final TemplateManager mgr = TemplateManager.getInstance(myTag.getProject());
mgr.startTemplate(editor, builder.buildInlineTemplate());
}
}.execute();
}
use of com.intellij.codeInsight.template.impl.MacroCallNode in project intellij-community by JetBrains.
the class CreateVariableFix method invoke.
public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
editor = editor instanceof EditorWindow ? ((EditorWindow) editor).getDelegate() : editor;
XmlTag tag = PsiTreeUtil.getContextOfType(myReference, XmlTag.class, true);
if (tag == null)
return;
XmlTag xmlTag = tag.createChildTag("variable", XsltSupport.XSLT_NS, null, false);
xmlTag.setAttribute("name", myReference.getReferencedName());
xmlTag.setAttribute("select", "dummy");
final XmlAttribute select = xmlTag.getAttribute("select", null);
assert select != null;
final PsiElement dummy = XsltSupport.getAttValueToken(select);
assert dummy != null;
final TemplateBuilderImpl builder = createTemplateBuilder(xmlTag);
builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
builder.setEndVariableAfter(select);
final Template template = builder.buildTemplate();
template.addTextSegment("\n");
template.setToIndent(true);
final XmlTag insertionPoint = findVariableInsertionPoint(tag);
moveTo(editor, insertionPoint);
TemplateManager.getInstance(project).startTemplate(editor, template);
}
Aggregations