use of com.intellij.codeInsight.template.Template in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoCreateWrapperTypeQuickFix method invoke.
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
if (editor == null) {
LOG.error("Cannot run quick fix without editor: " + getClass().getSimpleName(), AttachmentFactory.createAttachment(file.getVirtualFile()));
return;
}
if (!(startElement instanceof GoType))
return;
GoType type = (GoType) startElement;
PsiElement anchor = PsiTreeUtil.findPrevParent(file, type);
String name = "TypeName";
GoTypeDeclaration decl = (GoTypeDeclaration) file.addBefore(GoElementFactory.createTypeDeclaration(project, name, type), anchor);
if (decl == null)
return;
decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl);
if (decl == null)
return;
GoTypeSpec spec = ContainerUtil.getFirstItem(decl.getTypeSpecList());
if (spec == null)
return;
TemplateBuilderImpl builder = new TemplateBuilderImpl(file);
builder.replaceElement(type, OTHER_NAME, INPUT_NAME, false);
builder.replaceElement(spec.getIdentifier(), INPUT_NAME, new ConstantNode(name), true);
editor.getCaretModel().moveToOffset(file.getTextRange().getStartOffset());
Template template = builder.buildInlineTemplate();
editor.getCaretModel().moveToOffset(file.getTextRange().getStartOffset());
TemplateManager.getInstance(project).startTemplate(editor, template);
}
use of com.intellij.codeInsight.template.Template in project yii2support by nvlad.
the class MissingPropertiesQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemDescriptor) {
List<PhpDocPropertyTag> propertyTags = this.comment.getPropertyTags();
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
if (editor == null)
return;
Document document = editor.getDocument();
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
TemplateManager templateManager = TemplateManager.getInstance(project);
Template template = templateManager.createTemplate("", "");
template.setToReformat(true);
for (VirtualProperty missingProperty : this.missingProperties) {
String propertyText = "* @property " + (missingProperty.getType() != null ? missingProperty.getType() : "") + " $" + missingProperty.getName();
if (missingProperty.getComment() != null) {
propertyText += " " + missingProperty.getComment();
}
template.addTextSegment("\n" + propertyText);
}
template.addTextSegment("\n");
int offset = comment.getLastChild().getTextOffset();
if (propertyTags.size() > 0) {
PhpDocPropertyTag phpDocPropertyTag = propertyTags.get(comment.getPropertyTags().size() - 1);
offset = phpDocPropertyTag.getTextOffset() + phpDocPropertyTag.getTextLength();
}
editor.getCaretModel().moveToOffset(offset);
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
templateManager.startTemplate(editor, template);
}
Aggregations