Search in sources :

Example 16 with TemplateImpl

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

the class MavenUtil method runOrApplyFileTemplate.

private static void runOrApplyFileTemplate(Project project, VirtualFile file, String templateName, Properties properties, Properties conditions, boolean interactive) throws IOException {
    FileTemplateManager manager = FileTemplateManager.getInstance(project);
    FileTemplate fileTemplate = manager.getJ2eeTemplate(templateName);
    Properties allProperties = manager.getDefaultProperties();
    if (!interactive) {
        allProperties.putAll(properties);
    }
    allProperties.putAll(conditions);
    String text = fileTemplate.getText(allProperties);
    Pattern pattern = Pattern.compile("\\$\\{(.*)\\}");
    Matcher matcher = pattern.matcher(text);
    StringBuffer builder = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(builder, "\\$" + matcher.group(1).toUpperCase() + "\\$");
    }
    matcher.appendTail(builder);
    text = builder.toString();
    TemplateImpl template = (TemplateImpl) TemplateManager.getInstance(project).createTemplate("", "", text);
    for (int i = 0; i < template.getSegmentsCount(); i++) {
        if (i == template.getEndSegmentNumber())
            continue;
        String name = template.getSegmentName(i);
        String value = "\"" + properties.getProperty(name, "") + "\"";
        template.addVariable(name, value, value, true);
    }
    if (interactive) {
        OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file);
        Editor editor = FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
        editor.getDocument().setText("");
        TemplateManager.getInstance(project).startTemplate(editor, template);
    } else {
        VfsUtil.saveText(file, template.getTemplateText());
        PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
        if (psiFile != null) {
            new ReformatCodeProcessor(project, psiFile, null, false).run();
        }
    }
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) SystemProperties(com.intellij.util.SystemProperties) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor)

Example 17 with TemplateImpl

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

Example 18 with TemplateImpl

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

the class GenerationNode method invokeTemplate.

private static TemplateImpl invokeTemplate(@NotNull TemplateToken token, boolean hasChildren, final CustomTemplateCallback callback, @Nullable ZenCodingGenerator generator) {
    TemplateImpl template = token.getTemplate();
    if (generator != null) {
        assert template != null;
        template = generator.generateTemplate(token, hasChildren, callback.getContext());
        removeVariablesWhichHasNoSegment(template);
    }
    return template;
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl)

Example 19 with TemplateImpl

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

the class LoremNode method expand.

@NotNull
@Override
public List<GenerationNode> expand(int numberInIteration, int totalIterations, String surroundedText, CustomTemplateCallback callback, boolean insertSurroundedTextAtTheEnd, GenerationNode parent) {
    final TemplateToken templateToken = new TemplateToken("");
    final TemplateImpl template = new TemplateImpl("", myLoremGenerator.generate(myWordsCount, numberInIteration <= 0), "");
    templateToken.setTemplate(template, callback);
    final 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)

Example 20 with TemplateImpl

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

the class TextNode method expand.

@NotNull
@Override
public List<GenerationNode> expand(int numberInIteration, int totalIterations, String surroundedText, CustomTemplateCallback callback, boolean insertSurroundedTextAtTheEnd, GenerationNode parent) {
    final TemplateToken templateToken = new TemplateToken("");
    final boolean containsSurroundedTextMarker = ZenCodingUtil.containsSurroundedTextMarker(myText);
    final String text = ZenCodingUtil.replaceMarkers(myText.replace("${nl}", "\n"), numberInIteration, totalIterations, surroundedText);
    final TemplateImpl template = new TemplateImpl("", text, "");
    templateToken.setTemplate(template, callback);
    final GenerationNode node = new GenerationNode(templateToken, numberInIteration, totalIterations, containsSurroundedTextMarker ? null : 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