Skip to content

Commit

Permalink
Merge pull request #27 from tomv564/fix_selection_type_info
Browse files Browse the repository at this point in the history
Fix show selection type functionality
  • Loading branch information
tomv564 committed Oct 23, 2015
2 parents 537d3a9 + 3f479aa commit 2630a43
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
6 changes: 3 additions & 3 deletions test/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={}):
Expand Down
12 changes: 9 additions & 3 deletions test/stubs/sublime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 6 additions & 5 deletions text_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
12 changes: 9 additions & 3 deletions utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

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

0 comments on commit 2630a43

Please sign in to comment.