Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 137 lines (106 sloc) 4.071 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
dfa0ad2 @nh2 Fix hdevtools absence not being detected correctly. Fixes #33
nh2 authored
17
18 def show_hdevtools_error_and_disable():
19 # Looks like we can't always get an active window here,
20 # we use sublime.error_message() instead of
21 # output_error(sublime.active_window().
22 sublime.set_timeout(lambda: sublime.error_message(
23 "SublimeHaskell: hdevtools was not found!\n"
24 + "It's used for 'symbol info' and type inference\n"
25 + "Install it with 'cabal install hdevtools',\n"
26 + "or adjust the 'add_to_PATH' setting for a custom location.\n"
27 + "'enable_hdevtools' automatically set to False in the User settings."), 0)
28
29 set_setting_async('enable_hdevtools', False)
30
31
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
32 def call_hdevtools_and_wait(arg_list, filename = None, cabal = None):
33 """
34 Calls hdevtools with the given arguments.
35 Shows a sublime error message if hdevtools is not available.
36 """
3c23066 @mvoidex Checking if hdevtools enabled
mvoidex authored
37 if not hdevtools_enabled():
38 return None
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
39
870cf1a @mvoidex Using hdocs
mvoidex authored
40 ghc_opts_args = get_ghc_opts_args(filename, cabal = cabal)
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
41 hdevtools_socket = get_setting_async('hdevtools_socket')
2216239 @mvoidex Fixed another typo
mvoidex authored
42 source_dir = get_source_dir(filename)
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
43
44 if hdevtools_socket:
45 arg_list.append('--socket={0}'.format(hdevtools_socket))
46
47 try:
7fb3078 @mvoidex Fixed column calc
mvoidex authored
48 exit_code, out, err = call_and_wait(['hdevtools'] + arg_list + ghc_opts_args, cwd = source_dir)
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
49
50 if exit_code != 0:
51 raise Exception("hdevtools exited with status %d and stderr: %s" % (exit_code, err))
52
53 return crlf2lf(out)
54
55 except OSError as e:
56 if e.errno == errno.ENOENT:
dfa0ad2 @nh2 Fix hdevtools absence not being detected correctly. Fixes #33
nh2 authored
57 show_hdevtools_error_and_disable()
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
58
59 return None
60
e320cd6 @mvoidex Minor fixes
mvoidex authored
61 except Exception as e:
62 log('calling to hdevtools fails with {0}'.format(e))
63 return None
64
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
65 def admin(cmds, wait = False, **popen_kwargs):
3c23066 @mvoidex Checking if hdevtools enabled
mvoidex authored
66 if not hdevtools_enabled():
67 return None
68
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
69 hdevtools_socket = get_setting_async('hdevtools_socket')
70
71 if hdevtools_socket:
72 cmds.append('--socket={0}'.format(hdevtools_socket))
73
74 command = ["hdevtools", "admin"] + cmds
75
a31e3a4 @mvoidex Fixed browse module for module in sources, catching OSError in hdevtools...
mvoidex authored
76 try:
77 if wait:
78 (exit_code, stdout, stderr) = call_and_wait(command, **popen_kwargs)
79 if exit_code == 0:
80 return stdout
81 return ''
82 else:
83 call_no_wait(command, **popen_kwargs)
84 return ''
85
86 except OSError as e:
87 if e.errno == errno.ENOENT:
dfa0ad2 @nh2 Fix hdevtools absence not being detected correctly. Fixes #33
nh2 authored
88 show_hdevtools_error_and_disable()
a31e3a4 @mvoidex Fixed browse module for module in sources, catching OSError in hdevtools...
mvoidex authored
89
90 set_setting_async('enable_hdevtools', False)
91
92 return None
93 except Exception as e:
94 log('calling to hdevtools fails with {0}'.format(e))
95 return None
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
96
97 def is_running():
98 r = admin(['--status'], wait = True)
3c23066 @mvoidex Checking if hdevtools enabled
mvoidex authored
99 if r and re.search(r'running', r):
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
100 return True
101 else:
102 return False
103
104 def start_server():
105 if not is_running():
106 admin(["--start-server"])
107
108 def hdevtools_info(filename, symbol_name, cabal = None):
109 """
110 Uses hdevtools info filename symbol_name to get symbol info
111 """
112 contents = call_hdevtools_and_wait(['info', filename, symbol_name], filename = filename, cabal = cabal)
4bd1189 @mvoidex Fixed minor bug
mvoidex authored
113 return parse_info(symbol_name, contents) if contents else None
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
114
115 def hdevtools_check(filename, cabal = None):
116 """
117 Uses hdevtools to check file
118 """
3c23066 @mvoidex Checking if hdevtools enabled
mvoidex authored
119 return call_hdevtools_and_wait(['check', filename], filename = filename, cabal = cabal)
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
120
121 def hdevtools_type(filename, line, column, cabal = None):
122 """
123 Uses hdevtools to infer type
124 """
125 return call_hdevtools_and_wait(['type', filename, str(line), str(column)], filename = filename, cabal = cabal)
126
74c3b48 @mvoidex Changed hdevtools init one more time
mvoidex authored
127 def start_hdevtools():
4f839ae @mvoidex Integrate hdevtools #30
mvoidex authored
128 thread = threading.Thread(
129 target=start_server)
130 thread.start()
131
74c3b48 @mvoidex Changed hdevtools init one more time
mvoidex authored
132 def stop_hdevtools():
6521335 @nh2 Fix some whitespace
nh2 authored
133 admin(["--stop-server"])
3c23066 @mvoidex Checking if hdevtools enabled
mvoidex authored
134
135 def hdevtools_enabled():
136 return get_setting_async('enable_hdevtools') == True
Something went wrong with that request. Please try again.