Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added util module

  • Loading branch information...
commit b91dcb07ba4b3bf5f1cdcc89f8b571bf50e77161 1 parent 74c3b48
@mvoidex mvoidex authored
View
69 autocomplete.py
@@ -10,18 +10,16 @@
from sublime_haskell_common import *
import symbols
import cache
- from ghci import ghci_info, ghci_package_db
+ import util
from haskell_docs import haskell_docs
- from ghcmod import ghcmod_browse_module, ghcmod_info
- from hdevtools import hdevtools_info, start_hdevtools, stop_hdevtools
+ from hdevtools import start_hdevtools, stop_hdevtools
else:
from SublimeHaskell.sublime_haskell_common import *
import SublimeHaskell.symbols as symbols
import SublimeHaskell.cache as cache
- from SublimeHaskell.ghci import ghci_info, ghci_package_db
+ import SublimeHaskell.util as util
from SublimeHaskell.haskell_docs import haskell_docs
- from SublimeHaskell.ghcmod import ghcmod_browse_module, ghcmod_info
- from SublimeHaskell.hdevtools import hdevtools_info, start_hdevtools, stop_hdevtools
+ from SublimeHaskell.hdevtools import start_hdevtools, stop_hdevtools
# If true, files that have not changed will not be re-inspected.
@@ -58,14 +56,6 @@
# Get symbol module scope and its name within import statement
IMPORT_SYMBOL_RE = re.compile(r'import(\s+qualified)?\s+(?P<module>\w+(\.\w+)*)(\s+as\s+(?P<as>\w+))?\s*\(.*?(?P<identifier>\w*)$')
-def symbol_info(filename, module_name, symbol_name, cabal = None):
- result = None
- if get_setting_async('enable_hdevtools'):
- result = hdevtools_info(filename, symbol_name, cabal = cabal)
- if not result:
- result = symbol_info(filename, module_name, symbol_name, cabal = cabal)
- return result
-
def get_line_contents(view, location):
"""
Returns contents of line at the given location.
@@ -430,7 +420,7 @@ def run(self, edit, filename = None, module_name = None, decl = None):
if module_word:
# Full qualified name, just call to info
- info = ghci_info(module_word, ident)
+ info = util.symbol_info(module_word, ident)
if info:
self.show_symbol_info(info)
return
@@ -507,7 +497,7 @@ def on_candidate_selected(self, idx):
return
(module_name, ident_name) = self.candidates[idx]
- info = ghci_info(module_name, ident_name)
+ info = util.symbol_info(module_name, ident_name)
if info:
self.show_symbol_info(info)
else:
@@ -517,28 +507,7 @@ def show_symbol_info(self, decl):
output_view = self.view.window().get_output_panel('sublime_haskell_symbol_info')
output_view.set_read_only(False)
- # Symbol from cabal, try to load detailed info with ghci
- if not decl.location:
- decl_detailed = None
- decl_docs = None
- if decl.what == 'declaration':
- decl_detailed = ghci_info(decl.module.name, decl.name)
- if not decl.docs:
- decl_docs = haskell_docs(decl.module.name, decl.name)
-
- # Replace declaration with new one
- if decl_detailed:
- decl_detailed.docs = decl_docs
- autocompletion.database.add_declaration(decl_detailed, decl.module)
- decl = decl_detailed
- else:
- decl.docs = decl_docs
- # Symbol from sources, concrete type if it's not specified
- else:
- if decl.what == 'function' and not decl.type:
- info = ghcmod_info(decl.location.filename, decl.module.name, decl.name)
- if info:
- decl.type = info.type
+ util.refine_decl(decl)
# TODO: Move to separate command for Sublime Text 3
output_view.run_command('sublime_haskell_output_text', {
@@ -578,6 +547,9 @@ def run(self, module_name = None, filename = None):
decls = list(module_candidate.declarations.values())
self.candidates = decls
+
+ for decl in decls:
+ util.load_docs(decl)
self.window.show_quick_panel([[decl.brief(), decl.docs] if decl.docs else [decl.brief()] for decl in decls], self.on_symbol_selected)
return
@@ -806,7 +778,7 @@ def _load_standard_module(self, module_name, cabal = None):
if module_name not in autocompletion.database.get_cabal_modules():
try:
- m = ghcmod_browse_module(module_name, cabal = cabal)
+ m = util.browse_module(module_name, cabal = cabal)
autocompletion.database.add_module(m)
except Exception as e:
@@ -988,15 +960,7 @@ def _refresh_module_info(self, filename, standalone = True):
if modification_time <= inspection_time:
return
- hdevtools_enabled = get_setting_async('enable_hdevtools')
-
- ghc_opts = get_setting_async('ghc_opts')
- if not ghc_opts:
- ghc_opts = []
- package_db = ghci_package_db()
- if package_db:
- ghc_opts.append('-package-db {0}'.format(package_db))
-
+ ghc_opts = get_ghc_opts()
ghc_opts_args = [' '.join(ghc_opts)] if ghc_opts else []
exit_code, stdout, stderr = call_and_wait(
@@ -1027,12 +991,9 @@ def make_import(import_info):
for d in new_info['declarations']:
location = symbols.Location(filename, d['line'], d['column'])
if d['what'] == 'function':
- if not d['type'] and hdevtools_enabled:
- # Try to get type with hdevtools only
- info_typed = hdevtools_info(filename, d['name'])
- if info_typed:
- d['type'] = info_typed.type
- new_module.add_declaration(symbols.Function(d['name'], d['type'], d['docs'], location))
+ new_function = symbols.Function(d['name'], d['type'], d['docs'], location)
+ util.refine_type(new_function)
+ new_module.add_declaration(new_function)
elif d['what'] == 'type':
new_module.add_declaration(symbols.Type(d['name'], d['context'], d['args'], None, d['docs'], location))
elif d['what'] == 'newtype':
View
15 ghci.py
@@ -64,18 +64,3 @@ def ghci_info(module, name):
return parse_info(name, stdout)
return None
-
-def ghci_package_db():
- dev = get_setting_async('use_cabal_dev')
- box = 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):
- package_conf = ghci_package_db()
- if package_conf:
- cmd.extend(['-package-db', package_conf])
- return cmd
View
18 hdevtools.py
@@ -7,11 +7,11 @@
if int(sublime.version()) < 3000:
from sublime_haskell_common import *
- from ghci import ghci_package_db, parse_info
+ from ghci import parse_info
import symbols
else:
from SublimeHaskell.sublime_haskell_common import *
- from SublimeHaskell.ghci import ghci_package_db, parse_info
+ from SublimeHaskell.ghci import parse_info
import SublimeHaskell.symbols as symbols
def call_hdevtools_and_wait(arg_list, filename = None, cabal = None):
@@ -20,21 +20,9 @@ def call_hdevtools_and_wait(arg_list, filename = None, cabal = None):
Shows a sublime error message if hdevtools is not available.
"""
- ghc_opts = get_setting_async('ghc_opts')
+ ghc_opts_args = get_ghc_opts_args(filename)
hdevtools_socket = get_setting_async('hdevtools_socket')
- package_db = ghci_package_db()
- if package_db:
- ghc_opts.append('-package-db {0}'.format(package_db))
-
- source_dir = get_source_dir(filename)
- ghc_opts.append('-i {0}'.format(source_dir))
-
- ghc_opts_args = []
- if ghc_opts:
- for opt in ghc_opts:
- ghc_opts_args.extend(["-g", opt])
-
if hdevtools_socket:
arg_list.append('--socket={0}'.format(hdevtools_socket))
View
41 sublime_haskell_common.py
@@ -324,6 +324,21 @@ def set_setting(key, value):
def set_setting_async(key, value):
sublime.set_timeout(lambda: set_setting(key, value), 0)
+def ghci_package_db():
+ dev = get_setting_async('use_cabal_dev')
+ box = 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):
+ package_conf = ghci_package_db()
+ 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
@@ -362,6 +377,32 @@ def get_cwd(filename = None):
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):
+ """
+ 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 = []
+ package_db = ghci_package_db()
+ if package_db:
+ ghc_opts.append('-package-db {0}'.format(package_db))
+
+ if filename:
+ ghc_opts.append('-i {0}'.format(common.get_source_dir(filename)))
+
+ return ghc_opts
+
+def get_ghc_opts_args(filename = None):
+ """
+ Same as ghc_opts, but uses '-g' option for each option
+ """
+ opts = get_ghc_opts(filename)
+ 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.
View
2  symbols.py
@@ -3,8 +3,10 @@
if int(sublime.version()) < 3000:
from sublime_haskell_common import *
+ from haskell_docs import haskell_docs
else:
from SublimeHaskell.sublime_haskell_common import *
+ from SublimeHaskell.haskell_docs import haskell_docs
from functools import reduce
class Location(object):
View
72 util.py
@@ -0,0 +1,72 @@
+import sublime
+
+if int(sublime.version()) < 3000:
+ import ghci
+ import ghcmod
+ import haskell_docs
+ import hdevtools
+ import sublime_haskell_common as common
+ import symbols
+else:
+ import SublimeHaskell.ghci as ghci
+ import SublimeHaskell.ghcmod as ghcmod
+ import SublimeHaskell.haskell_docs as haskell_docs
+ import SublimeHaskell.hdevtools as hdevtools
+ import SublimeHaskell.sublime_haskell_common as common
+ import SublimeHaskell.symbols as symbols
+
+def symbol_info(filename, module_name, symbol_name, cabal = None):
+ result = None
+ if get_setting_async('enable_hdevtools'):
+ result = hdevtools.hdevtools_info(filename, symbol_name, cabal = cabal)
+ if not result:
+ result = ghcmod.ghcmod_info(filename, module_name, symbol_name, cabal = cabal)
+ if not result and filename:
+ result = ghci.ghci_info(module_name, symbol_name, cabal = cabal)
+ return result
+
+def load_docs(decl):
+ """
+ Tries to load docs for decl
+ """
+ if decl.docs is None:
+ decl.docs = haskell_docs.haskell_docs(decl.module.name, decl.name)
+
+def refine_type(decl, hdevtools_only = True):
+ """
+ Refine type for sources decl
+ """
+ hdevtools_enabled = common.get_setting_async('enable_hdevtools')
+
+ if decl.location:
+ if decl.what == 'function' and not decl.type:
+ info = None
+ if hdevtools_only and hdevtools_enabled:
+ info = hdevtools.hdevtools_info(decl.location.filename, decl.name)
+ else:
+ info = symbol_info(decl.location.filename, decl.module.name, decl.name)
+ if info:
+ decl.type = info.type
+
+def refine_decl(decl):
+ """
+ Refine decl information.
+ """
+ # Symbol from cabal, try to load detailed info with ghci
+ if not decl.location:
+ load_docs(decl)
+
+ if decl.what == 'declaration':
+ decl_detailed = symbol_info(decl.module.name, decl.name)
+ if decl_detailed:
+ decl.__dict__.update(decl_detailed.__dict__)
+
+ # Symbol from sources, concrete type if it's not specified
+ else:
+ refine_type(decl, False)
+
+def browse_module(module_name, cabal = None):
+ """
+ Returns symbols.Module with all declarations
+ """
+ return ghcmod.ghcmod_browse_module(module_name, cabal = cabal)
Please sign in to comment.
Something went wrong with that request. Please try again.