fixed PY-9337 Copy paste doesn't always preserve the relative indentation of what is pasted.

improved indent calculation
This commit is contained in:
Ekaterina Tuzova
2013-04-01 14:10:27 +04:00
parent b16f001f8f
commit ae3e43830c
8 changed files with 81 additions and 3 deletions

View File

@@ -64,7 +64,8 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor {
final int lineStartOffset = getLineStartSafeOffset(document, lineNumber);
text = addLeadingSpaces(text, NOT_INDENT_FILTER, indentSize, indentChar);
final String indentText = getIndentText(file, document, caretOffset, lineNumber);
int firstLineIndent = StringUtil.findFirst(text, NOT_INDENT_FILTER);
final String indentText = getIndentText(file, document, caretOffset, lineNumber, firstLineIndent);
int toRemove = calculateIndentToRemove(text, NOT_INDENT_FILTER);
if (StringUtil.isEmptyOrSpaces(indentText) && isApplicable(file, text, caretOffset)) {
@@ -105,7 +106,7 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor {
private static String getIndentText(@NotNull final PsiFile file,
@NotNull final Document document,
int caretOffset,
int lineNumber) {
int lineNumber, int firstLineIndent) {
PsiElement nonWS = PyUtil.findNextAtOffset(file, caretOffset, PsiWhiteSpace.class);
if (nonWS != null) {
@@ -132,6 +133,8 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor {
indentText = strings.get(0);
}
}
if (indentText.length() == firstLineIndent)
return "";
}
return indentText;
}
@@ -141,7 +144,7 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor {
int minIndent = StringUtil.findFirst(text, filter);
for (String s : strings) {
final int indent = StringUtil.findFirst(s, filter);
if (indent < minIndent)
if (indent < minIndent && !StringUtil.isEmptyOrSpaces(s))
minIndent = indent;
}
return minIndent;

View File

@@ -0,0 +1,16 @@
# original function # this is line 1 of the code.
def foo():
print 'f00'
def bar(num):
for _ in range(num):
print 'bar'
bar(7)
def bar(num):
for _ in range(num):
print 'bar'
bar(7)

View File

@@ -0,0 +1,10 @@
# original function # this is line 1 of the code.
def foo():
print 'f00'
def bar(num):
for _ in range(num):
print 'bar'
bar(7)
<caret> <selection></selection>

View File

@@ -0,0 +1,7 @@
# original function # this is line 1 of the code.
def foo():
print 'f00'
<selection>def bar(num):
for _ in range(num):
print 'bar'
bar(7)</selection><caret>

View File

@@ -0,0 +1,16 @@
# original function # this is line 1 of the code.
def foo():
print 'f00'
def bar(num):
for _ in range(num):
print 'bar'
bar(7)
def bar(num):
for _ in range(num):
print 'bar'
bar(7)

View File

@@ -0,0 +1,10 @@
# original function # this is line 1 of the code.
def foo():
print 'f00'
def bar(num):
for _ in range(num):
print 'bar'
bar(7)
<caret> <selection></selection>

View File

@@ -0,0 +1,8 @@
# original function # this is line 1 of the code.
def foo():
print 'f00'
<selection>def bar(num):
for _ in range(num):
print 'bar'
bar(7)</selection><caret>

View File

@@ -301,6 +301,14 @@ public class PyCopyPasteTest extends PyTestCase {
doTest();
}
public void testEmptyLineInList() {
doTest();
}
public void testCaretAtTheBeginningOfIndent() {
doTest();
}
private void doTestTabs() {
final CommonCodeStyleSettings.IndentOptions indentOptions =
CodeStyleSettingsManager.getSettings(myFixture.getProject()).getIndentOptions(PythonFileType.INSTANCE);