forked from jupyter-xeus/xeus-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxinspect.cpp
135 lines (111 loc) · 4.47 KB
/
xinspect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/***************************************************************************
* Copyright (c) 2018, Martin Renou, Johan Mabille, Sylvain Corlay, and *
* Wolf Vollprecht *
* Copyright (c) 2018, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include <string>
#include <vector>
#include "xeus-python/xutils.hpp"
#include "xinspect.hpp"
#include "pybind11/pybind11.h"
#include "xinternal_utils.hpp"
using namespace pybind11::literals;
namespace xpyt
{
py::object static_inspect(const std::string& code)
{
py::module jedi = py::module::import("jedi");
return jedi.attr("Interpreter")(code, py::make_tuple(py::globals()));
}
py::object static_inspect(const std::string& code, int cursor_pos)
{
std::string sub_code = code.substr(0, cursor_pos);
return static_inspect(sub_code);
}
py::list get_completions(const std::string& code, int cursor_pos)
{
return static_inspect(code, cursor_pos).attr("complete")();
}
py::list get_completions(const std::string& code)
{
return static_inspect(code).attr("complete")();
}
py::list get_completions(const std::string& code);
std::string formatted_docstring_impl(py::object inter)
{
py::object definition = py::none();
// If it's a function call
py::list call_sig = inter.attr("get_signatures")();
py::list definitions = inter.attr("infer")();
if (py::len(call_sig) != 0)
{
definition = call_sig[0];
}
else if (py::len(definitions) != 0)
{
definition = definitions[0];
}
else
{
return "";
}
auto name = definition.attr("name").cast<std::string>();
auto docstring = definition.attr("docstring")().cast<std::string>();
auto type = definition.attr("type").cast<std::string>();
std::string result;
// Retrieving the argument names and default values for keyword arguments if it's a function
py::list signatures = definition.attr("get_signatures")();
if (py::len(signatures) != 0)
{
py::list py_params = signatures[0].attr("params");
result.append(red_text("Signature: ") + name + blue_text("("));
for (py::handle param : py_params)
{
py::list param_description = param.attr("to_string")().attr("split")("=");
std::string param_name = param_description[0].cast<std::string>();
// The argument is not a kwarg (no default value)
if (py::len(param_description) == 1)
{
result.append(param_name);
}
else
{
std::string default_value = param_description[1].cast<std::string>();
result.append(param_name + blue_text("=") + green_text(default_value));
}
// If it's not the last element, add a comma.
if (!param.is(py_params[py::len(py_params) - 1]))
{
result.append(blue_text(", "));
}
}
result.append(blue_text(")"));
// Remove signature from the docstring
py::list splitted_docstring = definition.attr("docstring")().attr("split")("\n\n", 1);
if (py::len(splitted_docstring) == 2)
{
docstring = splitted_docstring[1].cast<std::string>();
}
}
else
{
result.append(red_text("Name: ") + name);
}
result.append(red_text("\nType: ") + type + red_text("\nDocstring: ") + docstring);
return result;
}
std::string formatted_docstring(const std::string& code, int cursor_pos)
{
py::object inter = static_inspect(code, cursor_pos);
return formatted_docstring_impl(inter);
}
std::string formatted_docstring(const std::string& code)
{
py::object inter = static_inspect(code);
return formatted_docstring_impl(inter);
}
}