forked from SublimeHaskell/SublimeHaskell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hdocs.py
79 lines (60 loc) · 2.12 KB
/
hdocs.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
import sublime
import json
import time
if int(sublime.version()) < 3000:
from sublime_haskell_common import *
else:
from SublimeHaskell.sublime_haskell_common import *
def call_hdocs_and_wait(args, filename = None, cabal = None):
if not hdocs_enabled():
return None
ghc_opts_args = get_ghc_opts_args(filename, cabal = cabal)
source_dir = get_source_dir(filename)
try:
command = ['hdocs'] + args + ghc_opts_args
log(command)
exit_code, out, err = call_and_wait(command, cwd = source_dir)
if exit_code != 0:
raise Exception("hdocs exited with status %d and stderr: %s" % (exit_code, err))
return crlf2lf(out)
except OSError as e:
if e.errno == errno.ENOENT:
sublime.set_timeout(lambda: output_error(sublime.active_window(), "SublimeHaskell: hdocs was not found!\n'enable_hdocs' is set to False"), 0)
set_setting_async('enable_hdocs', False)
return None
except Exception as e:
log('hdocs fails with {0}'.format(e))
return None
def module_docs(module_name, cabal = None):
if not hdocs_enabled():
return None
try:
contents = call_hdocs_and_wait(['docs', module_name], cabal = cabal)
if contents:
return json.loads(contents)
else:
return None
except Exception as e:
log('hdocs fails with {0}'.format(e))
return None
def symbol_docs(module_name, symbol_name, cabal = None):
if not hdocs_enabled():
return None
return call_hdocs_and_wait(['docs', module_name, symbol_name], cabal = cabal)
def load_module_docs(module):
if not hdocs_enabled():
return False
if module.location:
return False
if 'hdocs' in module.tags:
return False
docs = module_docs(module.name, module.cabal)
if docs:
module.tags['hdocs'] = time.clock()
for decl in module.declarations.values():
if decl.name in docs:
decl.docs = docs[decl.name]
return True
return False
def hdocs_enabled():
return get_setting_async('enable_hdocs') == True