mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Introduce method DocumentUtil#getIndent
added test
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user