This repository was archived by the owner on Mar 27, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlangserver-python.py
More file actions
executable file
·100 lines (76 loc) · 2.32 KB
/
Copy pathlangserver-python.py
File metadata and controls
executable file
·100 lines (76 loc) · 2.32 KB
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
#!/usr/bin/env python3.5
import argparse
import sys
import jedi
import json
def hover(args):
source = open(args.path, "r").read()
script = jedi.api.Script(source=source, line=args.line, column=args.column, path=args.path)
for c in script.completions():
docstring = c.docstring()
if docstring == "":
continue
print(c.docstring())
sys.exit(0)
break
print("No definition found")
def definition(args):
source = open(args.path, "r").read()
script = jedi.api.Script(source=source, line=args.line, column=args.column, path=args.path)
for a in script.goto_assignments():
if not a.is_definition():
continue
json.dump({
"path": a.module_path,
"line": a.line,
"column": a.column,
}, sys.stdout, sort_keys=True)
sys.exit(0)
break
print("No definition found")
def references(args):
source = open(args.path, "r").read()
script = jedi.api.Script(source=source, line=args.line, column=args.column, path=args.path)
usages = script.usages()
if len(usages) == 0:
print("No references found")
return
results = []
for u in usages:
if u.is_definition():
continue
results.append({
"path": u.module_path,
"line": u.line,
"column": u.column,
})
json.dump(results, sys.stdout, sort_keys=True)
def main():
parser = argparse.ArgumentParser(description="")
parser.add_argument('--path', help='The path of the file in the file system.', default="")
parser.add_argument('--line', help='The line to perform actions on (starting with 1).', default=1, type=int)
parser.add_argument('--column', help='The column of the cursor (starting with 0).', default=0, type=int)
subparsers = parser.add_subparsers(help="", dest="subcmd")
hover_parser = subparsers.add_parser("hover", help="")
definition_parser = subparsers.add_parser("definition", help="")
references_parser = subparsers.add_parser("references", help="")
args = parser.parse_args()
if args.path == "":
print("ls-python: --path is empty")
sys.exit(2)
elif args.line < 1:
print("ls-python: --line is not valid")
sys.exit(2)
elif args.column < 0:
print("ls-python: --column is not valid")
sys.exit(2)
if args.subcmd == "hover":
hover(args)
elif args.subcmd == "definition":
definition(args)
elif args.subcmd == "references":
references(args)
else:
print("Sorry, I don't understand..")
if __name__ == '__main__':
main()