fixed PY-4375 Join lines: delete escaping backslash when joining one string

This commit is contained in:
Ekaterina Tuzova
2011-11-11 23:39:12 +01:00
parent 56c1f2819e
commit 779d803d6a
4 changed files with 26 additions and 1 deletions

View File

@@ -1,12 +1,12 @@
package com.jetbrains.python.editor;
import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.PyTokenTypes;
@@ -70,6 +70,27 @@ public class PyJoinLinesHandler implements JoinRawLinesHandlerDelegate {
return cut_start + res.getCursorOffset();
}
}
// single string case PY-4375
if (request.leftElem() == request.rightElem()) {
IElementType type = request.leftElem().getNode().getElementType();
if (PyTokenTypes.SINGLE_QUOTED_STRING == type || PyTokenTypes.SINGLE_QUOTED_UNICODE == type) {
PyExpression element = request.leftExpr();
if (element == null) return CANNOT_JOIN;
String[] substrings = element.getText().split("\n");
if (substrings.length != 1) {
StringBuilder replacement = new StringBuilder();
for (String string : substrings) {
if (string.trim().endsWith("\\"))
replacement.append(string.substring(0, string.length()-1));
else
replacement.append(string);
}
document.replaceString(element.getTextOffset(), element.getTextOffset()+element.getTextLength(), replacement);
return element.getTextOffset();
}
}
}
}
return CANNOT_JOIN;
}

View File

@@ -0,0 +1 @@
a = 'some string'

View File

@@ -0,0 +1,2 @@
<caret>a = 'some \
string'

View File

@@ -62,4 +62,5 @@ public class PyJoinLinesTest extends PyTestCase {
public void testTupleRPar() { doTest(); }
public void testTwoComments() { doTest(); }
public void testTwoStatements() { doTest(); }
public void testStringWithSlash() { doTest(); }
}