我们有一个pybind11模块,希望Jedi为其类和功能提供代码完成。我们的方法是设置函数的文档字符串__doc__
,以便Jedi可以使用它们来完成功能。当我们调用completions()
时,我们得到一个空列表。绝地不会打印错误,也不会通过set_debug_function打印。
我们在传递给Jedi的字符串中设置模块的路径。此字符串中还包括模块的导入。
要让Jedi自动完成自定义C ++库类和函数,我们需要做什么?通常使用__doc__
可以正常工作吗?如果可以,格式应该是什么样?
更新:由于Dave的评论,我们得以进一步分析问题。下面是一个示例:
import bitbuffer
bb = bitbuffer.BitBuffer(100)
bitbuffer. # completion works for the module
bitBuffer.BitBuffer. # completion works for the class
bb. # completion does not work for the instance of the class
以下是Jedi 0.14.0的set_debug_function()
输出,用于完成bb.
:
speed: init 17.217941999435425
speed: parsed 17.218032836914062
dbg: Start: completions
dbg: eval_node <Name: bb@5,0>@(5, 0) in <ModuleContext: @1-5 is_stub=False>
dbg: finder.filter_name 'bb' in (<ModuleContext: @1-5 is_stub=False>): [<TreeNameDefinition: string_name=bb start_pos=(3, 0)>]@(5, 0)
dbg: eval_expr_stmt <ExprStmt: bb = bitbuffer.BitBuffer(100)@3,0> (<Name: bb@3,0>)
dbg: eval_node PythonNode(atom_expr, [<Name: bitbuffer@3,5>, PythonNode(trailer, [<Operator: .>, <Name: BitBuffer@3,15>]), PythonNode(trailer, [<Operator: (>, <Number: 100>, <Operator: )>])])@(3, 5) in <ModuleContext: @1-5 is_stub=False>
dbg: eval_node <Name: bitbuffer@3,5>@(3, 5) in <ModuleContext: @1-5 is_stub=False>
dbg: finder.filter_name 'bitbuffer' in (<ModuleContext: @1-5 is_stub=False>): [<TreeNameDefinition: string_name=bitbuffer start_pos=(1, 7)>]@(3, 0)
speed: import (<Name: bitbuffer@1,7>,) <ModuleContext: @1-5 is_stub=False> 0.0019109249114990234
dbg: global search_module 'bitbuffer': <CompiledObject: <module 'bitbuffer' from '/home/software/build-Debug/install/myApp/python/lib/python3.6/site-packages/bitbuffer.so'>>
dbg: after import: S{<CompiledObject: <module 'bitbuffer' from '/home/software/build-Debug/install/myApp/python/lib/python3.6/site-packages/bitbuffer.so'>>}
dbg: finder._names_to_types: [<TreeNameDefinition: string_name=bitbuffer start_pos=(1, 7)>] -> S{<CompiledObject: <module 'bitbuffer' from '/home/software/build-Debug/install/myApp/python/lib/python3.6/site-packages/bitbuffer.so'>>}
dbg: eval_trailer: PythonNode(trailer, [<Operator: .>, <Name: BitBuffer@3,15>]) in S{<CompiledObject: <module 'bitbuffer' from '/home/software/build-Debug/install/myApp/python/lib/python3.6/site-packages/bitbuffer.so'>>}
dbg: finder.filter_name 'BitBuffer' in (<CompiledObject: <module 'bitbuffer' from '/home/software/build-Debug/install/myApp/python/lib/python3.6/site-packages/bitbuffer.so'>>): [<CompiledName: (<CompiledContextName: string_name=bitbuffer>).BitBuffer>]@None
dbg: finder._names_to_types: [<CompiledName: (<CompiledContextName: string_name=bitbuffer>).BitBuffer>] -> S{<CompiledObject: <pybind11_builtins.pybind11_type object at 0x55be2f8f7278>>}
dbg: eval_trailer: PythonNode(trailer, [<Operator: (>, <Number: 100>, <Operator: )>]) in S{<CompiledObject: <pybind11_builtins.pybind11_type object at 0x55be2f8f7278>>}
dbg: global search_module 'builtins': <CompiledObject: <module 'builtins' (built-in)>>
dbg: execute: <CompiledObject: <pybind11_builtins.pybind11_type object at 0x55be2f8f7278>> <TreeArguments: <Number: 100>>
dbg: execute result: S{<CompiledInstance of <CompiledObject: <pybind11_builtins.pybind11_type object at 0x55be2f8f7278>>(<TreeArguments: <Number: 100>>)>} in <CompiledObject: <pybind11_builtins.pybind11_type object at 0x55be2f8f7278>>
dbg: eval_expr_stmt result S{<CompiledInstance of <CompiledObject: <pybind11_builtins.pybind11_type object at 0x55be2f8f7278>>(<TreeArguments: <Number: 100>>)>}
dbg: finder._names_to_types: [<TreeNameDefinition: string_name=bb start_pos=(3, 0)>] -> S{<CompiledInstance of <CompiledObject: <pybind11_builtins.pybind11_type object at 0x55be2f8f7278>>(<TreeArguments: <Number: 100>>)>}
dbg: trailer completion contexts: S{<CompiledInstance of <CompiledObject: <pybind11_builtins.pybind11_type object at 0x55be2f8f7278>>(<TreeArguments: <Number: 100>>)>}
dbg: Start: convert contexts
dbg: End: convert contexts
dbg: End: completions
Jedi为什么不返回对象实例bb
的完成?外壳程序中的dir(bb)
可以工作并生成以下输出:
['__bool__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'capacity', 'clear', 'copy', 'data', 'desc', 'fill', 'from_int', 'marks', 'name', 'offset', 'pack', 'shadow', 'size', 'unpack']
更新#2提示: 绝地解释员能够完成我们的目标:
script = jedi.Interpreter("bb.", [locals()])
script.completions() # this works
我们得出结论,对象本身没有问题。
更新#3:
对象的自动完成功能适用于jedi 0.11.1,但不适用于0.14.0。我们假定更高版本中有问题。