Skip to content

Commit

Permalink
Some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jwortmann committed Sep 25, 2023
1 parent 1ae9f26 commit c7b86a0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
6 changes: 3 additions & 3 deletions plugin/core/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(self, window: sublime.Window, workspace: ProjectFolders, config_man
self.panel_manager = PanelManager(self._window) # type: Optional[PanelManager]
self.tree_view_sheets = {} # type: Dict[str, TreeViewSheet]
self.formatters = {} # type: Dict[str, str]
self.formatter_updated_in_project = False
self.suppress_sessions_restart_on_project_update = False
self.total_error_count = 0
self.total_warning_count = 0
sublime.set_timeout(functools.partial(self._update_panel_main_thread, _NO_DIAGNOSTICS_PLACEHOLDER, []))
Expand All @@ -108,8 +108,8 @@ def on_load_project_async(self) -> None:
self._config_manager.update()

def on_post_save_project_async(self) -> None:
if self.formatter_updated_in_project:
self.formatter_updated_in_project = False
if self.suppress_sessions_restart_on_project_update:
self.suppress_sessions_restart_on_project_update = False
return
self.on_load_project_async()

Expand Down
45 changes: 23 additions & 22 deletions plugin/formatting.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .core.collections import DottedDict
from .core.edit import parse_text_edit
from .core.promise import Promise
from .core.protocol import Error
Expand Down Expand Up @@ -109,7 +110,7 @@ def is_enabled(self, event: Optional[dict] = None, select: bool = False) -> bool

def run(self, edit: sublime.Edit, event: Optional[dict] = None, select: bool = False) -> None:
session_names = [session.config.name for session in self.sessions(self.capability)]
base_scope = self.view.scope_name(0).split()[0]
base_scope = self.view.syntax().scope
if select:
self.select_formatter(base_scope, session_names)
elif len(session_names) > 1:
Expand All @@ -119,7 +120,7 @@ def run(self, edit: sublime.Edit, event: Optional[dict] = None, select: bool = F
formatter = None
project_data = window.project_data()
if isinstance(project_data, dict):
formatter = project_data.get('settings', {}).get('LSP', {}).get('formatters', {}).get(base_scope)
formatter = DottedDict(project_data).get('settings.LSP.formatters.{}'.format(base_scope))
else:
window_manager = windows.lookup(window)
if window_manager:
Expand All @@ -145,26 +146,26 @@ def select_formatter(self, base_scope: str, session_names: List[str]) -> None:
session_names, partial(self.on_select_formatter, base_scope, session_names), placeholder="Select Formatter")

def on_select_formatter(self, base_scope: str, session_names: List[str], index: int) -> None:
if index > -1:
session_name = session_names[index]
window = self.view.window()
if window:
window_manager = windows.lookup(window)
if window_manager:
project_data = window.project_data()
if isinstance(project_data, dict):
project_settings = project_data.setdefault('settings', dict())
project_lsp_settings = project_settings.setdefault('LSP', dict())
project_formatter_settings = project_lsp_settings.setdefault('formatters', dict())
project_formatter_settings[base_scope] = session_name
# Prevent restart of all sessions after project file save
window_manager.formatter_updated_in_project = True
window.set_project_data(project_data)
else: # Save temporarily for this window
window_manager.formatters[base_scope] = session_name
session = self.session_by_name(session_name, self.capability)
if session:
session.send_request_task(text_document_formatting(self.view)).then(self.on_result)
if index == -1:
return
session_name = session_names[index]
window = self.view.window()
if window:
window_manager = windows.lookup(window)
if window_manager:
project_data = window.project_data()
if isinstance(project_data, dict):
project_settings = project_data.setdefault('settings', dict())
project_lsp_settings = project_settings.setdefault('LSP', dict())
project_formatter_settings = project_lsp_settings.setdefault('formatters', dict())
project_formatter_settings[base_scope] = session_name
window_manager.suppress_sessions_restart_on_project_update = True
window.set_project_data(project_data)
else: # Save temporarily for this window
window_manager.formatters[base_scope] = session_name
session = self.session_by_name(session_name, self.capability)
if session:
session.send_request_task(text_document_formatting(self.view)).then(self.on_result)


class LspFormatDocumentRangeCommand(LspTextCommand):
Expand Down

0 comments on commit c7b86a0

Please sign in to comment.