diff --git a/plugin/core/windows.py b/plugin/core/windows.py index 888ae8d0d..5ebebcc36 100644 --- a/plugin/core/windows.py +++ b/plugin/core/windows.py @@ -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, [])) @@ -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() diff --git a/plugin/formatting.py b/plugin/formatting.py index 33350b38d..628676047 100644 --- a/plugin/formatting.py +++ b/plugin/formatting.py @@ -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 @@ -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: @@ -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: @@ -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):