PY-18522 Reuse methods from PyIndentUtil in PythonCopyPasteProcessor

This commit is contained in:
Mikhail Golubev
2016-04-05 17:04:17 +03:00
parent 3d6d284dab
commit 793a8a4b77
2 changed files with 4 additions and 20 deletions
@@ -91,8 +91,6 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor {
text = addLeadingSpaces(text, NOT_INDENT_FILTER, indentSize, indentChar);
final String indentText = getIndentText(file, document, caretOffset, lineNumber);
int toRemove = calculateIndentToRemove(text, NOT_INDENT_FILTER);
final String line = document.getText(TextRange.create(lineStartOffset, lineEndOffset));
if (StringUtil.isEmptyOrSpaces(indentText) && shouldPasteOnPreviousLine(file, text, caretOffset)) {
caretModel.moveToOffset(lineStartOffset);
@@ -108,12 +106,9 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor {
}
}
final List<String> strings = StringUtil.split(text, "\n", false);
String newText = "";
String newText;
if (StringUtil.isEmptyOrSpaces(indentText)) {
for (String s : strings) {
newText += indentText + StringUtil.trimStart(s, StringUtil.repeat(indentChar, toRemove));
}
newText = PyIndentUtil.changeIndent(text, false, indentText);
}
else {
newText = text;
@@ -177,17 +172,6 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor {
return document.getText(TextRange.create(lineStartOffset, caretOffset));
}
private static int calculateIndentToRemove(@NotNull String text, @NotNull final CharFilter filter) {
final List<String> strings = StringUtil.split(text, "\n", false);
int minIndent = StringUtil.findFirst(text, filter);
for (String s : strings) {
final int indent = StringUtil.findFirst(s, filter);
if (indent < minIndent && !StringUtil.isEmptyOrSpaces(s))
minIndent = indent;
}
return minIndent >= 0 ? minIndent : 0;
}
private static boolean shouldPasteOnPreviousLine(@NotNull final PsiFile file, @NotNull String text, int caretOffset) {
final boolean useTabs = CodeStyleSettingsManager.getSettings(file.getProject()).useTabCharacter(PythonFileType.INSTANCE);
final PsiElement nonWS = PyUtil.findNextAtOffset(file, caretOffset, PsiWhiteSpace.class);
@@ -154,13 +154,13 @@ public class PyIndentUtil {
@NotNull
public static String removeCommonIndent(@NotNull String s, boolean ignoreFirstLine) {
final List<String> trimmed = removeCommonIndent(LineTokenizer.tokenizeIntoList(s, false), ignoreFirstLine);
final List<String> trimmed = removeCommonIndent(LineTokenizer.tokenizeIntoList(s, false, false), ignoreFirstLine);
return StringUtil.join(trimmed, "\n");
}
@NotNull
public static String changeIndent(@NotNull String s, boolean ignoreFirstLine, String newIndent) {
final List<String> trimmed = changeIndent(LineTokenizer.tokenizeIntoList(s, false), ignoreFirstLine, newIndent);
final List<String> trimmed = changeIndent(LineTokenizer.tokenizeIntoList(s, false, false), ignoreFirstLine, newIndent);
return StringUtil.join(trimmed, "\n");
}