mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
fixed PY-4375 Join lines: delete escaping backslash when joining one string
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
1
python/testData/joinLines/StringWithSlash-after.py
Normal file
1
python/testData/joinLines/StringWithSlash-after.py
Normal file
@@ -0,0 +1 @@
|
||||
a = 'some string'
|
||||
2
python/testData/joinLines/StringWithSlash.py
Normal file
2
python/testData/joinLines/StringWithSlash.py
Normal file
@@ -0,0 +1,2 @@
|
||||
<caret>a = 'some \
|
||||
string'
|
||||
@@ -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(); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user