forked from SublimeHaskell/SublimeHaskell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sublime_haskell_common.py
732 lines (587 loc) · 23.2 KB
/
sublime_haskell_common.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
import errno
import fnmatch
import os
import re
import json
import sublime
import sublime_plugin
import subprocess
import threading
import time
from sys import version
PyV3 = version[0] == "3"
# Maximum seconds to wait for window to appear
# This dirty hack is used in wait_for_window function
MAX_WAIT_FOR_WINDOW = 10
# Panel for SublimeHaskell errors
SUBLIME_ERROR_PANEL_NAME = 'haskell_sublime_load'
# Used to detect hs-source-dirs for project
CABAL_INSPECTOR_EXE_PATH = None
# unicode function
def to_unicode(s):
return s if PyV3 else unicode(s)
# Object with lock attacjed
class LockedObject(object):
"""
Object with lock
x = LockedObject(some_value)
with x as v:
v...
"""
def __init__(self, obj, lock = None):
self.object_lock = lock if lock else threading.Lock()
self.object = obj
def __enter__(self):
self.object_lock.__enter__()
return self.object
def __exit__(self, type, value, traceback):
self.object_lock.__exit__()
# Setting can't be get from not main threads
# So we using a trick:
# Once setting loaded from main thread, it also stored in sublime_haskell_settings dictionary
# and callback attached to update its value
# And then setting can be get from any thread with get_setting_async
# But setting must be loaded at least once from main thread
# Some settings are loaded only from secondary threads, so we loading them here for first time
def preload_settings():
# Now we can use get_setting_async for 'add_to_PATH' safely
get_setting('add_to_PATH')
get_setting('use_cabal_dev')
get_setting('cabal_dev_sandbox')
get_setting('cabal_dev_sandbox_list')
get_setting('enable_auto_build')
get_setting('show_output_window')
get_setting('enable_ghc_mod')
get_setting('enable_hdevtools')
get_setting('enable_hdocs')
get_setting('snippet_replace')
get_setting('ghc_opts')
# SublimeHaskell settings dictionary
# used to retrieve it async from any thread
sublime_haskell_settings = LockedObject({})
def is_enabled_haskell_command(view = None, must_be_project=True, must_be_main=False, must_be_file = False):
"""Returns True if command for .hs can be invoked"""
window, view, file_shown_in_view = get_haskell_command_window_view_file_project(view)
if not window or not view:
return False
if must_be_file and not file_shown_in_view:
return False
syntax_file_for_view = view.settings().get('syntax').lower()
if 'haskell' not in syntax_file_for_view:
return False
if not must_be_project:
return True
cabal_project_dir = get_cabal_project_dir_of_view(view)
if not cabal_project_dir:
return False
return True
def get_haskell_command_window_view_file_project(view = None):
"""Returns window, view and file"""
if view:
return view.window(), view, view.file_name()
window = sublime.active_window()
view = None
if window:
view = window.active_view()
file_name = None
if view:
file_name = view.file_name()
return window, view, file_name
def decode_bytes(s):
if s is None:
return None
return s.decode('utf-8')
def encode_bytes(s):
if s is None:
return None
return s.encode('utf-8')
# Get extended environment from settings for Popen
def get_extended_env():
ext_env = dict(os.environ)
PATH = os.getenv('PATH') or ""
add_to_PATH = get_setting_async('add_to_PATH', [])
if not PyV3:
# convert unicode strings to strings (for Python < 3) as env can contain only strings
add_to_PATH = map(str, add_to_PATH)
ext_env['PATH'] = os.pathsep.join(add_to_PATH + [PATH])
return ext_env
def call_and_wait(command, **popen_kwargs):
return call_and_wait_with_input(command, None, **popen_kwargs)
def call_no_wait(command, **popen_kwargs):
"""Run the specified command with no block"""
if subprocess.mswindows:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
popen_kwargs['startupinfo'] = startupinfo
extended_env = get_extended_env()
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
env=extended_env,
**popen_kwargs)
def call_and_wait_with_input(command, input_string, **popen_kwargs):
"""Run the specified command, block until it completes, and return
the exit code, stdout, and stderr.
Extends os.environment['PATH'] with the 'add_to_PATH' setting.
Additional parameters to Popen can be specified as keyword parameters."""
if subprocess.mswindows:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
popen_kwargs['startupinfo'] = startupinfo
# For the subprocess, extend the env PATH to include the 'add_to_PATH' setting.
extended_env = get_extended_env()
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
env=extended_env,
**popen_kwargs)
stdout, stderr = process.communicate(encode_bytes(input_string))
exit_code = process.wait()
return (exit_code, decode_bytes(stdout), decode_bytes(stderr))
def log(message):
print(u'Sublime Haskell: {0}'.format(message))
def get_cabal_project_dir_and_name_of_view(view):
"""Return the path to the .cabal file project for the source file in the
specified view. The view must show a saved file, the file must be Haskell
source code, and the file must be under a directory containing a .cabal file.
Otherwise, return None.
"""
# Check that the view is showing a saved file:
file_shown_in_view = view.file_name()
if file_shown_in_view is None:
return None, None
# Check that the file is Haskell source code:
syntax_file_for_view = view.settings().get('syntax').lower()
if 'haskell' not in syntax_file_for_view:
return None, None
return get_cabal_project_dir_and_name_of_file(file_shown_in_view)
def get_cabal_project_dir_of_view(view):
return get_cabal_project_dir_and_name_of_view(view)[0]
def get_cabal_project_dir_and_name_of_file(filename):
"""Return the path to the .cabal file and name of project for the specified file."""
# Check that a .cabal file is present:
directory_of_file = os.path.dirname(filename)
cabal_file_path = find_file_in_parent_dir(directory_of_file, '*.cabal')
if cabal_file_path is None:
return None, None
# Return the directory containing the .cabal file:
project_path, cabal_file = os.path.split(cabal_file_path)
project_name = os.path.splitext(cabal_file)[0]
return project_path, project_name
def get_cabal_project_dir_of_file(filename):
"""Return the path to the .cabal file project for the specified file."""
return get_cabal_project_dir_and_name_of_file(filename)[0]
def get_cabal_in_dir(cabal_dir):
"""Return .cabal file for cabal directory"""
for entry in os.listdir(cabal_dir):
if entry.endswith(".cabal"):
project_name = os.path.splitext(entry)[0]
return (project_name, os.path.join(cabal_dir, entry))
return (None, None)
def find_file_in_parent_dir(subdirectory, filename_pattern):
"""Look for a file with the specified name in a parent directory of the
specified directory. If found, return the file's full path. Otherwise,
return None."""
current_dir = subdirectory
while True:
# See if the current directory contains the desired file:
for name in os.listdir(current_dir):
full_path = os.path.join(current_dir, name)
matches_pattern = fnmatch.fnmatch(name, filename_pattern)
if matches_pattern and os.path.isfile(full_path):
return full_path
# Get the next directory up:
last_dir = current_dir
current_dir = os.path.dirname(current_dir)
# Check to see if we have reached the root directory:
if last_dir == current_dir:
return None
def are_paths_equal(path, other_path):
"Test whether filesystem paths are equal."
path = os.path.abspath(path)
other_path = os.path.abspath(other_path)
return path == other_path
def current_cabal():
"""
Returns current cabal-dev sandbox or 'cabal'
"""
if get_setting_async('use_cabal_dev'):
return get_setting_async('cabal_dev_sandbox')
else:
return 'cabal'
def current_sandbox():
"""
Returns current cabal-def sandbox or None
"""
if get_setting_async('use_cabal_dev'):
return get_setting_async('cabal_dev_sandbox')
else:
return None
def cabal_name_by_sandbox(sandbox):
if not sandbox:
return current_cabal()
return sandbox
def sandbox_by_cabal_name(cabal):
if cabal == 'cabal':
return None
return cabal
def attach_sandbox(cmd, sandbox = None):
"""Attach sandbox arguments to command"""
if not sandbox:
sandbox = get_setting_async('cabal_dev_sandbox')
if len(sandbox) > 0:
return cmd + ['-s', sandbox]
return cmd
def try_attach_sandbox(cmd, sandbox = None):
"""Attach sandbox if use_cabal_dev enabled"""
if not get_setting_async('use_cabal_dev'):
return cmd
return attach_sandbox(cmd, sandbox)
def attach_cabal_sandbox(cmd, cabal = None):
"""
Attach sandbox if cabal is sandbox path, attach nothing on 'cabal',
and attach sandbox by settings on None
"""
if not cabal:
cabal = current_cabal()
if cabal == 'cabal':
return cmd
return cmd + ['-s', cabal]
def get_settings():
return sublime.load_settings("SublimeHaskell.sublime-settings")
def save_settings():
sublime.save_settings("SublimeHaskell.sublime-settings")
def get_setting(key, default=None):
"This should be used only from main thread"
# Get setting
result = get_settings().get(key, default)
# Key was not retrieved, save its value and add callback to auto-update
with sublime_haskell_settings as settings:
if key not in settings:
get_settings().add_on_change(key, lambda: update_setting(key))
settings[key] = result
return result
def update_setting(key):
"Updates setting as it was changed"
get_setting(key)
def get_setting_async(key, default=None):
"""
Get setting from any thread
Note, that setting must be loaded before by get_setting from main thread
"""
# Reload it in main thread for future calls of get_setting_async
sublime.set_timeout(lambda: update_setting(key), 0)
with sublime_haskell_settings as settings:
if key not in settings:
# Load it in main thread, but for now all we can do is result default
return default
return settings[key]
def set_setting(key, value):
"""Set setting and update dictionary"""
with sublime_haskell_settings as settings:
settings[key] = value
get_settings().set(key, value)
save_settings()
def set_setting_async(key, value):
sublime.set_timeout(lambda: set_setting(key, value), 0)
def ghci_package_db(cabal = None):
if cabal == 'cabal':
return None
dev = True if cabal else get_setting_async('use_cabal_dev')
box = cabal if cabal else get_setting_async('cabal_dev_sandbox')
if dev and box:
package_conf = (filter(lambda x: re.match('packages-(.*)\.conf', x), os.listdir(box)) + [None])[0]
if package_conf:
return os.path.join(box, package_conf)
return None
def ghci_append_package_db(cmd, cabal = None):
package_conf = ghci_package_db(cabal)
if package_conf:
cmd.extend(['-package-db', package_conf])
return cmd
def get_source_dir(filename):
"""
Get root of hs-source-dirs for filename in project
"""
if not filename:
return os.path.expanduser('~')
# return os.getcwd()
(cabal_dir, project_name) = get_cabal_project_dir_and_name_of_file(filename)
if not cabal_dir:
return os.path.dirname(filename)
_project_name, cabal_file = get_cabal_in_dir(cabal_dir)
exit_code, out, err = call_and_wait([CABAL_INSPECTOR_EXE_PATH, cabal_file])
if exit_code == 0:
info = json.loads(out)
dirs = ["."]
if 'error' not in info:
# collect all hs-source-dirs
if info['library']:
dirs.extend(info['library']['info']['source-dirs'])
for i in info['executables']:
dirs.extend(i['info']['source-dirs'])
for t in info['tests']:
dirs.extend(t['info']['source-dirs'])
paths = [os.path.abspath(os.path.join(cabal_dir, d)) for d in dirs]
paths.sort(key = lambda p: -len(p))
for p in paths:
if filename.startswith(p):
return p
return os.path.dirname(filename)
def get_cwd(filename = None):
"""
Get cwd for filename: cabal project path, file path or os.getcwd()
"""
cwd = (get_cabal_project_dir_of_file(filename) or os.path.dirname(filename)) if filename else os.getcwd()
return cwd
def get_ghc_opts(filename = None, add_package_db = True, cabal = None):
"""
Gets ghc_opts, used in several tools, as list with extra '-package-db' option and '-i' option if filename passed
"""
ghc_opts = get_setting_async('ghc_opts')
if not ghc_opts:
ghc_opts = []
if add_package_db:
package_db = ghci_package_db(cabal = cabal)
if package_db:
ghc_opts.append('-package-db {0}'.format(package_db))
if filename:
ghc_opts.append('-i {0}'.format(get_source_dir(filename)))
return ghc_opts
def get_ghc_opts_args(filename = None, add_package_db = True, cabal = None):
"""
Same as ghc_opts, but uses '-g' option for each option
"""
opts = get_ghc_opts(filename, add_package_db, cabal)
args = []
for opt in opts:
args.extend(["-g", opt])
return args
def call_ghcmod_and_wait(arg_list, filename=None, cabal = None):
"""
Calls ghc-mod with the given arguments.
Shows a sublime error message if ghc-mod is not available.
"""
ghc_opts_args = get_ghc_opts_args(filename, add_package_db = False, cabal = cabal)
try:
command = attach_cabal_sandbox(['ghc-mod'] + arg_list + ghc_opts_args, cabal)
# log('running ghc-mod: {0}'.format(command))
# Set cwd to user directory
# Otherwise ghc-mod will fail with 'cannot satisfy package...'
# Seems, that user directory works well
# Current source directory is set with -i argument in get_ghc_opts_args
#
# When cabal project is available current directory is set to the project root
# to avoid troubles with possible template haskell openFile calls
ghc_mod_current_dir = get_source_dir(filename)
if filename:
cabal_project_dir = get_cabal_project_dir_of_file(filename)
if cabal_project_dir:
ghc_mod_current_dir = cabal_project_dir
exit_code, out, err = call_and_wait(command, cwd=ghc_mod_current_dir)
if exit_code != 0:
raise Exception("%s exited with status %d and stderr: %s" % (' '.join(command), exit_code, err))
return crlf2lf(out)
except OSError as e:
if e.errno == errno.ENOENT:
output_error(sublime.active_window(),
"SublimeHaskell: ghc-mod was not found!\n"
+ "It is used for LANGUAGE and import autocompletions and type inference.\n"
+ "Try adjusting the 'add_to_PATH' setting.\n"
+ "You can also turn this off using the 'enable_ghc_mod' setting.")
# Re-raise so that calling code doesn't try to work on the `None` return value
raise e
def wait_for_window_callback(on_appear, seconds_to_wait):
window = sublime.active_window()
if window:
on_appear(window)
return
if seconds_to_wait == 0:
return
sublime.set_timeout(lambda: wait_for_window_callback(on_appear, seconds_to_wait - 1), 1000)
def wait_for_window(on_appear, seconds_to_wait=MAX_WAIT_FOR_WINDOW):
"""
Wait for window to appear on startup
It's dirty hack, but I have no idea how to make it better
"""
sublime.set_timeout(lambda: wait_for_window_callback(on_appear, seconds_to_wait), 0)
class SublimeHaskellOutputText(sublime_plugin.TextCommand):
"""
Helper command to output text to any view
TODO: Is there any default command for this purpose?
"""
def run(self, edit, text = None):
if not text:
return
self.view.insert(edit, self.view.size(), text)
def output_error(window, text):
"Write text to Sublime's output panel with important information about SublimeHaskell error during load"
output_view = window.get_output_panel(SUBLIME_ERROR_PANEL_NAME)
output_view.set_read_only(False)
output_view.run_command('sublime_haskell_output_text', {
'text': text})
output_view.set_read_only(True)
window.run_command('show_panel', {'panel': 'output.' + SUBLIME_ERROR_PANEL_NAME})
class SublimeHaskellError(RuntimeError):
def __init__(self, what):
self.reason = what
def sublime_status_message(msg):
"""
Pure msg with 'SublimeHaskell' prefix and set_timeout
"""
sublime.set_timeout(lambda: sublime.status_message(u'SublimeHaskell: {0}'.format(msg)), 0)
def show_status_message(msg, isok = None):
"""
Show status message with check mark (isok = true), ballot x (isok = false) or ... (isok = None)
"""
mark = u'...'
if isok is not None:
mark = u' \u2714' if isok else u' \u2718'
sublime_status_message(u'{0}{1}'.format(msg, mark))
def with_status_message(msg, action):
"""
Show status message for action with check mark or with ballot x
Returns whether action exited properly
"""
try:
show_status_message(msg)
action()
show_status_message(msg, True)
return True
except SublimeHaskellError as e:
show_status_message(msg, False)
log(e.reason)
return False
def crlf2lf(s):
" CRLF -> LF "
if not s:
return ''
return s.replace('\r\n', '\n')
class StatusMessage(threading.Thread):
messages = {}
# List of ((priority, time), StatusMessage)
# At start, messages adds itself to list, at cancel - removes
# First element of list is message with highest priority
priorities_lock = threading.Lock()
priorities = []
def __init__(self, msg, timeout, priority):
super(StatusMessage, self).__init__()
self.interval = 0.5
self.start_timeout = timeout
self.timeout = timeout
self.priority = priority
self.msg = msg
self.times = 0
self.event = threading.Event()
self.event.set()
self.timer = None
def run(self):
self.add_to_priorities()
try:
self.update_message()
while self.event.is_set():
self.timer = threading.Timer(self.interval, self.update_message)
self.timer.start()
self.timer.join()
finally:
self.remove_from_priorities()
def cancel(self):
self.event.clear()
if self.timer:
self.timer.cancel()
def update_message(self):
dots = self.times % 4
self.times += 1
self.timeout -= self.interval
if self.is_highest_priority():
sublime_status_message(u'{0}{1}'.format(self.msg, '.' * dots))
if self.timeout <= 0:
self.cancel()
def add_to_priorities(self):
with StatusMessage.priorities_lock:
StatusMessage.priorities.append(((self.priority, time.clock()), self))
StatusMessage.priorities.sort(key = lambda x: (-x[0][0], x[0][1], x[1]))
def remove_from_priorities(self):
with StatusMessage.priorities_lock:
StatusMessage.priorities = [(i, msg) for i, msg in StatusMessage.priorities if msg != self]
def is_highest_priority(self):
with StatusMessage.priorities_lock:
if StatusMessage.priorities:
return StatusMessage.priorities[0][1] == self
else:
return False
def change_message(self, new_msg):
# There's progress, don't timeout
self.timeout = self.start_timeout
self.msg = new_msg
def show_status_message_process(msg, isok = None, timeout = 300, priority = 0):
"""
Same as show_status_message, but shows permanently until called with isok not None
There can be only one message process in time, message with highest priority is shown
For example, when building project, there must be only message about building
"""
if isok is not None:
if msg in StatusMessage.messages:
StatusMessage.messages[msg].cancel()
del StatusMessage.messages[msg]
show_status_message(msg, isok)
else:
if msg in StatusMessage.messages:
StatusMessage.messages[msg].cancel()
StatusMessage.messages[msg] = StatusMessage(msg, timeout, priority)
StatusMessage.messages[msg].start()
def is_haskell_source(view = None):
window, view, file_shown_in_view = get_haskell_command_window_view_file_project(view)
if not window or not view:
return False
syntax_file_for_view = view.settings().get('syntax').lower()
if not syntax_file_for_view.endswith("Haskell.tmLanguage".lower()):
return False
return True
class with_status_message(object):
def __init__(self, msg, isok, show_message):
self.msg = msg
self.isok = isok
self.show_message = show_message
def __enter__(self):
self.show_message(self.msg)
return self
def __exit__(self, type, value, traceback):
if type:
self.show_message(self.msg, False)
else:
self.show_message(self.msg, self.isok)
def ok(self):
self.isok = True
def fail(self):
self.isok = False
def change_message(self, new_msg):
if self.msg in StatusMessage.messages:
StatusMessage.messages[self.msg].change_message(new_msg)
def percentage_message(self, current, total = 100):
self.change_message('{0} ({1}%)'.format(self.msg, int(current * 100 / total)))
def status_message(msg, isok = True):
return with_status_message(msg, isok, show_status_message)
def status_message_process(msg, isok = True, timeout = 300, priority = 0):
return with_status_message(msg, isok, lambda m, ok = None: show_status_message_process(m, ok, timeout, priority))
def sublime_haskell_package_path():
"""Get the path to where this package is installed"""
return os.path.dirname(os.path.realpath(__file__))
def sublime_haskell_cache_path():
"""Get the path where compiled tools and caches are stored"""
return os.path.join(sublime_haskell_package_path(), os.path.expandvars(get_setting('cache_path', '.')))
def plugin_loaded():
global CABAL_INSPECTOR_EXE_PATH
package_path = sublime_haskell_package_path()
cache_path = sublime_haskell_cache_path()
log("store compiled tools and caches to {0}".format(cache_path))
if not os.path.exists(cache_path):
os.makedirs(cache_path)
CABAL_INSPECTOR_EXE_PATH = os.path.join(cache_path, 'CabalInspector')
preload_settings()
if int(sublime.version()) < 3000:
plugin_loaded()