-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonOutline.py
More file actions
51 lines (39 loc) · 1.62 KB
/
pythonOutline.py
File metadata and controls
51 lines (39 loc) · 1.62 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
import os
import sys
import sublime
import sublime_plugin
sys.path.append(os.path.dirname(__file__))
import classBrowser # noqa e402
class PythonOutlineCommand(sublime_plugin.TextCommand):
def run(self, edit):
'''
folds = [
(1, 5),
(18, 22),
(14, 17),
(6 ,13),
(23, 24),
]
'''
# Pullout the start and end lines of each method and function into pairs.
structure = classBrowser._readmodule(self.view.substr(sublime.Region(0, self.view.size())))
folds = []
for a, b in structure.items():
if isinstance(b, classBrowser.Class):
for m in b.methods:
folds.append(( b.methods[m], b.methodends[m]) )
else:
folds.append(( b.lineno, b.linenoend ))
lines = self.view.split_by_newlines( sublime.Region(0, self.view.size()) )
# Since classBrowswer returns lines as 1-based (human-readable), the
# first line is fine (we want to see the def) but the end needs to offset.
for start, end in folds[:-1]:
endRegion = lines[end-1]
self.view.fold( sublime.Region( lines[start].a - 1, endRegion.b ) )
# Handle if the last line is at the end
start, end = folds[-1]
if end < len(lines):
endRegion = lines[end-1]
else:
endRegion = lines[-1]
self.view.fold( sublime.Region( lines[start].a - 1, endRegion.b ) )