Search in sources :

Example 91 with LogicalPosition

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

the class HighlighterUtil method formatTooltip.

private static Object formatTooltip(Editor e, PsiElement element) {
    if (!(element instanceof XmlTag)) {
        final String text = element.getText();
        if ((text == null || text.length() == 0) && MyPsiUtil.isNameElement(element)) {
            final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, true);
            if (tag != null) {
                return tag.getName();
            }
        }
        return text;
    }
    // have to use html/preformatted or else the tooltip gets formatted totally weird.
    final CodeStyleSettingsManager instance = CodeStyleSettingsManager.getInstance(element.getProject());
    final int tabSize = instance.getCurrentSettings().getTabSize(FileTypeManager.getInstance().getFileTypeByExtension("xml"));
    final char[] spaces = new char[tabSize];
    for (int i = 0; i < spaces.length; i++) {
        spaces[i] = ' ';
    }
    final int textOffset = element.getTextOffset();
    final int lineStartOffset = e.logicalPositionToOffset(new LogicalPosition(e.offsetToLogicalPosition(textOffset).line, 0));
    final CharSequence chars = e.getDocument().getCharsSequence();
    int indent = 0;
    for (int i = lineStartOffset; i < textOffset; i++) {
        if (chars.charAt(i) == ' ') {
            indent++;
        } else if (chars.charAt(i) == '\t') {
            indent += ((indent + tabSize) / tabSize) * tabSize - indent;
        } else {
            break;
        }
    }
    final String text = element.getText().replaceAll("\\t", new String(spaces)).replaceAll("&", "&amp;").replaceAll("<", "&lt;");
    final Pattern indentPattern = Pattern.compile("^(\\s*).+");
    final StringBuilder sb = new StringBuilder("<html><pre>");
    final String[] lines = text.split("\\n");
    for (String line : lines) {
        final Matcher matcher = indentPattern.matcher(line);
        if (matcher.matches()) {
            // strip off the amount of spaces the top-level element is indented with
            line = line.substring(Math.min(matcher.group(1).length(), indent));
        }
        sb.append(line).append("\n");
    }
    return sb.append("</pre></html>").toString();
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) Pattern(java.util.regex.Pattern) CodeStyleSettingsManager(com.intellij.psi.codeStyle.CodeStyleSettingsManager) Matcher(java.util.regex.Matcher) XmlTag(com.intellij.psi.xml.XmlTag)

Example 92 with LogicalPosition

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

the class PyEditingTest method testGreedyBackspace.

public void testGreedyBackspace() {
    // PY-254
    final EditorSettingsExternalizable settings = EditorSettingsExternalizable.getInstance();
    boolean oldVSpaceValue = settings.isVirtualSpace();
    try {
        settings.setVirtualSpace(true);
        doTestBackspace("py254", new LogicalPosition(4, 8));
    } finally {
        settings.setVirtualSpace(oldVSpaceValue);
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) EditorSettingsExternalizable(com.intellij.openapi.editor.ex.EditorSettingsExternalizable)

Example 93 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 94 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 95 with LogicalPosition

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

the class ActionUtil method getCodePointer.

static CodePointer getCodePointer(Editor editor) {
    CodePointer codePointer;
    int selectionStart = editor.getSelectionModel().getSelectionStart();
    int selectionEnd = editor.getSelectionModel().getSelectionEnd();
    if (selectionStart != selectionEnd) {
        LogicalPosition start = editor.offsetToLogicalPosition(selectionStart);
        LogicalPosition end = editor.offsetToLogicalPosition(selectionEnd);
        codePointer = new CodePointer(start.line, start.column, end.line, end.column);
    } else {
        LogicalPosition pos = editor.getCaretModel().getLogicalPosition();
        codePointer = new CodePointer(pos.line, pos.column);
    }
    return codePointer;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) CodePointer(jetbrains.communicator.core.vfs.CodePointer)

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