Search in sources :

Example 1 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project idea-handlebars by dmarcotte.

the class HbFormatterTest method doTextTest.

/**
   * This method runs both a full-file reformat on beforeText, and a line-by-line reformat.  Though the tests
   * would output slightly better errors if these were separate tests, enforcing that they are always both run
   * for any test defined is the easiest way to ensure that the line-by-line is not messed up by formatter changes
   *
   * @param beforeText               The text run the formatter on
   * @param textAfter                The expected result after running the formatter
   * @param templateDataLanguageType The templated language of the file
   * @throws IncorrectOperationException
   */
void doTextTest(final String beforeText, String textAfter, LanguageFileType templateDataLanguageType) throws IncorrectOperationException {
    // define action to run "Reformat Code" on the whole "file" defined by beforeText
    FormatRunnableFactory fullFormatRunnableFactory = new FormatRunnableFactory() {

        @Override
        Runnable createFormatRunnable(final PsiFile file) {
            return new Runnable() {

                @Override
                public void run() {
                    try {
                        TextRange rangeToUse = file.getTextRange();
                        CodeStyleManager styleManager = CodeStyleManager.getInstance(getProject());
                        styleManager.reformatText(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
                    } catch (IncorrectOperationException e) {
                        assertTrue(e.getLocalizedMessage(), false);
                    }
                }
            };
        }
    };
    // define action to run "Adjust line indent" on every line in the "file" defined by beforeText
    FormatRunnableFactory lineFormatRunnableFactory = new FormatRunnableFactory() {

        @Override
        Runnable createFormatRunnable(final PsiFile file) {
            return new Runnable() {

                @Override
                public void run() {
                    try {
                        final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
                        final Document document = manager.getDocument(file);
                        assert document != null;
                        for (int lineNum = 0; lineNum < document.getLineCount(); lineNum++) {
                            CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
                            int offset = document.getLineStartOffset(lineNum);
                            // if this breaks at some point, we should
                            @SuppressWarnings("deprecation") boolean // instead of doing the indent directly
                            lineToBeIndented = codeStyleManager.isLineToBeIndented(file, offset);
                            if (lineToBeIndented) {
                                codeStyleManager.adjustLineIndent(file, offset);
                            }
                        }
                    } catch (IncorrectOperationException e) {
                        assertTrue(e.getLocalizedMessage(), false);
                    }
                }
            };
        }
    };
    doFormatterActionTest(fullFormatRunnableFactory, beforeText, textAfter, templateDataLanguageType);
    doFormatterActionTest(lineFormatRunnableFactory, beforeText, textAfter, templateDataLanguageType);
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 2 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project qi4j-sdk by Qi4j.

the class AbstractCreateElementActionBase method createFromTemplateInternal.

protected static PsiFile createFromTemplateInternal(@NotNull PsiDirectory directory, @NotNull String name, @NotNull String fileName, @NotNull String templateName, @NonNls String... parameters) throws IncorrectOperationException {
    // Load template
    FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance();
    FileTemplate template = fileTemplateManager.getJ2eeTemplate(templateName);
    // Process template properties
    Properties properties = new Properties(fileTemplateManager.getDefaultProperties());
    JavaTemplateUtil.setPackageNameAttribute(properties, directory);
    properties.setProperty(NAME_TEMPLATE_PROPERTY, name);
    // Add parameters
    for (int i = 0; i < parameters.length; i += 2) {
        properties.setProperty(parameters[i], parameters[i + 1]);
    }
    // Create text from template with specified properties
    String text;
    try {
        text = template.getText(properties);
    } catch (Exception e) {
        String message = "Unable to load template for " + fileTemplateManager.internalTemplateToSubject(templateName);
        throw new RuntimeException(message, e);
    }
    // Serialized text to file
    PsiManager psiManager = PsiManager.getInstance(directory.getProject());
    PsiFileFactory fileFactory = PsiFileFactory.getInstance(directory.getProject());
    PsiFile file = fileFactory.createFileFromText(fileName, text);
    // Reformat the file according to project/default style
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(psiManager);
    codeStyleManager.reformat(file);
    // Add newly created file to directory
    return (PsiFile) directory.add(file);
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) Properties(java.util.Properties) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 3 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.

the class PyStatementMover method adjustLineIndents.

private static void adjustLineIndents(@NotNull final Editor editor, @NotNull final PsiElement scope, @NotNull final Project project, @NotNull final PsiElement addedElement, int size) {
    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    final Document document = editor.getDocument();
    if (!(scope instanceof PsiFile)) {
        int line1 = editor.offsetToLogicalPosition(scope.getTextRange().getStartOffset()).line;
        int line2 = editor.offsetToLogicalPosition(scope.getTextRange().getEndOffset()).line;
        codeStyleManager.adjustLineIndent(scope.getContainingFile(), new TextRange(document.getLineStartOffset(line1), document.getLineEndOffset(line2)));
    } else {
        int line1 = editor.offsetToLogicalPosition(addedElement.getTextRange().getStartOffset()).line;
        PsiElement end = addedElement;
        while (size > 0) {
            PsiElement tmp = end.getNextSibling();
            if (tmp == null)
                break;
            size -= 1;
            end = tmp;
        }
        int endOffset = end.getTextRange().getEndOffset();
        int line2 = editor.offsetToLogicalPosition(endOffset).line;
        codeStyleManager.adjustLineIndent(scope.getContainingFile(), new TextRange(document.getLineStartOffset(line1), document.getLineEndOffset(line2)));
    }
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document)

Example 4 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.

the class PyFormatterTest method doTest.

private void doTest(final boolean reformatText) {
    myFixture.configureByFile("formatter/" + getTestName(true) + ".py");
    WriteCommandAction.runWriteCommandAction(null, () -> {
        CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myFixture.getProject());
        PsiFile file = myFixture.getFile();
        if (reformatText) {
            codeStyleManager.reformatText(file, 0, file.getTextLength());
        } else {
            codeStyleManager.reformat(file);
        }
    });
    myFixture.checkResultByFile("formatter/" + getTestName(true) + "_after.py");
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) PsiFile(com.intellij.psi.PsiFile)

Example 5 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.

the class Generator method generateBean.

// todo: inline
private static void generateBean(final PsiClass aClass, final String[] properties, final HashMap<String, String> property2fqClassName) throws MyException {
    final StringBuffer membersBuffer = new StringBuffer();
    final StringBuffer methodsBuffer = new StringBuffer();
    final CodeStyleManager formatter = CodeStyleManager.getInstance(aClass.getProject());
    final JavaCodeStyleManager styler = JavaCodeStyleManager.getInstance(aClass.getProject());
    for (final String property : properties) {
        LOG.assertTrue(property != null);
        final String type = property2fqClassName.get(property);
        LOG.assertTrue(type != null);
        generateProperty(styler, property, type, membersBuffer, methodsBuffer);
    }
    final PsiClass fakeClass;
    try {
        fakeClass = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createClassFromText(membersBuffer.toString() + methodsBuffer.toString(), null);
        final PsiField[] fields = fakeClass.getFields();
        for (final PsiField field : fields) {
            aClass.add(field);
        }
        final PsiMethod[] methods = fakeClass.getMethods();
        for (final PsiMethod method : methods) {
            aClass.add(method);
        }
        styler.shortenClassReferences(aClass);
        formatter.reformat(aClass);
    } catch (IncorrectOperationException e) {
        throw new MyException(e.getMessage());
    }
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

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