private Indent computeMinIndent(Editor editor, PsiFile psiFile, int line1, int line2, FileType fileType) { Document document = editor.getDocument(); Indent minIndent = CommentUtil.getMinLineIndent(myProject, document, line1, line2, fileType); if (line1 > 0) { int commentOffset = getCommentStart(editor, psiFile, line1 - 1); if (commentOffset >= 0) { int lineStart = document.getLineStartOffset(line1 - 1); String space = document.getCharsSequence().subSequence(lineStart, commentOffset).toString(); Indent indent = myCodeStyleManager.getIndent(space, fileType); minIndent = minIndent != null ? indent.min(minIndent) : indent; } } if (minIndent == null) { minIndent = myCodeStyleManager.zeroIndent(); } return minIndent; }
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); if (c == '\n' || c == '\r') continue; // empty line } 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); } //if (minIndent == Integer.MAX_VALUE){ // minIndent = 0; //} return minIndent; }
@Override public String fillIndent(Indent indent, FileType fileType) { IndentImpl indent1 = (IndentImpl)indent; int indentLevel = indent1.getIndentLevel(); int spaceCount = indent1.getSpaceCount(); if (indentLevel < 0) { spaceCount += indentLevel * getSettings().getIndentSize(fileType); indentLevel = 0; if (spaceCount < 0) { spaceCount = 0; } } else { if (spaceCount < 0) { int v = (-spaceCount + getSettings().getIndentSize(fileType) - 1) / getSettings().getIndentSize(fileType); indentLevel -= v; spaceCount += v * getSettings().getIndentSize(fileType); if (indentLevel < 0) { indentLevel = 0; } } } return IndentHelperImpl.fillIndent(myProject, fileType, indentLevel * IndentHelperImpl.INDENT_FACTOR + spaceCount); }
private Indent computeMinIndent(int line1, int line2, CharSequence chars, CodeStyleManager codeStyleManager, FileType fileType) { Indent minIndent = CommentUtil.getMinLineIndent(myProject, myDocument, line1, line2, fileType); if (line1 > 0) { int commentOffset = getCommentStart(line1 - 1); if (commentOffset >= 0) { int lineStart = myDocument.getLineStartOffset(line1 - 1); String space = chars.subSequence(lineStart, commentOffset).toString(); Indent indent = codeStyleManager.getIndent(space, fileType); minIndent = minIndent != null ? indent.min(minIndent) : indent; } } if (minIndent == null) { minIndent = codeStyleManager.zeroIndent(); } return minIndent; }
private void doIndentCommenting(final Block block) { final Document document = block.editor.getDocument(); final CharSequence chars = document.getCharsSequence(); final FileType fileType = block.psiFile.getFileType(); final Indent minIndent = computeMinIndent(block.editor, block.psiFile, block.startLine, block.endLine, fileType); DocumentUtil.executeInBulk( document, block.endLine - block.startLine > Registry.intValue("comment.by.line.bulk.lines.trigger"), new Runnable() { @Override public void run() { for (int line = block.endLine; line >= block.startLine; line--) { int lineStart = document.getLineStartOffset(line); int offset = lineStart; final StringBuilder buffer = new StringBuilder(); while (true) { String space = buffer.toString(); Indent indent = myCodeStyleManager.getIndent(space, fileType); if (indent.isGreaterThan(minIndent) || indent.equals(minIndent)) break; char c = chars.charAt(offset); if (c != ' ' && c != '\t') { String newSpace = myCodeStyleManager.fillIndent(minIndent, fileType); document.replaceString(lineStart, offset, newSpace); offset = lineStart + newSpace.length(); break; } buffer.append(c); offset++; } commentLine(block, line, offset); } } }); }
@Override public Indent getIndent(String text, FileType fileType) { int indent = IndentHelperImpl.getIndent(myProject, fileType, text, true); int indenLevel = indent / IndentHelperImpl.INDENT_FACTOR; int spaceCount = indent - indenLevel * IndentHelperImpl.INDENT_FACTOR; return new IndentImpl(getSettings(), indenLevel, spaceCount, fileType); }
private void doIndentCommenting(final Commenter commenter) { final CharSequence chars = myDocument.getCharsSequence(); final FileType fileType = myFile.getFileType(); final Indent minIndent = computeMinIndent(myStartLine, myEndLine, chars, myCodeStyleManager, fileType); DocumentUtil.executeInBulk( myDocument, myEndLine - myStartLine > Registry.intValue("comment.by.line.bulk.lines.trigger"), new Runnable() { @Override public void run() { for (int line = myEndLine; line >= myStartLine; line--) { int lineStart = myDocument.getLineStartOffset(line); int offset = lineStart; final StringBuilder buffer = new StringBuilder(); while (true) { String space = buffer.toString(); Indent indent = myCodeStyleManager.getIndent(space, fileType); if (indent.isGreaterThan(minIndent) || indent.equals(minIndent)) break; char c = chars.charAt(offset); if (c != ' ' && c != '\t') { String newSpace = myCodeStyleManager.fillIndent(minIndent, fileType); myDocument.replaceString(lineStart, offset, newSpace); offset = lineStart + newSpace.length(); break; } buffer.append(c); offset++; } commentLine(line, offset, commenter); } } }); }
@Override public Indent getIndent(String text, FileType fileType) { int indent = IndentHelperImpl.getIndent(myProject, fileType, text, true); int indentLevel = indent / IndentHelperImpl.INDENT_FACTOR; int spaceCount = indent - indentLevel * IndentHelperImpl.INDENT_FACTOR; return new IndentImpl(getSettings(), indentLevel, spaceCount, fileType); }
@Override public Indent getIndent(String text, FileType fileType) { throw new UnsupportedOperationException("com.intellij.codeInsight.actions.MockCodeStyleManager.getIndent(...)"); }
@Override public String fillIndent(Indent indent, FileType fileType) { throw new UnsupportedOperationException("com.intellij.codeInsight.actions.MockCodeStyleManager.fillIndent(...)"); }
@Override public Indent zeroIndent() { throw new UnsupportedOperationException("com.intellij.codeInsight.actions.MockCodeStyleManager.zeroIndent(...)"); }
@Override public Indent zeroIndent() { return new IndentImpl(getSettings(), 0, 0, null); }
@Override public boolean isGreaterThan(Indent indent) { return getSize() > ((IndentImpl)indent).getSize(); }
@Override public Indent min(Indent anotherIndent) { return isGreaterThan(anotherIndent) ? anotherIndent : this; }
@Override public Indent max(Indent anotherIndent) { return isGreaterThan(anotherIndent) ? this : anotherIndent; }
@Override public Indent add(Indent indent) { IndentImpl indent1 = (IndentImpl)indent; return new IndentImpl(mySettings, myIndentLevel + indent1.myIndentLevel, mySpaceCount + indent1.mySpaceCount, myFileType); }
@Override public Indent subtract(Indent indent) { IndentImpl indent1 = (IndentImpl)indent; return new IndentImpl(mySettings, myIndentLevel - indent1.myIndentLevel, mySpaceCount - indent1.mySpaceCount, myFileType); }
@Override public Indent getIndent(String text, FileType fileType) { return delegate.getIndent(text, fileType); }
@Override public String fillIndent(Indent indent, FileType fileType) { return delegate.fillIndent(indent, fileType); }
@Override public Indent zeroIndent() { return delegate.zeroIndent(); }
@Override public Indent getIndent(String text, FileType fileType) { return original.getIndent(text, fileType); }
@Override public String fillIndent(Indent indent, FileType fileType) { return original.fillIndent(indent, fileType); }
@Override public Indent zeroIndent() { return original.zeroIndent(); }