Skip to content

provide incomple block completion #1820

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

Merged
merged 1 commit into from
Apr 10, 2022
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 @@ -435,6 +435,13 @@ public void addCompletions(@NotNull CompletionParameters parameters, ProcessingC
new IncompleteEmbedCompletionProvider()
);

// {% bl => {% block '...'
extend(
CompletionType.BASIC,
PlatformPatterns.psiElement(TwigTokenTypes.TAG_NAME),
new IncompleteBlockCompletionProvider()
);

// {{ in => {{ include('...')
extend(
CompletionType.BASIC,
Expand Down Expand Up @@ -933,6 +940,41 @@ public boolean accepts(@NotNull String s, ProcessingContext processingContext) {
}
}

/**
* {% bl => {% block '...'
*/
private class IncompleteBlockCompletionProvider extends CompletionProvider<CompletionParameters> {
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, @NotNull ProcessingContext processingContext, @NotNull CompletionResultSet resultSet) {
PsiElement position = completionParameters.getOriginalPosition();
if(!Symfony2ProjectComponent.isEnabled(position)) {
return;
}

resultSet.restartCompletionOnPrefixChange(StandardPatterns.string().longerThan(1).with(new PatternCondition<>("embed startsWith") {
@Override
public boolean accepts(@NotNull String s, ProcessingContext processingContext) {
return "block".startsWith(s);
}
}));

if (!isCompletionStartingMatch("block", completionParameters, 2)) {
return;
}

Pair<Collection<PsiFile>, Boolean> scopedContext = TwigUtil.findScopedFile(position);

Collection<LookupElement> blockLookupElements = TwigUtil.getBlockLookupElements(
position.getProject(),
TwigFileUtil.collectParentFiles(scopedContext.getSecond(), scopedContext.getFirst())
);

for (LookupElement blockLookupElement : blockLookupElements) {
resultSet.addElement(LookupElementBuilder.create("block " + blockLookupElement.getLookupString()).withIcon(TwigIcons.TwigFileIcon));
}
}
}

@NotNull
private Collection<LookupElement> processVariables(@NotNull PsiElement psiElement, @NotNull Predicate<PhpType> filter, @NotNull Function<Map.Entry<String, Pair<String, LookupElement>>, String> map) {
Project project = psiElement.getProject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ private static void visitParentFiles(@NotNull PsiFile file, int depth, Collectio
return;
}

// secure loading
VirtualFile virtualFile1 = file.getVirtualFile();
if (virtualFile1 == null) {
return;
}

Set<VirtualFile> myVirtualFiles = new HashSet<>();
Set<String> templates = new HashSet<>();

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

FileBasedIndex.getInstance()
.getFileData(TwigExtendsStubIndex.KEY, file.getVirtualFile(), file.getProject())
.getFileData(TwigExtendsStubIndex.KEY, virtualFile1, file.getProject())
.forEach((templateName, aVoid) -> templates.add(templateName));

for (String template : templates) {
Expand Down