Search in sources :

Example 56 with LogicalPosition

use of com.intellij.openapi.editor.LogicalPosition in project intellij-code-outline by sitano.

the class CodeOutlineImage method getImgRepaintRect.

/**
     * Returns the area in this code outline image that should be repainted to
     * update the given text region.
     *
     * @param range the region to be updated
     * @return the area in this code outline image that should be repainted to
     *         update the given region
     */
public Rectangle getImgRepaintRect(TextRange range) {
    if (img == null)
        return null;
    int len = document.getTextLength();
    int startoff = range.getStartOffset();
    int endoff = range.getEndOffset();
    LogicalPosition start;
    if (startoff > len) {
        // this region is past the end of the file, so we repaint the entire
        // area below the end of the file
        LogicalPosition eof = editor.offsetToLogicalPosition(len);
        return getRectangle(0, eof.line, visibleImgWidth + 1, visibleImgHeight - eof.line + 1, scale);
    } else {
        start = editor.offsetToLogicalPosition(startoff);
        if (endoff > len) {
            // the region spans the end of the file
            return getRectangle(0, start.line, visibleImgWidth + 1, visibleImgHeight - start.line + 1, scale);
        }
    }
    LogicalPosition end = editor.offsetToLogicalPosition(endoff);
    if (start.line == end.line) {
        // we only need to repaint the characters modified on that line
        return getRectangle(start.column, start.line, visibleImgWidth + 1, end.line + 1, scale);
    } else {
        // we should repaint the entire lines that changed
        return getRectangle(0, start.line, visibleImgWidth + 1, end.line + 1, scale);
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition)

Example 57 with LogicalPosition

use of com.intellij.openapi.editor.LogicalPosition in project intellij-code-outline by sitano.

the class CodeOutlineImage method getImgRect.

/**
     * Returns a the rectangle to be drawn on this code outline image to
     * represent the given editor visible region.
     *
     * @param visible an editor visible region rectangle
     * @return a rectangle that represents the given editor visible region
     */
public Rectangle getImgRect(Rectangle visible) {
    if (img == null)
        return null;
    // we try to see how wide the visible area is by first getting the
    // position of the last character visible on the first line, and then
    // that position on the last line. a bug in xyToLogicalPosition when
    // code folding is present forces us to do this, and this code still
    // messes up when collapsed folding regions are present on the first and
    // last visible lines.
    Rectangle visibleRect;
    LogicalPosition start = editor.xyToLogicalPosition(new Point(visible.x, visible.y));
    LogicalPosition start2 = editor.xyToLogicalPosition(new Point(visible.x + visible.width, visible.y));
    LogicalPosition end = editor.xyToLogicalPosition(new Point(visible.x + visible.width, visible.y + visible.height));
    int w = Math.min(visibleImgWidth - start.column, Math.max(start2.column, end.column) - start.column) - 1;
    int h = end.line - start.line;
    visibleRect = getRectangle(start.column, start.line, w, h, scale);
    return visibleRect;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition)

Example 58 with LogicalPosition

use of com.intellij.openapi.editor.LogicalPosition in project android by JetBrains.

the class AndroidValueResourcesTest method testNavigationInPlatformXml2_NavigateFromNameAttr.

public void testNavigationInPlatformXml2_NavigateFromNameAttr() throws Exception {
    VirtualFile themes_holo = LocalFileSystem.getInstance().findFileByPath(TestUtils.getPlatformFile("data/res/values/themes_holo.xml").toString());
    assertNotNull(themes_holo);
    VirtualFile themes = LocalFileSystem.getInstance().findFileByPath(TestUtils.getPlatformFile("data/res/values/themes.xml").toString());
    assertNotNull(themes);
    // In themes_holo.xml: point to value of "Theme" in the name attribute on line:
    //     <style name="Theme.Holo.NoActionBar">
    // Goto action should navigate to "Theme" in themes.xml, on line: "<style name="Theme">"
    myFixture.configureFromExistingVirtualFile(themes_holo);
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(776, 19));
    PsiElement[] targets = GotoDeclarationAction.findAllTargetElements(myFixture.getProject(), myFixture.getEditor(), myFixture.getCaretOffset());
    assertNotNull(targets);
    assertEquals(1, targets.length);
    PsiElement targetElement = LazyValueResourceElementWrapper.computeLazyElement(targets[0]);
    assertInstanceOf(targetElement, XmlAttributeValue.class);
    XmlAttributeValue targetAttrValue = (XmlAttributeValue) targetElement;
    assertEquals("Theme", targetAttrValue.getValue());
    assertEquals("name", ((XmlAttribute) targetAttrValue.getParent()).getName());
    assertEquals("style", ((XmlTag) targetAttrValue.getParent().getParent()).getName());
    assertEquals(themes, targetElement.getContainingFile().getVirtualFile());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiElement(com.intellij.psi.PsiElement)

Example 59 with LogicalPosition

use of com.intellij.openapi.editor.LogicalPosition in project android by JetBrains.

the class AndroidValueResourcesTest method testNavigationInPlatformXml1_NavigateFromParentAttr.

public void testNavigationInPlatformXml1_NavigateFromParentAttr() throws Exception {
    VirtualFile themes_holo = LocalFileSystem.getInstance().findFileByPath(TestUtils.getPlatformFile("data/res/values/themes_holo.xml").toString());
    assertNotNull(themes_holo);
    VirtualFile themes = LocalFileSystem.getInstance().findFileByPath(TestUtils.getPlatformFile("data/res/values/themes.xml").toString());
    assertNotNull(themes);
    // In themes_holo.xml: point to value of "Theme" in the parent attribute on line:
    //     <style name="Theme.Holo.Light" parent="Theme.Light">
    // Goto action should navigate to "Theme" in themes.xml, on line: "<style name="Theme">"
    myFixture.configureFromExistingVirtualFile(themes_holo);
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(406, 45));
    PsiElement[] targets = GotoDeclarationAction.findAllTargetElements(myFixture.getProject(), myFixture.getEditor(), myFixture.getCaretOffset());
    assertNotNull(targets);
    assertEquals(1, targets.length);
    PsiElement targetElement = LazyValueResourceElementWrapper.computeLazyElement(targets[0]);
    assertInstanceOf(targetElement, XmlAttributeValue.class);
    XmlAttributeValue targetAttrValue = (XmlAttributeValue) targetElement;
    assertEquals("Theme", targetAttrValue.getValue());
    assertEquals("name", ((XmlAttribute) targetAttrValue.getParent()).getName());
    assertEquals("style", ((XmlTag) targetAttrValue.getParent().getParent()).getName());
    assertEquals(themes, targetElement.getContainingFile().getVirtualFile());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiElement(com.intellij.psi.PsiElement)

Example 60 with LogicalPosition

use of com.intellij.openapi.editor.LogicalPosition in project intellij-plugins by JetBrains.

the class DartStatementMover method calcInsertOffset.

private boolean calcInsertOffset(@NotNull PsiFile file, @NotNull Editor editor, @NotNull LineRange range, @NotNull final MoveInfo info, final boolean down) {
    int destLine = getDestLineForAnon(editor, range, down);
    int startLine = down ? range.endLine : range.startLine - 1;
    if (destLine < 0 || startLine < 0)
        return false;
    boolean firstTime = true;
    boolean isExpr = isMovingExpr(info.toMove);
    PsiElement elementStart = null;
    if (isExpr) {
        int offset = editor.logicalPositionToOffset(new LogicalPosition(startLine, 0));
        elementStart = firstNonWhiteMovableElement(offset, file, true);
        if (elementStart instanceof DartArgumentList) {
            elementStart = elementStart.getFirstChild();
        } else if (isRightBracket(elementStart) && info.toMove.firstElement instanceof DartNamedArgument) {
            // Possibly a named arg with list value
            elementStart = elementStart.getParent().getParent();
        }
        if (elementStart instanceof DartExpression || elementStart instanceof DartNamedArgument) {
            TextRange elementTextRange = elementStart.getTextRange();
            LogicalPosition pos = editor.offsetToLogicalPosition(elementTextRange.getEndOffset());
            int endOffset = editor.logicalPositionToOffset(new LogicalPosition(pos.line + 1, 0));
            PsiElement elementEnd = firstNonWhiteMovableElement(endOffset, file, false);
            if (elementEnd instanceof DartArgumentList && elementStart instanceof DartNamedArgument) {
                elementEnd = elementEnd.getLastChild();
                if (!isComma(elementEnd)) {
                    // Require trailing comma
                    return false;
                } else {
                    info.toMove2 = new LineRange(startLine, pos.line + 1);
                    return true;
                }
            }
            if (elementEnd != null && isComma(elementEnd)) {
                PsiElement elementTail = UsefulPsiTreeUtil.getPrevSiblingSkipWhiteSpacesAndComments(elementEnd, true);
                if (elementTail instanceof DartExpressionList) {
                    elementTail = elementTail.getLastChild();
                }
                if (elementStart == elementTail) {
                    if (down) {
                        info.toMove2 = new LineRange(startLine, pos.line + 1);
                    } else {
                        destLine = pos.line;
                        elementTextRange = elementTail.getTextRange();
                        pos = editor.offsetToLogicalPosition(elementTextRange.getStartOffset());
                        info.toMove2 = new LineRange(pos.line, destLine + 1);
                    }
                    return true;
                }
            }
        } else if (elementStart != null && isRightParen(elementStart)) {
            PsiElement start = elementStart.getParent().getParent();
            TextRange elementTextRange = start.getTextRange();
            LogicalPosition pos = editor.offsetToLogicalPosition(elementTextRange.getStartOffset());
            int startOffset = editor.logicalPositionToOffset(new LogicalPosition(pos.line, 0));
            PsiElement startElement = firstNonWhiteMovableElement(startOffset, file, true);
            if (startElement == start) {
                info.toMove2 = new LineRange(pos.line, down ? startLine : startLine + 1);
                return true;
            }
        }
    }
    while (true) {
        int offset = editor.logicalPositionToOffset(new LogicalPosition(destLine, 0));
        PsiElement element = firstNonWhiteMovableElement(offset, file, !isExpr || !down);
        if (firstTime) {
            if (element != null && element.getNode().getElementType() == (down ? DartTokenTypes.RBRACE : DartTokenTypes.LBRACE)) {
                PsiElement elementParent = element.getParent();
                if (elementParent != null && (isStatement(elementParent) || elementParent instanceof DartBlock)) {
                    return true;
                }
            }
        }
        if (element instanceof DartStatements)
            element = element.getFirstChild();
        while (element != null && !(element instanceof PsiFile)) {
            TextRange elementTextRange = element.getTextRange();
            if (elementTextRange.isEmpty() || !elementTextRange.grown(-1).shiftRight(1).contains(offset)) {
                PsiElement elementToSurround = null;
                boolean found = false;
                if (isExpr) {
                    if (firstTime && element instanceof DartExpression) {
                        found = true;
                    } else if (isComma(element) && UsefulPsiTreeUtil.getPrevSiblingSkipWhiteSpacesAndComments(element, true) == elementStart) {
                        found = true;
                    } else if (element instanceof DartArgumentList) {
                        element = element.getParent();
                        if (element.getParent() == elementStart) {
                            element = element.getParent().getNextSibling();
                            boolean hasComma = false;
                            while (element != null) {
                                if (UsefulPsiTreeUtil.isWhitespaceOrComment(element)) {
                                    if (element.getText().contains("\n")) {
                                        destLine += 1;
                                        break;
                                    }
                                } else if (isComma(element)) {
                                    hasComma = true;
                                } else {
                                    break;
                                }
                                element = element.getNextSibling();
                            }
                            if (!hasComma) {
                                // Disallow move if following expr has no trailing comma
                                return false;
                            }
                            found = true;
                        }
                    }
                } else if ((isStatement(element) || element instanceof PsiComment) && statementCanBePlacedAlong(element)) {
                    found = true;
                    if (!(element.getParent() instanceof IDartBlock)) {
                        elementToSurround = element;
                    }
                } else if ((element.getNode().getElementType() == DartTokenTypes.RBRACE && element.getParent() instanceof IDartBlock && (!isStatement(element.getParent().getParent()) || statementCanBePlacedAlong(element.getParent().getParent()))) || (!down && element instanceof DartStatements)) {
                    // Before/after code block closing/opening brace.
                    found = true;
                }
                if (found) {
                    if (elementToSurround != null) {
                        final SmartPointerManager manager = SmartPointerManager.getInstance(elementToSurround.getProject());
                        statementToSurroundWithCodeBlock = manager.createSmartPsiElementPointer(elementToSurround);
                    }
                    info.toMove = range;
                    int endLine = destLine;
                    if (startLine > endLine) {
                        int tmp = endLine;
                        endLine = startLine;
                        startLine = tmp;
                    }
                    info.toMove2 = new LineRange(startLine, down ? endLine : endLine + 1);
                    return true;
                }
            }
            element = element.getParent();
        }
        firstTime = false;
        destLine += down ? 1 : -1;
        if (destLine <= 0 || destLine >= editor.getDocument().getLineCount()) {
            return false;
        }
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) TextRange(com.intellij.openapi.util.TextRange) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Aggregations

LogicalPosition (com.intellij.openapi.editor.LogicalPosition)97 Document (com.intellij.openapi.editor.Document)12 PsiElement (com.intellij.psi.PsiElement)12 Editor (com.intellij.openapi.editor.Editor)11 TextRange (com.intellij.openapi.util.TextRange)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 EditorEx (com.intellij.openapi.editor.ex.EditorEx)7 NotNull (org.jetbrains.annotations.NotNull)6 CaretModel (com.intellij.openapi.editor.CaretModel)5 RelativePoint (com.intellij.ui.awt.RelativePoint)5 JSFile (com.intellij.lang.javascript.psi.JSFile)4 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)4 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)4 Project (com.intellij.openapi.project.Project)4 PsiFile (com.intellij.psi.PsiFile)4 PsiReference (com.intellij.psi.PsiReference)4 PsiMultiReference (com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)4 Mark (com.maddyhome.idea.vim.common.Mark)4 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)3 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)3