Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context menu entry in log panel for toggling lines limit #2047

Merged
merged 4 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Context LSP Log Panel.sublime-menu
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[
{
"caption": "Clear Log Panel",
"command": "lsp_clear_log_panel",
"caption": "Clear Log Panel"
},
{
"caption": "Limit to 500 Lines",
predragnikolic marked this conversation as resolved.
Show resolved Hide resolved
"command": "lsp_toggle_log_panel_lines_limit",
predragnikolic marked this conversation as resolved.
Show resolved Hide resolved
},
{
"caption": "-"
Expand Down
1 change: 1 addition & 0 deletions boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .plugin.core.panels import destroy_output_panels
from .plugin.core.panels import LspClearLogPanelCommand
from .plugin.core.panels import LspClearPanelCommand
from .plugin.core.panels import LspToggleLogPanelLinesLimitCommand
from .plugin.core.panels import LspUpdatePanelCommand
from .plugin.core.panels import LspUpdateLogPanelCommand
from .plugin.core.panels import WindowPanelListener
Expand Down
48 changes: 37 additions & 11 deletions plugin/core/panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,41 @@ def create_panel(window: sublime.Window, name: str, result_file_regex: str, resu
return panel


def get_panel(window: Optional[sublime.Window], panel_name: str) -> Optional[sublime.View]:
if not window:
return None
return window.find_output_panel(panel_name)


def is_panel_open(window: sublime.Window, panel_name: str) -> bool:
return window.is_valid() and window.active_panel() == "output.{}".format(panel_name)


def ensure_panel(window: sublime.Window, name: str, result_file_regex: str, result_line_regex: str,
syntax: str, context_menu: Optional[str] = None) -> Optional[sublime.View]:
return window.find_output_panel(name) or \
return get_panel(window, name) or \
create_panel(window, name, result_file_regex, result_line_regex, syntax, context_menu)


class LspToggleLogPanelLinesLimitCommand(sublime_plugin.TextCommand):
SETTING_NAME = 'lsp_limit_lines'

@classmethod
def is_limit_enabled(cls, window: Optional[sublime.Window]) -> bool:
panel = get_panel(window, PanelName.Log)
return bool(panel and panel.settings().get(cls.SETTING_NAME, True))

def run(self, edit: sublime.Edit) -> None:
window = self.view.window()
panel = get_panel(window, PanelName.Log)
if panel:
settings = panel.settings()
settings.set(self.SETTING_NAME, not self.is_limit_enabled(window))

def is_checked(self) -> bool:
return self.is_limit_enabled(self.view.window())


class LspClearPanelCommand(sublime_plugin.TextCommand):
"""
A clear_panel command to clear the error panel.
Expand Down Expand Up @@ -162,7 +187,7 @@ def log_server_message(window: sublime.Window, prefix: str, message: str) -> Non
return
WindowPanelListener.server_log_map[window_id].append((prefix, message))
list_len = len(WindowPanelListener.server_log_map[window_id])
if list_len >= LOG_PANEL_MAX_LINES:
if LspToggleLogPanelLinesLimitCommand.is_limit_enabled(window) and list_len >= LOG_PANEL_MAX_LINES:
# Trim leading items in the list, leaving only the max allowed count.
del WindowPanelListener.server_log_map[window_id][:list_len - LOG_PANEL_MAX_LINES]
panel = ensure_log_panel(window)
Expand All @@ -186,15 +211,16 @@ def run(self, edit: sublime.Edit, window_id: int) -> None:
new_lines.append("{}: {}\n".format(prefix, message))
if new_lines:
self.view.insert(edit, self.view.size(), ''.join(new_lines))
last_region_end = 0 # Starting from point 0 in the panel ...
total_lines, _ = self.view.rowcol(self.view.size())
for _ in range(0, max(0, total_lines - LOG_PANEL_MAX_LINES)):
# ... collect all regions that span an entire line ...
region = self.view.full_line(last_region_end)
last_region_end = region.b
erase_region = sublime.Region(0, last_region_end)
if not erase_region.empty():
self.view.erase(edit, erase_region)
if LspToggleLogPanelLinesLimitCommand.is_limit_enabled(self.view.window()):
last_region_end = 0 # Starting from point 0 in the panel ...
total_lines, _ = self.view.rowcol(self.view.size())
for _ in range(0, max(0, total_lines - LOG_PANEL_MAX_LINES)):
# ... collect all regions that span an entire line ...
region = self.view.full_line(last_region_end)
last_region_end = region.b
erase_region = sublime.Region(0, last_region_end)
if not erase_region.empty():
self.view.erase(edit, erase_region)
clear_undo_stack(self.view)


Expand Down