use of com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator in project intellij-community by JetBrains.
the class ZenCodingTemplate method isApplicable.
@Override
public boolean isApplicable(PsiFile file, int offset, boolean wrapping) {
if (file == null) {
return false;
}
PsiElement element = CustomTemplateCallback.getContext(file, offset);
final ZenCodingGenerator applicableGenerator = findApplicableDefaultGenerator(element, wrapping);
return applicableGenerator != null && applicableGenerator.isEnabled();
}
use of com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator in project intellij-community by JetBrains.
the class ZenCodingTemplate method addCompletions.
@Override
public void addCompletions(CompletionParameters parameters, CompletionResultSet result) {
if (!parameters.isAutoPopup()) {
return;
}
PsiFile file = parameters.getPosition().getContainingFile();
int offset = parameters.getOffset();
Editor editor = parameters.getEditor();
ZenCodingGenerator generator = findApplicableDefaultGenerator(CustomTemplateCallback.getContext(file, offset), false);
if (generator != null && generator.hasCompletionItem()) {
final CollectCustomTemplateCallback callback = new CollectCustomTemplateCallback(editor, file);
final String templatePrefix = computeTemplateKeyWithoutContextChecking(callback);
if (templatePrefix != null) {
List<TemplateImpl> regularTemplates = TemplateManagerImpl.listApplicableTemplates(file, offset, false);
boolean regularTemplateWithSamePrefixExists = !ContainerUtil.filter(regularTemplates, template -> templatePrefix.equals(template.getKey())).isEmpty();
result = result.withPrefixMatcher(result.getPrefixMatcher().cloneWithPrefix(templatePrefix));
result.restartCompletionOnPrefixChange(StandardPatterns.string().startsWith(templatePrefix));
if (!regularTemplateWithSamePrefixExists) {
// exclude perfect matches with existing templates because LiveTemplateCompletionContributor handles it
final Collection<SingleLineEmmetFilter> extraFilters = ContainerUtil.newLinkedList(new SingleLineEmmetFilter());
try {
expand(templatePrefix, callback, generator, extraFilters, false, 0);
} catch (EmmetException ignore) {
}
final TemplateImpl template = callback.getGeneratedTemplate();
if (template != null) {
template.setKey(templatePrefix);
template.setDescription(template.getTemplateText());
final CustomLiveTemplateLookupElement lookupElement = new CustomLiveTemplateLookupElement(this, template.getKey(), template.getKey(), template.getDescription(), !LiveTemplateCompletionContributor.shouldShowAllTemplates(), true) {
@Override
public void renderElement(LookupElementPresentation presentation) {
super.renderElement(presentation);
presentation.setTailText("\t Emmet abbreviation", true);
}
};
result.addElement(lookupElement);
}
}
} else if (result.getPrefixMatcher().getPrefix().isEmpty()) {
result.restartCompletionOnPrefixChange(StandardPatterns.string().longerThan(0));
}
}
}
use of com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator in project intellij-community by JetBrains.
the class ZenCodingTemplate method findApplicableGenerator.
@Nullable
private static ZenCodingGenerator findApplicableGenerator(ZenCodingNode node, PsiElement context, boolean wrapping) {
ZenCodingGenerator defaultGenerator = null;
ZenCodingGenerator[] generators = ZenCodingGenerator.getInstances();
for (ZenCodingGenerator generator : generators) {
if (generator.isMyContext(context, wrapping) && generator.isAppliedByDefault(context)) {
defaultGenerator = generator;
break;
}
}
while (node instanceof FilterNode) {
FilterNode filterNode = (FilterNode) node;
String suffix = filterNode.getFilter();
for (ZenCodingGenerator generator : generators) {
if (generator.isMyContext(context, wrapping)) {
if (suffix != null && suffix.equals(generator.getSuffix())) {
return generator;
}
}
}
node = filterNode.getNode();
}
return defaultGenerator;
}
use of com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator in project intellij-community by JetBrains.
the class ZenCodingTemplate method checkTemplateKey.
public static boolean checkTemplateKey(String inputString, CustomTemplateCallback callback) {
ZenCodingGenerator generator = findApplicableDefaultGenerator(callback.getContext(), true);
if (generator == null) {
int offset = callback.getEditor().getCaretModel().getOffset();
LOG.error("Emmet is disabled for context for file " + callback.getFileType().getName() + " in offset: " + offset, AttachmentFactory.createAttachment(callback.getEditor().getDocument()));
return false;
}
return checkTemplateKey(inputString, callback, generator);
}
use of com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator in project intellij-community by JetBrains.
the class ZenCodingTemplate method hasCompletionItem.
@Override
public boolean hasCompletionItem(@NotNull PsiFile file, int offset) {
PsiElement element = CustomTemplateCallback.getContext(file, offset);
final ZenCodingGenerator applicableGenerator = findApplicableDefaultGenerator(element, false);
return applicableGenerator != null && applicableGenerator.isEnabled() && applicableGenerator.hasCompletionItem();
}
Aggregations