Search in sources :

Example 31 with LogicalPosition

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

the class TestEditorManagerImpl method openTextEditor.

@Override
public Editor openTextEditor(@NotNull OpenFileDescriptor descriptor, boolean focusEditor) {
    final VirtualFile file = descriptor.getFile();
    Editor editor = myVirtualFile2Editor.get(file);
    if (editor == null) {
        PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file);
        LOG.assertTrue(psiFile != null, file);
        Document document = PsiDocumentManager.getInstance(myProject).getDocument(psiFile);
        LOG.assertTrue(document != null, psiFile);
        editor = EditorFactory.getInstance().createEditor(document, myProject);
        final EditorHighlighter highlighter = HighlighterFactory.createHighlighter(myProject, file);
        ((EditorEx) editor).setHighlighter(highlighter);
        ((EditorEx) editor).setFile(file);
        myVirtualFile2Editor.put(file, editor);
    }
    if (descriptor.getOffset() >= 0) {
        editor.getCaretModel().moveToOffset(descriptor.getOffset());
    } else if (descriptor.getLine() >= 0 && descriptor.getColumn() >= 0) {
        editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(descriptor.getLine(), descriptor.getColumn()));
    }
    editor.getSelectionModel().removeSelection();
    myActiveFile = file;
    return editor;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) EditorEx(com.intellij.openapi.editor.ex.EditorEx) PsiFile(com.intellij.psi.PsiFile) com.intellij.openapi.fileEditor(com.intellij.openapi.fileEditor) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 32 with LogicalPosition

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

the class NamedElementDuplicateHandler method executeWriteAction.

@Override
public void executeWriteAction(Editor editor, DataContext dataContext) {
    Project project = editor.getProject();
    if (project != null && !editor.getSelectionModel().hasSelection()) {
        PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file != null) {
            VisualPosition caret = editor.getCaretModel().getVisualPosition();
            Pair<LogicalPosition, LogicalPosition> lines = EditorUtil.calcSurroundingRange(editor, caret, caret);
            TextRange toDuplicate = new TextRange(editor.logicalPositionToOffset(lines.first), editor.logicalPositionToOffset(lines.second));
            PsiElement name = findNameIdentifier(editor, file, toDuplicate);
            if (name != null && !name.getTextRange().containsOffset(editor.getCaretModel().getOffset())) {
                editor.getCaretModel().moveToOffset(name.getTextOffset());
            }
        }
    }
    myOriginal.execute(editor, dataContext);
}
Also used : Project(com.intellij.openapi.project.Project) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) VisualPosition(com.intellij.openapi.editor.VisualPosition) PsiElement(com.intellij.psi.PsiElement)

Example 33 with LogicalPosition

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

the class AndroidValueResourcesTest method testNavigationInPlatformXml3.

public void testNavigationInPlatformXml3() throws Exception {
    VirtualFile themes_holo = LocalFileSystem.getInstance().findFileByPath(TestUtils.getPlatformFile("data/res/values/themes_holo.xml").toString());
    assertNotNull(themes_holo);
    VirtualFile colors_holo = LocalFileSystem.getInstance().findFileByPath(TestUtils.getPlatformFile("data/res/values/colors_holo.xml").toString());
    assertNotNull(colors_holo);
    // In themes_holo.xml: point to value of "bright_foreground_holo_light" on line:
    //    <item name="colorForeground">@color/bright_foreground_holo_light</item>
    // Goto action should navigate to "bright_foreground_holo_light" in colors_holo.xml, on line:
    //    <color name="bright_foreground_holo_light">@color/background_holo_dark</color>
    myFixture.configureFromExistingVirtualFile(themes_holo);
    myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(407, 60));
    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("bright_foreground_holo_light", targetAttrValue.getValue());
    assertEquals("name", ((XmlAttribute) targetAttrValue.getParent()).getName());
    assertEquals("color", ((XmlTag) targetAttrValue.getParent().getParent()).getName());
    assertEquals(colors_holo, 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 34 with LogicalPosition

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

the class EditorFixture method select.

/**
   * Given a {@code regex} with one capturing group, selects the subsequence captured in the first match found in the selected text editor.
   *
   * @throws IllegalStateException if there is no currently selected text editor or no match is found
   * @throws IllegalArgumentException if {@code regex} does not have exactly one capturing group
   */
@NotNull
public EditorFixture select(String regex) {
    Matcher matcher = Pattern.compile(regex).matcher(getCurrentFileContents());
    checkArgument(matcher.groupCount() == 1, "must have exactly one capturing group: %s", regex);
    matcher.find();
    int start = matcher.start(1);
    int end = matcher.end(1);
    SelectTarget selectTarget = GuiQuery.getNonNull(() -> {
        Editor editor = FileEditorManager.getInstance(myFrame.getProject()).getSelectedTextEditor();
        checkState(editor != null, "no currently selected text editor");
        LogicalPosition startPosition = editor.offsetToLogicalPosition(start);
        LogicalPosition endPosition = editor.offsetToLogicalPosition(end);
        // CENTER_DOWN tries to make endPosition visible; if that fails, write selectWithKeyboard and rename this method selectWithMouse?
        editor.getScrollingModel().scrollTo(startPosition, ScrollType.CENTER_DOWN);
        SelectTarget target = new SelectTarget();
        target.component = editor.getContentComponent();
        target.startPoint = editor.logicalPositionToXY(startPosition);
        target.endPoint = editor.logicalPositionToXY(endPosition);
        return target;
    });
    robot.pressMouse(selectTarget.component, selectTarget.startPoint);
    robot.moveMouse(selectTarget.component, selectTarget.endPoint);
    robot.releaseMouseButtons();
    return this;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) GenericTypeMatcher(org.fest.swing.core.GenericTypeMatcher) Matcher(java.util.regex.Matcher) JLabelMatcher(org.fest.swing.core.matcher.JLabelMatcher) StringResourceEditor(com.android.tools.idea.editors.strings.StringResourceEditor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) NlEditor(com.android.tools.idea.uibuilder.editor.NlEditor) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull)

Example 35 with LogicalPosition

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

the class ConsoleHighlighter method appendToDocument.

private void appendToDocument() {
    Document document = myEditor.getDocument();
    if (document != null) {
        String pendingString = StringUtil.convertLineSeparators(getPendingString());
        document.insertString(document.getTextLength(), pendingString);
        if (myEditor instanceof Editor) {
            Editor editor = (Editor) myEditor;
            int lineCount = document.getLineCount();
            editor.getScrollingModel().scrollTo(new LogicalPosition(lineCount - 1, 0), ScrollType.MAKE_VISIBLE);
        }
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) Document(com.intellij.openapi.editor.Document) Editor(com.intellij.openapi.editor.Editor)

Aggregations

LogicalPosition (com.intellij.openapi.editor.LogicalPosition)106 Document (com.intellij.openapi.editor.Document)16 PsiElement (com.intellij.psi.PsiElement)14 Editor (com.intellij.openapi.editor.Editor)12 TextRange (com.intellij.openapi.util.TextRange)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 EditorEx (com.intellij.openapi.editor.ex.EditorEx)7 CaretModel (com.intellij.openapi.editor.CaretModel)6 NotNull (org.jetbrains.annotations.NotNull)6 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 EditorWindow (com.intellij.injected.editor.EditorWindow)3