Search in sources :

Example 96 with CodeStyleManager

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");
}
Also used : PyStatement(com.jetbrains.python.psi.PyStatement) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) PsiElement(com.intellij.psi.PsiElement)

Example 97 with CodeStyleManager

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);
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) XmlAttribute(com.intellij.psi.xml.XmlAttribute) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project) HashSet(com.intellij.util.containers.HashSet) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)97 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)29 Project (com.intellij.openapi.project.Project)26 TextRange (com.intellij.openapi.util.TextRange)19 NonNls (org.jetbrains.annotations.NonNls)18 IncorrectOperationException (com.intellij.util.IncorrectOperationException)16 NotNull (org.jetbrains.annotations.NotNull)8 Document (com.intellij.openapi.editor.Document)6 PsiFile (com.intellij.psi.PsiFile)6 Module (com.intellij.openapi.module.Module)5 PsiElement (com.intellij.psi.PsiElement)4 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)4 Nullable (org.jetbrains.annotations.Nullable)4 CaretModel (com.intellij.openapi.editor.CaretModel)3 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)3 JavaLanguage (com.intellij.lang.java.JavaLanguage)2 FileType (com.intellij.openapi.fileTypes.FileType)2 Comparing (com.intellij.openapi.util.Comparing)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 com.intellij.psi (com.intellij.psi)2