Search in sources :

Example 1 with ZenCodingGenerator

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;
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) XmlFile(com.intellij.psi.xml.XmlFile) XmlZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator) XmlZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator) ZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ZenCodingGenerator

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));
}
Also used : XmlZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator) ZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator) Caret(com.intellij.openapi.editor.Caret) PsiElement(com.intellij.psi.PsiElement) CaretAction(com.intellij.openapi.editor.CaretAction)

Example 3 with ZenCodingGenerator

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);
}
Also used : XmlZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator) ZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator) PsiElement(com.intellij.psi.PsiElement) ZenCodingFilter(com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter)

Example 4 with ZenCodingGenerator

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;
                }
            });
        }
    }
}
Also used : ZenCodingTemplate(com.intellij.codeInsight.template.emmet.ZenCodingTemplate) Project(com.intellij.openapi.project.Project) CustomLiveTemplateLookupElement(com.intellij.codeInsight.template.impl.CustomLiveTemplateLookupElement) LookupElementAction(com.intellij.codeInsight.lookup.LookupElementAction) EmmetCompositeConfigurable(com.intellij.application.options.emmet.EmmetCompositeConfigurable) ZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator) Configurable(com.intellij.openapi.options.Configurable) XmlEmmetConfigurable(com.intellij.application.options.emmet.XmlEmmetConfigurable) EmmetCompositeConfigurable(com.intellij.application.options.emmet.EmmetCompositeConfigurable) XmlEmmetConfigurable(com.intellij.application.options.emmet.XmlEmmetConfigurable) PsiElement(com.intellij.psi.PsiElement)

Example 5 with ZenCodingGenerator

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;
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) PsiFileFactory(com.intellij.psi.PsiFileFactory) XmlZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator) ZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator) PsiFile(com.intellij.psi.PsiFile) HashMap(com.intellij.util.containers.HashMap)

Aggregations

ZenCodingGenerator (com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator)10 XmlZenCodingGenerator (com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator)9 PsiElement (com.intellij.psi.PsiElement)6 TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)2 PsiFile (com.intellij.psi.PsiFile)2 Nullable (org.jetbrains.annotations.Nullable)2 EmmetCompositeConfigurable (com.intellij.application.options.emmet.EmmetCompositeConfigurable)1 XmlEmmetConfigurable (com.intellij.application.options.emmet.XmlEmmetConfigurable)1 LookupElementAction (com.intellij.codeInsight.lookup.LookupElementAction)1 LookupElementPresentation (com.intellij.codeInsight.lookup.LookupElementPresentation)1 ZenCodingTemplate (com.intellij.codeInsight.template.emmet.ZenCodingTemplate)1 SingleLineEmmetFilter (com.intellij.codeInsight.template.emmet.filters.SingleLineEmmetFilter)1 ZenCodingFilter (com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter)1 CustomLiveTemplateLookupElement (com.intellij.codeInsight.template.impl.CustomLiveTemplateLookupElement)1 Caret (com.intellij.openapi.editor.Caret)1 CaretAction (com.intellij.openapi.editor.CaretAction)1 Editor (com.intellij.openapi.editor.Editor)1 Configurable (com.intellij.openapi.options.Configurable)1 Project (com.intellij.openapi.project.Project)1 PsiFileFactory (com.intellij.psi.PsiFileFactory)1