Introduce method DocumentUtil#getIndent

added test
This commit is contained in:
Andrey Vokin
2018-10-22 23:38:57 +03:00
parent d59d564033
commit db67345437
2 changed files with 20 additions and 3 deletions
@@ -146,9 +146,9 @@ public final class DocumentUtil {
/**
* Calculates indent of the line containing {@code offset}
* @return amount of characters in indent
* @return Whitespaces at the beginning of the line
*/
public static int getIndent(@NotNull Document document, int offset) {
public static CharSequence getIndent(@NotNull Document document, int offset) {
int lineOffset = getLineStartOffset(offset, document);
int result = 0;
while (lineOffset + result < document.getTextLength() &&
@@ -158,6 +158,6 @@ public final class DocumentUtil {
if (result + lineOffset > document.getTextLength()) {
result--;
}
return Math.max(result, 0);
return document.getCharsSequence().subSequence(lineOffset, lineOffset + Math.max(result, 0));
}
}
@@ -0,0 +1,17 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.editor;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.util.DocumentUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class DocumentUtilTest {
@Test
public void testGetIndent() {
final Document doc = new DocumentImpl("line1\n" +
" \t line2");
assertEquals(" \t ", DocumentUtil.getIndent(doc, doc.getTextLength() - 1).toString());
}
}