if(!Symfony2ProjectComponent.isEnabled(parameters.getPosition())) {
return;
}
TranslationStringMap map = TranslationIndex.getInstance(parameters.getPosition().getProject()).getTranslationMap();
if(map == null) {
return;
}
PsiElement psiElement = parameters.getPosition();
String domainName = TwigUtil.getPsiElementTranslationDomain(psiElement);
resultSet.addAllElements(TranslationUtil.getTranslationLookupElementsOnDomain(psiElement.getProject(), domainName));
}
}
);
// provides support for 'a'|trans({'%foo%' : bar|default}, '<xxx>')
// provides support for 'a'|transchoice(2, {'%foo%' : bar|default}, '<xxx>')
extend(
CompletionType.BASIC,
TwigHelper.getTransDomainPattern(),
new CompletionProvider<CompletionParameters>() {
public void addCompletions(@NotNull CompletionParameters parameters,
ProcessingContext context,
@NotNull CompletionResultSet resultSet) {
if(!Symfony2ProjectComponent.isEnabled(parameters.getPosition())) {
return;
}
if(PsiElementUtils.getPrevSiblingOfType(parameters.getPosition(), PlatformPatterns.psiElement(TwigTokenTypes.IDENTIFIER).withText(PlatformPatterns.string().oneOf("trans", "transchoice"))) == null) {
return;
}
resultSet.addAllElements(
TranslationUtil.getTranslationDomainLookupElements(parameters.getPosition().getProject())
);
}
}
);
// provides support for {% block |
extend(CompletionType.BASIC, TwigHelper.getBlockTagPattern(), new BlockCompletionProvider());
// provides support for {% from 'twig..' import |
extend(
CompletionType.BASIC,
TwigHelper.getTemplateImportFileReferenceTagPattern(),
new CompletionProvider<CompletionParameters>() {
public void addCompletions(@NotNull CompletionParameters parameters,
ProcessingContext context,
@NotNull CompletionResultSet resultSet) {
if(!Symfony2ProjectComponent.isEnabled(parameters.getPosition())) {
return;
}
// find {% from "<template.name>"
PsiElement psiElement = PsiElementUtils.getPrevSiblingOfType(parameters.getPosition(), getFromTemplateElement());
if(psiElement == null) {
return;
}
String templateName = psiElement.getText();
Map<String, VirtualFile> twigFilesByName = TwigHelper.getTwigFilesByName(parameters.getPosition().getProject());
if(!twigFilesByName.containsKey(templateName)) {
return;
}
VirtualFile virtualFile = twigFilesByName.get(templateName);
PsiFile psiFile = PsiManager.getInstance(psiElement.getProject()).findFile(virtualFile);
if(psiFile != null) {
for (Map.Entry<String, String> entry: new TwigMarcoParser().getMacros(psiFile).entrySet()) {
resultSet.addElement(LookupElementBuilder.create(entry.getKey()).withTypeText(entry.getValue(), true).withIcon(TwigIcons.TwigFileIcon));
}
}
}
private PsiElementPattern.Capture<PsiElement> getFromTemplateElement() {
return PlatformPatterns
.psiElement(TwigTokenTypes.STRING_TEXT)
.afterLeafSkipping(
PlatformPatterns.or(
PlatformPatterns.psiElement(PsiWhiteSpace.class),
PlatformPatterns.psiElement(TwigTokenTypes.WHITE_SPACE),
PlatformPatterns.psiElement(TwigTokenTypes.SINGLE_QUOTE),
PlatformPatterns.psiElement(TwigTokenTypes.DOUBLE_QUOTE)
),
PlatformPatterns.psiElement(TwigTokenTypes.TAG_NAME).withText(PlatformPatterns.string().oneOf("from"))
)
.withLanguage(TwigLanguage.INSTANCE);
}
}
);
// workaround for blocked twig filter completion in pre PhpStorm8 (WI-19022)
extend(
CompletionType.SMART,
PlatformPatterns.psiElement().withParent(PlatformPatterns.psiElement().withLanguage(TwigLanguage.INSTANCE)),
new FilterCompletionProvider()
);
// PhpStorm8 (since Twig plugin 136.1770.) allows twig filter completion without hack
// @TODO: use twig FILTER pattern if we have a working eap
extend(
CompletionType.BASIC,
PlatformPatterns.psiElement().withParent(PlatformPatterns.psiElement().withLanguage(TwigLanguage.INSTANCE)),
new FilterCompletionProvider()
);
// provides support for {{ '<xxx>' }}
extend(
CompletionType.BASIC,
TwigHelper.getCompletablePattern(),
new CompletionProvider<CompletionParameters>() {
public void addCompletions(@NotNull CompletionParameters parameters,
ProcessingContext context,
@NotNull CompletionResultSet resultSet) {
if(!Symfony2ProjectComponent.isEnabled(parameters.getPosition())) {
return;
}
PsiElement psiElement = parameters.getPosition().getOriginalElement();
for(Map.Entry<String, TwigExtension> entry : new TwigExtensionParser(parameters.getPosition().getProject()).getFunctions().entrySet()) {
resultSet.addElement(new TwigExtensionLookupElement(psiElement.getProject(), entry.getKey(), entry.getValue()));
}
for(TwigMacro twigMacro: TwigUtil.getImportedMacros(psiElement.getContainingFile())) {
resultSet.addElement(LookupElementBuilder.create(twigMacro.getName()).withTypeText(twigMacro.getTemplate(), true).withIcon(TwigIcons.TwigFileIcon).withInsertHandler(FunctionInsertHandler.getInstance()));
}
for(TwigMacro twigMacro: TwigUtil.getImportedMacrosNamespaces(psiElement.getContainingFile())) {
resultSet.addElement(LookupElementBuilder.create(twigMacro.getName()).withTypeText(twigMacro.getTemplate(), true).withIcon(TwigIcons.TwigFileIcon).withInsertHandler(FunctionInsertHandler.getInstance()));
}
for(TwigSet twigSet: TwigUtil.getSetDeclaration(psiElement.getContainingFile())) {
resultSet.addElement(LookupElementBuilder.create(twigSet.getName()).withTypeText("set", true));
}
for(Map.Entry<String, PsiVariable> entry: TwigTypeResolveUtil.collectScopeVariables(parameters.getOriginalPosition()).entrySet()) {
resultSet.addElement(LookupElementBuilder.create(entry.getKey()).withTypeText(TwigTypeResolveUtil.getTypeDisplayName(psiElement.getProject(), entry.getValue().getTypes()), true).withIcon(PhpIcons.CLASS));
}
for(Map.Entry<String, TwigGlobalVariable> entry: ServiceXmlParserFactory.getInstance(psiElement.getProject(), TwigGlobalsServiceParser.class).getTwigGlobals().entrySet()) {
if(entry.getValue().getTwigGlobalEnum() == TwigGlobalEnum.TEXT) {
resultSet.addElement(LookupElementBuilder.create(entry.getKey()).withTypeText(entry.getValue().getValue(), true).withIcon(PhpIcons.CONSTANT));
}
}
}
}
);
// {% for user in "users" %}
extend(
CompletionType.BASIC,
TwigHelper.getVariableTypePattern(),
new CompletionProvider<CompletionParameters>() {
public void addCompletions(@NotNull CompletionParameters parameters,
ProcessingContext context,
@NotNull CompletionResultSet resultSet) {
if(!Symfony2ProjectComponent.isEnabled(parameters.getPosition())) {
return;
}
PsiElement psiElement = parameters.getOriginalPosition();
if(psiElement == null) {
return;
}
for(Map.Entry<String, PsiVariable> entry: TwigTypeResolveUtil.collectScopeVariables(parameters.getOriginalPosition()).entrySet()) {
resultSet.addElement(LookupElementBuilder.create(entry.getKey()).withTypeText(TwigTypeResolveUtil.getTypeDisplayName(psiElement.getProject(), entry.getValue().getTypes())).withIcon(PhpIcons.CLASS));
}
}
}
);
// {% trans_default_domain <> %}
// {% trans_default_domain '<>' %}
extend(
CompletionType.BASIC,
TwigHelper.getTransDefaultDomain(),
new CompletionProvider<CompletionParameters>() {
public void addCompletions(@NotNull CompletionParameters parameters,
ProcessingContext context,
@NotNull CompletionResultSet resultSet) {
if(!Symfony2ProjectComponent.isEnabled(parameters.getPosition())) {
return;
}
TranslationStringMap map = TranslationIndex.getInstance(parameters.getPosition().getProject()).getTranslationMap();
for(String domainKey : map.getDomainList()) {
resultSet.addElement(new TranslatorLookupElement(domainKey, domainKey));
}
}
}