From 048e421e553f1be50d2e03b49b09dcc81b2df4c7 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Thu, 31 Aug 2017 15:12:00 +0300 Subject: [PATCH] PY-25779 Show documentation for method if caret is placed inside parameters list --- .../PythonDocumentationProvider.java | 22 ++++++++++++++++--- python/testData/quickdoc/ArgumentList.html | 1 + python/testData/quickdoc/ArgumentList.py | 6 +++++ python/testData/quickdoc/BuiltinLen.html | 2 +- .../com/jetbrains/python/PyQuickDocTest.java | 8 +++++++ 5 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 python/testData/quickdoc/ArgumentList.html create mode 100644 python/testData/quickdoc/ArgumentList.py diff --git a/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java b/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java index bb32343dda0d..2c3758a0946d 100644 --- a/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java +++ b/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java @@ -31,9 +31,11 @@ import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.Computable; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; +import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.QualifiedName; import com.jetbrains.python.PyNames; +import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.PythonDialectsTokenSetProvider; import com.jetbrains.python.codeInsight.stdlib.PyStdlibDocumentationLinkProvider; import com.jetbrains.python.console.PydevConsoleRunner; @@ -547,9 +549,23 @@ public class PythonDocumentationProvider extends AbstractDocumentationProvider i public PsiElement getCustomDocumentationElement(@NotNull Editor editor, @NotNull PsiFile file, @Nullable PsiElement contextElement) { - if (contextElement != null && - PythonDialectsTokenSetProvider.INSTANCE.getKeywordTokens().contains(contextElement.getNode().getElementType())) { - return contextElement; + if (contextElement != null) { + final IElementType elementType = contextElement.getNode().getElementType(); + if (PythonDialectsTokenSetProvider.INSTANCE.getKeywordTokens().contains(elementType)) { + return contextElement; + } + if (PyTokenTypes.LPAR == elementType || PyTokenTypes.RPAR == elementType) { + final PyCallExpression expression = PsiTreeUtil.getParentOfType(contextElement, PyCallExpression.class); + if (expression != null) { + final PyExpression callee = expression.getCallee(); + if (callee != null) { + final PsiReference reference = callee.getReference(); + if (reference != null) { + return reference.resolve(); + } + } + } + } } return super.getCustomDocumentationElement(editor, file, contextElement); } diff --git a/python/testData/quickdoc/ArgumentList.html b/python/testData/quickdoc/ArgumentList.html new file mode 100644 index 000000000000..e4cfe0fd4e36 --- /dev/null +++ b/python/testData/quickdoc/ArgumentList.html @@ -0,0 +1 @@ +def foo()
Inferred type: () -> None

Doc of foo.
\ No newline at end of file diff --git a/python/testData/quickdoc/ArgumentList.py b/python/testData/quickdoc/ArgumentList.py new file mode 100644 index 000000000000..6aff48527784 --- /dev/null +++ b/python/testData/quickdoc/ArgumentList.py @@ -0,0 +1,6 @@ +# directly in function +def foo(): + "Doc of foo." + pass + +foo() diff --git a/python/testData/quickdoc/BuiltinLen.html b/python/testData/quickdoc/BuiltinLen.html index db51bb72f7e6..805ad69fbbd3 100644 --- a/python/testData/quickdoc/BuiltinLen.html +++ b/python/testData/quickdoc/BuiltinLen.html @@ -1 +1 @@ -def len(o: Sized)
Inferred type: (o: Sized) -> int


len(object) -> integer

Return the number of items of a sequence or collection.

External documentation:
http://docs.python.org/2.7 Mock SDK/library/__builtin__.html#__builtin__.len +def len(o: Sized)
Inferred type: (o: Sized) -> int


len(object) -> integer

Return the number of items of a sequence or collection.
diff --git a/python/testSrc/com/jetbrains/python/PyQuickDocTest.java b/python/testSrc/com/jetbrains/python/PyQuickDocTest.java index 1e7823e065f5..ce5ae9d67346 100644 --- a/python/testSrc/com/jetbrains/python/PyQuickDocTest.java +++ b/python/testSrc/com/jetbrains/python/PyQuickDocTest.java @@ -292,6 +292,14 @@ public class PyQuickDocTest extends LightMarkedTestCase { checkHTMLOnly(); } + public void testArgumentList() { + Map marks = loadTest(); + final PsiElement originalElement = marks.get(""); + + final PsiElement element = myProvider.getCustomDocumentationElement(myFixture.getEditor(), myFile, originalElement); + checkByHTML(myProvider.generateDoc(element, originalElement)); + } + public void testReferenceToMethodQualifiedWithInstance() { checkHTMLOnly(); }