use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class PyFormatterTest method testReformatOfSingleElementPossible.
/**
* This test merely checks that call to {@link com.intellij.psi.codeStyle.CodeStyleManager#reformat(com.intellij.psi.PsiElement)}
* is possible for Python sources.
*/
public void testReformatOfSingleElementPossible() {
myFixture.configureByFile("formatter/" + getTestName(true) + ".py");
WriteCommandAction.runWriteCommandAction(myFixture.getProject(), () -> {
final PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
assertNotNull(elementAtCaret);
final PyStatement statement = PsiTreeUtil.getParentOfType(elementAtCaret, PyStatement.class, false);
assertNotNull(statement);
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myFixture.getProject());
codeStyleManager.reformat(statement);
});
myFixture.checkResultByFile("formatter/" + getTestName(true) + "_after.py");
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project android by JetBrains.
the class AndroidExtractAsIncludeAction method doRefactor.
private static void doRefactor(AndroidFacet facet, PsiFile file, XmlFile newFile, PsiElement from, PsiElement to, XmlTag parentTag, boolean wrapWithMerge) {
final Project project = facet.getModule().getProject();
final String textToExtract = file.getText().substring(from.getTextRange().getStartOffset(), to.getTextRange().getEndOffset());
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final Document document = documentManager.getDocument(newFile);
assert document != null;
document.setText("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + (wrapWithMerge ? "<merge>\n" + textToExtract + "\n</merge>" : textToExtract));
documentManager.commitDocument(document);
final Set<String> unknownPrefixes = new HashSet<String>();
newFile.accept(new XmlRecursiveElementVisitor() {
@Override
public void visitXmlTag(XmlTag tag) {
super.visitXmlTag(tag);
final String prefix = tag.getNamespacePrefix();
if (!unknownPrefixes.contains(prefix) && tag.getNamespace().length() == 0) {
unknownPrefixes.add(prefix);
}
}
@Override
public void visitXmlAttribute(XmlAttribute attribute) {
final String prefix = attribute.getNamespacePrefix();
if (!unknownPrefixes.contains(prefix) && attribute.getNamespace().length() == 0) {
unknownPrefixes.add(prefix);
}
}
});
final XmlTag rootTag = newFile.getRootTag();
assert rootTag != null;
final XmlElementFactory elementFactory = XmlElementFactory.getInstance(project);
final XmlAttribute[] attributes = rootTag.getAttributes();
final XmlAttribute firstAttribute = attributes.length > 0 ? attributes[0] : null;
for (String prefix : unknownPrefixes) {
final String namespace = parentTag.getNamespaceByPrefix(prefix);
final String xmlNsAttrName = "xmlns:" + prefix;
if (namespace.length() > 0 && rootTag.getAttribute(xmlNsAttrName) == null) {
final XmlAttribute xmlnsAttr = elementFactory.createXmlAttribute(xmlNsAttrName, namespace);
if (firstAttribute != null) {
rootTag.addBefore(xmlnsAttr, firstAttribute);
} else {
rootTag.add(xmlnsAttr);
}
}
}
String includingLayout = SdkConstants.LAYOUT_RESOURCE_PREFIX + ResourceHelper.getResourceName(file);
IncludeReference.setIncludingLayout(project, newFile, includingLayout);
final String resourceName = AndroidCommonUtils.getResourceName(ResourceType.LAYOUT.getName(), newFile.getName());
final XmlTag includeTag = elementFactory.createTagFromText("<include layout=\"@layout/" + resourceName + "\"/>");
parentTag.addAfter(includeTag, to);
parentTag.deleteChildRange(from, to);
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
codeStyleManager.reformat(newFile);
}
Aggregations