Search in sources :

Example 1 with CodeFormatterFacade

use of com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade in project intellij-community by JetBrains.

the class ParagraphFillHandler method performOnElement.

protected void performOnElement(@NotNull final PsiElement element, @NotNull final Editor editor) {
    final Document document = editor.getDocument();
    final TextRange textRange = getTextRange(element, editor);
    if (textRange.isEmpty())
        return;
    final String text = textRange.substring(element.getContainingFile().getText());
    final List<String> subStrings = StringUtil.split(text, "\n", true);
    final String prefix = getPrefix(element);
    final String postfix = getPostfix(element);
    final StringBuilder stringBuilder = new StringBuilder();
    appendPrefix(element, text, stringBuilder);
    for (String string : subStrings) {
        final String startTrimmed = StringUtil.trimStart(string.trim(), prefix.trim());
        final String str = StringUtil.trimEnd(startTrimmed, postfix.trim());
        final String finalString = str.trim();
        if (!StringUtil.isEmptyOrSpaces(finalString))
            stringBuilder.append(finalString).append(" ");
    }
    appendPostfix(element, text, stringBuilder);
    final String replacementText = stringBuilder.toString();
    CommandProcessor.getInstance().executeCommand(element.getProject(), () -> {
        document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), replacementText);
        final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(CodeStyleSettingsManager.getSettings(element.getProject()), element.getLanguage());
        final PsiFile file = element.getContainingFile();
        FormatterTagHandler formatterTagHandler = new FormatterTagHandler(CodeStyleSettingsManager.getSettings(file.getProject()));
        List<TextRange> enabledRanges = formatterTagHandler.getEnabledRanges(file.getNode(), TextRange.create(0, document.getTextLength()));
        codeFormatter.doWrapLongLinesIfNecessary(editor, element.getProject(), document, textRange.getStartOffset(), textRange.getStartOffset() + replacementText.length() + 1, enabledRanges);
    }, null, document);
}
Also used : FormatterTagHandler(com.intellij.formatting.FormatterTagHandler) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) CodeFormatterFacade(com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade) Document(com.intellij.openapi.editor.Document)

Example 2 with CodeFormatterFacade

use of com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade in project intellij-community by JetBrains.

the class FileSetFormatter method reformatFile.

private void reformatFile(@NotNull Project project, @NotNull final PsiFile file, @NotNull Document document) {
    WriteCommandAction.runWriteCommandAction(project, () -> {
        CodeFormatterFacade formatterFacade = new CodeFormatterFacade(mySettings, file.getLanguage());
        formatterFacade.processText(file, new FormatTextRanges(new TextRange(0, file.getTextLength()), true), false);
        PsiDocumentManager.getInstance(project).commitDocument(document);
    });
}
Also used : TextRange(com.intellij.openapi.util.TextRange) CodeFormatterFacade(com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade) FormatTextRanges(com.intellij.formatting.FormatTextRanges)

Example 3 with CodeFormatterFacade

use of com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade in project intellij-community by JetBrains.

the class PostprocessReformattingAspect method getFormatterFacade.

@NotNull
private CodeFormatterFacade getFormatterFacade(@NotNull FileViewProvider viewProvider) {
    final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(myPsiManager.getProject());
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myPsiManager.getProject());
    final Document document = viewProvider.getDocument();
    assert document != null;
    final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(styleSettings, viewProvider.getBaseLanguage());
    documentManager.commitDocument(document);
    return codeFormatter;
}
Also used : CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) CodeFormatterFacade(com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with CodeFormatterFacade

use of com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade in project intellij-community by JetBrains.

the class JavaExtractor method getDiffer.

@NotNull
@Override
public Differ getDiffer(final Project project, PsiFile psiFile, CodeStyleSettings settings) {
    return new DifferBase(project, psiFile, settings) {

        @Override
        public String reformattedText() {
            JavaCodeFragment file = JavaCodeFragmentFactory.getInstance(project).createCodeBlockCodeFragment(myOrigText, myFile, false);
            WriteCommandAction.runWriteCommandAction(myProject, "CodeStyleSettings extractor", "CodeStyleSettings extractor", () -> {
                ASTNode treeElement = SourceTreeToPsiMap.psiToTreeNotNull(file);
                SourceTreeToPsiMap.treeElementToPsi(new CodeFormatterFacade(mySettings, file.getLanguage()).processElement(treeElement));
            }, file);
            return file.getText();
        }
    };
}
Also used : ASTNode(com.intellij.lang.ASTNode) CodeFormatterFacade(com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade) JavaCodeFragment(com.intellij.psi.JavaCodeFragment) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CodeFormatterFacade (com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade)4 Document (com.intellij.openapi.editor.Document)2 TextRange (com.intellij.openapi.util.TextRange)2 NotNull (org.jetbrains.annotations.NotNull)2 FormatTextRanges (com.intellij.formatting.FormatTextRanges)1 FormatterTagHandler (com.intellij.formatting.FormatterTagHandler)1 ASTNode (com.intellij.lang.ASTNode)1 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)1 JavaCodeFragment (com.intellij.psi.JavaCodeFragment)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1