Search in sources :

Example 96 with IncorrectOperationException

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

the class AbstractSuppressByNoInspectionCommentFix method invoke.

@Override
public void invoke(@NotNull final Project project, @Nullable Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException {
    PsiElement container = getContainer(element);
    if (container == null)
        return;
    final List<? extends PsiElement> comments = getCommentsFor(container);
    if (comments != null) {
        for (PsiElement comment : comments) {
            if (comment instanceof PsiComment && SuppressionUtil.isSuppressionComment(comment)) {
                replaceSuppressionComment(comment);
                return;
            }
        }
    }
    boolean caretWasBeforeStatement = editor != null && editor.getCaretModel().getOffset() == container.getTextRange().getStartOffset();
    try {
        createSuppression(project, element, container);
    } catch (IncorrectOperationException e) {
        if (!ApplicationManager.getApplication().isUnitTestMode() && editor != null) {
            Messages.showErrorDialog(editor.getComponent(), InspectionsBundle.message("suppress.inspection.annotation.syntax.error", e.getMessage()));
        }
    }
    if (caretWasBeforeStatement) {
        editor.getCaretModel().moveToOffset(container.getTextRange().getStartOffset());
    }
    UndoUtil.markPsiFileForUndo(element.getContainingFile());
}
Also used : PsiComment(com.intellij.psi.PsiComment) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement)

Example 97 with IncorrectOperationException

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

the class DefaultCreateFromTemplateHandler method createFromTemplate.

@NotNull
@Override
public PsiElement createFromTemplate(final Project project, final PsiDirectory directory, String fileName, final FileTemplate template, final String templateText, @NotNull final Map<String, Object> props) throws IncorrectOperationException {
    fileName = checkAppendExtension(fileName, template);
    if (FileTypeManager.getInstance().isFileIgnored(fileName)) {
        throw new IncorrectOperationException("This filename is ignored (Settings | Editor | File Types | Ignore files and folders)");
    }
    directory.checkCreateFile(fileName);
    FileType type = FileTypeRegistry.getInstance().getFileTypeByFileName(fileName);
    PsiFile file = PsiFileFactory.getInstance(project).createFileFromText(fileName, type, templateText);
    if (template.isReformatCode()) {
        CodeStyleManager.getInstance(project).reformat(file);
    }
    file = (PsiFile) directory.add(file);
    return file;
}
Also used : FileType(com.intellij.openapi.fileTypes.FileType) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 98 with IncorrectOperationException

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

the class CodeFormatterFacade method doProcessRange.

private ASTNode doProcessRange(final ASTNode element, final int startOffset, final int endOffset, @Nullable RangeMarker rangeMarker) {
    final PsiElement psiElement = SourceTreeToPsiMap.treeElementToPsi(element);
    assert psiElement != null;
    final PsiFile file = psiElement.getContainingFile();
    final Document document = file.getViewProvider().getDocument();
    PsiElement elementToFormat = document instanceof DocumentWindow ? InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file) : psiElement;
    final PsiFile fileToFormat = elementToFormat.getContainingFile();
    final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(fileToFormat);
    if (builder != null) {
        if (rangeMarker == null && document != null && endOffset < document.getTextLength()) {
            rangeMarker = document.createRangeMarker(startOffset, endOffset);
        }
        TextRange range = preprocess(element, TextRange.create(startOffset, endOffset));
        if (document instanceof DocumentWindow) {
            DocumentWindow documentWindow = (DocumentWindow) document;
            range = documentWindow.injectedToHost(range);
        }
        //final SmartPsiElementPointer pointer = SmartPointerManager.getInstance(psiElement.getProject()).createSmartPsiElementPointer(psiElement);
        final FormattingModel model = CoreFormatterUtil.buildModel(builder, elementToFormat, mySettings, FormattingMode.REFORMAT);
        if (file.getTextLength() > 0) {
            try {
                FormatterEx.getInstanceEx().format(model, mySettings, mySettings.getIndentOptionsByFile(fileToFormat, range), new FormatTextRanges(range, true));
                wrapLongLinesIfNecessary(file, document, startOffset, endOffset);
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
        }
        if (!psiElement.isValid()) {
            if (rangeMarker != null) {
                final PsiElement at = file.findElementAt(rangeMarker.getStartOffset());
                final PsiElement result = PsiTreeUtil.getParentOfType(at, psiElement.getClass(), false);
                assert result != null;
                rangeMarker.dispose();
                return result.getNode();
            } else {
                assert false;
            }
        }
    //      return SourceTreeToPsiMap.psiElementToTree(pointer.getElement());
    }
    if (rangeMarker != null) {
        rangeMarker.dispose();
    }
    return element;
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) DocumentBasedFormattingModel(com.intellij.psi.formatter.DocumentBasedFormattingModel) TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 99 with IncorrectOperationException

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

the class CodeFormatterFacade method processText.

public void processText(PsiFile file, final FormatTextRanges ranges, boolean doPostponedFormatting) {
    final Project project = file.getProject();
    Document document = PsiDocumentManager.getInstance(project).getDocument(file);
    final List<FormatTextRange> textRanges = ranges.getRanges();
    if (document instanceof DocumentWindow) {
        file = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
        final DocumentWindow documentWindow = (DocumentWindow) document;
        for (FormatTextRange range : textRanges) {
            range.setTextRange(documentWindow.injectedToHost(range.getTextRange()));
        }
        document = documentWindow.getDelegate();
    }
    final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file);
    final Language contextLanguage = file.getLanguage();
    if (builder != null) {
        if (file.getTextLength() > 0) {
            LOG.assertTrue(document != null);
            try {
                final FileViewProvider viewProvider = file.getViewProvider();
                final PsiElement startElement = viewProvider.findElementAt(textRanges.get(0).getTextRange().getStartOffset(), contextLanguage);
                final PsiElement endElement = viewProvider.findElementAt(textRanges.get(textRanges.size() - 1).getTextRange().getEndOffset() - 1, contextLanguage);
                final PsiElement commonParent = startElement != null && endElement != null ? PsiTreeUtil.findCommonParent(startElement, endElement) : null;
                ASTNode node = null;
                if (commonParent != null) {
                    node = commonParent.getNode();
                }
                if (node == null) {
                    node = file.getNode();
                }
                for (FormatTextRange range : ranges.getRanges()) {
                    TextRange rangeToUse = preprocess(node, range.getTextRange());
                    range.setTextRange(rangeToUse);
                }
                if (doPostponedFormatting) {
                    RangeMarker[] markers = new RangeMarker[textRanges.size()];
                    int i = 0;
                    for (FormatTextRange range : textRanges) {
                        TextRange textRange = range.getTextRange();
                        int start = textRange.getStartOffset();
                        int end = textRange.getEndOffset();
                        if (start >= 0 && end > start && end <= document.getTextLength()) {
                            markers[i] = document.createRangeMarker(textRange);
                            markers[i].setGreedyToLeft(true);
                            markers[i].setGreedyToRight(true);
                            i++;
                        }
                    }
                    final PostprocessReformattingAspect component = file.getProject().getComponent(PostprocessReformattingAspect.class);
                    FormattingProgressTask.FORMATTING_CANCELLED_FLAG.set(false);
                    component.doPostponedFormatting(file.getViewProvider());
                    i = 0;
                    for (FormatTextRange range : textRanges) {
                        RangeMarker marker = markers[i];
                        if (marker != null) {
                            range.setTextRange(TextRange.create(marker));
                            marker.dispose();
                        }
                        i++;
                    }
                }
                if (FormattingProgressTask.FORMATTING_CANCELLED_FLAG.get()) {
                    return;
                }
                final FormattingModel originalModel = CoreFormatterUtil.buildModel(builder, file, mySettings, FormattingMode.REFORMAT);
                final FormattingModel model = new DocumentBasedFormattingModel(originalModel, document, project, mySettings, file.getFileType(), file);
                FormatterEx formatter = FormatterEx.getInstanceEx();
                if (CodeStyleManager.getInstance(project).isSequentialProcessingAllowed()) {
                    formatter.setProgressTask(new FormattingProgressTask(project, file, document));
                }
                CommonCodeStyleSettings.IndentOptions indentOptions = mySettings.getIndentOptionsByFile(file, textRanges.size() == 1 ? textRanges.get(0).getTextRange() : null);
                formatter.format(model, mySettings, indentOptions, ranges, myReformatContext);
                for (FormatTextRange range : textRanges) {
                    TextRange textRange = range.getTextRange();
                    wrapLongLinesIfNecessary(file, document, textRange.getStartOffset(), textRange.getEndOffset());
                }
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
        }
    }
}
Also used : DocumentBasedFormattingModel(com.intellij.psi.formatter.DocumentBasedFormattingModel) TextRange(com.intellij.openapi.util.TextRange) DocumentWindow(com.intellij.injected.editor.DocumentWindow) DocumentBasedFormattingModel(com.intellij.psi.formatter.DocumentBasedFormattingModel) Project(com.intellij.openapi.project.Project) PostprocessReformattingAspect(com.intellij.psi.impl.source.PostprocessReformattingAspect) Language(com.intellij.lang.Language) ASTNode(com.intellij.lang.ASTNode) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 100 with IncorrectOperationException

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

the class SmartCompletionTemplateItem method getType.

@Override
public PsiType getType() {
    final Template template = getTemplate();
    String text = template.getTemplateText();
    StringBuilder resultingText = new StringBuilder(text);
    int segmentsCount = template.getSegmentsCount();
    for (int j = segmentsCount - 1; j >= 0; j--) {
        if (template.getSegmentName(j).equals(TemplateImpl.END)) {
            continue;
        }
        resultingText.insert(template.getSegmentOffset(j), "xxx");
    }
    try {
        final PsiExpression templateExpression = JavaPsiFacade.getElementFactory(myContext.getProject()).createExpressionFromText(resultingText.toString(), myContext);
        return templateExpression.getType();
    } catch (IncorrectOperationException e) {
        // can happen when text of the template does not form an expression
        return null;
    }
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Template(com.intellij.codeInsight.template.Template)

Aggregations

IncorrectOperationException (com.intellij.util.IncorrectOperationException)485 Project (com.intellij.openapi.project.Project)91 NotNull (org.jetbrains.annotations.NotNull)91 Nullable (org.jetbrains.annotations.Nullable)54 PsiElement (com.intellij.psi.PsiElement)50 TextRange (com.intellij.openapi.util.TextRange)43 VirtualFile (com.intellij.openapi.vfs.VirtualFile)38 Document (com.intellij.openapi.editor.Document)37 ASTNode (com.intellij.lang.ASTNode)35 PsiFile (com.intellij.psi.PsiFile)35 Editor (com.intellij.openapi.editor.Editor)33 NonNls (org.jetbrains.annotations.NonNls)32 UsageInfo (com.intellij.usageView.UsageInfo)31 ArrayList (java.util.ArrayList)31 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)24 IOException (java.io.IOException)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