diff --git a/test/mocks.py b/test/mocks.py index f68fd6e..539f600 100644 --- a/test/mocks.py +++ b/test/mocks.py @@ -24,11 +24,11 @@ def mock_view(file_path, window): window.views = Mock(return_value=[view]) view.window = Mock(return_value=window) region = MagicMock() - region.begin = Mock(return_value=1) - region.end = Mock(return_value=2) + region.begin = Mock(return_value=4) + region.end = Mock(return_value=4) view.sel = Mock(return_value=[region]) view.rowcol = Mock(return_value=(0, 0)) - view.text_point = Mock(return_value=20) + view.text_point = Mock(return_value=4) return view def setup_fake_backend(window, responses={}): diff --git a/test/stubs/sublime.py b/test/stubs/sublime.py index 47f0eaf..bf62a8d 100644 --- a/test/stubs/sublime.py +++ b/test/stubs/sublime.py @@ -74,6 +74,12 @@ def windows(): class Region(): - def __init__(self, start, end): - self.start = start - self.end = end + def __init__(self, begin, end): + self._begin = begin + self._end = end + + def begin(self): + return self._begin + + def end(self): + return self._end diff --git a/text_commands.py b/text_commands.py index 01c887f..9f45ad4 100644 --- a/text_commands.py +++ b/text_commands.py @@ -6,7 +6,7 @@ import os, sys sys.path.append(os.path.dirname(os.path.realpath(__file__))) -from utility import span_from_view_selection, first_folder +from utility import span_from_view_selection, first_folder, filter_enclosing from req import Req from stack_ide_manager import send_request from response import parse_span_info_response, parse_exp_types @@ -35,10 +35,11 @@ def run(self,edit): send_request(self.view.window(),request, self._handle_response) def _handle_response(self,response): - types = list(parse_exp_types(response)) - if types: - (type, span) = types[0] # types are ordered by relevance? - self.view.show_popup(type) + type_spans = list(parse_exp_types(response)) + if type_spans: + _type = next(filter_enclosing(self.view, self.view.sel()[0], type_spans), None) + if not _type is None: + self.view.show_popup(_type) class ShowHsInfoAtCursorCommand(sublime_plugin.TextCommand): diff --git a/utility.py b/utility.py index 5c6b6e5..e5a5078 100644 --- a/utility.py +++ b/utility.py @@ -66,6 +66,12 @@ def relative_view_file_name(view): def span_from_view_selection(view): return span_from_view_region(view, view.sel()[0]) +def within(smaller, larger): + return smaller.begin() >= larger.begin() and smaller.end() <= larger.end() + +def filter_enclosing(view, region, span_pairs): + return (item for item, span in span_pairs if within(region, view_region_from_span(view, span))) + def is_haskell_view(view): return view.match_selector(view.sel()[0].begin(), "source.haskell") @@ -78,9 +84,9 @@ def view_region_from_span(view, span): :rtype sublime.Region: The created Region """ - from_point = view.text_point(span.fromLine - 1, span.fromColumn - 1) - to_point = view.text_point(span.toLine - 1, span.toColumn - 1) - return sublime.Region(from_point, to_point) + return sublime.Region( + view.text_point(span.fromLine - 1, span.fromColumn - 1), + view.text_point(span.toLine - 1, span.toColumn - 1)) def span_from_view_region(view, region): (from_line, from_col) = view.rowcol(region.begin())