Extracted methods to utility class

IDEA-CR-34731
This commit is contained in:
Alexey Merkulov
2018-07-23 14:52:34 +03:00
parent 1488e1c699
commit a68bf87f86
2 changed files with 61 additions and 16 deletions
@@ -59,7 +59,7 @@ public class YAMLElementGenerator {
text = keyName + ": " + valueText;
}
else {
text = keyName + ":\n" + indentText(valueText);
text = keyName + ":\n" + YAMLTextUtil.indentText(valueText, 2);
}
final PsiFile tempFile = createDummyYamlWithText(text);
@@ -105,19 +105,4 @@ public class YAMLElementGenerator {
assert at != null && at.getNode().getElementType() == YAMLTokenTypes.COLON;
return at;
}
@NotNull
private static String indentText(@NotNull String text) {
StringBuilder buffer = new StringBuilder();
String indentString = StringUtil.repeatSymbol(' ', 2);
buffer.append(indentString);
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
buffer.append(c);
if (c == '\n') {
buffer.append(indentString);
}
}
return buffer.toString();
}
}
@@ -0,0 +1,60 @@
// 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 org.jetbrains.yaml;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
/**
* This is text-level utility collection
* <p/>
* Many of this methods could be used in other plug-ins (especially for indent-based languages).
* Maybe it will be better to move this class or part of it into IDEA platform.
*/
public class YAMLTextUtil {
private YAMLTextUtil() {}
// Copy-paste from com.jetbrains.python.editor.PythonCopyPasteProcessor.getLineStartSafeOffset
/** @return start line character number or document border if specified line is outside the document range */
public static int getLineStartSafeOffset(@NotNull Document document, int line) {
if (line >= document.getLineCount()) return document.getTextLength();
if (line < 0) return 0;
return document.getLineStartOffset(line);
}
// Copy-paste from com.jetbrains.python.psi.PyIndentUtil#getLineIndentSize(CharSequence)
/** @return text first line indent size (just character number) */
public static int getStartIndentSize(@NotNull CharSequence text) {
int stop;
for (stop = 0; stop < text.length(); stop++) {
final char c = text.charAt(stop);
if (!(c == ' ' || c == '\t')) {
break;
}
}
return stop;
}
// This method is similar with com.jetbrains.python.psi.PyIndentUtil.changeIndent
/**
* This method indents each line of text by specified space number.
* Empty lines will be indented also.
*
* @return indented text
*/
@NotNull
public static String indentText(@NotNull String text, int indent) {
StringBuilder buffer = new StringBuilder();
String indentString = StringUtil.repeatSymbol(' ', indent);
buffer.append(indentString);
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
buffer.append(c);
if (c == '\n') {
buffer.append(indentString);
}
}
return buffer.toString();
}
}