|
| 1 | +package com.cedricziel.idea.typo3.codeInsight; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.completion.*; |
| 4 | +import com.intellij.codeInsight.lookup.LookupElement; |
| 5 | +import com.intellij.codeInsight.lookup.LookupElementPresentation; |
| 6 | +import com.intellij.icons.AllIcons; |
| 7 | +import com.intellij.openapi.project.Project; |
| 8 | +import com.intellij.patterns.PlatformPatterns; |
| 9 | +import com.intellij.psi.PsiElement; |
| 10 | +import com.intellij.util.ProcessingContext; |
| 11 | +import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; |
| 12 | +import org.codehaus.plexus.util.DirectoryScanner; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | + |
| 17 | +public class PathResourceCompletionContributor extends CompletionContributor { |
| 18 | + public PathResourceCompletionContributor() { |
| 19 | + extend( |
| 20 | + CompletionType.BASIC, |
| 21 | + PlatformPatterns.psiElement().withParent(PlatformPatterns.psiElement(StringLiteralExpression.class)), |
| 22 | + new CompletionProvider<CompletionParameters>() { |
| 23 | + @Override |
| 24 | + protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) { |
| 25 | + PsiElement position = parameters.getPosition(); |
| 26 | + |
| 27 | + Project project = position.getProject(); |
| 28 | + PsiElement parent = position.getParent(); |
| 29 | + if (!(parent instanceof StringLiteralExpression)) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + String currentText = parent.getText(); |
| 34 | + if (project.getBasePath() == null || !currentText.contains("EXT:")) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + String[] includes; |
| 39 | + if (currentText.contains("/")) { |
| 40 | + String current = currentText |
| 41 | + .split("/", 1)[0] |
| 42 | + .replace("EXT:", "") |
| 43 | + .replace("IntellijIdeaRulezzz", "") |
| 44 | + .replace("'", "") |
| 45 | + .trim(); |
| 46 | + |
| 47 | + includes = new String[]{ |
| 48 | + "**/sysext/" + current + "**", |
| 49 | + "**/typo3conf/ext/" + current + "**", |
| 50 | + }; |
| 51 | + } else { |
| 52 | + includes = new String[]{ |
| 53 | + "**/sysext/*/", |
| 54 | + "**/typo3conf/ext/*/", |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + DirectoryScanner scanner = new DirectoryScanner(); |
| 59 | + |
| 60 | + scanner.setBasedir(project.getBasePath()); |
| 61 | + scanner.addDefaultExcludes(); |
| 62 | + scanner.setIncludes(includes); |
| 63 | + scanner.scan(); |
| 64 | + |
| 65 | + Arrays.stream(scanner.getIncludedFiles()).forEach(f -> { |
| 66 | + result.addElement(new LookupElement() { |
| 67 | + @NotNull |
| 68 | + @Override |
| 69 | + public String getLookupString() { |
| 70 | + String[] splitted = f.split("sysext/"); |
| 71 | + if (splitted.length > 1) { |
| 72 | + return "EXT:" + splitted[1]; |
| 73 | + } |
| 74 | + |
| 75 | + splitted = f.split("typo3conf/ext/"); |
| 76 | + if (splitted.length > 1) { |
| 77 | + return "EXT:" + splitted[1]; |
| 78 | + } |
| 79 | + |
| 80 | + return "EXT:" + f; |
| 81 | + } |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + Arrays.stream(scanner.getIncludedDirectories()).forEach(f -> { |
| 86 | + result.addElement(new LookupElement() { |
| 87 | + |
| 88 | + @Override |
| 89 | + public void renderElement(LookupElementPresentation presentation) { |
| 90 | + presentation.setIcon(AllIcons.Modules.SourceFolder); |
| 91 | + |
| 92 | + super.renderElement(presentation); |
| 93 | + } |
| 94 | + |
| 95 | + @NotNull |
| 96 | + @Override |
| 97 | + public String getLookupString() { |
| 98 | + String[] splitted = f.split("sysext/"); |
| 99 | + if (splitted.length > 1) { |
| 100 | + return "EXT:" + splitted[1]; |
| 101 | + } |
| 102 | + |
| 103 | + splitted = f.split("typo3conf/ext/"); |
| 104 | + if (splitted.length > 1) { |
| 105 | + return "EXT:" + splitted[1]; |
| 106 | + } |
| 107 | + |
| 108 | + return "EXT:" + f; |
| 109 | + } |
| 110 | + }); |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
0 commit comments