// Path: src/main/java/com/kalessil/phpStorm/yii2inspections/inspectors/utils/StringLiteralExtractUtil.java
// final public class StringLiteralExtractUtil {
//     @Nullable
//     private static PsiElement getExpressionTroughParenthesis(@Nullable PsiElement expression) {
//         if (!(expression instanceof ParenthesizedExpression)) {
//             return expression;
//         }
// 
//         PsiElement innerExpression = ((ParenthesizedExpression) expression).getArgument();
//         while (innerExpression instanceof ParenthesizedExpression) {
//             innerExpression = ((ParenthesizedExpression) innerExpression).getArgument();
//         }
// 
//         return innerExpression;
//     }
// 
//     @Nullable
//     private static Function getScope(@NotNull PsiElement expression) {
//         PsiElement parent = expression.getParent();
//         while (null != parent && !(parent instanceof PhpFile)) {
//             if (parent instanceof Function) {
//                 return (Function) parent;
//             }
// 
//             parent = parent.getParent();
//         }
// 
//         return null;
//     }
// 
//     @Nullable
//     public static StringLiteralExpression resolveAsStringLiteral(@Nullable PsiElement expression, boolean resolve) {
//         if (null == expression) {
//             return null;
//         }
//         expression = getExpressionTroughParenthesis(expression);
// 
//         if (expression instanceof StringLiteralExpression) {
//             return (StringLiteralExpression) expression;
//         }
// 
//         if (expression instanceof FieldReference || expression instanceof ClassConstantReference) {
//             final Field fieldOrConstant = resolve ? (Field) ((MemberReference) expression).resolve() : null;
//             if (null != fieldOrConstant && fieldOrConstant.getDefaultValue() instanceof StringLiteralExpression) {
//                 return (StringLiteralExpression) fieldOrConstant.getDefaultValue();
//             }
//         }
// 
//         if (expression instanceof Variable) {
//             final String variable = ((Variable) expression).getName();
//             if (!StringUtil.isEmpty(variable)) {
//                 final Function scope = getScope(expression);
//                 if (null != scope) {
//                     final Set<AssignmentExpression> matched = new HashSet<>();
// 
//                     Collection<AssignmentExpression> assignments
//                             = PsiTreeUtil.findChildrenOfType(scope, AssignmentExpression.class);
//                     /* collect self-assignments as well */
//                     for (AssignmentExpression assignment : assignments) {
//                         if (assignment.getVariable() instanceof Variable && assignment.getValue() instanceof StringLiteralExpression) {
//                             final String name = assignment.getVariable().getName();
//                             if (!StringUtil.isEmpty(name) && name.equals(variable)) {
//                                 matched.add(assignment);
//                             }
//                         }
//                     }
//                     assignments.clear();
// 
//                     if (matched.size() == 1) {
//                         StringLiteralExpression result = (StringLiteralExpression) matched.iterator().next().getValue();
// 
//                         matched.clear();
//                         return result;
//                     }
//                     matched.clear();
//                 }
//             }
//         }
// 
//         return null;
//     }
// }


import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.ProcessingContext;
import com.intellij.util.indexing.FileBasedIndex;
import com.jetbrains.php.lang.PhpLanguage;
import com.jetbrains.php.lang.parser.PhpElementTypes;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.ParameterList;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import com.kalessil.phpStorm.yii2inspections.inspectors.utils.StringLiteralExtractUtil;
import org.jetbrains.annotations.NotNull;
import java.util.*;
package com.kalessil.phpStorm.yii2inspections.codeInsight;



public class TranslationAutocompleteContributor extends CompletionContributor {
    public TranslationAutocompleteContributor() {
        final CompletionProvider<CompletionParameters> provider = new CompletionProvider<CompletionParameters>() {
            @Override
            protected void addCompletions(
                    @NotNull CompletionParameters completionParameters,
                    ProcessingContext processingContext,
                    @NotNull CompletionResultSet completionResultSet
            ) {
                /* validate the autocompletion target */
                final PsiElement target = completionParameters.getOriginalPosition();
                if (target == null || !(target.getParent() instanceof StringLiteralExpression)) {
                    return;
                }

                /* suggest only to target code structure */
                final StringLiteralExpression parameter = (StringLiteralExpression) target.getParent();
                final PsiElement context                = parameter.getParent().getParent();
                if (!(context instanceof MethodReference)) {
                    return;
                }

                final MethodReference reference         = (MethodReference) context;
                final String name                       = reference.getName();
                final PsiElement[] arguments            = reference.getParameters();
                if (name == null || arguments.length == 0 || (!name.equals("t") && !name.equals("registerTranslations"))) {
                    return;
                }

                /* generate proposals */
                final boolean autocompleteCategory = arguments[0] == parameter;
                final boolean autocompleteMessage  = arguments.length > 1 && arguments[1] == parameter;
                if (autocompleteCategory || autocompleteMessage) {
                    StringLiteralExpression categoryLiteral = null;
                    if (autocompleteMessage) {
// Path: src/main/java/com/kalessil/phpStorm/yii2inspections/inspectors/utils/StringLiteralExtractUtil.java
// final public class StringLiteralExtractUtil {
//     @Nullable
//     private static PsiElement getExpressionTroughParenthesis(@Nullable PsiElement expression) {
//         if (!(expression instanceof ParenthesizedExpression)) {
//             return expression;
//         }
// 
//         PsiElement innerExpression = ((ParenthesizedExpression) expression).getArgument();
//         while (innerExpression instanceof ParenthesizedExpression) {
//             innerExpression = ((ParenthesizedExpression) innerExpression).getArgument();
//         }
// 
//         return innerExpression;
//     }
// 
//     @Nullable
//     private static Function getScope(@NotNull PsiElement expression) {
//         PsiElement parent = expression.getParent();
//         while (null != parent && !(parent instanceof PhpFile)) {
//             if (parent instanceof Function) {
//                 return (Function) parent;
//             }
// 
//             parent = parent.getParent();
//         }
// 
//         return null;
//     }
// 
//     @Nullable
//     public static StringLiteralExpression resolveAsStringLiteral(@Nullable PsiElement expression, boolean resolve) {
//         if (null == expression) {
//             return null;
//         }
//         expression = getExpressionTroughParenthesis(expression);
// 
//         if (expression instanceof StringLiteralExpression) {
//             return (StringLiteralExpression) expression;
//         }
// 
//         if (expression instanceof FieldReference || expression instanceof ClassConstantReference) {
//             final Field fieldOrConstant = resolve ? (Field) ((MemberReference) expression).resolve() : null;
//             if (null != fieldOrConstant && fieldOrConstant.getDefaultValue() instanceof StringLiteralExpression) {
//                 return (StringLiteralExpression) fieldOrConstant.getDefaultValue();
//             }
//         }
// 
//         if (expression instanceof Variable) {
//             final String variable = ((Variable) expression).getName();
//             if (!StringUtil.isEmpty(variable)) {
//                 final Function scope = getScope(expression);
//                 if (null != scope) {
//                     final Set<AssignmentExpression> matched = new HashSet<>();
// 
//                     Collection<AssignmentExpression> assignments
//                             = PsiTreeUtil.findChildrenOfType(scope, AssignmentExpression.class);
//                     /* collect self-assignments as well */
//                     for (AssignmentExpression assignment : assignments) {
//                         if (assignment.getVariable() instanceof Variable && assignment.getValue() instanceof StringLiteralExpression) {
//                             final String name = assignment.getVariable().getName();
//                             if (!StringUtil.isEmpty(name) && name.equals(variable)) {
//                                 matched.add(assignment);
//                             }
//                         }
//                     }
//                     assignments.clear();
// 
//                     if (matched.size() == 1) {
//                         StringLiteralExpression result = (StringLiteralExpression) matched.iterator().next().getValue();
// 
//                         matched.clear();
//                         return result;
//                     }
//                     matched.clear();
//                 }
//             }
//         }
// 
//         return null;
//     }
// }


// Path: src/main/java/com/kalessil/phpStorm/yii2inspections/codeInsight/TranslationAutocompleteContributor.java
import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.ProcessingContext;
import com.intellij.util.indexing.FileBasedIndex;
import com.jetbrains.php.lang.PhpLanguage;
import com.jetbrains.php.lang.parser.PhpElementTypes;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.ParameterList;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import com.kalessil.phpStorm.yii2inspections.inspectors.utils.StringLiteralExtractUtil;
import org.jetbrains.annotations.NotNull;
import java.util.*;

package com.kalessil.phpStorm.yii2inspections.codeInsight;



public class TranslationAutocompleteContributor extends CompletionContributor {
    public TranslationAutocompleteContributor() {
        final CompletionProvider<CompletionParameters> provider = new CompletionProvider<CompletionParameters>() {
            @Override
            protected void addCompletions(
                    @NotNull CompletionParameters completionParameters,
                    ProcessingContext processingContext,
                    @NotNull CompletionResultSet completionResultSet
            ) {
                /* validate the autocompletion target */
                final PsiElement target = completionParameters.getOriginalPosition();
                if (target == null || !(target.getParent() instanceof StringLiteralExpression)) {
                    return;
                }

                /* suggest only to target code structure */
                final StringLiteralExpression parameter = (StringLiteralExpression) target.getParent();
                final PsiElement context                = parameter.getParent().getParent();
                if (!(context instanceof MethodReference)) {
                    return;
                }

                final MethodReference reference         = (MethodReference) context;
                final String name                       = reference.getName();
                final PsiElement[] arguments            = reference.getParameters();
                if (name == null || arguments.length == 0 || (!name.equals("t") && !name.equals("registerTranslations"))) {
                    return;
                }

                /* generate proposals */
                final boolean autocompleteCategory = arguments[0] == parameter;
                final boolean autocompleteMessage  = arguments.length > 1 && arguments[1] == parameter;
                if (autocompleteCategory || autocompleteMessage) {
                    StringLiteralExpression categoryLiteral = null;
                    if (autocompleteMessage) {
                        categoryLiteral = StringLiteralExtractUtil.resolveAsStringLiteral(arguments[0], true);