use of com.intellij.codeInsight.template.TemplateEditingAdapter in project intellij-community by JetBrains.
the class GrAliasImportIntention method runTemplate.
private static void runTemplate(Project project, final GrImportStatement context, PsiMember resolved, final GroovyFileBase file, final List<UsageInfo> usages, GrImportStatement templateImport) {
PostprocessReformattingAspect.getInstance(project).doPostponedFormatting();
TemplateBuilderImpl templateBuilder = new TemplateBuilderImpl(templateImport);
LinkedHashSet<String> names = getSuggestedNames(resolved, context);
final PsiElement aliasNameElement = templateImport.getAliasNameElement();
assert aliasNameElement != null;
templateBuilder.replaceElement(aliasNameElement, new MyLookupExpression(resolved.getName(), names, (PsiNamedElement) resolved, resolved, true, null));
Template built = templateBuilder.buildTemplate();
final Editor newEditor = IntentionUtils.positionCursor(project, file, templateImport);
final Document document = newEditor.getDocument();
final RangeMarker contextImportPointer = document.createRangeMarker(context.getTextRange());
final TextRange range = templateImport.getTextRange();
document.deleteString(range.getStartOffset(), range.getEndOffset());
final String name = resolved.getName();
TemplateManager manager = TemplateManager.getInstance(project);
manager.startTemplate(newEditor, built, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
final GrImportStatement importStatement = ApplicationManager.getApplication().runReadAction(new Computable<GrImportStatement>() {
@Nullable
@Override
public GrImportStatement compute() {
return PsiTreeUtil.findElementOfClassAtOffset(file, range.getStartOffset(), GrImportStatement.class, true);
}
});
if (brokenOff) {
if (importStatement != null) {
ApplicationManager.getApplication().runWriteAction(() -> importStatement.delete());
}
return;
}
updateRefs(usages, name, importStatement);
ApplicationManager.getApplication().runWriteAction(() -> {
final GrImportStatement context1 = PsiTreeUtil.findElementOfClassAtRange(file, contextImportPointer.getStartOffset(), contextImportPointer.getEndOffset(), GrImportStatement.class);
if (context1 != null) {
context1.delete();
}
});
}
});
}
use of com.intellij.codeInsight.template.TemplateEditingAdapter in project intellij-community by JetBrains.
the class XmlTagInsertHandler method insertIncompleteTag.
public static void insertIncompleteTag(char completionChar, final Editor editor, XmlTag tag) {
XmlElementDescriptor descriptor = tag.getDescriptor();
final Project project = editor.getProject();
TemplateManager templateManager = TemplateManager.getInstance(project);
Template template = templateManager.createTemplate("", "");
template.setToIndent(true);
// temp code
PsiFile containingFile = tag.getContainingFile();
boolean htmlCode = HtmlUtil.hasHtml(containingFile) || HtmlUtil.supportsXmlTypedHandlers(containingFile);
template.setToReformat(!htmlCode);
StringBuilder indirectRequiredAttrs = addRequiredAttributes(descriptor, tag, template, containingFile);
final boolean chooseAttributeName = addTail(completionChar, descriptor, htmlCode, tag, template, indirectRequiredAttrs);
templateManager.startTemplate(editor, template, new TemplateEditingAdapter() {
private RangeMarker myAttrValueMarker;
@Override
public void waitingForInput(Template template) {
int offset = editor.getCaretModel().getOffset();
myAttrValueMarker = editor.getDocument().createRangeMarker(offset + 1, offset + 4);
}
@Override
public void templateFinished(final Template template, boolean brokenOff) {
final int offset = editor.getCaretModel().getOffset();
if (chooseAttributeName && offset > 0) {
char c = editor.getDocument().getCharsSequence().charAt(offset - 1);
if (c == '/' || (c == ' ' && brokenOff)) {
new WriteCommandAction.Simple(project) {
@Override
protected void run() throws Throwable {
editor.getDocument().replaceString(offset, offset + 3, ">");
}
}.execute();
}
}
}
@Override
public void templateCancelled(final Template template) {
if (myAttrValueMarker == null) {
return;
}
final UndoManager manager = UndoManager.getInstance(project);
if (manager.isUndoInProgress() || manager.isRedoInProgress()) {
return;
}
if (chooseAttributeName && myAttrValueMarker.isValid()) {
final int startOffset = myAttrValueMarker.getStartOffset();
final int endOffset = myAttrValueMarker.getEndOffset();
new WriteCommandAction.Simple(project) {
@Override
protected void run() throws Throwable {
editor.getDocument().replaceString(startOffset, endOffset, ">");
}
}.execute();
}
}
});
}
Aggregations