mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
fixed PY-7088 Insert docstring stub: leads to syntactically incorrect code when invoked for one-line function
This commit is contained in:
+18
-3
@@ -3,6 +3,7 @@ package com.jetbrains.python.codeInsight.intentions;
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInsight.template.*;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
@@ -131,15 +132,29 @@ public class SpecifyTypeInDocstringIntention implements IntentionAction {
|
||||
docStringExpression = pyFunction.getDocStringExpression();
|
||||
}
|
||||
else {
|
||||
PyExpressionStatement str = elementGenerator.createDocstring(replacementToOffset.getFirst());
|
||||
|
||||
final PyStatementList list = pyFunction.getStatementList();
|
||||
final Document document = editor.getDocument();
|
||||
startOffset = replacementToOffset.getSecond();
|
||||
|
||||
if (list != null && list.getStatements().length != 0) {
|
||||
list.addBefore(str, list.getStatements()[0]);
|
||||
if (document.getLineNumber(list.getTextOffset()) == document.getLineNumber(pyFunction.getTextOffset())) {
|
||||
PyFunction func = elementGenerator.createFromText(LanguageLevel.forElement(pyFunction),
|
||||
PyFunction.class, "def " + pyFunction.getName() + pyFunction.getParameterList().getText()
|
||||
+":\n\t"+replacementToOffset.getFirst() + "\n\t" + list.getText());
|
||||
|
||||
pyFunction = (PyFunction)pyFunction.replace(func);
|
||||
startOffset = replacementToOffset.getSecond() + 2;
|
||||
}
|
||||
else {
|
||||
PyExpressionStatement str = elementGenerator.createDocstring(replacementToOffset.getFirst());
|
||||
list.addBefore(str, list.getStatements()[0]);
|
||||
}
|
||||
}
|
||||
|
||||
pyFunction = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(pyFunction);
|
||||
docStringExpression = pyFunction.getDocStringExpression();
|
||||
startOffset = replacementToOffset.getSecond();
|
||||
|
||||
endOffset = startOffset;
|
||||
}
|
||||
assert docStringExpression != null;
|
||||
|
||||
@@ -503,8 +503,12 @@ public class PythonDocumentationProvider extends AbstractDocumentationProvider i
|
||||
}
|
||||
String docContent = ws + generateDocumentationContentStub(function, ws, true);
|
||||
PyExpressionStatement string = elementGenerator.createDocstring("\"\"\"" + docContent + "\"\"\"");
|
||||
if (insertPlace.getStatements().length != 0)
|
||||
insertPlace.addBefore(string, insertPlace.getStatements()[0]);
|
||||
if (insertPlace.getStatements().length != 0) {
|
||||
PyFunction func = elementGenerator.createFromText(LanguageLevel.forElement(function),
|
||||
PyFunction.class, "def " + function.getName() + function.getParameterList().getText()
|
||||
+":\n\t"+ string.getText() + "\n\t" + insertPlace.getText());
|
||||
function.replace(func);
|
||||
}
|
||||
PyStringLiteralExpression docstring = function.getDocStringExpression();
|
||||
if (editor != null && docstring != null) {
|
||||
int offset = docstring.getTextOffset();
|
||||
@@ -603,8 +607,9 @@ public class PythonDocumentationProvider extends AbstractDocumentationProvider i
|
||||
private static int addParamOrType(StringBuilder replacementText, PyDocStringOwner function, boolean addWS, String keyword,
|
||||
String paramName, String prefix) {
|
||||
PsiWhiteSpace whitespace = null;
|
||||
if (function instanceof PyFunction)
|
||||
if (function instanceof PyFunction) {
|
||||
whitespace = PsiTreeUtil.getPrevSiblingOfType(((PyFunction)function).getStatementList(), PsiWhiteSpace.class);
|
||||
}
|
||||
String ws = "\n";
|
||||
if (whitespace != null) {
|
||||
String[] spaces = whitespace.getText().split("\n");
|
||||
|
||||
Reference in New Issue
Block a user