Combine snap and error under cursor code
@@ -411,6 +411,11 @@ def fill_panel(window):
update_panel_selection(**State)
+if False:
+ from typing import Any, Dict, List, Tuple, Iterable
+ LintError = Dict[str, Any]
+
+
def update_panel_selection(active_view, cursor, **kwargs):
"""Alter panel selection according to errors belonging to current position.
@@ -432,53 +437,34 @@ def update_panel_selection(active_view, cursor, **kwargs):
all_errors = sorted(persist.errors[bid], key=lambda e: e['panel_line'])
mark_visible_viewport(panel, active_view, all_errors)
- errors_under_cursor = [
- error
- for error in all_errors
- if error['region'].contains(cursor)
- ]
-
- if errors_under_cursor:
- start = panel.text_point(errors_under_cursor[0]['panel_line'], 0)
- end = panel.text_point(errors_under_cursor[-1]['panel_line'], 0)
- region = panel.line(sublime.Region(start, end))
-
- clear_position_marker(panel)
- update_selection(panel, region)
-
- else:
- SNAP = (3, 0) # (lines, characters)
-
row, _ = active_view.rowcol(cursor)
- next_error = next(
+ errors_with_position = (
(
- (error, (error['line'] - row, error['region'].begin() - cursor))
- for error in all_errors
- if error['region'].begin() > cursor
- ),
- None
- )
- previous_error = next(
+ error,
(
- (error, (row - error['line'], cursor - error['region'].end()))
- for error in reversed(all_errors)
- if error['region'].end() < cursor
- ),
- None
+ abs(error['line'] - row),
+ min(
+ abs(error['region'].begin() - cursor),
+ abs(error['region'].end() - cursor)
+ )
)
+ )
+ for error in all_errors
+ ) # type: Iterable[Tuple[LintError, Tuple[int, int]]]
+ SNAP = (3, ) # [lines]
nearest_error = None
try:
nearest_error, _ = min(
(
- e_d
- for e_d in (next_error, previous_error)
- if e_d and e_d[1] < SNAP
+ e_p
+ for e_p in errors_with_position
+ if e_p[1] < SNAP
),
- key=lambda e_d: e_d[1]
+ key=lambda e_p: e_p[1]
)
except ValueError:
- ...
+ nearest_error = None
if nearest_error:
nearest_errors = [
@@ -492,12 +478,19 @@ def update_panel_selection(active_view, cursor, **kwargs):
clear_position_marker(panel)
update_selection(panel, region)
+
else:
- if next_error:
- panel_line = next_error[0]['panel_line']
- else:
+ try:
+ next_error = next(
+ error
+ for error in all_errors
+ if error['region'].begin() > cursor
+ )
+ except StopIteration:
last_error = all_errors[-1]
panel_line = last_error['panel_line'] + 1
+ else:
+ panel_line = next_error['panel_line']
start = panel.text_point(panel_line, 0)
region = sublime.Region(start)
@@ -506,10 +499,6 @@ def update_panel_selection(active_view, cursor, **kwargs):
update_selection(panel, region)
-if False:
- from typing import Any, Dict, List
-
-
def mark_visible_viewport(panel, view, errors):
# type: (sublime.View, sublime.View, List[Dict[str, Any]]) -> None
KEY = 'SL.Panel.ViewportMarker'
