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();
}
}
}
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;
}
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;
}
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);
}
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);
}
Aggregations