use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.
the class FileTemplateTabAsList method removeSelected.
@Override
public void removeSelected() {
final FileTemplate selectedTemplate = getSelectedTemplate();
if (selectedTemplate == null) {
return;
}
final DefaultListModel model = (DefaultListModel) myList.getModel();
final int selectedIndex = myList.getSelectedIndex();
model.remove(selectedIndex);
if (!model.isEmpty()) {
myList.setSelectedIndex(Math.min(selectedIndex, model.size() - 1));
}
onTemplateSelected();
}
use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.
the class FileHeaderChecker method checkFileHeader.
static ProblemDescriptor checkFileHeader(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean onTheFly) {
TIntObjectHashMap<String> offsetToProperty = new TIntObjectHashMap<>();
FileTemplate defaultTemplate = FileTemplateManager.getInstance(file.getProject()).getDefaultTemplate(FileTemplateManager.FILE_HEADER_TEMPLATE_NAME);
Pattern pattern = FileTemplateUtil.getTemplatePattern(defaultTemplate, file.getProject(), offsetToProperty);
Matcher matcher = pattern.matcher(file.getViewProvider().getContents());
if (!matcher.matches()) {
return null;
}
PsiComment element = PsiTreeUtil.findElementOfClassAtRange(file, matcher.start(1), matcher.end(1), PsiComment.class);
if (element == null) {
return null;
}
LocalQuickFix[] fixes = createQuickFix(matcher, offsetToProperty, file.getProject(), onTheFly);
String description = InspectionsBundle.message("default.file.template.description");
return manager.createProblemDescriptor(element, description, onTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.
the class FileHeaderChecker method createQuickFix.
private static LocalQuickFix[] createQuickFix(final Matcher matcher, final TIntObjectHashMap<String> offsetToProperty, Project project, boolean onTheFly) {
final FileTemplate template = FileTemplateManager.getInstance(project).getPattern(FileTemplateManager.FILE_HEADER_TEMPLATE_NAME);
ReplaceWithFileTemplateFix replaceTemplateFix = new ReplaceWithFileTemplateFix() {
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
if (element == null)
return;
String newText;
try {
newText = template.getText(computeProperties(matcher, offsetToProperty, project)).trim();
} catch (IOException e) {
LOG.error(e);
return;
}
if (!newText.isEmpty()) {
PsiElement parent = element.getParent();
PsiFile tempFile = PsiFileFactory.getInstance(project).createFileFromText("template.java", JavaFileType.INSTANCE, newText);
for (PsiElement child : tempFile.getChildren()) {
if (child.getTextLength() > 0) {
parent.addBefore(child, element);
}
}
}
element.delete();
}
};
if (onTheFly) {
LocalQuickFix editFileTemplateFix = DefaultFileTemplateUsageInspection.createEditFileTemplateFix(template, replaceTemplateFix);
return template.isDefault() ? new LocalQuickFix[] { editFileTemplateFix } : new LocalQuickFix[] { replaceTemplateFix, editFileTemplateFix };
}
return template.isDefault() ? null : new LocalQuickFix[] { replaceTemplateFix };
}
use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.
the class CreateClassAction method buildDialog.
@Override
protected void buildDialog(final Project project, PsiDirectory directory, CreateFileFromTemplateDialog.Builder builder) {
builder.setTitle(IdeBundle.message("action.create.new.class")).addKind("Class", PlatformIcons.CLASS_ICON, JavaTemplateUtil.INTERNAL_CLASS_TEMPLATE_NAME).addKind("Interface", PlatformIcons.INTERFACE_ICON, JavaTemplateUtil.INTERNAL_INTERFACE_TEMPLATE_NAME);
if (PsiUtil.getLanguageLevel(directory).isAtLeast(LanguageLevel.JDK_1_5)) {
builder.addKind("Enum", PlatformIcons.ENUM_ICON, JavaTemplateUtil.INTERNAL_ENUM_TEMPLATE_NAME);
builder.addKind("Annotation", PlatformIcons.ANNOTATION_TYPE_ICON, JavaTemplateUtil.INTERNAL_ANNOTATION_TYPE_TEMPLATE_NAME);
}
for (FileTemplate template : FileTemplateManager.getInstance(project).getAllTemplates()) {
final JavaCreateFromTemplateHandler handler = new JavaCreateFromTemplateHandler();
if (handler.handlesTemplate(template) && JavaCreateFromTemplateHandler.canCreate(directory)) {
builder.addKind(template.getName(), JavaFileType.INSTANCE.getIcon(), template.getName());
}
}
builder.setValidator(new InputValidatorEx() {
@Override
public String getErrorText(String inputString) {
if (inputString.length() > 0 && !PsiNameHelper.getInstance(project).isQualifiedName(inputString)) {
return "This is not a valid Java qualified name";
}
return null;
}
@Override
public boolean checkInput(String inputString) {
return true;
}
@Override
public boolean canClose(String inputString) {
return !StringUtil.isEmptyOrSpaces(inputString) && getErrorText(inputString) == null;
}
});
}
use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.
the class TestIntegrationUtils method createTestMethodTemplate.
public static Template createTestMethodTemplate(@NotNull MethodKind methodKind, TestFramework descriptor, @NotNull PsiClass targetClass, @Nullable PsiClass sourceClass, @Nullable String name, boolean automatic, Set<String> existingNames) {
FileTemplateDescriptor templateDesc = methodKind.getFileTemplateDescriptor(descriptor);
String templateName = templateDesc.getFileName();
FileTemplate fileTemplate = FileTemplateManager.getInstance(targetClass.getProject()).getCodeTemplate(templateName);
Template template = TemplateManager.getInstance(targetClass.getProject()).createTemplate("", "");
String templateText;
try {
Properties properties = new Properties();
if (sourceClass != null && sourceClass.isValid()) {
properties.setProperty(FileTemplate.ATTRIBUTE_CLASS_NAME, sourceClass.getQualifiedName());
}
templateText = fileTemplate.getText(properties);
} catch (IOException e) {
LOG.warn(e);
templateText = fileTemplate.getText();
}
if (name == null)
name = methodKind.getDefaultName();
if (existingNames != null && !existingNames.add(name)) {
int idx = 1;
while (existingNames.contains(name)) {
final String newName = name + (idx++);
if (existingNames.add(newName)) {
name = newName;
break;
}
}
}
templateText = StringUtil.replace(templateText, "${BODY}\n", "");
int from = 0;
while (true) {
int index = templateText.indexOf("${NAME}", from);
if (index == -1)
break;
template.addTextSegment(templateText.substring(from, index));
if (index > 0 && !Character.isWhitespace(templateText.charAt(index - 1))) {
name = StringUtil.capitalize(name);
} else {
name = StringUtil.decapitalize(name);
}
if (from == 0) {
Expression nameExpr = new ConstantNode(name);
template.addVariable("name", nameExpr, nameExpr, !automatic);
} else {
template.addVariableSegment("name");
}
from = index + "${NAME}".length();
}
template.addTextSegment(templateText.substring(from, templateText.length()));
template.setToIndent(true);
template.setToReformat(true);
template.setToShortenLongNames(true);
return template;
}
Aggregations