Search in sources :

Example 6 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project Main by SpartanRefactoring.

the class SpartanizerAnnotator method annotate.

@Override
public void annotate(@NotNull final PsiElement e, @NotNull AnnotationHolder h) {
    try {
        if (!Spartanizer.canTip(e) || e.getContainingFile().getName().contains("Spartanizer"))
            return;
        Annotation annotation = h.createInfoAnnotation(e, "Spartanize This!");
        annotation.registerFix(new IntentionAction() {

            @SuppressWarnings("unchecked")
            @Nls
            @NotNull
            @Override
            public String getText() {
                return Toolbox.getInstance().getTipper(e).description(e);
            }

            @Nls
            @NotNull
            @Override
            public String getFamilyName() {
                return "SpartanizerAction";
            }

            @Override
            public boolean isAvailable(@NotNull Project __, Editor e, PsiFile f) {
                return true;
            }

            @Override
            public void invoke(@NotNull Project p, Editor ed, PsiFile f) throws IncorrectOperationException {
                Spartanizer.spartanizeElement(e);
            }

            @Override
            public boolean startInWriteAction() {
                return false;
            }
        });
        TextAttributesKey.createTextAttributesKey("");
        annotation.setEnforcedTextAttributes((new TextAttributes(null, null, JBColor.BLUE, EffectType.WAVE_UNDERSCORE, 0)));
    } catch (Throwable t) {
        Logger l = new Logger(this.getClass());
        l.error("", t);
    }
}
Also used : Logger(il.org.spartan.Leonidas.plugin.utils.logging.Logger) NotNull(org.jetbrains.annotations.NotNull) Annotation(com.intellij.lang.annotation.Annotation) Project(com.intellij.openapi.project.Project) Nls(org.jetbrains.annotations.Nls) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor)

Example 7 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project kotlin by JetBrains.

the class AbstractFormatterTest method doTextTest.

public void doTextTest(final Action action, final String text, File fileAfter, String extension) throws IncorrectOperationException {
    final PsiFile file = createFile("A" + extension, text);
    if (myLineRange != null) {
        DocumentImpl document = new DocumentImpl(text);
        myTextRange = new TextRange(document.getLineStartOffset(myLineRange.getStartOffset()), document.getLineEndOffset(myLineRange.getEndOffset()));
    }
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
    final Document document = manager.getDocument(file);
    CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {

        @Override
        public void run() {
            ApplicationManager.getApplication().runWriteAction(new Runnable() {

                @Override
                public void run() {
                    document.replaceString(0, document.getTextLength(), text);
                    manager.commitDocument(document);
                    try {
                        TextRange rangeToUse = myTextRange;
                        if (rangeToUse == null) {
                            rangeToUse = file.getTextRange();
                        }
                        ACTIONS.get(action).run(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
                    } catch (IncorrectOperationException e) {
                        assertTrue(e.getLocalizedMessage(), false);
                    }
                }
            });
        }
    }, "", "");
    if (document == null) {
        fail("Don't expect the document to be null");
        return;
    }
    KotlinTestUtils.assertEqualsToFile(fileAfter, document.getText());
    manager.commitDocument(document);
    KotlinTestUtils.assertEqualsToFile(fileAfter, file.getText());
}
Also used : PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Document(com.intellij.openapi.editor.Document) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 8 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project kotlin by JetBrains.

the class MoveKotlinTopLevelDeclarationsDialog method doAction.

@Override
protected void doAction() {
    KotlinMoveTarget target = selectMoveTarget();
    if (target == null)
        return;
    saveRefactoringSettings();
    List<KtNamedDeclaration> elementsToMove = getSelectedElementsToMove();
    List<KtFile> sourceFiles = getSourceFiles(elementsToMove);
    final PsiDirectory sourceDirectory = getSourceDirectory(sourceFiles);
    for (PsiElement element : elementsToMove) {
        String message = target.verify(element.getContainingFile());
        if (message != null) {
            CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), message, null, myProject);
            return;
        }
    }
    try {
        boolean deleteSourceFile = false;
        if (isFullFileMove()) {
            if (isMoveToPackage()) {
                Pair<VirtualFile, ? extends MoveDestination> sourceRootWithMoveDestination = selectPackageBasedTargetDirAndDestination(false);
                //noinspection ConstantConditions
                final MoveDestination moveDestination = sourceRootWithMoveDestination.getSecond();
                PsiDirectory targetDir = moveDestination.getTargetIfExists(sourceDirectory);
                String targetFileName = sourceFiles.size() > 1 ? null : tfFileNameInPackage.getText();
                List<PsiFile> filesExistingInTargetDir = getFilesExistingInTargetDir(sourceFiles, targetFileName, targetDir);
                if (filesExistingInTargetDir.isEmpty()) {
                    PsiDirectory targetDirectory = ApplicationUtilsKt.runWriteAction(new Function0<PsiDirectory>() {

                        @Override
                        public PsiDirectory invoke() {
                            return moveDestination.getTargetDirectory(sourceDirectory);
                        }
                    });
                    for (KtFile sourceFile : sourceFiles) {
                        MoveUtilsKt.setUpdatePackageDirective(sourceFile, cbUpdatePackageDirective.isSelected());
                    }
                    invokeRefactoring(new MoveFilesWithDeclarationsProcessor(myProject, sourceFiles, targetDirectory, targetFileName, isSearchInComments(), isSearchInNonJavaFiles(), moveCallback));
                    return;
                }
            }
            int ret = Messages.showYesNoCancelDialog(myProject, "You are about to move all declarations out of the source file(s). Do you want to delete empty files?", RefactoringBundle.message("move.title"), Messages.getQuestionIcon());
            if (ret == Messages.CANCEL)
                return;
            deleteSourceFile = ret == Messages.YES;
        }
        MoveDeclarationsDescriptor options = new MoveDeclarationsDescriptor(elementsToMove, target, MoveDeclarationsDelegate.TopLevel.INSTANCE, isSearchInComments(), isSearchInNonJavaFiles(), true, deleteSourceFile, moveCallback, false);
        invokeRefactoring(new MoveKotlinDeclarationsProcessor(myProject, options, Mover.Default.INSTANCE));
    } catch (IncorrectOperationException e) {
        CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), e.getMessage(), null, myProject);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MoveDestination(com.intellij.refactoring.MoveDestination) AutocreatingSingleSourceRootMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.AutocreatingSingleSourceRootMoveDestination) MultipleRootsMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.MultipleRootsMoveDestination) KtNamedDeclaration(org.jetbrains.kotlin.psi.KtNamedDeclaration) KtFile(org.jetbrains.kotlin.psi.KtFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 9 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class DomRootInvocationHandler method setEmptyXmlTag.

@Override
protected XmlTag setEmptyXmlTag() {
    final XmlTag[] result = new XmlTag[] { null };
    getManager().runChange(() -> {
        try {
            final String namespace = getXmlElementNamespace();
            @NonNls final String nsDecl = StringUtil.isEmpty(namespace) ? "" : " xmlns=\"" + namespace + "\"";
            final XmlFile xmlFile = getFile();
            final XmlTag tag = XmlElementFactory.getInstance(xmlFile.getProject()).createTagFromText("<" + getXmlElementName() + nsDecl + "/>");
            result[0] = ((XmlDocument) xmlFile.getDocument().replace(((XmlFile) tag.getContainingFile()).getDocument())).getRootTag();
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    });
    return result[0];
}
Also used : NonNls(org.jetbrains.annotations.NonNls) XmlFile(com.intellij.psi.xml.XmlFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag)

Example 10 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class IndexedElementInvocationHandler method setEmptyXmlTag.

@Override
protected XmlTag setEmptyXmlTag() {
    final DomInvocationHandler parent = getParentHandler();
    assert parent != null : "write operations should be performed on the DOM having a parent, your DOM may be not very fresh";
    final FixedChildDescriptionImpl description = getChildDescription();
    final XmlFile xmlFile = getFile();
    parent.createFixedChildrenTags(getXmlName(), description, myIndex);
    final List<XmlTag> tags = DomImplUtil.findSubTags(parent.getXmlTag(), getXmlName(), xmlFile);
    if (tags.size() > myIndex) {
        return tags.get(myIndex);
    }
    final XmlTag[] newTag = new XmlTag[1];
    getManager().runChange(() -> {
        try {
            final XmlTag parentTag = parent.getXmlTag();
            newTag[0] = (XmlTag) parentTag.add(parent.createChildTag(getXmlName()));
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    });
    return newTag[0];
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

IncorrectOperationException (com.intellij.util.IncorrectOperationException)494 Project (com.intellij.openapi.project.Project)91 NotNull (org.jetbrains.annotations.NotNull)91 PsiElement (com.intellij.psi.PsiElement)55 Nullable (org.jetbrains.annotations.Nullable)55 TextRange (com.intellij.openapi.util.TextRange)43 VirtualFile (com.intellij.openapi.vfs.VirtualFile)39 Document (com.intellij.openapi.editor.Document)38 PsiFile (com.intellij.psi.PsiFile)38 ASTNode (com.intellij.lang.ASTNode)35 Editor (com.intellij.openapi.editor.Editor)33 ArrayList (java.util.ArrayList)32 NonNls (org.jetbrains.annotations.NonNls)32 UsageInfo (com.intellij.usageView.UsageInfo)31 IOException (java.io.IOException)27 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)24 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 XmlTag (com.intellij.psi.xml.XmlTag)20 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)19 Module (com.intellij.openapi.module.Module)18