diff --git a/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java index 97598a2cdc54..8581a67353b1 100644 --- a/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java +++ b/python/src/com/jetbrains/python/editor/PythonCopyPasteProcessor.java @@ -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 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 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); diff --git a/python/src/com/jetbrains/python/psi/PyIndentUtil.java b/python/src/com/jetbrains/python/psi/PyIndentUtil.java index 219ac86178ba..f09a059b67c8 100644 --- a/python/src/com/jetbrains/python/psi/PyIndentUtil.java +++ b/python/src/com/jetbrains/python/psi/PyIndentUtil.java @@ -154,13 +154,13 @@ public class PyIndentUtil { @NotNull public static String removeCommonIndent(@NotNull String s, boolean ignoreFirstLine) { - final List trimmed = removeCommonIndent(LineTokenizer.tokenizeIntoList(s, false), ignoreFirstLine); + final List 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 trimmed = changeIndent(LineTokenizer.tokenizeIntoList(s, false), ignoreFirstLine, newIndent); + final List trimmed = changeIndent(LineTokenizer.tokenizeIntoList(s, false, false), ignoreFirstLine, newIndent); return StringUtil.join(trimmed, "\n"); }