Skip to content

Commit 0314b6f

Browse files
committed
drop Twig set model; simplify Twig asset api;
1 parent 6053597 commit 0314b6f

File tree

16 files changed

+95
-141
lines changed

16 files changed

+95
-141
lines changed
Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package fr.adrienbrault.idea.symfony2plugin.asset.dic;
22

3-
43
import com.intellij.openapi.project.Project;
54
import com.intellij.openapi.vfs.VfsUtil;
65
import com.intellij.openapi.vfs.VirtualFile;
@@ -13,32 +12,24 @@
1312
import org.jetbrains.annotations.NotNull;
1413
import org.jetbrains.annotations.Nullable;
1514

16-
import java.util.ArrayList;
17-
import java.util.Arrays;
18-
import java.util.List;
15+
import java.util.*;
1916

2017
/**
2118
* @author Daniel Espendiller <daniel@espendiller.net>
2219
*/
2320
public class AssetDirectoryReader {
21+
final private boolean includeBundleDir;
2422

25-
protected Project project;
26-
protected boolean includeBundleDir = false;
27-
protected String[] filterExtension;
23+
@NotNull
24+
final private Collection<String> filterExtension = new HashSet<>();
2825

29-
public AssetDirectoryReader setProject(Project project) {
30-
this.project = project;
31-
return this;
26+
public AssetDirectoryReader() {
27+
includeBundleDir = false;
3228
}
3329

34-
public AssetDirectoryReader setIncludeBundleDir(boolean includeBundleDir) {
30+
public AssetDirectoryReader(@NotNull String[] filterExtension, boolean includeBundleDir) {
3531
this.includeBundleDir = includeBundleDir;
36-
return this;
37-
}
38-
39-
public AssetDirectoryReader setFilterExtension(String... filterExtension) {
40-
this.filterExtension = filterExtension;
41-
return this;
32+
this.filterExtension.addAll(Arrays.asList(filterExtension));
4233
}
4334

4435
@Nullable
@@ -48,10 +39,10 @@ public static VirtualFile getProjectAssetRoot(@NotNull Project project) {
4839
return VfsUtil.findRelativeFile(projectDirectory, webDirectoryName.split("/"));
4940
}
5041

51-
public List<AssetFile> getAssetFiles() {
52-
final List<AssetFile> files = new ArrayList<>();
42+
public List<AssetFile> getAssetFiles(@NotNull Project project) {
43+
List<AssetFile> files = new ArrayList<>();
5344

54-
final VirtualFile webDirectory = getProjectAssetRoot(project);
45+
VirtualFile webDirectory = getProjectAssetRoot(project);
5546
if (null == webDirectory) {
5647
return files;
5748
}
@@ -70,19 +61,17 @@ public boolean visitFile(@NotNull VirtualFile virtualFile) {
7061
return files;
7162
}
7263

73-
SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(PhpIndex.getInstance(this.project));
64+
SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(PhpIndex.getInstance(project));
7465
for(final SymfonyBundle bundle : symfonyBundleUtil.getBundles()) {
75-
7666
PsiDirectory bundleDirectory = bundle.getDirectory();
7767
if(null == bundleDirectory) {
7868
continue;
7969
}
8070

81-
final VirtualFile bundleDirectoryVirtual = bundleDirectory.getVirtualFile();
71+
VirtualFile bundleDirectoryVirtual = bundleDirectory.getVirtualFile();
8272
VirtualFile resourceDirectory = VfsUtil.findRelativeFile(bundleDirectoryVirtual, "Resources");
8373

8474
if (null != resourceDirectory) {
85-
8675
VfsUtil.visitChildrenRecursively(resourceDirectory, new VirtualFileVisitor() {
8776
@Override
8877
public boolean visitFile(@NotNull VirtualFile virtualFile) {
@@ -92,29 +81,18 @@ public boolean visitFile(@NotNull VirtualFile virtualFile) {
9281
return super.visitFile(virtualFile);
9382
}
9483
});
95-
9684
}
97-
9885
}
9986

10087
return files;
10188
}
10289

103-
private boolean isValidFile(VirtualFile virtualFile) {
104-
105-
if (virtualFile.isDirectory()) {
90+
private boolean isValidFile(@NotNull VirtualFile virtualFile) {
91+
if (this.filterExtension.size() == 0 || virtualFile.isDirectory()) {
10692
return false;
10793
}
10894

109-
if (this.filterExtension != null) {
110-
String extension = virtualFile.getExtension();
111-
112-
// file need extension and it must be in list
113-
return null != extension && Arrays.asList(this.filterExtension).contains(extension);
114-
115-
}
116-
117-
return true;
95+
String extension = virtualFile.getExtension();
96+
return extension != null && this.filterExtension.contains(extension);
11897
}
119-
12098
}

src/fr/adrienbrault/idea/symfony2plugin/asset/provider/AssetCompletionProvider.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,36 @@
1111
import fr.adrienbrault.idea.symfony2plugin.asset.AssetLookupElement;
1212
import fr.adrienbrault.idea.symfony2plugin.asset.dic.AssetDirectoryReader;
1313
import fr.adrienbrault.idea.symfony2plugin.asset.dic.AssetFile;
14-
import fr.adrienbrault.idea.symfony2plugin.templating.assets.TwigNamedAssetsServiceParser;
14+
import fr.adrienbrault.idea.symfony2plugin.twig.assets.TwigNamedAssetsServiceParser;
1515
import fr.adrienbrault.idea.symfony2plugin.util.service.ServiceXmlParserFactory;
1616
import org.jetbrains.annotations.NotNull;
1717

1818
/**
1919
* @author Daniel Espendiller <daniel@espendiller.net>
2020
*/
2121
public class AssetCompletionProvider extends CompletionProvider<CompletionParameters> {
22+
@NotNull
23+
final private AssetDirectoryReader assetParser;
2224

23-
protected AssetDirectoryReader assetParser;
24-
protected boolean includeCustom = false;
25+
final private boolean includeCustom;
2526

26-
public void addCompletions(@NotNull CompletionParameters parameters,
27-
ProcessingContext context,
28-
@NotNull final CompletionResultSet resultSet) {
27+
public AssetCompletionProvider(@NotNull AssetDirectoryReader assetParser, boolean includeCustom) {
28+
this.assetParser = assetParser;
29+
this.includeCustom = includeCustom;
30+
}
31+
32+
public AssetCompletionProvider(@NotNull AssetDirectoryReader assetParser) {
33+
this(assetParser, false);
34+
}
35+
36+
public void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull final CompletionResultSet resultSet) {
2937
Project project = parameters.getPosition().getProject();
3038

3139
if(!Symfony2ProjectComponent.isEnabled(parameters.getPosition())) {
3240
return;
3341
}
3442

35-
this.assetParser.setProject(project);
36-
for (final AssetFile assetFile : this.assetParser.getAssetFiles()) {
43+
for (AssetFile assetFile : assetParser.getAssetFiles(project)) {
3744
resultSet.addElement(new AssetLookupElement(assetFile, project));
3845
}
3946

@@ -43,17 +50,5 @@ public void addCompletions(@NotNull CompletionParameters parameters,
4350
resultSet.addElement(LookupElementBuilder.create("@" + s).withIcon(PlatformIcons.FOLDER_ICON).withTypeText("Custom Assets", true));
4451
}
4552
}
46-
4753
}
48-
49-
public AssetCompletionProvider setAssetParser(AssetDirectoryReader assetParser) {
50-
this.assetParser = assetParser;
51-
return this;
52-
}
53-
54-
public AssetCompletionProvider setIncludeCustom(boolean includeCustom) {
55-
this.includeCustom = includeCustom;
56-
return this;
57-
}
58-
5954
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement psiElement, @Not
5656
private void attachTemplateFoldingDescriptors(PsiElement psiElement, List<FoldingDescriptor> descriptors) {
5757
// find path calls in file
5858
PsiElement[] fileReferences = PsiTreeUtil.collectElements(psiElement, psiElement1 ->
59-
TwigPattern.getTemplateFileReferenceTagPattern().accepts(psiElement1) || TwigUtil.getFormThemeFileTag().accepts(psiElement1)
59+
TwigPattern.getTemplateFileReferenceTagPattern().accepts(psiElement1) || TwigPattern.getFormThemeFileTagPattern().accepts(psiElement1)
6060
);
6161

6262
if(fileReferences.length == 0) {
@@ -68,7 +68,6 @@ private void attachTemplateFoldingDescriptors(PsiElement psiElement, List<Foldin
6868
if(templateShortcutName != null) {
6969
descriptors.add(new FoldingDescriptor(fileReference.getNode(),
7070
new TextRange(fileReference.getTextRange().getStartOffset(), fileReference.getTextRange().getEndOffset())) {
71-
@Nullable
7271
@Override
7372
public String getPlaceholderText() {
7473
return templateShortcutName;

src/fr/adrienbrault/idea/symfony2plugin/templating/TwigPattern.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,22 @@ public static PsiElementPattern getParameterAsStringPattern() {
13471347
.afterLeafSkipping(PlatformPatterns.or(elementPatterns), PlatformPatterns.psiElement(TwigTokenTypes.COMMA));
13481348
}
13491349

1350+
public static PsiElementPattern.Capture<PsiComment> getTwigDocBlockMatchPattern(@NotNull String pattern) {
1351+
return PlatformPatterns
1352+
.psiComment().withText(PlatformPatterns.string().matches(pattern))
1353+
.withLanguage(TwigLanguage.INSTANCE);
1354+
}
1355+
1356+
/**
1357+
* {% form_theme form 'foobar.html.twig' %}
1358+
*/
1359+
public static PsiElementPattern.Capture<PsiElement> getFormThemeFileTagPattern() {
1360+
return PlatformPatterns
1361+
.psiElement(TwigTokenTypes.STRING_TEXT)
1362+
.withParent(PlatformPatterns.psiElement().withText(PlatformPatterns.string().matches("\\{%\\s+form_theme.*")))
1363+
.withLanguage(TwigLanguage.INSTANCE);
1364+
}
1365+
13501366
/**
13511367
* trans({
13521368
* %some%': "button.reserve"|trans,

src/fr/adrienbrault/idea/symfony2plugin/templating/TwigTemplateCompletionContributor.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ public void addCompletions(@NotNull CompletionParameters parameters, ProcessingC
197197
);
198198
}
199199

200-
for(TwigSet twigSet: TwigUtil.getSetDeclaration(psiElement.getContainingFile())) {
201-
resultSet.addElement(LookupElementBuilder.create(twigSet.getName()).withTypeText("set", true));
200+
for(String twigSet: TwigUtil.getSetDeclaration(psiElement.getContainingFile())) {
201+
resultSet.addElement(LookupElementBuilder.create(twigSet).withTypeText("set", true));
202202
}
203203

204204
for(Map.Entry<String, PsiVariable> entry: TwigTypeResolveUtil.collectScopeVariables(parameters.getOriginalPosition()).entrySet()) {
@@ -254,17 +254,25 @@ public void addCompletions(@NotNull CompletionParameters parameters, ProcessingC
254254
// assets completion:
255255
// stylesheets and javascripts tags
256256

257-
extend(CompletionType.BASIC, TwigPattern.getAutocompletableAssetPattern(), new AssetCompletionProvider().setAssetParser(
257+
extend(CompletionType.BASIC, TwigPattern.getAutocompletableAssetPattern(), new AssetCompletionProvider(
258258
new AssetDirectoryReader()
259259
));
260260

261-
extend(CompletionType.BASIC, TwigPattern.getAutocompletableAssetTag("stylesheets"), new AssetCompletionProvider().setIncludeCustom(true).setAssetParser(
262-
new AssetDirectoryReader().setFilterExtension(TwigUtil.CSS_FILES_EXTENSIONS).setIncludeBundleDir(true)
263-
));
261+
extend(
262+
CompletionType.BASIC,
263+
TwigPattern.getAutocompletableAssetTag("stylesheets"), new AssetCompletionProvider(
264+
new AssetDirectoryReader(TwigUtil.CSS_FILES_EXTENSIONS, true),
265+
true
266+
)
267+
);
264268

265-
extend(CompletionType.BASIC, TwigPattern.getAutocompletableAssetTag("javascripts"), new AssetCompletionProvider().setIncludeCustom(true).setAssetParser(
266-
new AssetDirectoryReader().setFilterExtension(TwigUtil.JS_FILES_EXTENSIONS).setIncludeBundleDir(true)
267-
));
269+
extend(
270+
CompletionType.BASIC,
271+
TwigPattern.getAutocompletableAssetTag("javascripts"), new AssetCompletionProvider(
272+
new AssetDirectoryReader(TwigUtil.JS_FILES_EXTENSIONS, true),
273+
true
274+
)
275+
);
268276

269277
// routing completion like path() function
270278
extend(
@@ -314,14 +322,14 @@ public void addCompletions(@NotNull CompletionParameters parameters, ProcessingC
314322
// {# @Container Foo:Bar #}
315323
extend(
316324
CompletionType.BASIC,
317-
TwigUtil.getTwigDocBlockMatchPattern(ControllerDocVariableCollector.DOC_PATTERN_COMPLETION),
325+
TwigPattern.getTwigDocBlockMatchPattern(ControllerDocVariableCollector.DOC_PATTERN_COMPLETION),
318326
new ControllerCompletionProvider()
319327
);
320328

321329
// {% form_theme * %}
322330
extend(
323331
CompletionType.BASIC,
324-
TwigUtil.getFormThemeFileTag(),
332+
TwigPattern.getFormThemeFileTagPattern(),
325333
new FormThemeCompletionProvider()
326334
);
327335

src/fr/adrienbrault/idea/symfony2plugin/templating/TwigTemplateGoToDeclarationHandler.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import fr.adrienbrault.idea.symfony2plugin.templating.dict.TwigBlock;
2424
import fr.adrienbrault.idea.symfony2plugin.templating.dict.TwigBlockParser;
2525
import fr.adrienbrault.idea.symfony2plugin.templating.dict.TwigExtension;
26-
import fr.adrienbrault.idea.symfony2plugin.templating.dict.TwigSet;
2726
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigExtensionParser;
2827
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigTypeResolveUtil;
2928
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
@@ -155,7 +154,7 @@ public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int offset,
155154
targets.addAll(getTypeGoto(psiElement));
156155
}
157156

158-
if(TwigUtil.getTwigDocBlockMatchPattern(ControllerDocVariableCollector.DOC_PATTERN).accepts(psiElement)) {
157+
if(TwigPattern.getTwigDocBlockMatchPattern(ControllerDocVariableCollector.DOC_PATTERN).accepts(psiElement)) {
159158
targets.addAll(getControllerNameGoto(psiElement));
160159
}
161160

@@ -492,8 +491,8 @@ private Collection<PsiElement> getFunctions(@NotNull PsiElement psiElement) {
492491

493492
private Collection<PsiElement> getSets(@NotNull PsiElement psiElement) {
494493
String funcName = psiElement.getText();
495-
for(TwigSet twigSet: TwigUtil.getSetDeclaration(psiElement.getContainingFile())) {
496-
if(twigSet.getName().equals(funcName)) {
494+
for(String twigSet: TwigUtil.getSetDeclaration(psiElement.getContainingFile())) {
495+
if(twigSet.equals(funcName)) {
497496
return Arrays.asList(PsiTreeUtil.collectElements(psiElement.getContainingFile(), new RegexPsiElementFilter(
498497
TwigTagWithFileReference.class,
499498
"\\{%\\s?set\\s?" + Pattern.quote(funcName) + "\\s?.*")

src/fr/adrienbrault/idea/symfony2plugin/templating/dict/TwigBlock.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,5 @@ public void setShortcutName(@Nullable String shortcutName) {
4848
// @TODO: remove this
4949
this.shortcutName = shortcutName;
5050
}
51-
5251
}
5352

src/fr/adrienbrault/idea/symfony2plugin/templating/dict/TwigExtensionLookupElement.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@
2222
* @author Daniel Espendiller <daniel@espendiller.net>
2323
*/
2424
public class TwigExtensionLookupElement extends LookupElement {
25-
25+
@NotNull
2626
final private TwigExtension twigExtension;
27+
28+
@NotNull
2729
final private String name;
30+
31+
@NotNull
2832
final private Project project;
2933

30-
public TwigExtensionLookupElement(Project project, String name, TwigExtension twigExtension) {
34+
public TwigExtensionLookupElement(@NotNull Project project, @NotNull String name, @NotNull TwigExtension twigExtension) {
3135
this.project = project;
3236
this.name = name;
3337
this.twigExtension = twigExtension;
@@ -59,7 +63,7 @@ public void renderElement(LookupElementPresentation presentation) {
5963
presentation.setTypeGrayed(true);
6064
}
6165

62-
private void buildTailText(LookupElementPresentation presentation) {
66+
private void buildTailText(@NotNull LookupElementPresentation presentation) {
6367
if(this.twigExtension.getTwigExtensionType() == TwigExtensionParser.TwigExtensionType.SIMPLE_TEST) {
6468
return;
6569
}

src/fr/adrienbrault/idea/symfony2plugin/templating/dict/TwigMacro.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public String getParameter() {
5252
@NotNull
5353
public TwigMacro withParameter(@Nullable String parameter) {
5454
this.parameter = parameter;
55-
5655
return this;
5756
}
5857
}

src/fr/adrienbrault/idea/symfony2plugin/templating/dict/TwigMacroTag.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public class TwigMacroTag implements TwigMacroTagInterface {
2323
* @param parameters Raw parameter string (name, value, type, size)
2424
*/
2525
public TwigMacroTag(@NotNull String name, @Nullable String parameters) {
26-
2726
this.name = name;
2827
this.parameters = parameters;
2928
}

0 commit comments

Comments
 (0)