Search in sources :

Example 31 with CodeStyleManager

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

the class InjectionsFormattingTest method doReformat.

private void doReformat(@NotNull PsiElement file) {
    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
    WriteCommandAction.runWriteCommandAction(getProject(), () -> {
        codeStyleManager.reformat(file);
    });
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Example 32 with CodeStyleManager

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

the class GenerateAntTest method checkBuildsEqual.

private void checkBuildsEqual(String generated, String expected) throws IncorrectOperationException {
    final CodeStyleManager manager = CodeStyleManager.getInstance(myProject);
    XmlTag genTag = XmlElementFactory.getInstance(myProject).createTagFromText(StringUtil.convertLineSeparators(generated));
    XmlTag expTag = XmlElementFactory.getInstance(myProject).createTagFromText(StringUtil.convertLineSeparators(expected));
    if (!tagsEqual(genTag, expTag)) {
        genTag = (XmlTag) manager.reformat(manager.reformat(genTag));
        expTag = (XmlTag) manager.reformat(manager.reformat(expTag));
        assertEquals("Text mismatch: ", expTag.getText(), genTag.getText());
    }
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) XmlTag(com.intellij.psi.xml.XmlTag)

Example 33 with CodeStyleManager

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

the class CommentUtil method getMinLineIndent.

public static Indent getMinLineIndent(Project project, Document document, int line1, int line2, FileType fileType) {
    CharSequence chars = document.getCharsSequence();
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    Indent minIndent = null;
    for (int line = line1; line <= line2; line++) {
        int lineStart = document.getLineStartOffset(line);
        int textStart = CharArrayUtil.shiftForward(chars, lineStart, " \t");
        if (textStart >= document.getTextLength()) {
            textStart = document.getTextLength();
        } else {
            char c = chars.charAt(textStart);
            // empty line
            if (c == '\n' || c == '\r')
                continue;
        }
        String space = chars.subSequence(lineStart, textStart).toString();
        Indent indent = codeStyleManager.getIndent(space, fileType);
        minIndent = minIndent != null ? indent.min(minIndent) : indent;
    }
    if (minIndent == null && line1 == line2 && line1 < document.getLineCount() - 1) {
        return getMinLineIndent(project, document, line1 + 1, line1 + 1, fileType);
    }
    //}
    return minIndent;
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) Indent(com.intellij.psi.codeStyle.Indent)

Example 34 with CodeStyleManager

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

the class MoverWrapper method indentLinesIn.

private static void indentLinesIn(final Editor editor, final PsiFile file, final Document document, final Project project, RangeMarker range) {
    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    int line1 = editor.offsetToLogicalPosition(range.getStartOffset()).line;
    int line2 = editor.offsetToLogicalPosition(range.getEndOffset()).line;
    while (!lineContainsNonSpaces(document, line1) && line1 < line2) line1++;
    while (!lineContainsNonSpaces(document, line2) && line1 < line2) line2--;
    final FileViewProvider provider = file.getViewProvider();
    PsiFile rootToAdjustIndentIn = provider.getPsi(provider.getBaseLanguage());
    codeStyleManager.adjustLineIndent(rootToAdjustIndentIn, new TextRange(document.getLineStartOffset(line1), document.getLineStartOffset(line2)));
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) FileViewProvider(com.intellij.psi.FileViewProvider) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange)

Example 35 with CodeStyleManager

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

the class FixDocCommentAction method generateComment.

/**
   * Generates a comment if possible.
   * <p/>
   * It's assumed that this method {@link PsiDocumentManager#commitDocument(Document) syncs} all PSI-document
   * changes during the processing.
   * 
   * @param anchor      target element for which a comment should be generated
   * @param editor      target editor
   * @param commenter   commenter to use
   * @param project     current project
   */
private static void generateComment(@NotNull PsiElement anchor, @NotNull Editor editor, @NotNull CodeDocumentationProvider documentationProvider, @NotNull CodeDocumentationAwareCommenter commenter, @NotNull Project project) {
    Document document = editor.getDocument();
    int commentStartOffset = anchor.getTextRange().getStartOffset();
    int lineStartOffset = document.getLineStartOffset(document.getLineNumber(commentStartOffset));
    if (lineStartOffset > 0 && lineStartOffset < commentStartOffset) {
        // Example:
        //    void test1() {
        //    }
        //    void test2() {
        //       <offset>
        //    }
        // We want to insert the comment at the start of the line where 'test2()' is declared.
        int nonWhiteSpaceOffset = CharArrayUtil.shiftBackward(document.getCharsSequence(), commentStartOffset - 1, " \t");
        commentStartOffset = Math.max(nonWhiteSpaceOffset, lineStartOffset);
    }
    int commentBodyRelativeOffset = 0;
    int caretOffsetToSet = -1;
    StringBuilder buffer = new StringBuilder();
    String commentPrefix = commenter.getDocumentationCommentPrefix();
    if (commentPrefix != null) {
        buffer.append(commentPrefix).append("\n");
        commentBodyRelativeOffset += commentPrefix.length() + 1;
    }
    String linePrefix = commenter.getDocumentationCommentLinePrefix();
    if (linePrefix != null) {
        buffer.append(linePrefix);
        commentBodyRelativeOffset += linePrefix.length();
        caretOffsetToSet = commentStartOffset + commentBodyRelativeOffset;
    }
    buffer.append("\n");
    commentBodyRelativeOffset++;
    String commentSuffix = commenter.getDocumentationCommentSuffix();
    if (commentSuffix != null) {
        buffer.append(commentSuffix).append("\n");
    }
    if (buffer.length() <= 0) {
        return;
    }
    document.insertString(commentStartOffset, buffer);
    PsiDocumentManager docManager = PsiDocumentManager.getInstance(project);
    docManager.commitDocument(document);
    Pair<PsiElement, PsiComment> pair = documentationProvider.parseContext(anchor);
    if (pair == null || pair.second == null) {
        return;
    }
    String stub = documentationProvider.generateDocumentationContentStub(pair.second);
    CaretModel caretModel = editor.getCaretModel();
    if (stub != null) {
        int insertionOffset = commentStartOffset + commentBodyRelativeOffset;
        //if (CodeStyleSettingsManager.getSettings(project).JD_ADD_BLANK_AFTER_DESCRIPTION) {
        //  buffer.setLength(0);
        //  if (linePrefix != null) {
        //    buffer.append(linePrefix);
        //  }
        //  buffer.append("\n");
        //  buffer.append(stub);
        //  stub = buffer.toString();
        //}
        document.insertString(insertionOffset, stub);
        docManager.commitDocument(document);
        pair = documentationProvider.parseContext(anchor);
    }
    if (caretOffsetToSet >= 0) {
        caretModel.moveToOffset(caretOffsetToSet);
    }
    if (pair == null || pair.second == null) {
        return;
    }
    int start = Math.min(calcStartReformatOffset(pair.first), calcStartReformatOffset(pair.second));
    int end = pair.second.getTextRange().getEndOffset();
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    codeStyleManager.reformatText(anchor.getContainingFile(), start, end);
    int caretOffset = caretModel.getOffset();
    if (caretOffset > 0 && caretOffset <= document.getTextLength()) {
        char c = document.getCharsSequence().charAt(caretOffset - 1);
        if (!StringUtil.isWhiteSpace(c)) {
            document.insertString(caretOffset, " ");
            caretModel.moveToOffset(caretOffset + 1);
        }
    }
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) CaretModel(com.intellij.openapi.editor.CaretModel) Document(com.intellij.openapi.editor.Document)

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