Search in sources :

Example 56 with TextRange

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

the class PyStringLiteralExpressionImpl method getDecodedFragments.

@Override
@NotNull
public List<Pair<TextRange, String>> getDecodedFragments() {
    if (myDecodedFragments == null) {
        final List<Pair<TextRange, String>> result = new ArrayList<>();
        final int elementStart = getTextRange().getStartOffset();
        final boolean unicodeByDefault = isUnicodeByDefault();
        for (ASTNode node : getStringNodes()) {
            final String text = node.getText();
            final TextRange textRange = getNodeTextRange(text);
            final int offset = node.getTextRange().getStartOffset() - elementStart + textRange.getStartOffset();
            final String encoded = textRange.substring(text);
            final boolean hasRawPrefix = PyStringLiteralUtil.isRawPrefix(PyStringLiteralUtil.getPrefix(text));
            final boolean hasUnicodePrefix = PyStringLiteralUtil.isUnicodePrefix(PyStringLiteralUtil.getPrefix(text));
            result.addAll(getDecodedFragments(encoded, offset, hasRawPrefix, unicodeByDefault || hasUnicodePrefix));
        }
        myDecodedFragments = result;
    }
    return myDecodedFragments;
}
Also used : ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 57 with TextRange

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

the class PyStringLiteralExpressionImpl method getStringValueTextRanges.

@NotNull
public List<TextRange> getStringValueTextRanges() {
    if (valueTextRanges == null) {
        int elStart = getTextRange().getStartOffset();
        List<TextRange> ranges = new ArrayList<>();
        for (ASTNode node : getStringNodes()) {
            TextRange range = getNodeTextRange(node.getText());
            int nodeOffset = node.getStartOffset() - elStart;
            ranges.add(TextRange.from(nodeOffset + range.getStartOffset(), range.getLength()));
        }
        valueTextRanges = Collections.unmodifiableList(ranges);
    }
    return valueTextRanges;
}
Also used : ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull)

Example 58 with TextRange

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

the class PyStringLiteralTest method testEscaperOffsetInEscapedBackslash.

public void testEscaperOffsetInEscapedBackslash() {
    final PyStringLiteralExpression expr = createLiteralFromText("'XXX foo.\\\\bar YYY'");
    assertNotNull(expr);
    final LiteralTextEscaper<? extends PsiLanguageInjectionHost> escaper = expr.createLiteralTextEscaper();
    final TextRange range = TextRange.create(5, 14);
    assertEquals(5, escaper.getOffsetInHost(0, range));
    assertEquals(6, escaper.getOffsetInHost(1, range));
    assertEquals(7, escaper.getOffsetInHost(2, range));
    assertEquals(8, escaper.getOffsetInHost(3, range));
    assertEquals(9, escaper.getOffsetInHost(4, range));
    assertEquals(11, escaper.getOffsetInHost(5, range));
    assertEquals(12, escaper.getOffsetInHost(6, range));
    assertEquals(13, escaper.getOffsetInHost(7, range));
    assertEquals(14, escaper.getOffsetInHost(8, range));
    assertEquals(-1, escaper.getOffsetInHost(9, range));
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) TextRange(com.intellij.openapi.util.TextRange)

Example 59 with TextRange

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

the class PyStringLiteralTest method testEscaperOffsetInHostSubString.

public void testEscaperOffsetInHostSubString() {
    final PyStringLiteralExpression expr = createLiteralFromText("'\\nfoo'");
    assertNotNull(expr);
    final LiteralTextEscaper<? extends PsiLanguageInjectionHost> escaper = expr.createLiteralTextEscaper();
    final TextRange fooOnly = TextRange.create(3, 6);
    assertEquals(3, escaper.getOffsetInHost(0, fooOnly));
    assertEquals(4, escaper.getOffsetInHost(1, fooOnly));
    assertEquals(5, escaper.getOffsetInHost(2, fooOnly));
    assertEquals(6, escaper.getOffsetInHost(3, fooOnly));
    assertEquals(-1, escaper.getOffsetInHost(4, fooOnly));
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) TextRange(com.intellij.openapi.util.TextRange)

Example 60 with TextRange

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

the class PyStringLiteralTest method testEscaperOffsetInSingleEscapedCharString.

public void testEscaperOffsetInSingleEscapedCharString() {
    final PyStringLiteralExpression expr = createLiteralFromText("'\\n'");
    assertNotNull(expr);
    final LiteralTextEscaper<? extends PsiLanguageInjectionHost> escaper = expr.createLiteralTextEscaper();
    final TextRange range = TextRange.create(1, 3);
    assertEquals(1, escaper.getOffsetInHost(0, range));
    assertEquals(3, escaper.getOffsetInHost(1, range));
    assertEquals(-1, escaper.getOffsetInHost(2, range));
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

TextRange (com.intellij.openapi.util.TextRange)1314 PsiElement (com.intellij.psi.PsiElement)261 NotNull (org.jetbrains.annotations.NotNull)237 Nullable (org.jetbrains.annotations.Nullable)167 Document (com.intellij.openapi.editor.Document)132 ArrayList (java.util.ArrayList)117 Project (com.intellij.openapi.project.Project)96 PsiFile (com.intellij.psi.PsiFile)96 ASTNode (com.intellij.lang.ASTNode)95 Editor (com.intellij.openapi.editor.Editor)75 PsiReference (com.intellij.psi.PsiReference)54 VirtualFile (com.intellij.openapi.vfs.VirtualFile)51 IElementType (com.intellij.psi.tree.IElementType)48 IncorrectOperationException (com.intellij.util.IncorrectOperationException)48 Pair (com.intellij.openapi.util.Pair)47 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)33 ProperTextRange (com.intellij.openapi.util.ProperTextRange)32 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)31 XmlTag (com.intellij.psi.xml.XmlTag)31 List (java.util.List)31