Skip to content

Commit e8b8d3c

Browse files
authored
Merge pull request #1820 from Haehnchen/feature/block-complete
provide incomple block completion
2 parents b848154 + 746d4c5 commit e8b8d3c

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,13 @@ public void addCompletions(@NotNull CompletionParameters parameters, ProcessingC
435435
new IncompleteEmbedCompletionProvider()
436436
);
437437

438+
// {% bl => {% block '...'
439+
extend(
440+
CompletionType.BASIC,
441+
PlatformPatterns.psiElement(TwigTokenTypes.TAG_NAME),
442+
new IncompleteBlockCompletionProvider()
443+
);
444+
438445
// {{ in => {{ include('...')
439446
extend(
440447
CompletionType.BASIC,
@@ -933,6 +940,41 @@ public boolean accepts(@NotNull String s, ProcessingContext processingContext) {
933940
}
934941
}
935942

943+
/**
944+
* {% bl => {% block '...'
945+
*/
946+
private class IncompleteBlockCompletionProvider extends CompletionProvider<CompletionParameters> {
947+
@Override
948+
protected void addCompletions(@NotNull CompletionParameters completionParameters, @NotNull ProcessingContext processingContext, @NotNull CompletionResultSet resultSet) {
949+
PsiElement position = completionParameters.getOriginalPosition();
950+
if(!Symfony2ProjectComponent.isEnabled(position)) {
951+
return;
952+
}
953+
954+
resultSet.restartCompletionOnPrefixChange(StandardPatterns.string().longerThan(1).with(new PatternCondition<>("embed startsWith") {
955+
@Override
956+
public boolean accepts(@NotNull String s, ProcessingContext processingContext) {
957+
return "block".startsWith(s);
958+
}
959+
}));
960+
961+
if (!isCompletionStartingMatch("block", completionParameters, 2)) {
962+
return;
963+
}
964+
965+
Pair<Collection<PsiFile>, Boolean> scopedContext = TwigUtil.findScopedFile(position);
966+
967+
Collection<LookupElement> blockLookupElements = TwigUtil.getBlockLookupElements(
968+
position.getProject(),
969+
TwigFileUtil.collectParentFiles(scopedContext.getSecond(), scopedContext.getFirst())
970+
);
971+
972+
for (LookupElement blockLookupElement : blockLookupElements) {
973+
resultSet.addElement(LookupElementBuilder.create("block " + blockLookupElement.getLookupString()).withIcon(TwigIcons.TwigFileIcon));
974+
}
975+
}
976+
}
977+
936978
@NotNull
937979
private Collection<LookupElement> processVariables(@NotNull PsiElement psiElement, @NotNull Predicate<PhpType> filter, @NotNull Function<Map.Entry<String, Pair<String, LookupElement>>, String> map) {
938980
Project project = psiElement.getProject();

src/main/java/fr/adrienbrault/idea/symfony2plugin/twig/utils/TwigFileUtil.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ private static void visitParentFiles(@NotNull PsiFile file, int depth, Collectio
5252
return;
5353
}
5454

55+
// secure loading
56+
VirtualFile virtualFile1 = file.getVirtualFile();
57+
if (virtualFile1 == null) {
58+
return;
59+
}
60+
5561
Set<VirtualFile> myVirtualFiles = new HashSet<>();
5662
Set<String> templates = new HashSet<>();
5763

@@ -60,7 +66,7 @@ private static void visitParentFiles(@NotNull PsiFile file, int depth, Collectio
6066
.forEach(templates::addAll);
6167

6268
FileBasedIndex.getInstance()
63-
.getFileData(TwigExtendsStubIndex.KEY, file.getVirtualFile(), file.getProject())
69+
.getFileData(TwigExtendsStubIndex.KEY, virtualFile1, file.getProject())
6470
.forEach((templateName, aVoid) -> templates.add(templateName));
6571

6672
for (String template : templates) {

0 commit comments

Comments
 (0)