refactoring
[fedora-idea.git] / platform / lang-impl / src / com / intellij / codeInsight / CommentUtil.java
blob1a7f4febdf423645955d508e10fdf5d6a28c5f64
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.codeInsight;
19 import com.intellij.psi.codeStyle.Indent;
20 import com.intellij.psi.codeStyle.CodeStyleManager;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.editor.Document;
23 import com.intellij.openapi.fileTypes.FileType;
24 import com.intellij.util.text.CharArrayUtil;
26 public class CommentUtil {
27 private CommentUtil() {
30 public static Indent getMinLineIndent(Project project, Document document, int line1, int line2, FileType fileType) {
31 CharSequence chars = document.getCharsSequence();
32 CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
33 Indent minIndent = null;
34 for (int line = line1; line <= line2; line++) {
35 int lineStart = document.getLineStartOffset(line);
36 int textStart = CharArrayUtil.shiftForward(chars, lineStart, " \t");
37 if (textStart >= document.getTextLength()) {
38 textStart = document.getTextLength();
40 else {
41 char c = chars.charAt(textStart);
42 if (c == '\n' || c == '\r') continue; // empty line
44 String space = chars.subSequence(lineStart, textStart).toString();
45 Indent indent = codeStyleManager.getIndent(space, fileType);
46 minIndent = minIndent != null ? indent.min(minIndent) : indent;
48 if (minIndent == null && line1 == line2 && line1 < document.getLineCount() - 1) {
49 return getMinLineIndent(project, document, line1 + 1, line1 + 1, fileType);
51 //if (minIndent == Integer.MAX_VALUE){
52 // minIndent = 0;
53 //}
54 return minIndent;