From e02021faba34980c820b0120ad55bcdddfcb82b7 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Mon, 23 Jul 2012 12:16:10 +0400 Subject: [PATCH] fixed PY-1413 Show first line of docstring in Ctrl-hover popup --- .../PythonDocumentationProvider.java | 25 +++++++++++++++++-- .../documentation/StructuredDocString.java | 10 ++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java b/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java index 8ed5fc982957..63663acfc9e7 100644 --- a/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java +++ b/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java @@ -69,11 +69,32 @@ public class PythonDocumentationProvider extends AbstractDocumentationProvider i cat.append("class ").append(cls_name).append("\n"); // It would be nice to have class import info here, but we don't know the ctrl+hovered reference and context } - return $(cat.toString()).add(describeDecorators(func, LSame2, ", ", LSame1)).add(describeFunction(func, LSame2, LSame1)).toString(); + String summary = ""; + final PyStringLiteralExpression docStringExpression = func.getDocStringExpression(); + if (docStringExpression != null) { + final StructuredDocString docString = StructuredDocString.parse(docStringExpression.getStringValue()); + if (docString != null) + summary = docString.getSummary(); + } + return $(cat.toString()).add(describeDecorators(func, LSame2, ", ", LSame1)).add(describeFunction(func, LSame2, LSame1)) + .toString() + summary; } else if (element instanceof PyClass) { PyClass cls = (PyClass)element; - return describeDecorators(cls, LSame2, ", ", LSame1).add(describeClass(cls, LSame2, false, false)).toString(); + String summary = ""; + PyStringLiteralExpression docStringExpression = cls.getDocStringExpression(); + if (docStringExpression == null) { + final PyFunction initOrNew = cls.findInitOrNew(false); + if (initOrNew != null) + docStringExpression = initOrNew.getDocStringExpression(); + } + if (docStringExpression != null) { + final StructuredDocString docString = StructuredDocString.parse(docStringExpression.getStringValue()); + if (docString != null) + summary = docString.getSummary(); + } + + return describeDecorators(cls, LSame2, ", ", LSame1).add(describeClass(cls, LSame2, false, false)).toString() + summary; } else if (element instanceof PyTargetExpression || element instanceof PyNamedParameter) { return describeExpression((PyExpression)element, originalElement); diff --git a/python/src/com/jetbrains/python/documentation/StructuredDocString.java b/python/src/com/jetbrains/python/documentation/StructuredDocString.java index f2b6e4641825..d1fe556b9af4 100644 --- a/python/src/com/jetbrains/python/documentation/StructuredDocString.java +++ b/python/src/com/jetbrains/python/documentation/StructuredDocString.java @@ -2,6 +2,7 @@ package com.jetbrains.python.documentation; import com.google.common.collect.Maps; import com.intellij.openapi.util.TextRange; +import com.intellij.openapi.util.text.StringUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -66,6 +67,15 @@ public abstract class StructuredDocString { return myDescription; } + public String getSummary() { + final List strings = StringUtil.split(StringUtil.trimLeading(myDescription), "\n", true, false); + if (strings.size() > 1) { + if (strings.get(1).isEmpty()) + return strings.get(0); + } + return ""; + } + @NotNull private Map getTagValuesMap(String key) { Map map = myArgTagValues.get(key);