From 8a059733eb9d7140f3320cc80395d77aaf81b4e1 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Thu, 19 Sep 2013 20:27:18 +0400 Subject: [PATCH] fixed PY-8946 Classmethod docstring creation --- .../psi-api/src/com/jetbrains/python/PyNames.java | 1 + .../documentation/PythonDocumentationProvider.java | 13 +++++++++---- .../jetbrains/python/editor/PythonEnterHandler.java | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/python/psi-api/src/com/jetbrains/python/PyNames.java b/python/psi-api/src/com/jetbrains/python/PyNames.java index 340a57dbda77..ca7bdadd22ff 100644 --- a/python/psi-api/src/com/jetbrains/python/PyNames.java +++ b/python/psi-api/src/com/jetbrains/python/PyNames.java @@ -328,6 +328,7 @@ public class PyNames { // canonical names, not forced by interpreter public static final String CANONICAL_SELF = "self"; + public static final String CANONICAL_CLS = "cls"; public static final String BASESTRING = "basestring"; /** diff --git a/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java b/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java index f9dfdfd352bf..66f44a2d62a6 100644 --- a/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java +++ b/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java @@ -534,8 +534,11 @@ 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 != null) { + final PyStatement[] statements = insertPlace.getStatements(); + if (statements.length != 0) { + insertPlace.addBefore(string, statements[0]); + } } PyStringLiteralExpression docstring = function.getDocStringExpression(); if (editor != null && docstring != null) { @@ -563,12 +566,14 @@ public class PythonDocumentationProvider extends AbstractDocumentationProvider i final StringBuilder builder = new StringBuilder(offset); final TypeEvalContext context = TypeEvalContext.userInitiated(function.getContainingFile()); PySignature signature = PySignatureCacheManager.getInstance(function.getProject()).findSignature(function); - + final PyDecoratorList decoratorList = function.getDecoratorList(); + final PyDecorator classMethod = decoratorList == null ? null : decoratorList.findDecorator(PyNames.CLASSMETHOD); for (PyParameter p : PyUtil.getParameters(function, context)) { final String parameterName = p.getName(); if (p.getText().equals(PyNames.CANONICAL_SELF) || parameterName == null) { continue; } + if (classMethod != null && parameterName.equals(PyNames.CANONICAL_CLS)) continue; String argType = signature == null ? null : signature.getArgTypeQualifiedName(parameterName); if (argType == null) { @@ -583,7 +588,7 @@ public class PythonDocumentationProvider extends AbstractDocumentationProvider i builder.append("type "); builder.append(parameterName); builder.append(": "); - if (signature != null) { + if (signature != null && argType != null) { builder.append(PySignatureUtil.getShortestImportableName(function, argType)); } builder.append(offset); diff --git a/python/src/com/jetbrains/python/editor/PythonEnterHandler.java b/python/src/com/jetbrains/python/editor/PythonEnterHandler.java index 967c285fe5db..e32fcb79c340 100644 --- a/python/src/com/jetbrains/python/editor/PythonEnterHandler.java +++ b/python/src/com/jetbrains/python/editor/PythonEnterHandler.java @@ -230,13 +230,13 @@ public class PythonEnterHandler extends EnterHandlerDelegateAdapter { if (fun != null) { String docStub = provider.generateDocumentationContentStub(fun, false); docStub += element.getParent().getText().substring(0,3); - if (docStub != null && docStub.length() != 0) { + if (docStub.length() != 0) { editor.getDocument().insertString(editor.getCaretModel().getOffset(), docStub); return; } } PyElement klass = PsiTreeUtil.getParentOfType(element, PyClass.class, PyFile.class); - if (klass != null) { + if (klass != null && element != null) { editor.getDocument().insertString(editor.getCaretModel().getOffset(), PythonDocCommentUtil.generateDocForClass(klass, element.getParent().getText().substring(0, 3))); }