Search in sources :

Example 11 with ProperTextRange

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

the class DaemonRespondToChangesTest method testRehighlightInDebuggerExpressionFragment.

public void testRehighlightInDebuggerExpressionFragment() throws Exception {
    PsiExpressionCodeFragment fragment = JavaCodeFragmentFactory.getInstance(getProject()).createExpressionCodeFragment("+ <caret>\"a\"", null, PsiType.getJavaLangObject(getPsiManager(), GlobalSearchScope.allScope(getProject())), true);
    myFile = fragment;
    Document document = PsiDocumentManager.getInstance(getProject()).getDocument(fragment);
    myEditor = EditorFactory.getInstance().createEditor(document, getProject(), StdFileTypes.JAVA, false);
    ProperTextRange visibleRange = makeEditorWindowVisible(new Point(0, 0), myEditor);
    assertEquals(document.getTextLength(), visibleRange.getLength());
    try {
        final EditorInfo editorInfo = new EditorInfo(document.getText());
        final String newFileText = editorInfo.getNewFileText();
        ApplicationManager.getApplication().runWriteAction(() -> {
            if (!document.getText().equals(newFileText)) {
                document.setText(newFileText);
            }
            editorInfo.applyToEditor(myEditor);
        });
        PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
        List<HighlightInfo> errors = highlightErrors();
        HighlightInfo error = assertOneElement(errors);
        assertEquals("Operator '+' cannot be applied to 'java.lang.String'", error.getDescription());
        type(" ");
        Collection<HighlightInfo> afterTyping = highlightErrors();
        HighlightInfo after = assertOneElement(afterTyping);
        assertEquals("Operator '+' cannot be applied to 'java.lang.String'", after.getDescription());
    } finally {
        EditorFactory.getInstance().releaseEditor(myEditor);
    }
}
Also used : EditorInfo(com.intellij.codeInsight.EditorInfo) ProperTextRange(com.intellij.openapi.util.ProperTextRange)

Example 12 with ProperTextRange

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

the class DaemonRespondToChangesTest method testErrorInTheEndOutsideVisibleArea.

public void testErrorInTheEndOutsideVisibleArea() throws Throwable {
    String text = "<xml> \n" + StringUtil.repeatSymbol('\n', 1000) + "</xml>\nxxxxx<caret>";
    configureByText(StdFileTypes.XML, text);
    ProperTextRange visibleRange = makeEditorWindowVisible(new Point(0, 1000), myEditor);
    assertTrue(visibleRange.getStartOffset() > 0);
    List<HighlightInfo> warns = highlightErrors();
    HighlightInfo info = assertOneElement(warns);
    assertEquals("Top level element is not completed", info.getDescription());
    type("xxx");
    List<HighlightInfo> errors = highlightErrors();
    info = assertOneElement(errors);
    assertEquals("Top level element is not completed", info.getDescription());
}
Also used : ProperTextRange(com.intellij.openapi.util.ProperTextRange)

Example 13 with ProperTextRange

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

the class VisibleHighlightingPassFactory method calculateVisibleRange.

@NotNull
public static ProperTextRange calculateVisibleRange(@NotNull Editor editor) {
    Rectangle rect = editor.getScrollingModel().getVisibleArea();
    LogicalPosition startPosition = editor.xyToLogicalPosition(new Point(rect.x, rect.y));
    int visibleStart = editor.logicalPositionToOffset(startPosition);
    LogicalPosition endPosition = editor.xyToLogicalPosition(new Point(rect.x + rect.width, rect.y + rect.height));
    int visibleEnd = editor.logicalPositionToOffset(new LogicalPosition(endPosition.line + 1, 0));
    return new ProperTextRange(visibleStart, Math.max(visibleEnd, visibleStart));
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) ProperTextRange(com.intellij.openapi.util.ProperTextRange) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with ProperTextRange

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

the class Divider method divideInsideAndOutsideInOneRoot.

private static void divideInsideAndOutsideInOneRoot(@NotNull PsiFile root, @NotNull TextRange restrictRange, @NotNull TextRange priorityRange, @NotNull List<PsiElement> inside, @NotNull List<ProperTextRange> insideRanges, @NotNull List<PsiElement> outside, @NotNull List<ProperTextRange> outsideRanges, @NotNull List<PsiElement> outParents, @NotNull List<ProperTextRange> outParentRanges, boolean includeParents) {
    int startOffset = restrictRange.getStartOffset();
    int endOffset = restrictRange.getEndOffset();
    final Condition<PsiElement>[] filters = Extensions.getExtensions(CollectHighlightsUtil.EP_NAME);
    final TIntStack starts = new TIntStack(STARTING_TREE_HEIGHT);
    starts.push(startOffset);
    final Stack<PsiElement> elements = new Stack<>(STARTING_TREE_HEIGHT);
    final Stack<PsiElement> children = new Stack<>(STARTING_TREE_HEIGHT);
    PsiElement element = root;
    PsiElement child = HAVE_TO_GET_CHILDREN;
    int offset = 0;
    while (true) {
        ProgressManager.checkCanceled();
        for (Condition<PsiElement> filter : filters) {
            if (!filter.value(element)) {
                assert child == HAVE_TO_GET_CHILDREN;
                // do not want to process children
                child = null;
                break;
            }
        }
        boolean startChildrenVisiting;
        if (child == HAVE_TO_GET_CHILDREN) {
            startChildrenVisiting = true;
            child = element.getFirstChild();
        } else {
            startChildrenVisiting = false;
        }
        if (child == null) {
            if (startChildrenVisiting) {
                // leaf element
                offset += element.getTextLength();
            }
            int start = starts.pop();
            if (startOffset <= start && offset <= endOffset) {
                if (priorityRange.containsRange(start, offset)) {
                    inside.add(element);
                    insideRanges.add(new ProperTextRange(start, offset));
                } else {
                    outside.add(element);
                    outsideRanges.add(new ProperTextRange(start, offset));
                }
            }
            if (elements.isEmpty())
                break;
            element = elements.pop();
            child = children.pop();
        } else {
            // composite element
            if (offset > endOffset)
                break;
            children.push(child.getNextSibling());
            starts.push(offset);
            elements.push(element);
            element = child;
            child = HAVE_TO_GET_CHILDREN;
        }
    }
    if (includeParents) {
        PsiElement parent = !outside.isEmpty() ? outside.get(outside.size() - 1) : !inside.isEmpty() ? inside.get(inside.size() - 1) : CollectHighlightsUtil.findCommonParent(root, startOffset, endOffset);
        while (parent != null && !(parent instanceof PsiFile)) {
            parent = parent.getParent();
            if (parent != null) {
                outParents.add(parent);
                TextRange textRange = parent.getTextRange();
                assert textRange != null : "Text range for " + parent + " is null. " + parent.getClass() + "; root: " + root + ": " + root.getVirtualFile();
                outParentRanges.add(ProperTextRange.create(textRange));
            }
        }
    }
    assert inside.size() == insideRanges.size();
    assert outside.size() == outsideRanges.size();
    assert outParents.size() == outParentRanges.size();
}
Also used : Condition(com.intellij.openapi.util.Condition) ProperTextRange(com.intellij.openapi.util.ProperTextRange) PsiFile(com.intellij.psi.PsiFile) ProperTextRange(com.intellij.openapi.util.ProperTextRange) TextRange(com.intellij.openapi.util.TextRange) TIntStack(gnu.trove.TIntStack) PsiElement(com.intellij.psi.PsiElement) TIntStack(gnu.trove.TIntStack) Stack(com.intellij.util.containers.Stack)

Example 15 with ProperTextRange

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

the class SelectInEditorManagerImpl method selectInEditor.

@Override
public void selectInEditor(VirtualFile file, final int startOffset, final int endOffset, final boolean toSelectLine, final boolean toUseNormalSelection) {
    releaseAll();
    final TextRange textRange;
    if (file instanceof VirtualFileWindow) {
        DocumentWindow documentWindow = ((VirtualFileWindow) file).getDocumentWindow();
        textRange = documentWindow.injectedToHost(new TextRange(startOffset, endOffset));
        file = ((VirtualFileWindow) file).getDelegate();
    } else {
        textRange = new ProperTextRange(startOffset, endOffset);
    }
    openEditor(file, endOffset);
    final Editor editor = openEditor(file, textRange.getStartOffset());
    SwingUtilities.invokeLater(() -> {
        if (editor != null && !editor.isDisposed()) {
            doSelect(toUseNormalSelection, editor, toSelectLine, textRange);
        }
    });
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) ProperTextRange(com.intellij.openapi.util.ProperTextRange) ProperTextRange(com.intellij.openapi.util.ProperTextRange) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor)

Aggregations

ProperTextRange (com.intellij.openapi.util.ProperTextRange)31 TextRange (com.intellij.openapi.util.TextRange)14 com.intellij.codeInsight.hint (com.intellij.codeInsight.hint)4 PsiElement (com.intellij.psi.PsiElement)4 RelativePoint (com.intellij.ui.awt.RelativePoint)4 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 Document (com.intellij.openapi.editor.Document)2 Segment (com.intellij.openapi.util.Segment)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 List (java.util.List)2 BackgroundEditorHighlighter (com.intellij.codeHighlighting.BackgroundEditorHighlighter)1 HighlightingPass (com.intellij.codeHighlighting.HighlightingPass)1 EditorInfo (com.intellij.codeInsight.EditorInfo)1 DefaultHighlightInfoProcessor (com.intellij.codeInsight.daemon.impl.DefaultHighlightInfoProcessor)1 GeneralHighlightingPass (com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass)1 LocalInspectionsPass (com.intellij.codeInsight.daemon.impl.LocalInspectionsPass)1 EmptyPass (com.intellij.codeInsight.daemon.impl.ProgressableTextEditorHighlightingPass.EmptyPass)1 GenericElementSignatureProvider (com.intellij.codeInsight.folding.impl.GenericElementSignatureProvider)1 InspectionManager (com.intellij.codeInspection.InspectionManager)1