Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support more dotenv files #1345

Merged
merged 1 commit into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ protected void addCompletions(@NotNull CompletionParameters completionParameters
private static class ArgumentParameterCompletionProvider extends CompletionProvider<CompletionParameters> {

@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {

protected void addCompletions(@NotNull CompletionParameters completionParameters, @NotNull ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
PsiElement psiElement = completionParameters.getOriginalPosition();
if(psiElement == null || !Symfony2ProjectComponent.isEnabled(psiElement)) {
return;
Expand All @@ -116,6 +115,7 @@ protected void addCompletions(@NotNull CompletionParameters completionParameters
// %env('foobar')%
for (String s : DotEnvUtil.getEnvironmentVariables(project)) {
completionResultSet.addElement(new ParameterLookupPercentElement(new ContainerParameter("env(" + s +")", false)));
completionResultSet.addElement(new ParameterLookupPercentElement(new ContainerParameter("env(resolve:" + s +")", false)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public YamlCompletionContributor() {
CompletionType.BASIC, YamlElementPatternHelper.getServiceParameterDefinition(),
new CompletionProvider<CompletionParameters>() {
public void addCompletions(@NotNull CompletionParameters parameters,
ProcessingContext context,
@NotNull ProcessingContext context,
@NotNull CompletionResultSet resultSet) {

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

for (String s : DotEnvUtil.getEnvironmentVariables(element.getProject())) {
resultSet.addElement(new ParameterLookupElement(new ContainerParameter("env(" + s +")", false), ParameterPercentWrapInsertHandler.getInstance(), element.getText()));
resultSet.addElement(new ParameterLookupElement(new ContainerParameter("env(resolve:" + s +")", false), ParameterPercentWrapInsertHandler.getInstance(), element.getText()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
Expand Down Expand Up @@ -53,8 +54,8 @@ public static Collection<PsiElement> getEnvironmentVariableTargetsForParameter(@

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

private static void visitEnvironment(@NotNull Project project, @NotNull Consumer<Pair<String, PsiElement>> consumer) {
for (VirtualFile virtualFile : FilenameIndex.getAllFilesByExt(project, "env", GlobalSearchScope.allScope(project))) {
Properties variables = new Properties();
try {
variables.load(virtualFile.getInputStream());
} catch (IOException e) {
continue;
Set<VirtualFile> files = new HashSet<>(
FilenameIndex.getAllFilesByExt(project, "env", GlobalSearchScope.allScope(project))
);

// try to find some env's ;)
for (String file : new String[]{".env", ".env.dist", ".env.test", ".env.local"}) {
files.addAll(FilenameIndex.getVirtualFilesByName(project, file, GlobalSearchScope.allScope(project)));
}

// search root directory for all ".env*" files
VirtualFile projectDir = VfsUtil.findRelativeFile(project.getBaseDir());
if (projectDir != null) {
for (VirtualFile child : projectDir.getChildren()) {
if (child.getName().startsWith(".env")) {
files.add(child);
}
}
}

for (VirtualFile virtualFile : files) {
PsiFile file = PsiManager.getInstance(project).findFile(virtualFile);
if(file == null) {
continue;
}

Properties variables = new Properties();
try {
variables.load(virtualFile.getInputStream());
} catch (IOException e) {
continue;
}

for (Map.Entry<Object, Object> variable : variables.entrySet()) {
Object key = variable.getKey();
if(key instanceof String) {
consumer.accept(Pair.create(key.toString(), file));
consumer.accept(Pair.create((String) key, file));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,11 @@ public void testGetEnvironmentVariableTargetsForParameter() {
.filter(psiElement -> psiElement instanceof PsiFile && "env.env".equals(((PsiFile) psiElement).getName()))
.count()
);

assertEquals(1, DotEnvUtil.getEnvironmentVariableTargetsForParameter(getProject(), "%env(int:json:foo_foo:foo-foo:foobar)%")
.stream()
.filter(psiElement -> psiElement instanceof PsiFile && "env.env".equals(((PsiFile) psiElement).getName()))
.count()
);
}
}