-
Notifications
You must be signed in to change notification settings - Fork 24
/
isort_file.py
61 lines (45 loc) · 1.57 KB
/
isort_file.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
import os
import sys
import sublime
import sublime_plugin
from .isort import SortImports
sys.path.append(os.path.dirname(__file__))
class IsortCommand(sublime_plugin.TextCommand):
view = None
def get_region(self, view):
return sublime.Region(0, view.size())
def get_buffer_contents(self, view):
return view.substr(self.get_region(view))
def set_view(self):
self.view = sublime.active_window().active_view()
return self.view
def get_view(self):
if self.view is None:
return self.set_view()
return self.view
def set_cursor_back(self, begin_positions):
this_view = self.get_view()
for pos in begin_positions:
this_view.sel().add(pos)
def get_positions(self):
pos = []
for region in self.get_view().sel():
pos.append(region)
return pos
def get_settings(self):
profile = sublime.active_window().active_view().settings().get('isort')
return profile or {}
def run(self, edit):
this_view = self.get_view()
current_positions = self.get_positions()
this_contents = self.get_buffer_contents(this_view)
settings = self.get_settings()
sorted_imports = SortImports(
file_contents=this_contents,
**settings
).output
this_view.replace(edit, self.get_region(this_view), sorted_imports)
# Our sel has moved now..
remove_sel = this_view.sel()[0]
this_view.sel().subtract(remove_sel)
self.set_cursor_back(current_positions)