Skip to content

Commit e16d2de

Browse files
authored
Merge pull request #74 from cedricziel/goto-resource-path
Introduce PathResourceGotoDeclarationHandler
2 parents e4fc1d9 + 0e46531 commit e16d2de

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

src/main/java/com/cedricziel/idea/typo3/codeInsight/PathResourceCompletionContributor.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@ public PathResourceCompletionContributor() {
2323
@Override
2424
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
2525
PsiElement position = parameters.getPosition();
26-
2726
Project project = position.getProject();
2827
PsiElement parent = position.getParent();
29-
if (!(parent instanceof StringLiteralExpression)) {
30-
return;
31-
}
3228

3329
String currentText = parent.getText();
3430
if (project.getBasePath() == null || !currentText.contains("EXT:")) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.cedricziel.idea.typo3.codeInsight.navigation;
2+
3+
import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler;
4+
import com.intellij.openapi.actionSystem.DataContext;
5+
import com.intellij.openapi.editor.Editor;
6+
import com.intellij.psi.PsiElement;
7+
import com.intellij.psi.PsiFile;
8+
import com.intellij.psi.search.FilenameIndex;
9+
import com.intellij.psi.search.GlobalSearchScope;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
import java.util.Arrays;
13+
import java.util.List;
14+
15+
import static com.cedricziel.idea.typo3.util.ResourceUtil.isExtResourcePath;
16+
17+
public class PathResourceGotoDeclarationHandler implements GotoDeclarationHandler {
18+
@Nullable
19+
@Override
20+
public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) {
21+
if (!isExtResourcePath(sourceElement)) {
22+
return new PsiElement[0];
23+
}
24+
25+
if (sourceElement != null) {
26+
String text = sourceElement.getText();
27+
String fileName = text.split("/")[text.split("/").length - 1];
28+
String relativePath = text.replaceFirst("EXT:", "");
29+
30+
List<PsiFile> psiFiles = Arrays.asList(
31+
FilenameIndex.getFilesByName(
32+
sourceElement.getProject(),
33+
fileName,
34+
GlobalSearchScope.allScope(sourceElement.getProject())
35+
));
36+
37+
return psiFiles
38+
.stream()
39+
.filter(x -> x.getVirtualFile().getPath().contains(relativePath))
40+
.toArray(PsiElement[]::new);
41+
}
42+
43+
return new PsiElement[0];
44+
}
45+
46+
@Nullable
47+
@Override
48+
public String getActionText(DataContext context) {
49+
return null;
50+
}
51+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.cedricziel.idea.typo3.util;
2+
3+
import com.intellij.patterns.PlatformPatterns;
4+
import com.intellij.psi.PsiElement;
5+
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
6+
7+
public class ResourceUtil {
8+
public static boolean isExtResourcePath(PsiElement element) {
9+
// must be a string element
10+
if (!PlatformPatterns.psiElement().withParent(PlatformPatterns.psiElement(StringLiteralExpression.class)).accepts(element)) {
11+
return false;
12+
}
13+
14+
String currentText = element.getParent().getText();
15+
16+
return element.getProject().getBasePath() != null && currentText.contains("EXT:");
17+
}
18+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ It is a great inspiration for possible solutions and parts of the code.</p>
297297

298298
<!-- inspections -->
299299
<inspectionToolProvider implementation="com.cedricziel.idea.typo3.codeInspection.TYPO3InspectionToolProvider"/>
300+
301+
<!-- got handlers -->
302+
<gotoDeclarationHandler implementation="com.cedricziel.idea.typo3.codeInsight.navigation.PathResourceGotoDeclarationHandler"/>
300303
</extensions>
301304

302305
<extensions defaultExtensionNs="com.jetbrains.php">

0 commit comments

Comments
 (0)