-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.py
143 lines (110 loc) · 3.84 KB
/
plugin.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
import sublime
import sublime_plugin
from .padawan import client
def sel_end(sel):
return max(sel.a, sel.b)
def is_php_file(view):
return view.score_selector(sel_end(view.sel()[0]), "source.php") > 0
class PadawanGenerateIndexCommand(sublime_plugin.TextCommand):
def run(self, edit):
fname = self.view.file_name()
if fname is None:
return None
client.Generate(fname)
class PadawanStartServerCommand(sublime_plugin.TextCommand):
def run(self, edit):
client.StartServer()
class PadawanStopServerCommand(sublime_plugin.TextCommand):
def run(self, edit):
client.StopServer()
class PadawanRestartServerCommand(sublime_plugin.TextCommand):
def run(self, edit):
client.RestartServer()
class PadawanPluginAddCommand(sublime_plugin.TextCommand):
def run(self, edit):
def success(name):
if not name or not isinstance(name, str):
return
client.AddPlugin(name)
def on_change(name):
return
def on_cancel():
return
sublime.active_window().show_input_panel(
"Plugin Name",
"",
success,
on_change,
on_cancel
)
class PadawanPluginRemoveCommand(sublime_plugin.TextCommand):
def run(self, edit):
items = client.GetInstalledPlugins()
def success(index):
if index >= len(items):
return
name = items[index]
if not name:
return
client.RemovePlugin(name)
sublime.active_window().show_quick_panel(
items,
success,
)
class PadawanCompleter(sublime_plugin.EventListener):
def run_completion(self, view):
view.run_command('auto_complete', {
'api_completions_only': True,
'disable_auto_insert': True
})
def on_modified_async(self, view):
if not is_php_file(view):
return
cursor = view.sel()[0].b
if cursor < 1:
return
while cursor > 0:
curChar = view.substr(sublime.Region(cursor-1, cursor))
if curChar == '\\':
return self.run_completion(view)
if curChar == '$':
return self.run_completion(view)
if curChar == '(':
return self.run_completion(view)
if cursor > 1:
curChar = view.substr(sublime.Region(cursor-2, cursor))
if curChar == '->':
return self.run_completion(view)
if curChar == '::':
return self.run_completion(view)
if cursor > 3:
curChar = view.substr(sublime.Region(cursor-4, cursor))
if curChar == 'use ':
return self.run_completion(view)
if curChar == 'new ':
return self.run_completion(view)
if cursor > 9:
curChar = view.substr(sublime.Region(cursor-10, cursor))
if curChar == 'namespace ':
return self.run_completion(view)
cursor -= 1
def on_query_completions(self, view, prefix, locations):
if not is_php_file(view):
return None
fname = view.file_name()
if fname is None:
return None
column = 16
line = 35
line, column = view.rowcol(locations[0])
contents = view.substr(sublime.Region(0, view.size()))
completions = client.GetCompletion(
fname,
line+1,
column+1,
contents
)["completion"]
return (
[[c["name"], c["name"]] for c in completions],
sublime.INHIBIT_WORD_COMPLETIONS
)