use of com.intellij.codeInsight.actions.ReformatCodeProcessor 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.actions.ReformatCodeProcessor in project android by JetBrains.
the class AndroidXmlFormatterTest method testLayout8.
public void testLayout8() throws Exception {
final VirtualFile f = myFixture.copyFileToProject(BASE_PATH + "layout8.xml", "res/layout/layout.xml");
myFixture.configureFromExistingVirtualFile(f);
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
RearrangeCodeProcessor processor = new RearrangeCodeProcessor(new ReformatCodeProcessor(getProject(), false));
processor.run();
}
});
myFixture.checkResultByFile(BASE_PATH + "layout8_after.xml");
}
use of com.intellij.codeInsight.actions.ReformatCodeProcessor in project android by JetBrains.
the class AndroidXmlFormatterTest method testLayoutNonFirstNamespace.
// Regression test for http://b.android.com/196833
// "Android" style for XML formatting doesn't insert a newline character before the first attribute
// (even when that setting is enabled) is a namespace declaration. However, it does it only when
// namespace declaration is already first attribute in the list. Given that Android XML formatting
// by default rearranges attributes, it would lead to weird results: if namespace declaration is
// not on the first line, on first "Reformat Code" run it would but namespace first and keep the
// line break if it was already there, and on the second run it would remove line break.
public void testLayoutNonFirstNamespace() throws Exception {
final VirtualFile f = myFixture.copyFileToProject(BASE_PATH + "layout_non_first_namespace.xml", "res/layout/layout.xml");
myFixture.configureFromExistingVirtualFile(f);
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
RearrangeCodeProcessor processor = new RearrangeCodeProcessor(new ReformatCodeProcessor(getProject(), false));
processor.run();
}
});
myFixture.checkResultByFile(BASE_PATH + "layout_non_first_namespace_after.xml");
}
use of com.intellij.codeInsight.actions.ReformatCodeProcessor in project intellij-plugins by StepicOrg.
the class ReformatUtils method reformatSelectedEditor.
public static void reformatSelectedEditor(@NotNull Project project, @NotNull Document document) {
PsiDocumentManager.getInstance(project).commitAllDocuments();
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document);
if (file == null)
return;
new ReformatCodeProcessor(new OptimizeImportsProcessor(project, file), false).run();
}
Aggregations