mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
String joiner fixes, tests.
This commit is contained in:
@@ -55,7 +55,7 @@ public class PyJoinLinesHandler implements JoinRawLinesHandlerDelegate {
|
||||
|
||||
Joiner[] joiners = { // these are featherweight, will create and gc instantly
|
||||
new OpenBracketJoiner(), new CloseBracketJoiner(),
|
||||
new StmtJoiner(), new StringLiteralJoiner(),
|
||||
new StringLiteralJoiner(), new StmtJoiner(), // strings before stmts to let doc strings join
|
||||
new BinaryExprJoiner(), new ListLikeExprJoiner()
|
||||
};
|
||||
|
||||
@@ -243,8 +243,10 @@ public class PyJoinLinesHandler implements JoinRawLinesHandlerDelegate {
|
||||
public Result join(Request req) {
|
||||
if (req.leftElem() != req.rightElem()) {
|
||||
final PsiElement parent = req.rightElem().getParent();
|
||||
if (req.leftElem().getParent() == parent && parent instanceof PyStringLiteralExpression) {
|
||||
// two quoted strings of same literal
|
||||
if ((req.leftElem().getParent() == parent && parent instanceof PyStringLiteralExpression) ||
|
||||
(req.leftExpr() instanceof PyStringLiteralExpression && req.rightExpr() instanceof PyStringLiteralExpression)
|
||||
) {
|
||||
// two quoted strings close by
|
||||
CharSequence text = req.document().getCharsSequence();
|
||||
StrMod left_mod = new StrMod(text, req.leftElem().getTextRange());
|
||||
StrMod right_mod = new StrMod(text, req.rightElem().getTextRange());
|
||||
@@ -255,18 +257,16 @@ public class PyJoinLinesHandler implements JoinRawLinesHandlerDelegate {
|
||||
}
|
||||
else if (left_mod.compatibleTo(right_mod) && lquo.length() == 1 && right_mod.quote().length() == 1) {
|
||||
// maybe fit one literal's quotes to match other's
|
||||
if (! containsChar(text, left_mod.getInnerRange(), right_mod.quote().charAt(0))) {
|
||||
if (! containsChar(text, right_mod.getInnerRange(), left_mod.quote().charAt(0))) {
|
||||
int quote_pos = right_mod.getInnerRange().getEndOffset();
|
||||
req.document().replaceString(quote_pos, quote_pos+1, left_mod.quote());
|
||||
return new Result("", 0, left_mod.quote().length(), right_mod.getStartPadding());
|
||||
}
|
||||
else if (! containsChar(text, left_mod.getInnerRange(), right_mod.quote().charAt(0))) {
|
||||
int quote_pos = left_mod.getInnerRange().getStartOffset()-1;
|
||||
req.document().replaceString(quote_pos, quote_pos+1, right_mod.quote());
|
||||
return new Result("", 0, left_mod.quote().length(), right_mod.getStartPadding());
|
||||
}
|
||||
else {
|
||||
if (! containsChar(text, right_mod.getInnerRange(), left_mod.quote().charAt(0))) {
|
||||
int quote_pos = right_mod.getInnerRange().getStartOffset()-1;
|
||||
req.document().replaceString(quote_pos, quote_pos+1, left_mod.quote());
|
||||
return new Result("", 0, left_mod.quote().length(), right_mod.getStartPadding());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
a = 1 + 2
|
||||
@@ -0,0 +1,2 @@
|
||||
a = <caret>1 +\
|
||||
2
|
||||
@@ -0,0 +1 @@
|
||||
a = 1 + 2
|
||||
@@ -0,0 +1,2 @@
|
||||
a = <caret>1 \
|
||||
+ 2
|
||||
@@ -0,0 +1,3 @@
|
||||
a = {'a': 1,
|
||||
'b': 2
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
a =<caret> {
|
||||
'a': 1,
|
||||
'b': 2
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
a = {
|
||||
'a': 1,
|
||||
'b': 2}
|
||||
@@ -0,0 +1,4 @@
|
||||
a = {
|
||||
'a': 1,
|
||||
<caret>'b': 2
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
a = [1, 2]
|
||||
@@ -0,0 +1,2 @@
|
||||
a =<caret> [1,
|
||||
2]
|
||||
@@ -0,0 +1,3 @@
|
||||
a = [111,
|
||||
222
|
||||
]
|
||||
@@ -0,0 +1,4 @@
|
||||
a = <caret>[
|
||||
111,
|
||||
222
|
||||
]
|
||||
@@ -0,0 +1,3 @@
|
||||
a = [
|
||||
111,
|
||||
222]
|
||||
@@ -0,0 +1,4 @@
|
||||
a = [
|
||||
111,
|
||||
<caret>222
|
||||
]
|
||||
@@ -0,0 +1,2 @@
|
||||
if True:
|
||||
return "No special handling"
|
||||
@@ -0,0 +1,2 @@
|
||||
if True:
|
||||
return "No special handling"
|
||||
@@ -0,0 +1 @@
|
||||
a = 1; b = 2 # a note about b
|
||||
@@ -0,0 +1,2 @@
|
||||
<caret>a = 1
|
||||
b = 2 # a note about b
|
||||
@@ -0,0 +1,2 @@
|
||||
a = 1 # surprise!
|
||||
b = 2
|
||||
@@ -0,0 +1,2 @@
|
||||
<caret>a = 1 # surprise!
|
||||
b = 2
|
||||
@@ -0,0 +1 @@
|
||||
a = (u'A single quote is never found in this literal')
|
||||
@@ -0,0 +1,2 @@
|
||||
a = <caret>(u'A single quote '
|
||||
u"is never found in this literal")
|
||||
@@ -0,0 +1 @@
|
||||
a = (u"A single quote is this literal's hallmark")
|
||||
@@ -0,0 +1,2 @@
|
||||
a = <caret>(u'A single quote '
|
||||
u"is this literal's hallmark")
|
||||
@@ -0,0 +1 @@
|
||||
a = "aaaaABbbbb"
|
||||
@@ -0,0 +1,2 @@
|
||||
<caret>a = "aaaaA"\
|
||||
"Bbbbb"
|
||||
@@ -0,0 +1,2 @@
|
||||
print (r"See, \n separates two lines: "
|
||||
"this and\nthe other")
|
||||
@@ -0,0 +1,2 @@
|
||||
<caret>print (r"See, \n separates two lines: "
|
||||
"this and\nthe other")
|
||||
@@ -0,0 +1,2 @@
|
||||
a = (u"Ein Deutsche Text. "
|
||||
"An English text")
|
||||
@@ -0,0 +1,2 @@
|
||||
<caret>a = (u"Ein Deutsche Text. "
|
||||
"An English text")
|
||||
@@ -0,0 +1,3 @@
|
||||
"""This doc comment
|
||||
needs to be joined
|
||||
with the one below"""
|
||||
@@ -0,0 +1,4 @@
|
||||
"""This doc comment
|
||||
<caret>needs to """
|
||||
"""be joined
|
||||
with the one below"""
|
||||
@@ -0,0 +1,3 @@
|
||||
<caret>'''This doc comment
|
||||
is quite separate'''
|
||||
"""from the literal below"""
|
||||
@@ -0,0 +1,3 @@
|
||||
<caret>'''This doc comment
|
||||
is quite separate'''
|
||||
"""from the literal below"""
|
||||
@@ -0,0 +1 @@
|
||||
a = (1, 2)
|
||||
@@ -0,0 +1,2 @@
|
||||
<caret>a = (1,
|
||||
2)
|
||||
@@ -0,0 +1,3 @@
|
||||
a = (1,
|
||||
2
|
||||
)
|
||||
@@ -0,0 +1,4 @@
|
||||
a =<caret> (
|
||||
1,
|
||||
2
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
a = (
|
||||
1,
|
||||
2,)
|
||||
@@ -0,0 +1,4 @@
|
||||
a = (
|
||||
1,
|
||||
<caret>2,
|
||||
)
|
||||
@@ -0,0 +1 @@
|
||||
a = 1; b = 2
|
||||
@@ -0,0 +1,2 @@
|
||||
<caret>a = 1
|
||||
b = 2
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.jetbrains.python;
|
||||
|
||||
import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate;
|
||||
import com.intellij.openapi.application.Result;
|
||||
import com.intellij.openapi.application.WriteAction;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.jetbrains.python.editor.PyJoinLinesHandler;
|
||||
import com.jetbrains.python.fixtures.PyLightFixtureTestCase;
|
||||
|
||||
/**
|
||||
* Tests the "Join lines" handler.
|
||||
* <br/>
|
||||
* User: dcheryasov
|
||||
* Date: 1/29/11 2:33 AM
|
||||
*/
|
||||
public class PyJoinLinesTest extends PyLightFixtureTestCase {
|
||||
private void doTest() {
|
||||
final String path = "joinLines/";
|
||||
myFixture.configureByFile(path + getTestName(false) + ".py");
|
||||
// detect whitespace around EOL, the way JoinLinesHandler does.
|
||||
final Editor editor = myFixture.getEditor();
|
||||
final Document doc = editor.getDocument();
|
||||
final int caret_line = doc.getLineNumber(editor.getCaretModel().getOffset());
|
||||
final int eol_pos = doc.getLineEndOffset(caret_line);
|
||||
CharSequence text = doc.getCharsSequence();
|
||||
int i = eol_pos;
|
||||
while (" \n\t".indexOf(text.charAt(i)) >= 0) i -= 1;
|
||||
final int start = i+1;
|
||||
i = eol_pos;
|
||||
while (" \n\t".indexOf(text.charAt(i)) >= 0) i += 1;
|
||||
final int end = i;
|
||||
final JoinRawLinesHandlerDelegate handler = new PyJoinLinesHandler();
|
||||
new WriteAction() {
|
||||
@Override
|
||||
protected void run(Result result) throws Throwable {
|
||||
int ret = handler.tryJoinRawLines(doc, myFixture.getFile(), start, end);
|
||||
}
|
||||
}.execute().throwException();
|
||||
myFixture.checkResultByFile(path + getTestName(false) + "-after.py");
|
||||
}
|
||||
|
||||
public void testBinaryOpBelow() { doTest(); }
|
||||
public void testBinaryOp() { doTest(); }
|
||||
public void testDictLCurly() { doTest(); }
|
||||
public void testDictRCurly() { doTest(); }
|
||||
public void testListLBracket() { doTest(); }
|
||||
public void testList() { doTest(); }
|
||||
public void testListRBracket() { doTest(); }
|
||||
public void testStatementColon() { doTest(); }
|
||||
public void testStatementComment() { doTest(); }
|
||||
public void testStatementCommentStatement() { doTest(); }
|
||||
public void testStringDifferentOneQuotes() { doTest(); }
|
||||
public void testStringDifferentOneQuotesBelow() { doTest(); }
|
||||
public void testStringOneQuoteEscEOL() { doTest(); }
|
||||
public void testStringOneQuotePlainRaw() { doTest(); }
|
||||
public void testStringOneQuotePlainU() { doTest(); }
|
||||
public void testStringTripleQuotesDifferent() { doTest(); }
|
||||
public void testStringTripleQuotes() { doTest(); }
|
||||
public void testTupleLPar() { doTest(); }
|
||||
public void testTuple() { doTest(); }
|
||||
public void testTupleRPar() { doTest(); }
|
||||
public void testTwoStatements() { doTest(); }
|
||||
}
|
||||
Reference in New Issue
Block a user