fixed PY-6907 Paste leads to syntactically incorrect code in case of dedent in copied text

This commit is contained in:
Ekaterina Tuzova
2012-07-11 18:45:11 +04:00
parent 05e4fb3e28
commit 4775d2a6f5
5 changed files with 37 additions and 0 deletions

View File

@@ -42,6 +42,23 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor {
final Document document = editor.getDocument();
String newText = text;
//a lot of different indents inside selected
final String[] split = StringUtil.trimTrailing(text).split("\n");
int firstIndent = 0;
int currentIndent;
for (String s : split) {
currentIndent = s.length() - s.trim().length();
if (split[0].equals(s) ) {
firstIndent = currentIndent;
}
if (!StringUtil.isEmptyOrSpaces(s) && firstIndent > currentIndent) {
if (caretModel.getLogicalPosition().column > 0)
return StringUtil.trimLeading(text);
else
return text;
}
}
if (file instanceof PyFile && (StringUtil.startsWithWhitespace(text) || StringUtil.endsWithLineBreak(text) ||
StringUtil.splitByLines(text).length > 1)) {
if (text.endsWith("\n")) text = text.substring(0, text.length() - 1);

View File

@@ -0,0 +1,6 @@
def f():
if True:
a = 1
print(0.1)
else:
print(0.1)

View File

@@ -0,0 +1,4 @@
def f():
if True:
a = 1
<caret>

View File

@@ -0,0 +1,6 @@
def f():
if False:
<selection> print(0.1)
else:
print(0.1)
</selection>

View File

@@ -167,6 +167,10 @@ public class PyCopyPasteTest extends PyTestCase {
doTestMultiLine();
}
public void testIndentIfElse() { //PY-6907
doTestMultiLine();
}
public void testIndentOnTopLevel() { //PY-6928
doTestSingleLine();
}