From 3b55bff17bd5654ff040190bd1b6db4bdd9c1a99 Mon Sep 17 00:00:00 2001 From: Andrew Herron Date: Tue, 6 May 2014 10:53:58 +1000 Subject: [PATCH] Started work on #8 --- sublime_ocp_index.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/sublime_ocp_index.py b/sublime_ocp_index.py index 568201f..bacce6e 100644 --- a/sublime_ocp_index.py +++ b/sublime_ocp_index.py @@ -4,6 +4,7 @@ import re OCPKEY = "OCaml Autocompletion" +DEBUG = False class SublimeOCPIndex(): local_cache = dict() @@ -37,6 +38,9 @@ def run_ocp(self, command, includes, module, query, context, settings): args.append(query) + if (DEBUG): + print(args) + # Assumes the first folder is the build directory. Usually a safe assumption. cwd = None if len(includes) < 1 else includes[0] @@ -53,6 +57,8 @@ def run_ocp(self, command, includes, module, query, context, settings): if error: return error else: + if (DEBUG): + print(output) return output def extract_query(self, view, location): @@ -99,6 +105,24 @@ def query_type(self, view, location): else: return "Type: %s" % result + def query_locate(self, view, location): + # TODO: de-dupe with query_type.... somehow + endword = view.word(location).end() + while view.substr(endword) in ['_', '#', '\'']: + endword = endword + 1 + if view.substr(endword) is not ' ': + endword = view.word(endword).end() + + query = self.extract_query(view, endword) + + if query is not None: + (module, queryString, context, settings) = query + + result = self.run_ocp('locate', view.window().folders(), module, queryString, context, settings) + + if not (result is None or len(result) == 0): + view.window().open_file(result, sublime.ENCODED_POSITION | sublime.TRANSIENT) + def query_completions(self, view, prefix, location): query = self.extract_query(view, location) @@ -156,6 +180,22 @@ def extract_locals(self, view): class SublimeOCPEventListener(sublime_plugin.EventListener): + def on_window_command(self, window, command_name, args): + if command_name == "goto_definition": + view = window.active_view() + location = view.sel()[0] + scopes = set(view.scope_name(location.begin()).split(" ")) + + if len(set(["source.ocaml", "source.ocamllex", "source.ocamlyacc"]) & scopes) > 0: + sublimeocp.query_locate(view, location) + + # 'noop' doesn't exist, just here to disable the goto_definition command. + # We could allow the default if ocp-index doesn't find anything, but + # then we get weird results on library functions. + return ("sublime_ocp_noop") + + # default return runs command unmodified + def on_query_completions(self, view, prefix, locations): if len(locations) != 1: return