Search in sources :

Example 1 with ZenCodingFilter

use of com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter in project intellij-community by JetBrains.

the class ZenCodingTemplate method expand.

private static void expand(ZenCodingNode node, ZenCodingGenerator generator, List<ZenCodingFilter> filters, String surroundedText, CustomTemplateCallback callback, boolean expandPrimitiveAbbreviations, int segmentsLimit) throws EmmetException {
    checkTemplateOutputLength(node, callback);
    if (surroundedText != null) {
        surroundedText = surroundedText.trim();
    }
    GenerationNode fakeParentNode = new GenerationNode(TemplateToken.EMPTY_TEMPLATE_TOKEN, -1, 1, surroundedText, true, null);
    node.expand(-1, 1, surroundedText, callback, true, fakeParentNode);
    if (!expandPrimitiveAbbreviations) {
        if (isPrimitiveNode(node)) {
            return;
        }
    }
    List<GenerationNode> genNodes = fakeParentNode.getChildren();
    LiveTemplateBuilder builder = new LiveTemplateBuilder(EmmetOptions.getInstance().isAddEditPointAtTheEndOfTemplate(), segmentsLimit);
    int end = -1;
    for (int i = 0, genNodesSize = genNodes.size(); i < genNodesSize; i++) {
        GenerationNode genNode = genNodes.get(i);
        TemplateImpl template = genNode.generate(callback, generator, filters, true, segmentsLimit);
        int e = builder.insertTemplate(builder.length(), template, null);
        if (i < genNodesSize - 1 && genNode.isInsertNewLineBetweenNodes()) {
            builder.insertText(e, "\n", false);
            e++;
        }
        if (end == -1 && end < builder.length()) {
            end = e;
        }
    }
    for (ZenCodingFilter filter : filters) {
        if (filter instanceof SingleLineEmmetFilter) {
            builder.setIsToReformat(false);
            break;
        }
    }
    callback.startTemplate(builder.buildTemplate(), null, null);
}
Also used : SingleLineEmmetFilter(com.intellij.codeInsight.template.emmet.filters.SingleLineEmmetFilter) LiveTemplateBuilder(com.intellij.codeInsight.template.LiveTemplateBuilder) ZenCodingFilter(com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter)

Example 2 with ZenCodingFilter

use of com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter 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 3 with ZenCodingFilter

use of com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter in project intellij-community by JetBrains.

the class GenerationNode method generate.

@NotNull
public TemplateImpl generate(@NotNull CustomTemplateCallback callback, @Nullable ZenCodingGenerator generator, @NotNull Collection<ZenCodingFilter> filters, boolean insertSurroundedText, int segmentsLimit) {
    myContainsSurroundedTextMarker = !(insertSurroundedText && myInsertSurroundedTextAtTheEnd);
    GenerationNode generationNode = this;
    if (generationNode != this) {
        return generationNode.generate(callback, generator, Collections.emptyList(), insertSurroundedText, segmentsLimit);
    }
    boolean shouldNotReformatTemplate = false;
    boolean oneLineTemplateExpanding = false;
    for (ZenCodingFilter filter : filters) {
        generationNode = filter.filterNode(generationNode);
        if (filter instanceof SingleLineEmmetFilter) {
            shouldNotReformatTemplate = true;
            oneLineTemplateExpanding = true;
        }
    }
    CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(callback.getProject());
    String indentStr;
    if (callback.isInInjectedFragment()) {
        Editor editor = callback.getEditor();
        Document document = editor.getDocument();
        if (document instanceof DocumentWindowImpl && ((DocumentWindowImpl) document).isOneLine()) {
            /* 
         * If document is one-line that in the moment of inserting text,
         * new line chars will be filtered (see DocumentWindowImpl#insertString).
         * So in this case we should filter text by SingleLineAvoid in order to avoid
         * inconsistency of template segments.
         */
            oneLineTemplateExpanding = true;
            filters.add(new SingleLineEmmetFilter());
        }
        indentStr = "";
    } else if (settings.useTabCharacter(callback.getFileType())) {
        indentStr = "\t";
    } else {
        int tabSize = settings.getTabSize(callback.getFileType());
        indentStr = StringUtil.repeatSymbol(' ', tabSize);
    }
    LiveTemplateBuilder builder = new LiveTemplateBuilder(EmmetOptions.getInstance().isAddEditPointAtTheEndOfTemplate(), segmentsLimit);
    int end = -1;
    boolean hasChildren = myChildren.size() > 0;
    TemplateImpl parentTemplate;
    Map<String, String> predefinedValues;
    if (generator instanceof XmlZenCodingGenerator) {
        TemplateToken xmlTemplateToken = myTemplateToken;
        parentTemplate = invokeXmlTemplate(xmlTemplateToken, callback, generator, hasChildren);
        predefinedValues = buildPredefinedValues(xmlTemplateToken.getAttributes(), (XmlZenCodingGenerator) generator, hasChildren);
    } else {
        parentTemplate = invokeTemplate(myTemplateToken, hasChildren, callback, generator);
        predefinedValues = null;
    }
    String s = parentTemplate.getString();
    for (ZenCodingFilter filter : filters) {
        s = filter.filterText(s, myTemplateToken);
    }
    parentTemplate = parentTemplate.copy();
    parentTemplate.setString(s);
    final String txt = hasChildren || myContainsSurroundedTextMarker ? null : mySurroundedText;
    parentTemplate = expandTemplate(parentTemplate, predefinedValues, txt, segmentsLimit);
    int offset = builder.insertTemplate(0, parentTemplate, null);
    int newOffset = gotoChild(callback.getProject(), builder.getText(), offset, 0, builder.length());
    if (offset < builder.length() && newOffset != offset) {
        end = offset;
    }
    offset = newOffset;
    if (end == -1 && offset < builder.length() && myChildren.size() == 0) {
        end = offset;
    }
    LiveTemplateBuilder.Marker marker = offset < builder.length() ? builder.createMarker(offset) : null;
    //noinspection ForLoopReplaceableByForEach
    for (int i = 0, myChildrenSize = myChildren.size(); i < myChildrenSize; i++) {
        GenerationNode child = myChildren.get(i);
        TemplateImpl childTemplate = child.generate(callback, generator, filters, !myContainsSurroundedTextMarker, segmentsLimit);
        boolean blockTag = child.isBlockTag();
        if (!oneLineTemplateExpanding && blockTag && !isNewLineBefore(builder.getText(), offset)) {
            builder.insertText(offset, "\n" + indentStr, false);
            offset += indentStr.length() + 1;
        }
        int e = builder.insertTemplate(offset, childTemplate, null);
        offset = marker != null ? marker.getEndOffset() : builder.length();
        if (!oneLineTemplateExpanding && ((blockTag && !isNewLineAfter(builder.getText(), offset)) || myInsertNewLineBetweenNodes)) {
            builder.insertText(offset, "\n" + indentStr, false);
            offset += indentStr.length() + 1;
        }
        if (end == -1 && e < offset) {
            end = e;
        }
    }
    if (shouldNotReformatTemplate) {
        builder.setIsToReformat(false);
    }
    return builder.buildTemplate();
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) XmlZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator) SingleLineEmmetFilter(com.intellij.codeInsight.template.emmet.filters.SingleLineEmmetFilter) Document(com.intellij.openapi.editor.Document) ZenCodingFilter(com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) DocumentWindowImpl(com.intellij.injected.editor.DocumentWindowImpl) TemplateToken(com.intellij.codeInsight.template.emmet.tokens.TemplateToken) Editor(com.intellij.openapi.editor.Editor) LiveTemplateBuilder(com.intellij.codeInsight.template.LiveTemplateBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ZenCodingFilter

use of com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter in project intellij-community by JetBrains.

the class XmlEmmetConfigurable method createFiltersCheckboxes.

public void createFiltersCheckboxes() {
    final List<ZenCodingFilter> filters = ZenCodingFilter.getInstances();
    final GridBagLayout layoutManager = new GridBagLayout();
    final GridBagConstraints constraints = new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, JBUI.emptyInsets(), 0, 0);
    myFiltersListPanel.setLayout(layoutManager);
    for (int i = 0; i < filters.size(); i++) {
        ZenCodingFilter filter = filters.get(i);
        final JBCheckBox checkBox = new JBCheckBox(filter.getDisplayName());
        myFilterCheckboxes.put(filter.getSuffix(), checkBox);
        constraints.gridy = i;
        myFiltersListPanel.add(checkBox, constraints);
    }
    myFiltersListPanel.revalidate();
}
Also used : ZenCodingFilter(com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter) JBCheckBox(com.intellij.ui.components.JBCheckBox)

Example 5 with ZenCodingFilter

use of com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter in project intellij-community by JetBrains.

the class ZenCodingTemplate method getFilters.

private static List<ZenCodingFilter> getFilters(ZenCodingNode node, PsiElement context) {
    List<ZenCodingFilter> result = new ArrayList<>();
    while (node instanceof FilterNode) {
        FilterNode filterNode = (FilterNode) node;
        String filterSuffix = filterNode.getFilter();
        for (ZenCodingFilter filter : ZenCodingFilter.getInstances()) {
            if (filter.isMyContext(context) && filter.getSuffix().equals(filterSuffix)) {
                result.add(filter);
            }
        }
        node = filterNode.getNode();
    }
    for (ZenCodingFilter filter : ZenCodingFilter.getInstances()) {
        if (filter.isMyContext(context) && filter.isAppliedByDefault(context)) {
            result.add(filter);
        }
    }
    Collections.reverse(result);
    return result;
}
Also used : ZenCodingFilter(com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter)

Aggregations

ZenCodingFilter (com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter)6 LiveTemplateBuilder (com.intellij.codeInsight.template.LiveTemplateBuilder)2 SingleLineEmmetFilter (com.intellij.codeInsight.template.emmet.filters.SingleLineEmmetFilter)2 XmlZenCodingGenerator (com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator)2 JBCheckBox (com.intellij.ui.components.JBCheckBox)2 ZenCodingGenerator (com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator)1 TemplateToken (com.intellij.codeInsight.template.emmet.tokens.TemplateToken)1 TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)1 DocumentWindowImpl (com.intellij.injected.editor.DocumentWindowImpl)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 PsiElement (com.intellij.psi.PsiElement)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 NotNull (org.jetbrains.annotations.NotNull)1