Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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