Skip to content

Commit 4fc1685

Browse files
authored
Merge pull request #1345 from Haehnchen/feature/better-dotenv-support
support more dotenv files
2 parents 3d51718 + 59f1294 commit 4fc1685

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/config/xml/XmlCompletionContributor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ protected void addCompletions(@NotNull CompletionParameters completionParameters
9999
private static class ArgumentParameterCompletionProvider extends CompletionProvider<CompletionParameters> {
100100

101101
@Override
102-
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
103-
102+
protected void addCompletions(@NotNull CompletionParameters completionParameters, @NotNull ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
104103
PsiElement psiElement = completionParameters.getOriginalPosition();
105104
if(psiElement == null || !Symfony2ProjectComponent.isEnabled(psiElement)) {
106105
return;
@@ -116,6 +115,7 @@ protected void addCompletions(@NotNull CompletionParameters completionParameters
116115
// %env('foobar')%
117116
for (String s : DotEnvUtil.getEnvironmentVariables(project)) {
118117
completionResultSet.addElement(new ParameterLookupPercentElement(new ContainerParameter("env(" + s +")", false)));
118+
completionResultSet.addElement(new ParameterLookupPercentElement(new ContainerParameter("env(resolve:" + s +")", false)));
119119
}
120120
}
121121
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/config/yaml/YamlCompletionContributor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public YamlCompletionContributor() {
132132
CompletionType.BASIC, YamlElementPatternHelper.getServiceParameterDefinition(),
133133
new CompletionProvider<CompletionParameters>() {
134134
public void addCompletions(@NotNull CompletionParameters parameters,
135-
ProcessingContext context,
135+
@NotNull ProcessingContext context,
136136
@NotNull CompletionResultSet resultSet) {
137137

138138
if(!Symfony2ProjectComponent.isEnabled(parameters.getPosition())) {
@@ -151,6 +151,7 @@ public void addCompletions(@NotNull CompletionParameters parameters,
151151

152152
for (String s : DotEnvUtil.getEnvironmentVariables(element.getProject())) {
153153
resultSet.addElement(new ParameterLookupElement(new ContainerParameter("env(" + s +")", false), ParameterPercentWrapInsertHandler.getInstance(), element.getText()));
154+
resultSet.addElement(new ParameterLookupElement(new ContainerParameter("env(resolve:" + s +")", false), ParameterPercentWrapInsertHandler.getInstance(), element.getText()));
154155
}
155156
}
156157
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/dic/container/util/DotEnvUtil.java

+29-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.intellij.openapi.project.Project;
44
import com.intellij.openapi.util.Key;
55
import com.intellij.openapi.util.Pair;
6+
import com.intellij.openapi.vfs.VfsUtil;
67
import com.intellij.openapi.vfs.VirtualFile;
78
import com.intellij.psi.PsiElement;
89
import com.intellij.psi.PsiFile;
@@ -53,8 +54,8 @@ public static Collection<PsiElement> getEnvironmentVariableTargetsForParameter(@
5354

5455
// https://github.com/symfony/symfony/pull/23901 => RegisterEnvVarProcessorsPass
5556
// '%env(int:DATABASE_PORT)%'
56-
// '%env(resolve:DB)%'
57-
Matcher matcher = Pattern.compile("^[\\w]+:(.*)$", Pattern.MULTILINE).matcher(parameterName);
57+
// '%env(resolve:int:foo:DB)%'
58+
Matcher matcher = Pattern.compile("^[\\w-_^:]+:(.*)$", Pattern.MULTILINE).matcher(parameterName);
5859
if(matcher.find()){
5960
parameterName = matcher.group(1);
6061
}
@@ -96,23 +97,42 @@ public static Collection<PsiElement> getEnvironmentVariableTargets(@NotNull Proj
9697
}
9798

9899
private static void visitEnvironment(@NotNull Project project, @NotNull Consumer<Pair<String, PsiElement>> consumer) {
99-
for (VirtualFile virtualFile : FilenameIndex.getAllFilesByExt(project, "env", GlobalSearchScope.allScope(project))) {
100-
Properties variables = new Properties();
101-
try {
102-
variables.load(virtualFile.getInputStream());
103-
} catch (IOException e) {
104-
continue;
100+
Set<VirtualFile> files = new HashSet<>(
101+
FilenameIndex.getAllFilesByExt(project, "env", GlobalSearchScope.allScope(project))
102+
);
103+
104+
// try to find some env's ;)
105+
for (String file : new String[]{".env", ".env.dist", ".env.test", ".env.local"}) {
106+
files.addAll(FilenameIndex.getVirtualFilesByName(project, file, GlobalSearchScope.allScope(project)));
107+
}
108+
109+
// search root directory for all ".env*" files
110+
VirtualFile projectDir = VfsUtil.findRelativeFile(project.getBaseDir());
111+
if (projectDir != null) {
112+
for (VirtualFile child : projectDir.getChildren()) {
113+
if (child.getName().startsWith(".env")) {
114+
files.add(child);
115+
}
105116
}
117+
}
106118

119+
for (VirtualFile virtualFile : files) {
107120
PsiFile file = PsiManager.getInstance(project).findFile(virtualFile);
108121
if(file == null) {
109122
continue;
110123
}
111124

125+
Properties variables = new Properties();
126+
try {
127+
variables.load(virtualFile.getInputStream());
128+
} catch (IOException e) {
129+
continue;
130+
}
131+
112132
for (Map.Entry<Object, Object> variable : variables.entrySet()) {
113133
Object key = variable.getKey();
114134
if(key instanceof String) {
115-
consumer.accept(Pair.create(key.toString(), file));
135+
consumer.accept(Pair.create((String) key, file));
116136
}
117137
}
118138
}

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/dic/container/util/DotEnvUtilTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,11 @@ public void testGetEnvironmentVariableTargetsForParameter() {
4444
.filter(psiElement -> psiElement instanceof PsiFile && "env.env".equals(((PsiFile) psiElement).getName()))
4545
.count()
4646
);
47+
48+
assertEquals(1, DotEnvUtil.getEnvironmentVariableTargetsForParameter(getProject(), "%env(int:json:foo_foo:foo-foo:foobar)%")
49+
.stream()
50+
.filter(psiElement -> psiElement instanceof PsiFile && "env.env".equals(((PsiFile) psiElement).getName()))
51+
.count()
52+
);
4753
}
4854
}

0 commit comments

Comments
 (0)