package fr.adrienbrault.idea.symfony2plugin.templating;

import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Editor;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.lang.PhpLanguage;
import com.jetbrains.php.lang.psi.PhpFile;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.TwigHelper;
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.Nullable;

/**
 * @author Daniel Espendiller <daniel@espendiller.net>
 */
public class PhpTemplateGlobalStringGoToDeclarationHandler implements GotoDeclarationHandler {

    @Nullable
    @Override
    public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Editor editor) {

        if(!Symfony2ProjectComponent.isEnabled(psiElement)) {
            return null;
        }

        if(!(psiElement.getContainingFile() instanceof PhpFile) || !(psiElement.getContext() instanceof StringLiteralExpression)) {
            return new PsiElement[0];
        }

        if(!Symfony2ProjectComponent.isEnabled(psiElement) || !PlatformPatterns.or(
            PlatformPatterns
                .psiElement(StringLiteralExpression.class)
                .withText(PlatformPatterns.or(
                    PlatformPatterns.string().endsWith("twig'"),
                    PlatformPatterns.string().endsWith("twig\"")
                ))
                .withLanguage(PhpLanguage.INSTANCE),
            PlatformPatterns
                .psiElement(StringLiteralExpression.class)
                .withText(PlatformPatterns.or(
                    PlatformPatterns.string().endsWith("twig'"),
                    PlatformPatterns.string().endsWith("twig\"")
                ))
                .withLanguage(PhpLanguage.INSTANCE)
            ).accepts(psiElement.getContext())) {

            return new PsiElement[0];
        }

        String templateName = PsiElementUtils.getText(psiElement);
        if(StringUtils.isBlank(templateName)) {
            return new PsiElement[0];
        }

        return TwigHelper.getTemplatePsiElements(psiElement.getProject(), templateName);
    }

    @Nullable
    @Override
    public String getActionText(DataContext dataContext) {
        return null;
    }

}

@@ -587,73 +587,8 @@
 er;%0A
-import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;%0A
 impo
@@ -951,25 +951,24 @@
 r editor) %7B%0A
-%0A
         if(!
@@ -1013,16 +1013,72 @@
 Element)
+ %7C%7C !(psiElement.getContainingFile() instanceof PhpFile)
 ) %7B%0A    
@@ -1121,14 +1121,9 @@
     
-if(!(p
+P
 siEl
@@ -1131,77 +1131,76 @@
 ment
-.getContainingFile() instanceof PhpFile) %7C%7C !(psiElement.getContext()
+ stringLiteral = psiElement.getContext();%0A        if(!(stringLiteral
  ins
@@ -1299,59 +1299,8 @@
  if(
-!Symfony2ProjectComponent.isEnabled(psiElement) %7C%7C 
 !Pla
@@ -2012,39 +2012,29 @@
 accepts(
-psiElement.getContext()
+stringLiteral
 )) %7B%0A%0A  
@@ -2114,42 +2114,62 @@
 e = 
-PsiElementUtils.getText(psiElem
+((StringLiteralExpression) stringLiteral).getCont
 ent
+s(
 );%0A 
@@ -2256,32 +2256,121 @@
 %5B0%5D;%0A        %7D%0A%0A
+        // file and directory navigation:%0A        // foo.html.twig, foobar/foo.html.twig%0A
         return T
@@ -2394,19 +2394,26 @@
 late
-PsiElements
+NavigationOnOffset
 (psi
@@ -2446,16 +2446,91 @@
 lateName
+, i - psiElement.getTextRange().getStartOffset()).toArray(new PsiElement%5B0%5D
 );%0A    %7D

allow path navigation for Twig templates in all php related navigation handler #1076