// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.spellchecker;

import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import com.intellij.psi.impl.source.tree.java.PsiLiteralExpressionImpl;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.spellchecker.inspections.PlainTextSplitter;
import com.intellij.spellchecker.tokenizer.EscapeSequenceTokenizer;
import com.intellij.spellchecker.tokenizer.TokenConsumer;
import com.siyeh.ig.psiutils.ExpressionUtils;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

/**
 * @author shkate@jetbrains.com
 */
public class LiteralExpressionTokenizer extends EscapeSequenceTokenizer<PsiLiteralExpression> {
  @Override
  public void tokenize(@NotNull PsiLiteralExpression element, TokenConsumer consumer) {
    PsiLiteralExpressionImpl literalExpression = (PsiLiteralExpressionImpl)element;
    IElementType literalElementType = literalExpression.getLiteralElementType();
    String text;
    if (literalElementType == JavaTokenType.STRING_LITERAL) {
      text = literalExpression.getInnerText();
    }
    else if (literalElementType == JavaTokenType.TEXT_BLOCK_LITERAL) {
      text = literalExpression.getText();
    }
    else {
      text = null;
    }
    
    if (StringUtil.isEmpty(text) || text.length() <= 2) { // optimization 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, AnnotationUtil.NON_NLS, AnnotationUtil.CHECK_EXTERNAL)) {
      PsiElement targetElement = getCompleteStringValueExpression(element);
      if (listOwner instanceof PsiMethod) {
        if (Arrays.stream(PsiUtil.findReturnStatements(((PsiMethod)listOwner))).map(s -> s.getReturnValue()).anyMatch(e -> e == targetElement)) {
          return;
        }
      }
      else if (listOwner instanceof PsiVariable && ((PsiVariable)listOwner).getInitializer() == targetElement) {
        return;
      }
    }

    if (!text.contains("\\")) {
      consumer.consumeToken(element, PlainTextSplitter.getInstance());
    }
    else {
      processTextWithEscapeSequences(element, text, consumer);
    }
  }

  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);
  }

  public static PsiElement getCompleteStringValueExpression(PsiExpression expression) {
    return ExpressionUtils.isStringConcatenationOperand(expression) ? expression.getParent() : expression;
  }
}
@@ -17,10 +17,10 @@
 0-20
-19
+20
  Jet
@@ -242,63 +242,39 @@
 lij.
-openapi.util.text.StringUtil;%0Aimport com.intellij.psi.*
+codeInsight.CodeInsightUtilCore
 ;%0Aim
@@ -295,37 +295,22 @@
 lij.
-psi.impl.source.tree
+lang
 .inject
-ed
+ion
 .Inj
@@ -322,20 +322,23 @@
 Language
-Util
+Manager
 ;%0Aimport
@@ -355,58 +355,63 @@
 lij.
-psi.impl.source.tree.java.PsiLiteralExpressionImpl
+openapi.util.text.StringUtil;%0Aimport com.intellij.psi.*
 ;%0Aim
@@ -436,25 +436,27 @@
 psi.
-tree.IElementType
+util.PsiLiteralUtil
 ;%0Aim
@@ -1040,22 +1040,25 @@
 ession e
-lement
+xpression
 , TokenC
@@ -1085,368 +1085,137 @@
     
-PsiLiteralExpressionImpl literalExpression = (PsiLiteralExpressionImpl)element;%0A    IElementType literalElementType = literalExpression.getLiteralElementType();%0A    String text;%0A    if (literalElementType == JavaTokenType.STRING_LITERAL) %7B%0A      text = literalExpression.getInnerText();%0A    %7D%0A    else if (literalElementType == JavaTokenType.TEXT_BLOCK_LITERAL
+String text;%0A    if (!ExpressionUtils.hasStringType(expression)) %7B%0A      text = null;%0A    %7D%0A    else if (expression.isTextBlock()
 ) %7B%0A
@@ -1227,24 +1227,17 @@
  text = 
-literalE
+e
 xpressio
@@ -1271,36 +1271,82 @@
  %7B%0A      text = 
-null
+PsiLiteralUtil.getStringLiteralContent(expression)
 ;%0A    %7D%0A    %0A   
@@ -1467,24 +1467,25 @@
 turn;%0A    %7D%0A
+%0A
     if (Inje
@@ -1500,35 +1500,74 @@
 uage
-Util.hasInjections(literalE
+Manager.getInstance(expression.getProject()).getInjectedPsiFiles(e
 xpre
@@ -1572,16 +1572,24 @@
 ression)
+ != null
 ) return
@@ -1663,22 +1663,25 @@
 OfType(e
-lement
+xpression
 , PsiMod
@@ -1895,22 +1895,25 @@
 ession(e
-lement
+xpression
 );%0A     
@@ -2338,22 +2338,25 @@
 eToken(e
-lement
+xpression
 , PlainT
@@ -2438,22 +2438,25 @@
 uences(e
-lement
+xpression
 , text, 
@@ -2709,32 +2709,27 @@
     
-PsiLiteralExpressionImpl
+CodeInsightUtilCore
 .par

use PsiLiteralExpression.isTextBlock() and clean up (IDEA-CR-60894)