From 0591ade123692cae95ccb15260e6a0390f0620c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Althviz=20Mor=C3=A9?= Date: Thu, 2 Apr 2020 20:05:49 -0500 Subject: [PATCH] PR: Fix hover request for numpy alias (np) and ufuncs (#768) * Fix hover request for numpy alias (np) and ufuncs * Change hover test string --- pyls/plugins/hover.py | 6 ++++++ test/plugins/test_hover.py | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/pyls/plugins/hover.py b/pyls/plugins/hover.py index 1ac57bf5..a98c0ea0 100644 --- a/pyls/plugins/hover.py +++ b/pyls/plugins/hover.py @@ -16,6 +16,12 @@ def pyls_hover(document, position): # Find first exact matching definition definition = next((x for x in definitions if x.name == word), None) + # Ensure a definition is used if only one is available + # even if the word doesn't match. An example of this case is 'np' + # where 'numpy' doesn't match with 'np'. Same for NumPy ufuncs + if len(definitions) == 1: + definition = definitions[0] + if not definition: return {'contents': ''} diff --git a/test/plugins/test_hover.py b/test/plugins/test_hover.py index f34c3513..4ae29cd9 100644 --- a/test/plugins/test_hover.py +++ b/test/plugins/test_hover.py @@ -13,6 +13,45 @@ def main(): pass """ +NUMPY_DOC = """ + +import numpy as np +np.sin + +""" + + +def test_numpy_hover(): + # Over the blank line + no_hov_position = {'line': 1, 'character': 0} + # Over 'numpy' in import numpy as np + numpy_hov_position_1 = {'line': 2, 'character': 8} + # Over 'np' in import numpy as np + numpy_hov_position_2 = {'line': 2, 'character': 17} + # Over 'np' in np.sin + numpy_hov_position_3 = {'line': 3, 'character': 1} + # Over 'sin' in np.sin + numpy_sin_hov_position = {'line': 3, 'character': 4} + + doc = Document(DOC_URI, NUMPY_DOC) + + if LooseVersion(_utils.JEDI_VERSION) >= LooseVersion('0.15.0'): + contents = '' + assert contents in pyls_hover(doc, no_hov_position)['contents'] + + contents = 'NumPy\n=====\n\nProvides\n' + assert contents in pyls_hover(doc, numpy_hov_position_1)['contents'][0] + + contents = 'NumPy\n=====\n\nProvides\n' + assert contents in pyls_hover(doc, numpy_hov_position_2)['contents'][0] + + contents = 'NumPy\n=====\n\nProvides\n' + assert contents in pyls_hover(doc, numpy_hov_position_3)['contents'][0] + + contents = 'Trigonometric sine, element-wise.\n\n' + assert contents in pyls_hover( + doc, numpy_sin_hov_position)['contents'][0] + def test_hover(): # Over 'main' in def main():