Search in sources :

Example 1 with PsiLiteralExpressionImpl

use of com.intellij.psi.impl.source.tree.java.PsiLiteralExpressionImpl in project intellij-community by JetBrains.

the class LiteralExpressionTokenizer method tokenize.

@Override
public void tokenize(@NotNull PsiLiteralExpression element, TokenConsumer consumer) {
    PsiLiteralExpressionImpl literalExpression = (PsiLiteralExpressionImpl) element;
    // not a string literal
    if (literalExpression.getLiteralElementType() != JavaTokenType.STRING_LITERAL)
        return;
    String text = literalExpression.getInnerText();
    if (StringUtil.isEmpty(text) || text.length() <= 2) {
        // optimisation to avoid expensive injection check
        return;
    }
    if (InjectedLanguageUtil.hasInjections(literalExpression))
        return;
    final PsiModifierListOwner listOwner = PsiTreeUtil.getParentOfType(element, PsiModifierListOwner.class);
    if (listOwner != null && AnnotationUtil.isAnnotated(listOwner, Collections.singleton(AnnotationUtil.NON_NLS), false, false)) {
        return;
    }
    if (!text.contains("\\")) {
        consumer.consumeToken(element, PlainTextSplitter.getInstance());
    } else {
        processTextWithEscapeSequences(element, text, consumer);
    }
}
Also used : PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner) PsiLiteralExpressionImpl(com.intellij.psi.impl.source.tree.java.PsiLiteralExpressionImpl)

Example 2 with PsiLiteralExpressionImpl

use of com.intellij.psi.impl.source.tree.java.PsiLiteralExpressionImpl in project intellij-community by JetBrains.

the class StringLiteralManipulator method getValueRange.

@NotNull
public static TextRange getValueRange(@NotNull PsiLiteralExpression element) {
    int length = element.getTextLength();
    boolean isQuoted;
    if (element instanceof PsiLiteralExpressionImpl) {
        // avoid calling getValue(): it allocates new string, it returns null for invalid escapes
        IElementType type = ((PsiLiteralExpressionImpl) element).getLiteralElementType();
        isQuoted = type == JavaTokenType.STRING_LITERAL || type == JavaTokenType.CHARACTER_LITERAL;
    } else {
        final Object value = element.getValue();
        isQuoted = value instanceof String || value instanceof Character;
    }
    return isQuoted ? new TextRange(1, Math.max(1, length - 1)) : TextRange.from(0, length);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) TextRange(com.intellij.openapi.util.TextRange) PsiLiteralExpressionImpl(com.intellij.psi.impl.source.tree.java.PsiLiteralExpressionImpl) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiLiteralExpressionImpl (com.intellij.psi.impl.source.tree.java.PsiLiteralExpressionImpl)2 TextRange (com.intellij.openapi.util.TextRange)1 PsiModifierListOwner (com.intellij.psi.PsiModifierListOwner)1 IElementType (com.intellij.psi.tree.IElementType)1 NotNull (org.jetbrains.annotations.NotNull)1