-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path__init__.py
161 lines (135 loc) · 5.12 KB
/
__init__.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import sublime
import sublime_plugin
import subprocess
import os
from refact.src.utils import *
from refact.src.refact_process import RefactProcessWrapper
from refact.src.refact_sessions import RefactSessionManager, RefactSession
start_refact = False
refact_session_manager = None
class RefactAutocomplete(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
if start_refact:
refact_session_manager.get_session(view).show_completions(prefix, locations)
def on_modified(self, view):
if not start_refact:
return
session = refact_session_manager.get_session(view)
session.notify_document_update()
session.update_completion()
def on_close(self, view):
if not start_refact:
return
session = refact_session_manager.get_session(view)
session.notify_close()
def on_post_save(self, view):
if not start_refact:
return
session = refact_session_manager.get_session(view)
session.notify_save()
def on_query_context(self, view, key, operator, operand, match_all):
if start_refact:
if key == "refact.show_completion":
return refact_session_manager.get_session(view).completion_visible()
def on_post_text_command(self, view, command_name, args):
if start_refact:
session = refact_session_manager.get_session(view)
if command_name == "insert" and args['characters'] == '\n':
session.clear_completion()
cursor_point = get_cursor_point(view)
session.show_completions(get_cursor_line(view), [cursor_point], True)
def on_text_command(self, view, command_name, args):
if start_refact:
session = refact_session_manager.get_session(view)
if command_name == "commit_completion":
if not session.get_completion() is None:
session.accept_completion()
return ["noop"]
if command_name == "auto_complete":
session.accept_completion()
elif command_name == "insert" and args['characters'] == '\t':
session.accept_completion()
elif command_name == "reindent":
session.accept_completion()
elif command_name == "hide_popup":
session.clear_completion()
# elif command_name == "left_delete" or command_name == "right_delete":
# session.clear_completion()
elif command_name == "move" or command_name == "move_to":
session.clear_completion()
elif command_name == "drag_select":
session.clear_completion()
def plugin_loaded():
global refact_session_manager
global start_refact
s = sublime.load_settings("refact.sublime-settings")
pause_completion = s.get("pause_completion", False)
if pause_completion:
sublime.status_message("⏸️ refact.ai")
else:
refact_start()
def refact_start():
global refact_session_manager
global start_refact
if refact_session_manager:
refact_session_manager.start()
else:
refact_session_manager = RefactSessionManager()
start_refact= True
class RefactStartCommand(sublime_plugin.TextCommand):
def run(self, edit):
refact_start()
class RefactStopCommand(sublime_plugin.TextCommand):
def run(self, edit):
global start_refact
start_refact= False
refact_session_manager.get_session(self.view).clear_completion()
class RefactAddTextCommand(sublime_plugin.TextCommand):
def run(self, edit, text = '', position = None):
view = self.view
position = position if not position is None else view.sel()[0].b
cursor_position = self.view.rowcol(self.view.sel()[0].b)
line = view.line(position)
self.view.insert(edit, line.b, text)
point = self.view.text_point(cursor_position[0], cursor_position[1], clamp_column = True)
set_cursor_position(self.view, sublime.Region(point, point))
class RefactClearSpaceCommand(sublime_plugin.TextCommand):
def run(self, edit, position = None):
if position is None:
return
view = self.view
cursor_position = self.view.rowcol(self.view.sel()[0].b)
line = view.line(position)
text = get_line(view, position)
if len(text) > 0 and text[len(text) - 1].isspace():
region = sublime.Region(line.b - 1, line.b)
view.erase(edit, region)
point = self.view.text_point(cursor_position[0], cursor_position[1], clamp_column = True)
set_cursor_position(self.view, sublime.Region(point, point))
class RefactReplaceTextCommand(sublime_plugin.TextCommand):
def run(self, edit, text = '', position = None):
view = self.view
position = position if not position is None else view.sel()[0].b
line = view.line(position)
view.replace(edit, sublime.Region(line.a, line.b), '')
view.insert(edit, line.a, text)
class RefactAcceptCompletion(sublime_plugin.TextCommand):
def run(self, edit):
refact_session_manager.get_session(self.view).accept_completion()
class RefactPause(sublime_plugin.TextCommand):
def run(self, edit):
global start_refact
start_refact= False
s = sublime.load_settings("refact.sublime-settings")
pause_status = s.get("pause_completion", False)
pause_status = not pause_status
s.set("pause_completion", pause_status)
sublime.save_settings("refact.sublime-settings")
if not pause_status:
refact_start()
else:
if refact_session_manager:
refact_session_manager.shutdown()
class RefactClearCompletion(sublime_plugin.TextCommand):
def run(self, edit):
refact_session_manager.get_session(self.view).clear_completion()