Search in sources :

Example 76 with RangeMarker

use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.

the class DocumentFoldingInfo method loadFromEditor.

void loadFromEditor(@NotNull Editor editor) {
    assertDispatchThread();
    LOG.assertTrue(!editor.isDisposed());
    clear();
    PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(editor.getDocument());
    SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject);
    EditorFoldingInfo info = EditorFoldingInfo.get(editor);
    FoldRegion[] foldRegions = editor.getFoldingModel().getAllFoldRegions();
    for (FoldRegion region : foldRegions) {
        if (!region.isValid())
            continue;
        PsiElement element = info.getPsiElement(region);
        boolean expanded = region.isExpanded();
        boolean collapseByDefault = element != null && FoldingPolicy.isCollapseByDefault(element) && !FoldingUtil.caretInsideRange(editor, TextRange.create(region));
        if (collapseByDefault == expanded || element == null) {
            FoldingInfo fi = new FoldingInfo(region.getPlaceholderText(), expanded);
            if (element != null) {
                myPsiElements.add(smartPointerManager.createSmartPsiElementPointer(element, file));
                element.putUserData(FOLDING_INFO_KEY, fi);
            } else {
                RangeMarker marker = editor.getDocument().createRangeMarker(region.getStartOffset(), region.getEndOffset());
                myRangeMarkers.add(marker);
                marker.putUserData(FOLDING_INFO_KEY, fi);
            }
        }
    }
}
Also used : FoldRegion(com.intellij.openapi.editor.FoldRegion) RangeMarker(com.intellij.openapi.editor.RangeMarker)

Example 77 with RangeMarker

use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.

the class DocumentFoldingInfo method equals.

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    DocumentFoldingInfo info = (DocumentFoldingInfo) o;
    if (myFile != null ? !myFile.equals(info.myFile) : info.myFile != null) {
        return false;
    }
    if (!myProject.equals(info.myProject) || !myPsiElements.equals(info.myPsiElements) || !mySerializedElements.equals(info.mySerializedElements)) {
        return false;
    }
    if (myRangeMarkers.size() != info.myRangeMarkers.size())
        return false;
    for (int i = 0; i < myRangeMarkers.size(); i++) {
        RangeMarker marker = myRangeMarkers.get(i);
        RangeMarker other = info.myRangeMarkers.get(i);
        if (marker == other || !marker.isValid() || !other.isValid()) {
            continue;
        }
        if (!TextRange.areSegmentsEqual(marker, other))
            return false;
        FoldingInfo fi = marker.getUserData(FOLDING_INFO_KEY);
        FoldingInfo ofi = other.getUserData(FOLDING_INFO_KEY);
        if (!Comparing.equal(fi, ofi))
            return false;
    }
    return true;
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker)

Example 78 with RangeMarker

use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.

the class JavaVariableInplaceIntroducer method appendTypeCasts.

private static void appendTypeCasts(List<RangeMarker> occurrenceMarkers, PsiFile file, Project project, @Nullable PsiVariable psiVariable) {
    if (occurrenceMarkers != null) {
        for (RangeMarker occurrenceMarker : occurrenceMarkers) {
            final PsiElement refVariableElement = file.findElementAt(occurrenceMarker.getStartOffset());
            final PsiReferenceExpression referenceExpression = PsiTreeUtil.getParentOfType(refVariableElement, PsiReferenceExpression.class);
            if (referenceExpression != null) {
                final PsiElement parent = referenceExpression.getParent();
                if (parent instanceof PsiVariable) {
                    createCastInVariableDeclaration(project, (PsiVariable) parent);
                } else if (parent instanceof PsiReferenceExpression && psiVariable != null) {
                    final PsiExpression initializer = psiVariable.getInitializer();
                    LOG.assertTrue(initializer != null);
                    final PsiType type = initializer.getType();
                    if (((PsiReferenceExpression) parent).resolve() == null && type != null) {
                        final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
                        final PsiExpression castedExpr = elementFactory.createExpressionFromText("((" + type.getCanonicalText() + ")" + referenceExpression.getText() + ")", parent);
                        JavaCodeStyleManager.getInstance(project).shortenClassReferences(referenceExpression.replace(castedExpr));
                    }
                }
            }
        }
    }
    if (psiVariable != null && psiVariable.isValid()) {
        createCastInVariableDeclaration(project, psiVariable);
    }
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker)

Example 79 with RangeMarker

use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.

the class GroovyMethodInliner method inlineReferenceImpl.

@Nullable
static RangeMarker inlineReferenceImpl(@NotNull GrCallExpression call, @NotNull GrMethod method, boolean resultOfCallExplicitlyUsed, boolean isTailMethodCall, @Nullable Editor editor) {
    try {
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(call.getProject());
        final Project project = call.getProject();
        // Variable declaration for qualifier expression
        GrVariableDeclaration qualifierDeclaration = null;
        GrReferenceExpression innerQualifier = null;
        GrExpression qualifier = null;
        if (call instanceof GrMethodCallExpression && ((GrMethodCallExpression) call).getInvokedExpression() != null) {
            GrExpression invoked = ((GrMethodCallExpression) call).getInvokedExpression();
            if (invoked instanceof GrReferenceExpression && ((GrReferenceExpression) invoked).getQualifierExpression() != null) {
                qualifier = ((GrReferenceExpression) invoked).getQualifierExpression();
                if (PsiUtil.isSuperReference(qualifier)) {
                    qualifier = null;
                } else if (!GroovyInlineMethodUtil.isSimpleReference(qualifier)) {
                    String qualName = generateQualifierName(call, method, project, qualifier);
                    qualifier = (GrExpression) PsiUtil.skipParentheses(qualifier, false);
                    qualifierDeclaration = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, qualifier, null, qualName);
                    innerQualifier = (GrReferenceExpression) factory.createExpressionFromText(qualName);
                } else {
                    innerQualifier = (GrReferenceExpression) qualifier;
                }
            }
        }
        GrMethod _method = prepareNewMethod(call, method, qualifier);
        GrExpression result = getAloneResultExpression(_method);
        if (result != null) {
            GrExpression expression = call.replaceWithExpression(result, false);
            TextRange range = expression.getTextRange();
            return editor != null ? editor.getDocument().createRangeMarker(range.getStartOffset(), range.getEndOffset(), true) : null;
        }
        GrMethod newMethod = prepareNewMethod(call, method, innerQualifier);
        String resultName = InlineMethodConflictSolver.suggestNewName("result", newMethod, call);
        // Add variable for method result
        Collection<GrStatement> returnStatements = ControlFlowUtils.collectReturns(newMethod.getBlock());
        final int returnCount = returnStatements.size();
        PsiType methodType = method.getInferredReturnType();
        GrOpenBlock body = newMethod.getBlock();
        assert body != null;
        GrExpression replaced;
        if (resultOfCallExplicitlyUsed && !isTailMethodCall) {
            GrExpression resultExpr = null;
            if (PsiType.VOID.equals(methodType)) {
                resultExpr = factory.createExpressionFromText("null");
            } else if (returnCount == 1) {
                final GrExpression returnExpression = ControlFlowUtils.extractReturnExpression(returnStatements.iterator().next());
                if (returnExpression != null) {
                    resultExpr = factory.createExpressionFromText(returnExpression.getText());
                }
            } else if (returnCount > 1) {
                resultExpr = factory.createExpressionFromText(resultName);
            }
            if (resultExpr == null) {
                resultExpr = factory.createExpressionFromText("null");
            }
            replaced = call.replaceWithExpression(resultExpr, false);
        } else {
            replaced = call;
        }
        // Calculate anchor to insert before
        GrExpression enclosingExpr = GroovyRefactoringUtil.addBlockIntoParent(replaced);
        GrVariableDeclarationOwner owner = PsiTreeUtil.getParentOfType(enclosingExpr, GrVariableDeclarationOwner.class);
        assert owner != null;
        PsiElement element = enclosingExpr;
        while (element != null && element.getParent() != owner) {
            element = element.getParent();
        }
        assert element != null && element instanceof GrStatement;
        GrStatement anchor = (GrStatement) element;
        if (!resultOfCallExplicitlyUsed) {
            assert anchor == enclosingExpr;
        }
        // add qualifier reference declaration
        if (qualifierDeclaration != null) {
            owner.addVariableDeclarationBefore(qualifierDeclaration, anchor);
        }
        // Process method return statements
        if (returnCount > 1 && !PsiType.VOID.equals(methodType) && !isTailMethodCall) {
            PsiType type = methodType != null && methodType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) ? null : methodType;
            GrVariableDeclaration resultDecl = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, "", type, resultName);
            GrStatement statement = ((GrStatementOwner) owner).addStatementBefore(resultDecl, anchor);
            JavaCodeStyleManager.getInstance(statement.getProject()).shortenClassReferences(statement);
            // Replace all return statements with assignments to 'result' variable
            for (GrStatement returnStatement : returnStatements) {
                GrExpression value = ControlFlowUtils.extractReturnExpression(returnStatement);
                if (value != null) {
                    GrExpression assignment = factory.createExpressionFromText(resultName + " = " + value.getText());
                    returnStatement.replaceWithStatement(assignment);
                } else {
                    returnStatement.replaceWithStatement(factory.createExpressionFromText(resultName + " = null"));
                }
            }
        }
        if (!isTailMethodCall && resultOfCallExplicitlyUsed && returnCount == 1) {
            returnStatements.iterator().next().removeStatement();
        } else if (!isTailMethodCall && (PsiType.VOID.equals(methodType) || returnCount == 1)) {
            for (GrStatement returnStatement : returnStatements) {
                if (returnStatement instanceof GrReturnStatement) {
                    final GrExpression returnValue = ((GrReturnStatement) returnStatement).getReturnValue();
                    if (returnValue != null && GroovyRefactoringUtil.hasSideEffect(returnValue)) {
                        returnStatement.replaceWithStatement(returnValue);
                        continue;
                    }
                } else if (GroovyRefactoringUtil.hasSideEffect(returnStatement)) {
                    continue;
                }
                returnStatement.removeStatement();
            }
        }
        // Add all method statements
        GrStatement[] statements = body.getStatements();
        for (GrStatement statement : statements) {
            ((GrStatementOwner) owner).addStatementBefore(statement, anchor);
        }
        if (resultOfCallExplicitlyUsed && !isTailMethodCall) {
            TextRange range = replaced.getTextRange();
            RangeMarker marker = editor != null ? editor.getDocument().createRangeMarker(range.getStartOffset(), range.getEndOffset(), true) : null;
            reformatOwner(owner);
            return marker;
        } else {
            GrStatement stmt;
            if (isTailMethodCall && enclosingExpr.getParent() instanceof GrReturnStatement) {
                stmt = (GrReturnStatement) enclosingExpr.getParent();
            } else {
                stmt = enclosingExpr;
            }
            stmt.removeStatement();
            reformatOwner(owner);
            return null;
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return null;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Project(com.intellij.openapi.project.Project) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) Nullable(org.jetbrains.annotations.Nullable)

Example 80 with RangeMarker

use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.

the class TodoPanel method updatePreviewPanel.

private void updatePreviewPanel() {
    if (myProject == null || myProject.isDisposed())
        return;
    List<UsageInfo> infos = new ArrayList<>();
    final TreePath path = myTree.getSelectionPath();
    if (path != null) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
        Object userObject = node.getUserObject();
        if (userObject instanceof NodeDescriptor) {
            Object element = ((NodeDescriptor) userObject).getElement();
            TodoItemNode pointer = myTodoTreeBuilder.getFirstPointerForElement(element);
            if (pointer != null) {
                final SmartTodoItemPointer value = pointer.getValue();
                final Document document = value.getDocument();
                final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
                final RangeMarker rangeMarker = value.getRangeMarker();
                if (psiFile != null) {
                    infos.add(new UsageInfo(psiFile, rangeMarker.getStartOffset(), rangeMarker.getEndOffset()));
                }
            }
        }
    }
    myUsagePreviewPanel.updateLayout(infos.isEmpty() ? null : infos);
}
Also used : TreePath(javax.swing.tree.TreePath) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ArrayList(java.util.ArrayList) NodeDescriptor(com.intellij.ide.util.treeView.NodeDescriptor) PsiFile(com.intellij.psi.PsiFile) RangeMarker(com.intellij.openapi.editor.RangeMarker) TodoItemNode(com.intellij.ide.todo.nodes.TodoItemNode) Document(com.intellij.openapi.editor.Document) UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

RangeMarker (com.intellij.openapi.editor.RangeMarker)111 Document (com.intellij.openapi.editor.Document)33 TextRange (com.intellij.openapi.util.TextRange)20 Project (com.intellij.openapi.project.Project)19 PsiFile (com.intellij.psi.PsiFile)14 PsiElement (com.intellij.psi.PsiElement)13 IncorrectOperationException (com.intellij.util.IncorrectOperationException)13 Editor (com.intellij.openapi.editor.Editor)11 Nullable (org.jetbrains.annotations.Nullable)11 NotNull (org.jetbrains.annotations.NotNull)10 Template (com.intellij.codeInsight.template.Template)8 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)7 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)6 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)6 RangeHighlighterEx (com.intellij.openapi.editor.ex.RangeHighlighterEx)5 THashMap (gnu.trove.THashMap)5 GutterMark (com.intellij.codeInsight.daemon.GutterMark)4 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)4 ApplicationManager (com.intellij.openapi.application.ApplicationManager)4 RelativePoint (com.intellij.ui.awt.RelativePoint)4