Search in sources :

Example 6 with OpenFileDescriptor

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);
                }
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Ref(com.intellij.openapi.util.Ref) IncorrectOperationException(com.intellij.util.IncorrectOperationException) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 7 with OpenFileDescriptor

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);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor)

Example 8 with OpenFileDescriptor

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);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) PsiNamedElement(com.intellij.psi.PsiNamedElement) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 9 with OpenFileDescriptor

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);
    }
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor)

Example 10 with OpenFileDescriptor

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);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) Template(com.intellij.codeInsight.template.Template) TemplateManager(com.intellij.codeInsight.template.TemplateManager) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Aggregations

OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)155 VirtualFile (com.intellij.openapi.vfs.VirtualFile)97 Project (com.intellij.openapi.project.Project)40 Editor (com.intellij.openapi.editor.Editor)30 PsiElement (com.intellij.psi.PsiElement)27 PsiFile (com.intellij.psi.PsiFile)21 Navigatable (com.intellij.pom.Navigatable)19 Nullable (org.jetbrains.annotations.Nullable)18 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)10 Document (com.intellij.openapi.editor.Document)9 FileEditor (com.intellij.openapi.fileEditor.FileEditor)9 TextRange (com.intellij.openapi.util.TextRange)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 File (java.io.File)9 Result (com.intellij.openapi.application.Result)8 EditorImpl (com.intellij.openapi.editor.impl.EditorImpl)8 NotNull (org.jetbrains.annotations.NotNull)6 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)5 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 XmlTag (com.intellij.psi.xml.XmlTag)5