mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
fixed PY-8581 Fill Paragraph should not corrupt indentation
PY-8937 fill paragraph works wrong on simple strings
This commit is contained in:
@@ -5,9 +5,12 @@ import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.psi.PsiComment;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiWhiteSpace;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.jetbrains.python.PythonStringUtil;
|
||||
import com.jetbrains.python.psi.PyDocStringOwner;
|
||||
import com.jetbrains.python.psi.PyFile;
|
||||
import com.jetbrains.python.psi.PyStatementList;
|
||||
import com.jetbrains.python.psi.PyStringLiteralExpression;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -24,7 +27,19 @@ public class PyFillParagraphHandler extends ParagraphFillHandler {
|
||||
if (stringLiteralExpression != null) {
|
||||
final Pair<String,String> quotes =
|
||||
PythonStringUtil.getQuotes(stringLiteralExpression.getText());
|
||||
return quotes != null? quotes.getFirst()+"\n" : "\"\n";
|
||||
final PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(stringLiteralExpression, PyDocStringOwner.class);
|
||||
if (docStringOwner != null && stringLiteralExpression.equals(docStringOwner.getDocStringExpression())) {
|
||||
final PyStatementList statementList = PsiTreeUtil.getParentOfType(stringLiteralExpression, PyStatementList.class);
|
||||
final PsiElement whiteSpace = statementList.getPrevSibling();
|
||||
String indent;
|
||||
if (whiteSpace instanceof PsiWhiteSpace)
|
||||
indent = whiteSpace.getText();
|
||||
else
|
||||
indent = "\n";
|
||||
return quotes != null? quotes.getFirst()+ indent : "\"" + indent;
|
||||
}
|
||||
else
|
||||
return quotes != null? quotes.getFirst() : "\"";
|
||||
}
|
||||
return element instanceof PsiComment? "# " : "";
|
||||
}
|
||||
@@ -37,7 +52,19 @@ public class PyFillParagraphHandler extends ParagraphFillHandler {
|
||||
if (stringLiteralExpression != null) {
|
||||
final Pair<String,String> quotes =
|
||||
PythonStringUtil.getQuotes(stringLiteralExpression.getText());
|
||||
return quotes != null? "\n" + quotes.getSecond() : "\n\"";
|
||||
final PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(stringLiteralExpression, PyDocStringOwner.class);
|
||||
if (docStringOwner != null && stringLiteralExpression.equals(docStringOwner.getDocStringExpression())) {
|
||||
final PyStatementList statementList = PsiTreeUtil.getParentOfType(stringLiteralExpression, PyStatementList.class);
|
||||
final PsiElement whiteSpace = statementList.getPrevSibling();
|
||||
String indent;
|
||||
if (whiteSpace instanceof PsiWhiteSpace)
|
||||
indent = whiteSpace.getText();
|
||||
else
|
||||
indent = "\n";
|
||||
return quotes != null? indent + quotes.getSecond() : indent + "\"";
|
||||
}
|
||||
else
|
||||
return quotes != null? quotes.getSecond() : "\"";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
def foo():
|
||||
"""
|
||||
This is my docstring. <caret>There are many like it, but this one mine. My docstring is my best friend. it is my life. I must master it as I must master my life.
|
||||
"""
|
||||
@@ -0,0 +1,7 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
def foo():
|
||||
"""
|
||||
This is my docstring. There are many like it, but this one mine. My docstring is my best friend. it is my life. I
|
||||
must master it as I must master my life.
|
||||
"""
|
||||
@@ -0,0 +1 @@
|
||||
p = "my new <caret>string blah blah blah ;j;dsjv sd;fj;dsjf;ds js;djgf ;jsg s;dgj; sjd;gj sd;gj ;sjg;j asdl j;sjdg; jasdgl j;sldjg ;jsd;gj "
|
||||
@@ -0,0 +1,2 @@
|
||||
p = "my new string blah blah blah ;j;dsjv sd;fj;dsjf;ds js;djgf ;jsg s;dgj; sjd;gj sd;gj ;sjg;j asdl j;sjdg; jasdgl " \
|
||||
"j;sldjg ;jsd;gj "
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.jetbrains.python;
|
||||
|
||||
import com.intellij.codeInsight.editorActions.fillParagraph.FillParagraphAction;
|
||||
import com.intellij.ide.DataManager;
|
||||
import com.intellij.openapi.actionSystem.ActionManager;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.jetbrains.python.fixtures.PyTestCase;
|
||||
|
||||
/**
|
||||
* User : ktisha
|
||||
*/
|
||||
public class PyFillParagraphTest extends PyTestCase {
|
||||
|
||||
public void testDocstring() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testString() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
String baseName = "/fillParagraph/" + getTestName(true);
|
||||
myFixture.configureByFile(baseName + ".py");
|
||||
CommandProcessor.getInstance().executeCommand(myFixture.getProject(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
FillParagraphAction action = new FillParagraphAction();
|
||||
action.actionPerformed(new AnActionEvent(null, DataManager.getInstance().getDataContext(), "",
|
||||
action.getTemplatePresentation(),
|
||||
ActionManager.getInstance(), 0));
|
||||
}
|
||||
}, "", null);
|
||||
myFixture.checkResultByFile(baseName + "_after.py", true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user