Search in sources :

Example 1 with Indent

use of com.intellij.psi.codeStyle.Indent 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 2 with Indent

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

the class CommentByBlockCommentHandler method commentRange.

public void commentRange(int startOffset, int endOffset, String commentPrefix, String commentSuffix, Commenter commenter) {
    final CharSequence chars = myDocument.getCharsSequence();
    LogicalPosition caretPosition = myCaret.getLogicalPosition();
    if (startOffset == 0 || chars.charAt(startOffset - 1) == '\n') {
        if (endOffset == myDocument.getTextLength() || endOffset > 0 && chars.charAt(endOffset - 1) == '\n') {
            CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myProject);
            CommonCodeStyleSettings settings = CodeStyleSettingsManager.getSettings(myProject).getCommonSettings(myFile.getLanguage());
            String space;
            if (!settings.BLOCK_COMMENT_AT_FIRST_COLUMN) {
                final FileType fileType = myFile.getFileType();
                int line1 = myEditor.offsetToLogicalPosition(startOffset).line;
                int line2 = myEditor.offsetToLogicalPosition(endOffset - 1).line;
                Indent minIndent = CommentUtil.getMinLineIndent(myProject, myDocument, line1, line2, fileType);
                if (minIndent == null) {
                    minIndent = codeStyleManager.zeroIndent();
                }
                space = codeStyleManager.fillIndent(minIndent, fileType);
            } else {
                space = "";
            }
            final StringBuilder nestingPrefix = new StringBuilder(space).append(commentPrefix);
            if (!commentPrefix.endsWith("\n")) {
                nestingPrefix.append("\n");
            }
            final StringBuilder nestingSuffix = new StringBuilder(space);
            nestingSuffix.append(commentSuffix.startsWith("\n") ? commentSuffix.substring(1) : commentSuffix);
            nestingSuffix.append("\n");
            TextRange range = insertNestedComments(startOffset, endOffset, nestingPrefix.toString(), nestingSuffix.toString(), commenter);
            myCaret.setSelection(range.getStartOffset(), range.getEndOffset());
            LogicalPosition pos = new LogicalPosition(caretPosition.line + 1, caretPosition.column);
            myCaret.moveToLogicalPosition(pos);
            return;
        }
    }
    TextRange range = insertNestedComments(startOffset, endOffset, commentPrefix, commentSuffix, commenter);
    myCaret.setSelection(range.getStartOffset(), range.getEndOffset());
    LogicalPosition pos = new LogicalPosition(caretPosition.line, caretPosition.column + commentPrefix.length());
    myCaret.moveToLogicalPosition(pos);
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) Indent(com.intellij.psi.codeStyle.Indent) FileType(com.intellij.openapi.fileTypes.FileType) AbstractFileType(com.intellij.openapi.fileTypes.impl.AbstractFileType) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)2 Indent (com.intellij.psi.codeStyle.Indent)2 FileType (com.intellij.openapi.fileTypes.FileType)1 AbstractFileType (com.intellij.openapi.fileTypes.impl.AbstractFileType)1 CustomSyntaxTableFileType (com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)1 TextRange (com.intellij.openapi.util.TextRange)1 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)1