Skip to content
This repository has been archived by the owner on Nov 2, 2019. It is now read-only.

Commit

Permalink
Started work on #8
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSpyder committed May 6, 2014
1 parent 1976a37 commit 3b55bff
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions sublime_ocp_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re

OCPKEY = "OCaml Autocompletion"
DEBUG = False

class SublimeOCPIndex():
local_cache = dict()
Expand Down Expand Up @@ -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]

Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3b55bff

Please sign in to comment.