forked from vishnevskiy/ElixirSublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elixir_sublime.py
322 lines (258 loc) · 9.56 KB
/
elixir_sublime.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import tempfile
import os
import sublime
import sublime_plugin
import subprocess
import json
import socket
import re
import webbrowser
_socket = None
_logfile = open(os.path.join(tempfile.gettempdir(), 'ElixirSublime.log'), 'w')
_sessions = {}
def plugin_loaded():
run_mix_task('deps.get')
global _socket
_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
_socket.bind(('', 0))
_socket.listen(1)
_socket.settimeout(5)
def plugin_unloaded():
if _logfile:
_logfile.close()
if _socket:
_socket.close()
for session in _sessions.values():
session.close()
def run_mix_task(cmd):
settings = sublime.load_settings('Preferences.sublime-settings')
cwd = os.path.join(os.path.dirname(__file__), 'sublime_completion')
env = os.environ.copy()
try:
env['PATH'] += os.pathsep + settings.get('env')['PATH']
except (TypeError, ValueError, KeyError):
pass
if _socket:
env['ELIXIR_SUBLIME_PORT'] = str(_socket.getsockname()[1])
if sublime.platform() == "windows":
# on Windows, mix is a .bat file, which `subprocess` can't just launch like that. Use cmd.exe to launch the .bat file
launcher = ['cmd', '/c', 'mix']
# don't show the console window
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
else:
launcher = ['mix']
startupinfo = None
return subprocess.Popen(
launcher + cmd.split(),
cwd=cwd,
stderr=_logfile.fileno(),
stdout=_logfile.fileno(),
env=env,
startupinfo=startupinfo)
def find_mix_project(cwd=None):
cwd = cwd or os.getcwd()
if cwd == os.path.realpath('/'):
return None
elif os.path.exists(os.path.join(cwd, 'mix.exs')):
return cwd
else:
return find_mix_project(os.path.dirname(cwd))
def find_ebin_folders(mix_project):
paths = []
if mix_project is not None:
lib_path = os.path.join(mix_project, '_build/dev/lib')
for lib in os.listdir(lib_path):
paths.append(os.path.join(lib_path, lib, 'ebin'))
return paths
def is_elixir_file(filename):
return filename and filename.endswith(('.ex', '.exs'))
def is_erlang_file(filename):
return filename and filename.endswith('erl')
def expand_selection(view, point_or_region, aliases={}):
region = view.expand_by_class(point_or_region,
sublime.CLASS_WORD_START |
sublime.CLASS_WORD_END, ' (){},[]%&')
selection = view.substr(region).strip()
if aliases:
parts = selection.split('.')
for alias, canonical in aliases.items():
if alias == parts[0]:
parts[0] = canonical
return '.'.join(parts)
return selection
def do_focus(fn, pattern):
window = sublime.active_window()
view = window.open_file(fn)
if view.is_loading():
focus(fn, pattern)
else:
window.focus_view(view)
if pattern:
r = view.find(pattern, 0)
if r:
row, col = view.rowcol(r.begin())
pt = view.text_point(row, col)
r = sublime.Region(pt, pt)
view.sel().clear()
view.sel().add(r)
view.show(pt)
def focus(fn, pattern, timeout=25):
sublime.set_timeout(lambda: do_focus(fn, pattern), timeout)
def focus_function(fn, function):
focus(fn, 'def(p|macrop?)?\s%s\(?' % function)
def find_aliases(view):
aliases = {}
for region in view.find_all('^[\s\t]*?alias\s.+?$'):
alias_line = view.substr(region).strip()
for (pattern, replacer) in [
(r'^alias (.+?)\.(.+?)$', lambda prefix, alias: '%s.%s' % (prefix, alias)),
(r'^alias (.+?), as: (.+)$', lambda prefix, _: prefix),
]:
matches = re.findall(pattern, alias_line)
if matches:
[(prefix, alias)] = matches
aliases[alias] = replacer(prefix, alias)
break
return aliases
class ElixirSession(object):
@classmethod
def ensure(cls, cwd=None):
mix_project = find_mix_project(cwd)
session = _sessions.get(mix_project)
if not session:
session = cls(mix_project)
_sessions[mix_project] = session
if not session.alive:
session.connect()
return session
def __init__(self, mix_project):
self.mix_project = mix_project
self.reset()
@property
def alive(self):
return self.process is not None and self.process.returncode is None
def reset(self):
self.socket = None
self.file = None
self.process = None
def connect(self):
self.process = run_mix_task('run --no-halt')
self.socket, _ = _socket.accept()
self.socket.settimeout(5)
self.file = self.socket.makefile()
for lib_path in find_ebin_folders(self.mix_project):
self.send('PATH', lib_path)
def send(self, cmd, args):
try:
self.socket.send(str.encode(cmd))
self.socket.send(b' ')
self.socket.send(str.encode(args))
self.socket.send(b'\n')
return True
except (OSError, IOError):
self.reset()
return False
def recv(self):
try:
return self.file.readline().strip()
except (OSError, IOError):
self.reset()
return None
def close(self):
if self.socket:
self.socket.close()
if self.process:
self.process.kill()
class ElixirGotoDefinition(sublime_plugin.TextCommand):
def run(self, edit):
aliases = find_aliases(self.view)
selection = expand_selection(self.view, self.view.sel()[0], aliases=aliases)
if selection:
session = ElixirSession.ensure(os.path.dirname(self.view.file_name()))
if session.send('GOTO', selection):
goto = json.loads(session.recv())
if goto:
source = goto['source']
function = goto['function']
if not os.path.exists(source):
url = None
if is_erlang_file(source):
matches = re.findall(r'/lib/(.+?)/src/(.+?)\.erl$', source)
if matches:
[(_, module)] = matches
url = 'http://www.erlang.org/doc/man/%s.html' % module
if function:
url += '#%s-%s' % (goto['function'], goto['arities'][0])
elif is_elixir_file(source):
matches = re.findall(r'/lib/(.+?)/lib/(.+?)\.exs?$', source)
if matches:
[(lib, _)] = matches
url = 'http://elixir-lang.org/docs/stable/%s/%s.html' % (lib, goto['module'])
if function:
url += '#%s/%s' % (goto['function'], goto['arities'][0])
if url:
webbrowser.open(url)
return
if function:
if is_erlang_file(source):
focus(source, '^%s\(' % function)
else:
focus_function(source, function)
elif is_elixir_file(source):
focus(source, 'defmodule?\s%(module)s\sdo' % goto)
else:
focus_function(self.view.file_name(), selection)
class ElixirAutocomplete(sublime_plugin.EventListener):
def on_activated_async(self, view):
self.on_load_async(view)
def on_load_async(self, view):
filename = view.file_name()
if is_elixir_file(filename):
ElixirSession.ensure(os.path.dirname(filename))
def on_query_completions(self, view, prefix, locations):
if not is_elixir_file(view.file_name()):
return None
aliases = find_aliases(view)
session = ElixirSession.ensure(os.path.dirname(view.file_name()))
if not session.send('COMPLETE', expand_selection(view, locations[0], aliases=aliases)):
return None
completions = session.recv()
if not completions:
return None
seen_completions = set()
rv = []
for completion in json.loads(completions):
seen_completions.add(completion['name'])
if completion['type'] == 'module':
rv.append(('%(name)s\t%(name)s' % completion, completion['content']))
else:
rv.append(('%(name)s\t%(name)s/%(arity)s' % completion, completion['content']))
for completion in view.extract_completions(prefix):
if completion not in seen_completions:
rv.append((completion,))
return rv
try:
from SublimeLinter.lint import Linter
class ElixirLinter(Linter):
syntax = 'elixir'
executable = 'elixirc'
tempfile_suffix = 'ex'
regex = (
r"^[^ ].+:(?P<line>\d+):"
r"(?:(?P<warning>\swarning:\s)|(?P<error>\s))"
r"(?P<message>.+)"
)
def cmd(self):
command = [
self.executable_path,
'--warnings-as-errors',
'--ignore-module-conflict',
'-o', os.path.join(tempfile.gettempdir(), 'SublimeLinter3')
]
for path in find_ebin_folders(find_mix_project()):
command.extend(['-pa', path])
return command
except ImportError:
pass