use of com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator in project intellij-community by JetBrains.
the class EmmetPreviewUtil method calculateTemplateText.
@Nullable
public static String calculateTemplateText(@NotNull Editor editor, @NotNull PsiFile file, boolean expandPrimitiveAbbreviations) {
if (file instanceof XmlFile) {
PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
CollectCustomTemplateCallback callback = new CollectCustomTemplateCallback(editor, file);
PsiElement context = callback.getContext();
ZenCodingGenerator generator = ZenCodingTemplate.findApplicableDefaultGenerator(context, false);
if (generator != null && generator instanceof XmlZenCodingGenerator) {
final String templatePrefix = new ZenCodingTemplate().computeTemplateKeyWithoutContextChecking(callback);
if (templatePrefix != null) {
try {
ZenCodingTemplate.expand(templatePrefix, callback, generator, Collections.<ZenCodingFilter>emptyList(), expandPrimitiveAbbreviations, 0);
TemplateImpl template = callback.getGeneratedTemplate();
String templateText = template != null ? template.getTemplateText() : null;
if (!StringUtil.isEmpty(templateText)) {
return template.isToReformat() ? reformatTemplateText(file, templateText) : templateText;
}
} catch (EmmetException e) {
return e.getMessage();
}
}
}
}
return null;
}
use of com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator in project intellij-community by JetBrains.
the class ZenCodingTemplate method doWrap.
public static void doWrap(@NotNull final String abbreviation, @NotNull final CustomTemplateCallback callback) {
final ZenCodingGenerator defaultGenerator = findApplicableDefaultGenerator(callback.getContext(), true);
assert defaultGenerator != null;
ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(callback.getProject(), () -> callback.getEditor().getCaretModel().runForEachCaret(new CaretAction() {
@Override
public void perform(Caret caret) {
String selectedText = callback.getEditor().getSelectionModel().getSelectedText();
if (selectedText != null) {
String selection = selectedText.trim();
ZenCodingNode node = parse(abbreviation, callback, defaultGenerator, selection);
assert node != null;
PsiElement context = callback.getContext();
ZenCodingGenerator generator = findApplicableGenerator(node, context, true);
List<ZenCodingFilter> filters = getFilters(node, context);
EditorModificationUtil.deleteSelectedText(callback.getEditor());
PsiDocumentManager.getInstance(callback.getProject()).commitAllDocuments();
try {
expand(node, generator, filters, selection, callback, true, Registry.intValue("emmet.segments.limit"));
} catch (EmmetException e) {
CommonRefactoringUtil.showErrorHint(callback.getProject(), callback.getEditor(), e.getMessage(), "Emmet error", "");
}
}
}
}), CodeInsightBundle.message("insert.code.template.command"), null));
}
use of com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator in project intellij-community by JetBrains.
the class ZenCodingTemplate method expand.
public static void expand(@NotNull String key, @NotNull CustomTemplateCallback callback, @NotNull ZenCodingGenerator defaultGenerator, @NotNull Collection<? extends ZenCodingFilter> extraFilters, boolean expandPrimitiveAbbreviations, int segmentsLimit) throws EmmetException {
final ZenCodingNode node = parse(key, callback, defaultGenerator, null);
if (node == null) {
return;
}
if (node instanceof TemplateNode) {
if (key.equals(((TemplateNode) node).getTemplateToken().getKey()) && callback.findApplicableTemplates(key).size() > 1) {
TemplateManagerImpl templateManager = (TemplateManagerImpl) callback.getTemplateManager();
Map<TemplateImpl, String> template2Argument = templateManager.findMatchingTemplates(callback.getFile(), callback.getEditor(), null, TemplateSettings.getInstance());
Runnable runnable = templateManager.startNonCustomTemplates(template2Argument, callback.getEditor(), null);
if (runnable != null) {
runnable.run();
}
return;
}
}
PsiElement context = callback.getContext();
ZenCodingGenerator generator = ObjectUtils.notNull(findApplicableGenerator(node, context, false), defaultGenerator);
List<ZenCodingFilter> filters = getFilters(node, context);
filters.addAll(extraFilters);
checkTemplateOutputLength(node, callback);
callback.deleteTemplateKey(key);
expand(node, generator, filters, null, callback, expandPrimitiveAbbreviations, segmentsLimit);
}
use of com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator in project intellij-community by JetBrains.
the class EmmetLookupActionProvider method fillActions.
@Override
public void fillActions(LookupElement element, final Lookup lookup, Consumer<LookupElementAction> consumer) {
if (element instanceof CustomLiveTemplateLookupElement && ((CustomLiveTemplateLookupElement) element).getCustomLiveTemplate() instanceof ZenCodingTemplate) {
final PsiElement context = lookup.getPsiElement();
final ZenCodingGenerator generator = context != null ? ZenCodingTemplate.findApplicableDefaultGenerator(context, false) : null;
if (generator != null) {
consumer.consume(new LookupElementAction(PlatformIcons.EDIT, "Edit Emmet settings") {
@Override
public Result performLookupAction() {
final Project project = lookup.getProject();
ApplicationManager.getApplication().invokeLater(() -> {
if (project.isDisposed())
return;
final Configurable generatorSpecificConfigurable = generator.createConfigurable();
Configurable configurable = generatorSpecificConfigurable != null ? generatorSpecificConfigurable : new XmlEmmetConfigurable();
ShowSettingsUtil.getInstance().editConfigurable(project, new EmmetCompositeConfigurable(configurable));
});
return Result.HIDE_LOOKUP;
}
});
consumer.consume(new LookupElementAction(AllIcons.Actions.Delete, "Disable Emmet") {
@Override
public Result performLookupAction() {
ApplicationManager.getApplication().invokeLater(generator::disableEmmet);
return Result.HIDE_LOOKUP;
}
});
}
}
}
use of com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator in project intellij-community by JetBrains.
the class GenerationNode method invokeXmlTemplate.
private TemplateImpl invokeXmlTemplate(final TemplateToken token, CustomTemplateCallback callback, @Nullable ZenCodingGenerator generator, final boolean hasChildren) {
ZenCodingGenerator zenCodingGenerator = ObjectUtils.notNull(generator, XmlZenCodingGeneratorImpl.INSTANCE);
Map<String, String> attributes = token.getAttributes();
TemplateImpl template = token.getTemplate();
assert template != null;
PsiFileFactory fileFactory = PsiFileFactory.getInstance(callback.getProject());
PsiFile dummyFile = fileFactory.createFileFromText("dummy.html", callback.getFile().getLanguage(), token.getTemplateText(), false, true);
XmlTag tag = PsiTreeUtil.findChildOfType(dummyFile, XmlTag.class);
if (tag != null) {
// autodetect href
if (EmmetOptions.getInstance().isHrefAutoDetectEnabled() && StringUtil.isNotEmpty(mySurroundedText)) {
final boolean isEmptyLinkTag = "a".equalsIgnoreCase(tag.getName()) && isEmptyValue(tag.getAttributeValue("href"));
if (!hasChildren && isEmptyLinkTag) {
if (HREF_PATTERN.matcher(mySurroundedText).matches()) {
attributes.put("href", PROTOCOL_PATTERN.matcher(mySurroundedText).find() ? mySurroundedText.trim() : "http://" + mySurroundedText.trim());
} else if (EMAIL_PATTERN.matcher(mySurroundedText).matches()) {
attributes.put("href", "mailto:" + mySurroundedText.trim());
}
}
}
for (Map.Entry<String, String> attribute : attributes.entrySet()) {
if (Strings.isNullOrEmpty(attribute.getValue())) {
template.addVariable(prepareVariableName(attribute.getKey()), "", "", true);
}
}
XmlTag tag1 = hasChildren ? expandEmptyTagIfNecessary(tag) : tag;
setAttributeValues(tag1, attributes, callback, zenCodingGenerator.isHtml(callback));
token.setTemplateText(tag1.getContainingFile().getText(), callback);
}
template = zenCodingGenerator.generateTemplate(token, hasChildren, callback.getContext());
removeVariablesWhichHasNoSegment(template);
return template;
}
Aggregations