Skip to content

Commit 991c450

Browse files
committed
add code folding for twig constants #327
1 parent a62a9dd commit 991c450

File tree

4 files changed

+76
-18
lines changed

4 files changed

+76
-18
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public class Settings implements PersistentStateComponent<Settings> {
6464
public boolean codeFoldingPhpTemplate = true;
6565
public boolean codeFoldingTwigRoute = true;
6666
public boolean codeFoldingTwigTemplate = true;
67+
public boolean codeFoldingTwigConstant = true;
6768

6869
public boolean dismissEnableNotification = false;
6970

src/fr/adrienbrault/idea/symfony2plugin/SettingsForm.form

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<grid id="27dc6" binding="panel1" default-binding="true" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="0" left="0" bottom="0" right="0"/>
55
<constraints>
6-
<xy x="20" y="20" width="528" height="609"/>
6+
<xy x="20" y="20" width="528" height="637"/>
77
</constraints>
88
<properties/>
99
<border type="none"/>
@@ -52,6 +52,8 @@
5252
<rowspec value="center:max(d;4px):noGrow"/>
5353
<rowspec value="top:4dlu:noGrow"/>
5454
<rowspec value="center:max(d;4px):noGrow"/>
55+
<rowspec value="top:4dlu:noGrow"/>
56+
<rowspec value="center:max(d;4px):noGrow"/>
5557
<colspec value="fill:max(d;4px):noGrow"/>
5658
<colspec value="left:4dlu:noGrow"/>
5759
<colspec value="fill:d:grow"/>
@@ -130,7 +132,7 @@
130132
</component>
131133
<component id="ae90c" class="javax.swing.JCheckBox" binding="twigAnnotateRoute">
132134
<constraints>
133-
<grid row="24" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
135+
<grid row="26" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
134136
<forms/>
135137
</constraints>
136138
<properties>
@@ -139,7 +141,7 @@
139141
</component>
140142
<component id="1d0f2" class="javax.swing.JCheckBox" binding="twigAnnotateTemplate">
141143
<constraints>
142-
<grid row="26" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
144+
<grid row="28" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
143145
<forms/>
144146
</constraints>
145147
<properties>
@@ -148,7 +150,7 @@
148150
</component>
149151
<component id="19ad4" class="javax.swing.JCheckBox" binding="twigAnnotateAsset">
150152
<constraints>
151-
<grid row="28" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
153+
<grid row="30" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
152154
<forms/>
153155
</constraints>
154156
<properties>
@@ -157,7 +159,7 @@
157159
</component>
158160
<component id="de54b" class="javax.swing.JLabel">
159161
<constraints>
160-
<grid row="22" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
162+
<grid row="24" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
161163
<forms/>
162164
</constraints>
163165
<properties>
@@ -166,7 +168,7 @@
166168
</component>
167169
<component id="59700" class="javax.swing.JLabel">
168170
<constraints>
169-
<grid row="22" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
171+
<grid row="24" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
170172
<forms/>
171173
</constraints>
172174
<properties>
@@ -175,7 +177,7 @@
175177
</component>
176178
<component id="d0cf7" class="javax.swing.JCheckBox" binding="phpAnnotateRoute">
177179
<constraints>
178-
<grid row="24" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
180+
<grid row="26" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
179181
<forms/>
180182
</constraints>
181183
<properties>
@@ -184,7 +186,7 @@
184186
</component>
185187
<component id="c9461" class="javax.swing.JCheckBox" binding="phpAnnotateService">
186188
<constraints>
187-
<grid row="28" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
189+
<grid row="30" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
188190
<forms/>
189191
</constraints>
190192
<properties>
@@ -193,7 +195,7 @@
193195
</component>
194196
<component id="5a5a0" class="javax.swing.JCheckBox" binding="phpAnnotateTemplate">
195197
<constraints>
196-
<grid row="26" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
198+
<grid row="28" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
197199
<forms/>
198200
</constraints>
199201
<properties>
@@ -202,7 +204,7 @@
202204
</component>
203205
<component id="a28d6" class="javax.swing.JCheckBox" binding="phpAnnotateTemplateAnnotation">
204206
<constraints>
205-
<grid row="32" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
207+
<grid row="34" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
206208
<forms/>
207209
</constraints>
208210
<properties>
@@ -211,7 +213,7 @@
211213
</component>
212214
<component id="93ec7" class="javax.swing.JLabel">
213215
<constraints>
214-
<grid row="36" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
216+
<grid row="38" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
215217
<forms/>
216218
</constraints>
217219
<properties>
@@ -220,7 +222,7 @@
220222
</component>
221223
<component id="42ded" class="javax.swing.JCheckBox" binding="yamlAnnotateServiceConfig">
222224
<constraints>
223-
<grid row="38" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
225+
<grid row="40" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
224226
<forms/>
225227
</constraints>
226228
<properties>
@@ -319,7 +321,7 @@
319321
</component>
320322
<component id="3cb6a" class="javax.swing.JLabel">
321323
<constraints>
322-
<grid row="40" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
324+
<grid row="42" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
323325
<forms/>
324326
</constraints>
325327
<properties>
@@ -328,7 +330,7 @@
328330
</component>
329331
<component id="57f0" class="javax.swing.JCheckBox" binding="highlightServices">
330332
<constraints>
331-
<grid row="42" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
333+
<grid row="44" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
332334
<forms/>
333335
</constraints>
334336
<properties>
@@ -337,7 +339,7 @@
337339
</component>
338340
<component id="9e0bc" class="javax.swing.JCheckBox" binding="phpAnnotateTranslation">
339341
<constraints>
340-
<grid row="34" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
342+
<grid row="36" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
341343
<forms/>
342344
</constraints>
343345
<properties>
@@ -400,7 +402,7 @@
400402
</component>
401403
<component id="e812e" class="javax.swing.JCheckBox" binding="twigAnnotateAssetTags">
402404
<constraints>
403-
<grid row="30" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
405+
<grid row="32" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
404406
<forms/>
405407
</constraints>
406408
<properties>
@@ -409,7 +411,7 @@
409411
</component>
410412
<component id="cb3c8" class="javax.swing.JCheckBox" binding="twigAnnotateTranslation">
411413
<constraints>
412-
<grid row="32" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
414+
<grid row="34" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
413415
<forms/>
414416
</constraints>
415417
<properties>
@@ -418,13 +420,22 @@
418420
</component>
419421
<component id="acd72" class="javax.swing.JCheckBox" binding="phpAnnotateWeakService">
420422
<constraints>
421-
<grid row="30" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
423+
<grid row="32" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
422424
<forms/>
423425
</constraints>
424426
<properties>
425427
<text value="Weak Service"/>
426428
</properties>
427429
</component>
430+
<component id="c828d" class="javax.swing.JCheckBox" binding="codeFoldingTwigConstant">
431+
<constraints>
432+
<grid row="22" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
433+
<forms/>
434+
</constraints>
435+
<properties>
436+
<text value="Constant (Twig)"/>
437+
</properties>
438+
</component>
428439
</children>
429440
</grid>
430441
<component id="2ef99" class="javax.swing.JCheckBox" binding="pluginEnabled">

src/fr/adrienbrault/idea/symfony2plugin/SettingsForm.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public class SettingsForm implements Configurable {
6666
private JCheckBox codeFoldingTwigRoute;
6767
private JCheckBox codeFoldingPhpTemplate;
6868
private JCheckBox codeFoldingTwigTemplate;
69+
private JCheckBox codeFoldingTwigConstant;
70+
6971
private JCheckBox phpAnnotateWeakService;
7072
private JButton buttonReindex;
7173

@@ -148,6 +150,7 @@ public boolean isModified() {
148150
|| !codeFoldingPhpTemplate.isSelected() == getSettings().codeFoldingPhpTemplate
149151
|| !codeFoldingTwigRoute.isSelected() == getSettings().codeFoldingTwigRoute
150152
|| !codeFoldingTwigTemplate.isSelected() == getSettings().codeFoldingTwigTemplate
153+
|| !codeFoldingTwigConstant.isSelected() == getSettings().codeFoldingTwigConstant
151154

152155
|| !yamlAnnotateServiceConfig.isSelected() == getSettings().yamlAnnotateServiceConfig
153156

@@ -188,6 +191,7 @@ public void apply() throws ConfigurationException {
188191
getSettings().codeFoldingPhpTemplate = codeFoldingPhpTemplate.isSelected();
189192
getSettings().codeFoldingTwigRoute = codeFoldingTwigRoute.isSelected();
190193
getSettings().codeFoldingTwigTemplate = codeFoldingTwigTemplate.isSelected();
194+
getSettings().codeFoldingTwigConstant = codeFoldingTwigConstant.isSelected();
191195

192196
getSettings().yamlAnnotateServiceConfig = yamlAnnotateServiceConfig.isSelected();
193197

@@ -239,6 +243,7 @@ private void updateUIFromSettings() {
239243
codeFoldingPhpTemplate.setSelected(getSettings().codeFoldingPhpTemplate);
240244
codeFoldingTwigRoute.setSelected(getSettings().codeFoldingTwigRoute);
241245
codeFoldingTwigTemplate.setSelected(getSettings().codeFoldingTwigTemplate);
246+
codeFoldingTwigConstant.setSelected(getSettings().codeFoldingTwigConstant);
242247

243248
yamlAnnotateServiceConfig.setSelected(getSettings().yamlAnnotateServiceConfig);
244249

src/fr/adrienbrault/idea/symfony2plugin/navigation/TwigFoldingBuilder.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper;
1818
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
1919
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
20+
import org.apache.commons.lang.StringUtils;
2021
import org.jetbrains.annotations.NotNull;
2122
import org.jetbrains.annotations.Nullable;
2223

@@ -45,6 +46,10 @@ public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement psiElement, @Not
4546
attachTemplateFoldingDescriptors(psiElement, descriptors);
4647
}
4748

49+
if(Settings.getInstance(psiElement.getProject()).codeFoldingTwigConstant) {
50+
attachConstantFoldingDescriptors(psiElement, descriptors);
51+
}
52+
4853
return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
4954
}
5055

@@ -124,6 +129,42 @@ public String getPlaceholderText() {
124129

125130
}
126131

132+
private void attachConstantFoldingDescriptors(PsiElement psiElement, List<FoldingDescriptor> descriptors) {
133+
// find path calls in file
134+
PsiElement[] constantReferences = PsiTreeUtil.collectElements(psiElement, new PsiElementFilter() {
135+
@Override
136+
public boolean isAccepted(PsiElement psiElement) {
137+
return TwigHelper.getPrintBlockFunctionPattern("constant").accepts(psiElement);
138+
}
139+
});
140+
141+
if(constantReferences.length == 0) {
142+
return;
143+
}
144+
145+
FoldingGroup group = FoldingGroup.newGroup("constant");
146+
147+
for(PsiElement fileReference: constantReferences) {
148+
String contents = fileReference.getText();
149+
if(StringUtils.isNotBlank(contents) && contents.contains(":")) {
150+
final String[] parts = contents.split("::");
151+
if(parts.length == 2) {
152+
descriptors.add(new FoldingDescriptor(fileReference.getNode(),
153+
new TextRange(fileReference.getTextRange().getStartOffset(), fileReference.getTextRange().getEndOffset()), group) {
154+
@Nullable
155+
@Override
156+
public String getPlaceholderText() {
157+
return parts[1];
158+
}
159+
});
160+
}
161+
162+
}
163+
164+
}
165+
166+
}
167+
127168
@Nullable
128169
@Override
129170
public String getPlaceholderText(@NotNull ASTNode astNode) {

0 commit comments

Comments
 (0)