Search in sources :

Example 1 with TemplateImpl

use of com.intellij.codeInsight.template.impl.TemplateImpl 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 TemplateImpl

use of com.intellij.codeInsight.template.impl.TemplateImpl in project intellij-community by JetBrains.

the class XmlEmmetParser method parseTemplate.

@Nullable
@Override
protected ZenCodingNode parseTemplate() {
    ZenCodingToken token = getToken();
    String templateKey = getDefaultTemplateKey();
    boolean mustHaveSelector = true;
    if (token instanceof IdentifierToken) {
        templateKey = ((IdentifierToken) token).getText();
        advance();
        if (startsWithIgnoreCase(templateKey, LOREM_KEYWORD) || startsWithIgnoreCase(templateKey, LIPSUM_KEYWORD)) {
            return parseLorem(templateKey);
        }
        mustHaveSelector = false;
    }
    if (templateKey == null) {
        return null;
    }
    TemplateImpl template = myCallback.findApplicableTemplate(templateKey);
    if (template == null && !ZenCodingUtil.isXML11ValidQName(templateKey) && !StringUtil.containsChar(templateKey, '$')) {
        return null;
    }
    final Map<String, String> attributes = parseSelectors();
    if (mustHaveSelector && attributes.isEmpty()) {
        return null;
    }
    final TemplateToken templateToken = new TemplateToken(templateKey, attributes);
    if (!setTemplate(templateToken, template)) {
        return null;
    }
    return new TemplateNode(templateToken, myGenerator);
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with TemplateImpl

use of com.intellij.codeInsight.template.impl.TemplateImpl in project intellij-community by JetBrains.

the class XmlEmmetParser method parseLorem.

@Nullable
private ZenCodingNode parseLorem(String templateKey) {
    Matcher matcher = LOREM_PATTERN.matcher(templateKey);
    if (matcher.matches()) {
        int loremWordsCount = DEFAULT_LOREM_LENGTH;
        if (matcher.groupCount() > 1) {
            String group = matcher.group(2);
            loremWordsCount = group == null || group.isEmpty() ? DEFAULT_LOREM_LENGTH : Integer.parseInt(group);
        }
        final Map<String, String> attributes = parseSelectors();
        ZenCodingToken token = getToken();
        boolean isRepeating = token instanceof OperationToken && ((OperationToken) token).getSign() == '*';
        if (!attributes.isEmpty() || isRepeating) {
            String wrapTag = suggestTagName();
            TemplateImpl template = myCallback.findApplicableTemplate(templateKey);
            if (template == null && !ZenCodingUtil.isXML11ValidQName(templateKey)) {
                return null;
            }
            final TemplateToken templateToken = new TemplateToken(wrapTag, attributes);
            if (!setTemplate(templateToken, template)) {
                return null;
            }
            return new MoreOperationNode(new TemplateNode(templateToken), new LoremNode(loremWordsCount));
        } else {
            return new LoremNode(loremWordsCount);
        }
    } else {
        return null;
    }
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) Matcher(java.util.regex.Matcher) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with TemplateImpl

use of com.intellij.codeInsight.template.impl.TemplateImpl 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 5 with TemplateImpl

use of com.intellij.codeInsight.template.impl.TemplateImpl in project intellij-community by JetBrains.

the class TemplateNode method expand.

@NotNull
@Override
public List<GenerationNode> expand(int numberInIteration, int totalIterations, String surroundedText, CustomTemplateCallback callback, boolean insertSurroundedTextAtTheEnd, GenerationNode parent) {
    TemplateToken templateToken = myTemplateToken;
    String templateKey = templateToken.getKey();
    if (myGenerator != null && StringUtil.containsChar(templateKey, '$') && callback.findApplicableTemplate(templateKey) == null) {
        String newTemplateKey = ZenCodingUtil.replaceMarkers(templateKey, numberInIteration, totalIterations, surroundedText);
        TemplateToken newTemplateToken = new TemplateToken(newTemplateKey, templateToken.getAttributes());
        TemplateImpl template = myGenerator.createTemplateByKey(newTemplateKey);
        if (template != null) {
            template.setDeactivated(true);
            newTemplateToken.setTemplate(template, callback);
            templateToken = newTemplateToken;
        }
    }
    GenerationNode node = new GenerationNode(templateToken, numberInIteration, totalIterations, surroundedText, insertSurroundedTextAtTheEnd, parent);
    return Collections.singletonList(node);
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) TemplateToken(com.intellij.codeInsight.template.emmet.tokens.TemplateToken) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)22 NotNull (org.jetbrains.annotations.NotNull)6 Nullable (org.jetbrains.annotations.Nullable)6 TemplateToken (com.intellij.codeInsight.template.emmet.tokens.TemplateToken)4 XmlZenCodingGenerator (com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator)3 Editor (com.intellij.openapi.editor.Editor)3 ZenCodingGenerator (com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator)2 InvokeTemplateAction (com.intellij.codeInsight.template.impl.InvokeTemplateAction)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 Matcher (java.util.regex.Matcher)2 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)1 ExpressionLookupItem (com.intellij.codeInsight.lookup.ExpressionLookupItem)1 CustomLiveTemplate (com.intellij.codeInsight.template.CustomLiveTemplate)1 LiveTemplateBuilder (com.intellij.codeInsight.template.LiveTemplateBuilder)1 SmartCompletionContextType (com.intellij.codeInsight.template.SmartCompletionContextType)1 SingleLineEmmetFilter (com.intellij.codeInsight.template.emmet.filters.SingleLineEmmetFilter)1 ZenCodingFilter (com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter)1 TemplateSettings (com.intellij.codeInsight.template.impl.TemplateSettings)1