use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-community by JetBrains.
the class CustomCreateProperty method generateCreateComponentsMethod.
public static void generateCreateComponentsMethod(final PsiClass aClass) {
final PsiFile psiFile = aClass.getContainingFile();
if (psiFile == null)
return;
final VirtualFile vFile = psiFile.getVirtualFile();
if (vFile == null)
return;
if (!FileModificationService.getInstance().prepareFileForWrite(psiFile))
return;
final Ref<SmartPsiElementPointer> refMethod = new Ref<>();
CommandProcessor.getInstance().executeCommand(aClass.getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> {
PsiElementFactory factory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory();
try {
PsiMethod method = factory.createMethodFromText("private void " + AsmCodeGenerator.CREATE_COMPONENTS_METHOD_NAME + "() { \n // TODO: place custom component creation code here \n }", aClass);
final PsiMethod psiMethod = (PsiMethod) aClass.add(method);
refMethod.set(SmartPointerManager.getInstance(aClass.getProject()).createSmartPsiElementPointer(psiMethod));
CodeStyleManager.getInstance(aClass.getProject()).reformat(psiMethod);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}), null, null);
if (!refMethod.isNull()) {
SwingUtilities.invokeLater(() -> {
final PsiMethod element = (PsiMethod) refMethod.get().getElement();
if (element != null) {
final PsiCodeBlock body = element.getBody();
assert body != null;
final PsiComment comment = PsiTreeUtil.getChildOfType(body, PsiComment.class);
if (comment != null) {
new OpenFileDescriptor(comment.getProject(), vFile, comment.getTextOffset()).navigate(true);
}
}
});
}
}
use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-community by JetBrains.
the class AddFieldQuickFix method showTemplateBuilder.
private void showTemplateBuilder(PsiElement initStatement, @NotNull final PsiFile file) {
initStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(initStatement);
if (initStatement instanceof PyAssignmentStatement) {
final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(initStatement);
final PyExpression assignedValue = ((PyAssignmentStatement) initStatement).getAssignedValue();
final PyExpression leftExpression = ((PyAssignmentStatement) initStatement).getLeftHandSideExpression();
if (assignedValue != null && leftExpression != null) {
if (replaceInitializer)
builder.replaceElement(assignedValue, myInitializer);
else
builder.replaceElement(leftExpression.getLastChild(), myIdentifier);
final VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile == null)
return;
final Editor editor = FileEditorManager.getInstance(file.getProject()).openTextEditor(new OpenFileDescriptor(file.getProject(), virtualFile), true);
if (editor == null)
return;
builder.run(editor, false);
}
}
}
use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-community by JetBrains.
the class PyRenameArgumentQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
if (!(element instanceof PsiNamedElement))
return;
final VirtualFile virtualFile = element.getContainingFile().getVirtualFile();
if (virtualFile != null) {
final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
final TemplateBuilderImpl builder = new TemplateBuilderImpl(element);
final String name = ((PsiNamedElement) element).getName();
assert name != null;
assert editor != null;
builder.replaceElement(element, TextRange.create(0, name.length()), name);
builder.run(editor, false);
}
}
use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-community by JetBrains.
the class PyDocstringGenerator method startTemplate.
public void startTemplate() {
Preconditions.checkNotNull(myDocStringOwner, "For this action docstring owner must be supplied");
final PyStringLiteralExpression docStringExpression = getDocStringExpression();
assert docStringExpression != null;
final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(docStringExpression);
if (myAddedParams.size() > 1) {
throw new IllegalArgumentException("TemplateBuilder can be created only for one parameter");
}
final DocstringParam paramToEdit = getParamToEdit();
final DocStringFormat format = myDocStringFormat;
if (format == DocStringFormat.PLAIN) {
return;
}
final StructuredDocString parsed = DocStringUtil.parseDocString(format, docStringExpression);
final Substring substring;
if (paramToEdit.isReturnValue()) {
substring = parsed.getReturnTypeSubstring();
} else {
final String paramName = paramToEdit.getName();
substring = parsed.getParamTypeSubstring(paramName);
}
if (substring == null) {
return;
}
builder.replaceRange(substring.getTextRange(), getDefaultType(getParamToEdit()));
Template template = ((TemplateBuilderImpl) builder).buildInlineTemplate();
final VirtualFile virtualFile = myDocStringOwner.getContainingFile().getVirtualFile();
if (virtualFile == null)
return;
final Project project = myDocStringOwner.getProject();
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile, docStringExpression.getTextOffset());
Editor targetEditor = FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
if (targetEditor != null) {
TemplateManager.getInstance(project).startTemplate(targetEditor, template);
}
}
use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-community by JetBrains.
the class AddDtdDeclarationFix method applyFix.
@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PsiFile containingFile = element.getContainingFile();
@NonNls String prefixToInsert = "";
@NonNls String suffixToInsert = "";
final int UNDEFINED_OFFSET = -1;
int anchorOffset = UNDEFINED_OFFSET;
PsiElement anchor = PsiTreeUtil.getParentOfType(element, XmlElementDecl.class, XmlAttlistDecl.class, XmlEntityDecl.class, XmlConditionalSection.class);
if (anchor != null)
anchorOffset = anchor.getTextRange().getStartOffset();
if (anchorOffset == UNDEFINED_OFFSET && containingFile.getLanguage() == XMLLanguage.INSTANCE) {
XmlFile file = (XmlFile) containingFile;
final XmlProlog prolog = file.getDocument().getProlog();
assert prolog != null;
final XmlDoctype doctype = prolog.getDoctype();
final XmlMarkupDecl markupDecl;
if (doctype != null) {
markupDecl = doctype.getMarkupDecl();
} else {
markupDecl = null;
}
if (doctype == null) {
final XmlTag rootTag = file.getDocument().getRootTag();
prefixToInsert = "<!DOCTYPE " + ((rootTag != null) ? rootTag.getName() : "null");
suffixToInsert = ">\n";
}
if (markupDecl == null) {
prefixToInsert += " [\n";
suffixToInsert = "]" + suffixToInsert;
if (doctype != null) {
// just before last '>'
anchorOffset = doctype.getTextRange().getEndOffset() - 1;
} else {
anchorOffset = prolog.getTextRange().getEndOffset();
}
}
}
if (anchorOffset == UNDEFINED_OFFSET)
anchorOffset = element.getTextRange().getStartOffset();
OpenFileDescriptor openDescriptor = new OpenFileDescriptor(project, containingFile.getVirtualFile(), anchorOffset);
final Editor editor = FileEditorManager.getInstance(project).openTextEditor(openDescriptor, true);
final TemplateManager templateManager = TemplateManager.getInstance(project);
final Template t = templateManager.createTemplate("", "");
if (!prefixToInsert.isEmpty())
t.addTextSegment(prefixToInsert);
t.addTextSegment("<!" + myElementDeclarationName + " " + myReference + " ");
t.addEndVariable();
t.addTextSegment(">\n");
if (!suffixToInsert.isEmpty())
t.addTextSegment(suffixToInsert);
templateManager.startTemplate(editor, t);
}
Aggregations