Search in sources :

Example 21 with PyStringLiteralExpression

use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.

the class PyBlockEvaluatorTest method testDictNoEvaluate.

public void testDictNoEvaluate() {
    PyBlockEvaluator eval = doEvaluate("a={'b': 'c'}", true);
    Map map = (Map) eval.getValue("a");
    assertEquals(1, map.size());
    assertTrue(map.get("b") instanceof PyStringLiteralExpression);
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PyBlockEvaluator(com.jetbrains.python.psi.impl.blockEvaluator.PyBlockEvaluator) Map(java.util.Map)

Example 22 with PyStringLiteralExpression

use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.

the class PySectionBasedDocStringTest method findFirstDocString.

@Nullable
private String findFirstDocString() {
    final PsiElementProcessor.FindElement<PsiElement> processor = new PsiElementProcessor.FindElement<PsiElement>() {

        @Override
        public boolean execute(@NotNull PsiElement element) {
            if (element instanceof PyStringLiteralExpression && element.getFirstChild().getNode().getElementType() == PyTokenTypes.DOCSTRING) {
                return setFound(element);
            }
            return true;
        }
    };
    PsiTreeUtil.processElements(myFixture.getFile(), processor);
    if (!processor.isFound()) {
        return null;
    }
    final PsiElement foundElement = processor.getFoundElement();
    assertNotNull(foundElement);
    return ((PyStringLiteralExpression) foundElement).getStringValue();
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with PyStringLiteralExpression

use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.

the class PyStringLiteralTest method getCharacterRanges.

private List<String> getCharacterRanges(String text) {
    final PyStringLiteralExpression expr = createLiteralFromText(text);
    assertNotNull(expr);
    final List<String> characters = new ArrayList<>();
    for (Pair<TextRange, String> fragment : expr.getDecodedFragments()) {
        characters.add(fragment.getSecond());
    }
    return characters;
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange)

Example 24 with PyStringLiteralExpression

use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.

the class PyStringLiteralTest method testEscaperOffsetInHost.

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

Example 25 with PyStringLiteralExpression

use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.

the class PyStringLiteralTest method testEscaperOffsetInSingleCharString.

public void testEscaperOffsetInSingleCharString() {
    final PyStringLiteralExpression expr = createLiteralFromText("'c'");
    assertNotNull(expr);
    final LiteralTextEscaper<? extends PsiLanguageInjectionHost> escaper = expr.createLiteralTextEscaper();
    final TextRange range = TextRange.create(1, 2);
    assertEquals(1, escaper.getOffsetInHost(0, range));
    assertEquals(2, 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

PyStringLiteralExpression (com.jetbrains.python.psi.PyStringLiteralExpression)28 TextRange (com.intellij.openapi.util.TextRange)10 PsiElement (com.intellij.psi.PsiElement)6 NotNull (org.jetbrains.annotations.NotNull)5 ASTNode (com.intellij.lang.ASTNode)4 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)4 ArrayList (java.util.ArrayList)3 Nullable (org.jetbrains.annotations.Nullable)3 PsiFile (com.intellij.psi.PsiFile)2 PsiReference (com.intellij.psi.PsiReference)2 UsageInfo (com.intellij.usageView.UsageInfo)2 PyFile (com.jetbrains.python.psi.PyFile)2 PyBlockEvaluator (com.jetbrains.python.psi.impl.blockEvaluator.PyBlockEvaluator)2 Map (java.util.Map)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1