Skip to content

Commit e3ec047

Browse files
ehussjasonwilliams
authored andcommitted
Hide, don't delete messages when dismissed. (#295)
* Hide, don't delete messages when dismissed. When dismissing messages, hide them and allow the user to bring them back (using a command such as List Messages or Next/Prev). * Fix opening view with messages after dismissing all messages.
1 parent 1b68358 commit e3ec047

File tree

2 files changed

+70
-30
lines changed

2 files changed

+70
-30
lines changed

cargo_build.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,11 @@ def on_load(self, view):
236236
def on_query_context(self, view, key, operator, operand, match_all):
237237
# Used by the Escape-key keybinding to dismiss inline phantoms.
238238
if key == 'rust_has_messages':
239-
has_messages = view.window().id() in messages.WINDOW_MESSAGES
239+
try:
240+
winfo = messages.WINDOW_MESSAGES[view.window().id()]
241+
has_messages = not winfo['hidden']
242+
except KeyError:
243+
has_messages = False
240244
if operator == sublime.OP_EQUAL:
241245
return operand == has_messages
242246
elif operator == sublime.OP_NOT_EQUAL:
@@ -287,7 +291,7 @@ class RustDismissMessagesCommand(sublime_plugin.WindowCommand):
287291
"""Removes all inline messages."""
288292

289293
def run(self):
290-
messages.clear_messages(self.window)
294+
messages.clear_messages(self.window, soft=True)
291295

292296

293297
class RustListMessagesCommand(sublime_plugin.WindowCommand):
@@ -500,6 +504,17 @@ def run(self, edit, region, replacement):
500504
self.view.replace(edit, region, replacement)
501505

502506

507+
class RustScrollToRegion(sublime_plugin.TextCommand):
508+
509+
"""Internal command used to scroll a view to a region."""
510+
511+
def run(self, edit, region):
512+
r = sublime.Region(*region)
513+
self.view.sel().clear()
514+
self.view.sel().add(r)
515+
self.view.show_at_center(r)
516+
517+
503518
def plugin_unloaded():
504519
messages.clear_all_messages()
505520
try:

rust/messages.py

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
# Value is a dictionary: {
2121
# 'paths': {path: [MessageBatch, ...]},
2222
# 'batch_index': (path_idx, message_idx),
23+
# 'hidden': bool
2324
# }
2425
# `paths` is an OrderedDict to handle next/prev message.
2526
# `path` is the absolute path to the file.
27+
# `hidden` indicates that all messages have been dismissed.
2628
WINDOW_MESSAGES = {}
2729

2830

@@ -157,11 +159,20 @@ def __repr__(self):
157159
return ''.join(result)
158160

159161

160-
def clear_messages(window):
161-
"""Remove all messages for the given window."""
162-
for path, batches in WINDOW_MESSAGES.pop(window.id(), {})\
163-
.get('paths', {})\
164-
.items():
162+
def clear_messages(window, soft=False):
163+
"""Remove all messages for the given window.
164+
165+
:param soft: If True, the messages are kept in memory and can be
166+
resurrected with various commands (such as list messages, or
167+
next/prev).
168+
"""
169+
if soft:
170+
winfo = WINDOW_MESSAGES.get(window.id(), {})
171+
winfo['hidden'] = True
172+
else:
173+
winfo = WINDOW_MESSAGES.pop(window.id(), {})
174+
175+
for path, batches in winfo.get('paths', {}).items():
165176
view = window.find_open_file(path)
166177
if view:
167178
for batch in batches:
@@ -241,8 +252,13 @@ def _draw_region_highlights(view, batch):
241252

242253
def message_popup(view, point, hover_zone):
243254
"""Displays a popup if there is a message at the given point."""
244-
paths = WINDOW_MESSAGES.get(view.window().id(), {}).get('paths', {})
245-
batches = paths.get(view.file_name(), [])
255+
try:
256+
winfo = WINDOW_MESSAGES[view.window().id()]
257+
except KeyError:
258+
return
259+
if winfo['hidden']:
260+
return
261+
batches = winfo['paths'].get(view.file_name(), [])
246262

247263
if hover_zone == sublime.HOVER_GUTTER:
248264
# Collect all messages on this line.
@@ -285,7 +301,7 @@ def filter_point(batch):
285301

286302
def _click_handler(view, url, hide_popup=False):
287303
if url == 'hide':
288-
clear_messages(view.window())
304+
clear_messages(view.window(), soft=True)
289305
if hide_popup:
290306
view.hide_popup()
291307
elif url.startswith('file:///'):
@@ -426,6 +442,8 @@ def _show_message(window, current_idx, transient=False, force_open=False):
426442
window_info = WINDOW_MESSAGES[window.id()]
427443
except KeyError:
428444
return
445+
if window_info['hidden']:
446+
redraw_all_open_views(window)
429447
paths = window_info['paths']
430448
path, batches = _ith_iter_item(paths.items(), current_idx[0])
431449
batch = batches[current_idx[1]]
@@ -482,36 +500,40 @@ def _scroll_build_panel(window, message):
482500
from . import opanel
483501
view = window.find_output_panel(opanel.PANEL_NAME)
484502
if view:
485-
view.sel().clear()
486-
region = message.output_panel_region
487-
view.sel().add(region)
488-
view.show(region)
489-
# Force panel to update.
490-
# TODO: See note about workaround below.
491-
view.add_regions('bug', [region], 'bug', 'dot', sublime.HIDDEN)
492-
view.erase_regions('bug')
503+
r = message.output_panel_region
504+
view.run_command('rust_scroll_to_region', {'region': (r.a, r.b)})
493505

494506

495507
def _scroll_to_message(view, message, transient):
496508
"""Scroll view to the message."""
497509
if not transient:
498510
view.window().focus_view(view)
499511
r = message.sublime_region(view)
500-
view.sel().clear()
501-
view.sel().add(r.a)
502-
view.show_at_center(r)
503-
# TODO: Fix this to use a TextCommand to properly handle undo.
504-
# See https://github.com/SublimeTextIssues/Core/issues/485
505-
view.add_regions('bug', [r], 'bug', 'dot', sublime.HIDDEN)
506-
view.erase_regions('bug')
512+
view.run_command('rust_scroll_to_region', {'region': (r.a, r.a)})
513+
514+
515+
def redraw_all_open_views(window):
516+
"""Re-display phantoms/regions after being hidden."""
517+
try:
518+
winfo = WINDOW_MESSAGES[window.id()]
519+
except KeyError:
520+
return
521+
winfo['hidden'] = False
522+
for path, batches in winfo['paths'].items():
523+
view = window.find_open_file(path)
524+
if view:
525+
show_messages_for_view(view)
507526

508527

509528
def show_messages_for_view(view):
510529
"""Adds all phantoms and region outlines for a view."""
511-
window = view.window()
512-
batches = WINDOW_MESSAGES.get(window.id(), {})\
513-
.get('paths', {})\
514-
.get(view.file_name(), [])
530+
try:
531+
winfo = WINDOW_MESSAGES[view.window().id()]
532+
except KeyError:
533+
return
534+
if winfo['hidden']:
535+
return
536+
batches = winfo['paths'].get(view.file_name(), [])
515537
for batch in batches:
516538
_show_phantom(view, batch)
517539
_draw_region_highlights(view, batch)
@@ -629,6 +651,8 @@ def list_messages(window):
629651
# XXX: Or dialog?
630652
window.show_quick_panel(["No messages available"], None)
631653
return
654+
if win_info['hidden']:
655+
redraw_all_open_views(window)
632656
panel_items = []
633657
jump_to = []
634658
for path_idx, (path, batches) in enumerate(win_info['paths'].items()):
@@ -1025,7 +1049,8 @@ def _save_batches(window, batches, msg_cb):
10251049
path_to_batches = collections.OrderedDict()
10261050
WINDOW_MESSAGES[wid] = {
10271051
'paths': path_to_batches,
1028-
'batch_index': (-1, -1)
1052+
'batch_index': (-1, -1),
1053+
'hidden': False,
10291054
}
10301055

10311056
for batch in batches:

0 commit comments

Comments
 (0)