Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 132 lines (104 sloc) 4.06 kB
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
1 import os
2 import re
3 import sublime
4 import sublime_plugin
5 import subprocess
6 import threading
7
8 if int(sublime.version()) < 3000:
9 from sublime_haskell_common import *
b91dcb0 @mvoidex Added util module
mvoidex authored
10 from ghci import parse_info
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
11 import symbols
12 else:
13 from SublimeHaskell.sublime_haskell_common import *
b91dcb0 @mvoidex Added util module
mvoidex authored
14 from SublimeHaskell.ghci import parse_info
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
15 import SublimeHaskell.symbols as symbols
16
17 def call_hdevtools_and_wait(arg_list, filename = None, cabal = None):
18 """
19 Calls hdevtools with the given arguments.
20 Shows a sublime error message if hdevtools is not available.
21 """
3c23066 @mvoidex Checking if hdevtools enabled
mvoidex authored
22 if not hdevtools_enabled():
23 return None
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
24
870cf1a @mvoidex Using hdocs
mvoidex authored
25 ghc_opts_args = get_ghc_opts_args(filename, cabal = cabal)
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
26 hdevtools_socket = get_setting_async('hdevtools_socket')
2216239 @mvoidex Fixed another typo
mvoidex authored
27 source_dir = get_source_dir(filename)
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
28
29 if hdevtools_socket:
30 arg_list.append('--socket={0}'.format(hdevtools_socket))
31
32 try:
7fb3078 @mvoidex Fixed column calc
mvoidex authored
33 exit_code, out, err = call_and_wait(['hdevtools'] + arg_list + ghc_opts_args, cwd = source_dir)
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
34
35 if exit_code != 0:
36 raise Exception("hdevtools exited with status %d and stderr: %s" % (exit_code, err))
37
38 return crlf2lf(out)
39
40 except OSError as e:
41 if e.errno == errno.ENOENT:
3c23066 @mvoidex Checking if hdevtools enabled
mvoidex authored
42 sublime.set_timeout(lambda: output_error(sublime.active_window(),
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
43 "SublimeHaskell: hdevtools was not found!\n"
44 + "It's used for 'symbol info' and type inference\n"
45 + "Try adjusting the 'add_to_PATH' setting.\n"
3c23066 @mvoidex Checking if hdevtools enabled
mvoidex authored
46 + "'enable_hdevtools' automatically set to False."), 0)
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
47
48 set_setting_async('enable_hdevtools', False)
49
50 return None
51
e320cd6 @mvoidex Minor fixes
mvoidex authored
52 except Exception as e:
53 log('calling to hdevtools fails with {0}'.format(e))
54 return None
55
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
56 def admin(cmds, wait = False, **popen_kwargs):
3c23066 @mvoidex Checking if hdevtools enabled
mvoidex authored
57 if not hdevtools_enabled():
58 return None
59
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
60 hdevtools_socket = get_setting_async('hdevtools_socket')
61
62 if hdevtools_socket:
63 cmds.append('--socket={0}'.format(hdevtools_socket))
64
65 command = ["hdevtools", "admin"] + cmds
66
a31e3a4 @mvoidex Fixed browse module for module in sources, catching OSError in hdevto…
mvoidex authored
67 try:
68 if wait:
69 (exit_code, stdout, stderr) = call_and_wait(command, **popen_kwargs)
70 if exit_code == 0:
71 return stdout
72 return ''
73 else:
74 call_no_wait(command, **popen_kwargs)
75 return ''
76
77 except OSError as e:
78 if e.errno == errno.ENOENT:
79 output_error(sublime.active_window(),
80 "SublimeHaskell: hdevtools was not found!\n"
81 + "It's used for 'symbol info' and type inference\n"
82 + "Try adjusting the 'add_to_PATH' setting.\n"
83 + "'enable_hdevtools' automatically set to False.")
84
85 set_setting_async('enable_hdevtools', False)
86
87 return None
88 except Exception as e:
89 log('calling to hdevtools fails with {0}'.format(e))
90 return None
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
91
92 def is_running():
93 r = admin(['--status'], wait = True)
3c23066 @mvoidex Checking if hdevtools enabled
mvoidex authored
94 if r and re.search(r'running', r):
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
95 return True
96 else:
97 return False
98
99 def start_server():
100 if not is_running():
101 admin(["--start-server"])
102
103 def hdevtools_info(filename, symbol_name, cabal = None):
104 """
105 Uses hdevtools info filename symbol_name to get symbol info
106 """
107 contents = call_hdevtools_and_wait(['info', filename, symbol_name], filename = filename, cabal = cabal)
4bd1189 @mvoidex Fixed minor bug
mvoidex authored
108 return parse_info(symbol_name, contents) if contents else None
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
109
110 def hdevtools_check(filename, cabal = None):
111 """
112 Uses hdevtools to check file
113 """
3c23066 @mvoidex Checking if hdevtools enabled
mvoidex authored
114 return call_hdevtools_and_wait(['check', filename], filename = filename, cabal = cabal)
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
115
116 def hdevtools_type(filename, line, column, cabal = None):
117 """
118 Uses hdevtools to infer type
119 """
120 return call_hdevtools_and_wait(['type', filename, str(line), str(column)], filename = filename, cabal = cabal)
121
74c3b48 @mvoidex Changed hdevtools init one more time
mvoidex authored
122 def start_hdevtools():
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
123 thread = threading.Thread(
124 target=start_server)
125 thread.start()
126
74c3b48 @mvoidex Changed hdevtools init one more time
mvoidex authored
127 def stop_hdevtools():
128 admin(["--stop-server"])
3c23066 @mvoidex Checking if hdevtools enabled
mvoidex authored
129
130 def hdevtools_enabled():
131 return get_setting_async('enable_hdevtools') == True
Something went wrong with that request. Please try again.