/* * Copyright 2000-2009 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.intellij.codeInsight; import com.intellij.openapi.editor.Document; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.project.Project; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.codeStyle.Indent; import com.intellij.util.codeInsight.CommentUtilCore; import com.intellij.util.text.CharArrayUtil; public class CommentUtil extends CommentUtilCore { private CommentUtil() { } 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; } }