@Override public void tokenize(@NotNull PsiLiteralExpression element, TokenConsumer consumer) { PsiLiteralExpressionImpl literalExpression = (PsiLiteralExpressionImpl)element; if (literalExpression.getLiteralElementType() != JavaTokenType.STRING_LITERAL) return; // not a string literal 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); } }
@Override public void tokenize(@NotNull PsiLiteralExpression element, TokenConsumer consumer) { PsiLiteralExpressionImpl literalExpression = (PsiLiteralExpressionImpl) element; if (literalExpression.getLiteralElementType() != JavaTokenType.STRING_LITERAL) { return; // not a string literal } final PsiModifierListOwner listOwner = PsiTreeUtil.getParentOfType(element, PsiModifierListOwner.class); if (listOwner != null && AnnotationUtil.isAnnotated(listOwner, Collections.singleton(AnnotationUtil.NON_NLS), false, false)) { return; } String text = literalExpression.getInnerText(); if (text == null) { return; } if (!text.contains("\\")) { consumer.consumeToken(element, PlainTextSplitter.getInstance()); } else { processTextWithEscapeSequences(element, text, consumer); } }
/** * Returns the Camel route from a PsiElement * * @param element the element * @return the String route or null if there nothing can be found */ private String findRouteFromElement(PsiElement element) { XmlTag xml = PsiTreeUtil.getParentOfType(element, XmlTag.class); if (xml != null) { return ((XmlTagImpl) element.getParent()).getAttributeValue("uri"); } if (element instanceof PsiLiteralExpressionImpl) { return ((PsiLiteralExpressionImpl) element).getValue() == null ? null : ((PsiLiteralExpressionImpl) element).getValue().toString(); } if (element instanceof PsiIdentifier) { PsiIdentifier id = (PsiIdentifier) element; String text = id.getText(); if (text != null) { return text; } } // Only variables can be resolved? Optional<PsiVariable> variable = resolvedIdentifier(element) .filter(PsiVariable.class::isInstance) .map(PsiVariable.class::cast); if (variable.isPresent()) { // Try to resolve variable and recursive search route return variable.map(PsiVariable::getInitializer) .map(this::findRouteFromElement) .orElse(null); } return null; }
@Nullable private static HighlightInfo checkUnderscores(PsiElement expression, String text, boolean isInt) { String[] parts = ArrayUtil.EMPTY_STRING_ARRAY; if (isInt) { int start = 0; int end = text.length(); if (text.startsWith(PsiLiteralExpressionImpl.HEX_PREFIX) || text.startsWith(PsiLiteralExpressionImpl.BIN_PREFIX)) start += 2; if (StringUtil.endsWithChar(text, 'l')) --end; parts = new String[]{text.substring(start, end)}; } else { Matcher matcher = FP_LITERAL_PARTS.matcher(text); if (matcher.matches()) { parts = new String[matcher.groupCount()]; for (int i = 0; i < matcher.groupCount(); i++) { parts[i] = matcher.group(i + 1); } } } for (String part : parts) { if (part != null && (StringUtil.startsWithChar(part, '_') || StringUtil.endsWithChar(part, '_'))) { String message = JavaErrorMessages.message("illegal.underscore"); return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create(); } } return null; }
public static void processTextWithEscapeSequences(PsiLiteralExpression element, String text, TokenConsumer consumer) { StringBuilder unescapedText = new StringBuilder(); int[] offsets = new int[text.length()+1]; PsiLiteralExpressionImpl.parseStringCharacters(text, unescapedText, offsets); processTextWithOffsets(element, consumer, unescapedText, offsets, 1); }
@Override public boolean decode(@NotNull final TextRange rangeInsideHost, @NotNull StringBuilder outChars) { ProperTextRange.assertProperRange(rangeInsideHost); String subText = rangeInsideHost.substring(myHost.getText()); outSourceOffsets = new int[subText.length()+1]; return PsiLiteralExpressionImpl.parseStringCharacters(subText, outChars, outSourceOffsets); }
public static void processTextWithEscapeSequences(PsiLiteralExpression element, String text, TokenConsumer consumer) { StringBuilder unescapedText = new StringBuilder(); int[] offsets = new int[text.length()+1]; PsiLiteralExpressionImpl.parseStringCharacters(text, unescapedText, offsets); EscapeSequenceTokenizer.processTextWithOffsets(element, consumer, unescapedText, offsets, 1); }
@Override public boolean decode(@NotNull final TextRange rangeInsideHost, @NotNull StringBuilder outChars) { String subText = rangeInsideHost.substring(myHost.getText()); outSourceOffsets = new int[subText.length()+1]; return PsiLiteralExpressionImpl.parseStringCharacters(subText, outChars, outSourceOffsets); }
@Override public PsiLiteralExpression createPsi(@NotNull ASTNode node) { return new PsiLiteralExpressionImpl(node); }
@Override public PsiLiteralExpression createPsi(@NotNull PsiLiteralStub stub) { return new PsiLiteralExpressionImpl(stub); }
@Nullable private static HighlightInfo checkUnderscores(PsiElement expression, String text, boolean isInt) { String[] parts = ArrayUtil.EMPTY_STRING_ARRAY; if(isInt) { int start = 0; if(text.startsWith(PsiLiteralExpressionImpl.HEX_PREFIX) || text.startsWith(PsiLiteralExpressionImpl.BIN_PREFIX)) { start += 2; } int end = text.length(); if(StringUtil.endsWithChar(text, 'l')) { --end; } parts = new String[]{text.substring(start, end)}; } else { Matcher matcher = FP_LITERAL_PARTS.matcher(text); if(matcher.matches()) { parts = new String[matcher.groupCount()]; for(int i = 0; i < matcher.groupCount(); i++) { parts[i] = matcher.group(i + 1); } } } for(String part : parts) { if(part != null && (StringUtil.startsWithChar(part, '_') || StringUtil.endsWithChar(part, '_'))) { String message = JavaErrorMessages.message("illegal.underscore"); return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create(); } } return null; }