mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-15 02:59:33 +07:00
fixed PY-9337 Copy paste doesn't always preserve the relative indentation of what is pasted.
improved indent calculation
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
10
python/testData/copyPaste/CaretAtTheBeginningOfIndent.dst.py
Normal file
10
python/testData/copyPaste/CaretAtTheBeginningOfIndent.dst.py
Normal 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>
|
||||
@@ -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>
|
||||
16
python/testData/copyPaste/EmptyLineInList.after.py
Normal file
16
python/testData/copyPaste/EmptyLineInList.after.py
Normal 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)
|
||||
10
python/testData/copyPaste/EmptyLineInList.dst.py
Normal file
10
python/testData/copyPaste/EmptyLineInList.dst.py
Normal 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>
|
||||
8
python/testData/copyPaste/EmptyLineInList.src.py
Normal file
8
python/testData/copyPaste/EmptyLineInList.src.py
Normal 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>
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user