forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolProvider.py
95 lines (78 loc) · 2.97 KB
/
symbolProvider.py
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import ast
import json
import sys
class Visitor(ast.NodeVisitor):
def __init__(self):
self.symbols = {"classes": [], "methods": [], "functions": []}
def visit_Module(self, node):
self.visitChildren(node)
def visitChildren(self, node, namespace=""):
for child in node.body:
if isinstance(child, ast.FunctionDef):
self.visitDef(child, namespace)
if isinstance(child, ast.ClassDef):
self.visitClassDef(child, namespace)
try:
if isinstance(child, ast.AsyncFunctionDef):
self.visitDef(child, namespace)
except Exception:
pass
def visitDef(self, node, namespace=""):
end_position = self.getEndPosition(node)
symbol = "functions" if namespace == "" else "methods"
self.symbols[symbol].append(self.getDataObject(node, namespace))
def visitClassDef(self, node, namespace=""):
end_position = self.getEndPosition(node)
self.symbols['classes'].append(self.getDataObject(node, namespace))
if len(namespace) > 0:
namespace = "{0}::{1}".format(namespace, node.name)
else:
namespace = node.name
self.visitChildren(node, namespace)
def getDataObject(self, node, namespace=""):
end_position = self.getEndPosition(node)
return {
"namespace": namespace,
"name": node.name,
"range": {
"start": {
"line": node.lineno - 1,
"character": node.col_offset
},
"end": {
"line": end_position[0],
"character": end_position[1]
}
}
}
def getEndPosition(self, node):
if not hasattr(node, 'body') or len(node.body) == 0:
return (node.lineno - 1, node.col_offset)
return self.getEndPosition(node.body[-1])
def provide_symbols(source):
"""Provides a list of all symbols in provided code.
The list comprises of 3-item tuples that contain the starting line number,
ending line number and whether the statement is a single line.
"""
tree = ast.parse(source)
visitor = Visitor()
visitor.visit(tree)
sys.stdout.write(json.dumps(visitor.symbols))
sys.stdout.flush()
if __name__ == "__main__":
if len(sys.argv) == 3:
contents = sys.argv[2]
else:
with open(sys.argv[1], "r") as source:
contents = source.read()
try:
default_encoding = sys.getdefaultencoding()
encoded_contents = contents.encode(default_encoding, 'surrogateescape')
contents = encoded_contents.decode(default_encoding, 'replace')
except (UnicodeError, LookupError):
pass
if isinstance(contents, bytes):
contents = contents.decode('utf8')
provide_symbols(contents)