Skip to content

Commit 2aec2a9

Browse files
committed
allow to disable Twig bundle namespace and support in autoconfigure
1 parent 52c8807 commit 2aec2a9

File tree

9 files changed

+80
-15
lines changed

9 files changed

+80
-15
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/Settings.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public class Settings implements PersistentStateComponent<Settings> {
5656
public boolean codeFoldingTwigTemplate = true;
5757
public boolean codeFoldingTwigConstant = true;
5858

59+
public boolean twigBundleNamespaceSupport = true;
60+
5961
public boolean dismissEnableNotification = false;
6062

6163
public boolean profilerLocalEnabled = false;

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/path/BundleOverwriteNamespaceExtensions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.util.ArrayList;
1111
import java.util.Collection;
12+
import java.util.Collections;
1213

1314
/**
1415
* "app/Resources/ParentBundle/Resources/views"
@@ -19,6 +20,10 @@ public class BundleOverwriteNamespaceExtensions implements TwigNamespaceExtensio
1920
@NotNull
2021
@Override
2122
public Collection<TwigPath> getNamespaces(@NotNull TwigNamespaceExtensionParameter parameter) {
23+
if (!TwigUtil.hasBundleNamespaceSupport(parameter.getProject())) {
24+
return Collections.emptyList();
25+
}
26+
2227
Collection<TwigPath> twigPaths = new ArrayList<>();
2328

2429
new SymfonyBundleUtil(parameter.getProject()).getParentBundles().forEach((key, virtualFile) -> {

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/path/BundleTwigNamespaceExtension.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public Collection<TwigPath> getNamespaces(@NotNull TwigNamespaceExtensionParamet
3535

3636
String bundleName = bundle.getName();
3737

38-
twigPaths.add(new TwigPath(path, bundleName, TwigUtil.NamespaceType.BUNDLE));
38+
if (TwigUtil.hasBundleNamespaceSupport(parameter.getProject())) {
39+
twigPaths.add(new TwigPath(path, bundleName, TwigUtil.NamespaceType.BUNDLE));
40+
}
3941

4042
// Bundle is stripped from its name on the ending "FooBarBundle" => "FooBarBundle"
4143
if (bundleName.endsWith("Bundle")) {

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/path/GlobalAppTwigNamespaceExtension.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public Collection<TwigPath> getNamespaces(@NotNull TwigNamespaceExtensionParamet
4444
Collection<TwigPath> paths = new ArrayList<>();
4545

4646
directories.stream().map(VirtualFile::getPath).forEach(path -> {
47-
paths.add(new TwigPath(path, TwigUtil.MAIN, TwigUtil.NamespaceType.BUNDLE));
47+
if (TwigUtil.hasBundleNamespaceSupport(parameter.getProject())) {
48+
paths.add(new TwigPath(path, TwigUtil.MAIN, TwigUtil.NamespaceType.BUNDLE));
49+
}
50+
4851
paths.add(new TwigPath(path, TwigUtil.MAIN, TwigUtil.NamespaceType.ADD_PATH));
4952
});
5053

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/util/TwigUtil.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,6 +2562,14 @@ public static void visitTemplateVariables(@NotNull TwigFile scope, @NotNull Cons
25622562
visitTemplateVariables(scope, consumer, 3);
25632563
}
25642564

2565+
2566+
/**
2567+
* Check if Twig pathes should be filled with the Bundle naming strategy "FooBar:Foo:Test"
2568+
*/
2569+
public static boolean hasBundleNamespaceSupport(@NotNull Project project) {
2570+
return Settings.getInstance(project).twigBundleNamespaceSupport;
2571+
}
2572+
25652573
/**
25662574
* Proxy to consume every given scope
25672575
*/

src/main/java/fr/adrienbrault/idea/symfony2plugin/ui/TwigSettingsForm.form

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="fr.adrienbrault.idea.symfony2plugin.ui.TwigSettingsForm">
33
<grid id="27dc6" binding="panel1" default-binding="true" layout-manager="BorderLayout" hgap="0" vgap="0">
44
<constraints>
5-
<xy x="20" y="20" width="710" height="400"/>
5+
<xy x="20" y="20" width="736" height="400"/>
66
</constraints>
77
<properties/>
88
<border type="none"/>
@@ -18,7 +18,7 @@
1818
<properties/>
1919
<border type="none"/>
2020
<children>
21-
<grid id="3c6d1" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
21+
<grid id="3c6d1" layout-manager="GridLayoutManager" row-count="1" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
2222
<margin top="0" left="0" bottom="0" right="0"/>
2323
<constraints border-constraint="North"/>
2424
<properties/>
@@ -34,15 +34,15 @@
3434
</component>
3535
<component id="bead7" class="javax.swing.JButton" binding="resetToDefault">
3636
<constraints>
37-
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
37+
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
3838
</constraints>
3939
<properties>
4040
<text value="Reset To Default"/>
4141
</properties>
4242
</component>
4343
<component id="67805" class="javax.swing.JButton" binding="buttonJsonExample">
4444
<constraints>
45-
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
45+
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
4646
</constraints>
4747
<properties>
4848
<text value="JSON Example"/>
@@ -53,6 +53,16 @@
5353
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
5454
</constraints>
5555
</hspacer>
56+
<component id="ec914" class="javax.swing.JCheckBox" binding="chkTwigBundleNamespaceSupport">
57+
<constraints>
58+
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
59+
</constraints>
60+
<properties>
61+
<selected value="true"/>
62+
<text value="Support Bundle Namespaces"/>
63+
<toolTipText value="Example: Foobar:Bar:Foo.html.twig"/>
64+
</properties>
65+
</component>
5666
</children>
5767
</grid>
5868
</children>

src/main/java/fr/adrienbrault/idea/symfony2plugin/ui/TwigSettingsForm.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.intellij.openapi.options.ConfigurationException;
55
import com.intellij.openapi.project.DumbService;
66
import com.intellij.openapi.project.Project;
7-
import com.intellij.openapi.ui.DialogWrapper;
87
import com.intellij.ui.ToolbarDecorator;
98
import com.intellij.ui.table.TableView;
109
import com.intellij.util.ui.ColumnInfo;
@@ -35,6 +34,7 @@ public class TwigSettingsForm implements Configurable {
3534
private JPanel panelTableView;
3635
private JButton resetToDefault;
3736
private JButton buttonJsonExample;
37+
private JCheckBox chkTwigBundleNamespaceSupport;
3838
private TableView<TwigPath> tableView;
3939
private Project project;
4040
private boolean changed = false;
@@ -169,6 +169,7 @@ public void apply() throws ConfigurationException {
169169
}
170170
}
171171

172+
getSettings().twigBundleNamespaceSupport = chkTwigBundleNamespaceSupport.isSelected();
172173
getSettings().twigNamespaces = twigPaths;
173174
this.changed = false;
174175
}
@@ -189,9 +190,14 @@ private void resetList() {
189190
public void reset() {
190191
this.resetList();
191192
this.attachItems();
193+
this.updateUIFromSettings();
192194
this.changed = false;
193195
}
194196

197+
private void updateUIFromSettings() {
198+
this.chkTwigBundleNamespaceSupport.setSelected(getSettings().twigBundleNamespaceSupport);
199+
}
200+
195201
@Override
196202
public void disposeUIResources() {
197203
this.resetList();

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/IdeHelper.java

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import java.awt.*;
2525
import java.io.IOException;
2626
import java.net.URISyntaxException;
27-
import java.util.Arrays;
27+
import java.util.*;
28+
import java.util.List;
29+
import java.util.function.Function;
30+
import java.util.stream.Collectors;
2831

2932
/**
3033
* @author Daniel Espendiller <daniel@espendiller.net>
@@ -123,16 +126,24 @@ public void run() {
123126

124127
public static void notifyEnableMessage(final Project project) {
125128

126-
Notification notification = new Notification("Symfony Plugin", "Symfony Plugin", "Enable the Symfony Plugin <a href=\"enable\">with auto configuration now</a>, open <a href=\"config\">Project Settings</a> or <a href=\"dismiss\">dismiss</a> further messages", NotificationType.INFORMATION, (notification1, event) -> {
129+
Notification notification = new Notification("Symfony Support", "Symfony", "Enable the Symfony Plugin <a href=\"enable\">with auto configuration now</a>, open <a href=\"config\">Project Settings</a> or <a href=\"dismiss\">dismiss</a> further messages", NotificationType.INFORMATION, (notification1, event) -> {
127130

128131
// handle html click events
129132
if("config".equals(event.getDescription())) {
130133

131134
// open settings dialog and show panel
132135
SettingsForm.show(project);
133136
} else if("enable".equals(event.getDescription())) {
134-
enablePluginAndConfigure(project);
135-
Notifications.Bus.notify(new Notification("Symfony Plugin", "Symfony Plugin", "Plugin enabled", NotificationType.INFORMATION), project);
137+
Collection<String> messages = enablePluginAndConfigure(project);
138+
139+
String message = "Plugin enabled";
140+
141+
if (!messages.isEmpty()) {
142+
List<String> collect = messages.stream().map(s -> "<br> - " + s).collect(Collectors.toList());
143+
message += StringUtils.join(collect, "");
144+
}
145+
146+
Notifications.Bus.notify(new Notification("Symfony Support", "Symfony", message, NotificationType.INFORMATION), project);
136147
} else if("dismiss".equals(event.getDescription())) {
137148

138149
// user doesnt want to show notification again
@@ -145,18 +156,36 @@ public static void notifyEnableMessage(final Project project) {
145156
Notifications.Bus.notify(notification, project);
146157
}
147158

148-
public static void enablePluginAndConfigure(@NotNull Project project) {
159+
public static Collection<String> enablePluginAndConfigure(@NotNull Project project) {
149160
Settings.getInstance(project).pluginEnabled = true;
150161

162+
Collection<String> messages = new ArrayList<>();
163+
164+
Set<String> versions = SymfonyUtil.getVersions(project);
165+
if (!versions.isEmpty()) {
166+
messages.add("Symfony Version: " + versions.iterator().next());
167+
}
168+
151169
// Symfony 3.0 structure
152-
if(VfsUtil.findRelativeFile(ProjectUtil.getProjectDir(project), "var", "cache") != null) {
170+
if (VfsUtil.findRelativeFile(ProjectUtil.getProjectDir(project), "var", "cache") != null) {
153171
Settings.getInstance(project).pathToTranslation = "var/cache/dev/translations";
172+
messages.add("Translations: var/cache/dev/translations");
154173
}
155174

156175
// Symfony 4.0 structure
157-
if(VfsUtil.findRelativeFile(ProjectUtil.getProjectDir(project), "public") != null) {
176+
if (VfsUtil.findRelativeFile(ProjectUtil.getProjectDir(project), "public") != null) {
158177
Settings.getInstance(project).directoryToWeb = "public";
178+
messages.add("Web Directory: public");
159179
}
180+
181+
// There no clean version when "FooBar:Foo:foo.html.twig" was dropped or deprecated
182+
// So we disable it in the 4 branch by default; following with a default switch to "false" soon
183+
if (SymfonyUtil.isVersionGreaterThenEquals(project, "4.0")) {
184+
Settings.getInstance(project).twigBundleNamespaceSupport = false;
185+
messages.add("Twig: Bundle names disabled");
186+
}
187+
188+
return messages;
160189
}
161190

162191
public static void navigateToPsiElement(@NotNull PsiElement psiElement) {

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/SymfonyUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private static boolean compare(@NotNull Project project, @NotNull String version
4343
}
4444

4545
@NotNull
46-
private static Set<String> getVersions(@NotNull Project project) {
46+
public static Set<String> getVersions(@NotNull Project project) {
4747
Set<String> versions = new HashSet<>();
4848

4949
for (PhpClass phpClass : PhpElementsUtil.getClassesInterface(project, "Symfony\\Component\\HttpKernel\\Kernel")) {

0 commit comments

Comments
 (0)