def _apply_auto_complete_triggers( self, settings: sublime.Settings, trigger_chars: List[str], registration_id: Optional[str] = None ) -> None: """This method actually modifies the auto_complete_triggers entries for the view.""" if not userprefs().register_trigger_chars: return selector = self.session.config.auto_complete_selector if not selector: # If the user did not set up an auto_complete_selector for this server configuration, fallback to the # "global" auto_complete_selector of the view. selector = str(settings.get("auto_complete_selector")) trigger = { "selector": selector, # This key is not used by Sublime, but is used as a "breadcrumb" to figure out what needs to be removed # from the auto_complete_triggers array once the session is stopped. "server": self.session.config.name } if trigger_chars: trigger["characters"] = "".join(trigger_chars) if isinstance(registration_id, str): # This key is not used by Sublime, but is used as a "breadcrumb" as well, for dynamic registrations. trigger["registration_id"] = registration_id triggers = settings.get(self.AC_TRIGGERS_KEY) or [] # type: List[Dict[str, str]] triggers.append(trigger) settings.set(self.AC_TRIGGERS_KEY, triggers)
def requires_missing_converter(settings: sublime.Settings, image_format: int) -> bool: if image_format == ImageFormat.SVG and settings.get('svg_converter') not in SUPPORTED_CONVERTERS[ImageFormat.SVG]: return True elif image_format == ImageFormat.WEBP and settings.get('webp_converter') not in SUPPORTED_CONVERTERS[ImageFormat.WEBP]: return True elif image_format == ImageFormat.AVIF and settings.get('avif_converter') not in SUPPORTED_CONVERTERS[ImageFormat.AVIF]: return True return False
def temporary_setting(settings: sublime.Settings, key: str, val: Any) -> Generator[None, None, None]: prev_val = None has_prev_val = settings.has(key) if has_prev_val: prev_val = settings.get(key) settings.set(key, val) yield settings.erase(key) if has_prev_val and settings.get(key) != prev_val: settings.set(key, prev_val)
def read_bool_setting(settings_obj: sublime.Settings, key: str, default: bool) -> bool: val = settings_obj.get(key) if isinstance(val, bool): return val else: return default
def setup_snitching(settings: sublime.Settings): console = sys.stdout if not isinstance(console, sublime._LogWriter): # This happens when hot reloading the package console = getattr(sys.stdout, "console", None) if console is None or not isinstance(console, sublime._LogWriter): logger.error( "Wasn't able to identify the Sublime console object, " "Snitching is disabled. Try restarting ST." ) logger.error("sys.stdout=%s, sys.__stdout=%s", sys.stdout, sys.__stdout__) return if not settings.get("snitch", False): # Restore the stdout sys.stdout = console return snitch = SnitchingStdout(console) logger.info( "Stdout will first go through %s before going to %s", snitch, snitch.console, ) sys.stdout = snitch # TODO: Move this to a test case print("This should be snitched to 'Log - Snitch' panel")
def read_int_setting(settings_obj: sublime.Settings, key: str, default: int) -> int: val = settings_obj.get(key) if isinstance(val, int): return val else: return default
def read_dict_setting(settings_obj: sublime.Settings, key: str, default: dict) -> dict: val = settings_obj.get(key) if isinstance(val, dict): return val else: return default
def read_array_setting(settings_obj: sublime.Settings, key: str, default: list) -> list: val = settings_obj.get(key) if isinstance(val, list): return val else: return default
def read_str_setting(settings_obj: sublime.Settings, key: str, default: str) -> str: val = settings_obj.get(key) if isinstance(val, str): return val else: return default
def get_java_path(settings: sublime.Settings) -> str: java_home = settings.get("java_home") if isinstance(java_home, str) and java_home: return os.path.join(java_home, "bin", "java") java_home = os.environ.get('JAVA_HOME') if java_home: return os.path.join(java_home, "bin", "java") return "java"
def from_sublime_settings(cls, name: str, s: sublime.Settings, file: str) -> "ClientConfig": base = sublime.decode_value(sublime.load_resource(file)) settings = DottedDict(base.get("settings", {})) # defined by the plugin author settings.update(read_dict_setting(s, "settings", {})) # overrides from the user init_options = DottedDict(base.get("initializationOptions", {})) init_options.update(read_dict_setting(s, "initializationOptions", {})) disabled_capabilities = s.get("disabled_capabilities") file_watcher = cast(FileWatcherConfig, read_dict_setting(s, "file_watcher", {})) if isinstance(disabled_capabilities, dict): disabled_capabilities = DottedDict(disabled_capabilities) else: disabled_capabilities = DottedDict() return ClientConfig( name=name, selector=_read_selector(s), priority_selector=_read_priority_selector(s), schemes=s.get("schemes"), command=read_list_setting(s, "command", []), tcp_port=s.get("tcp_port"), auto_complete_selector=s.get("auto_complete_selector"), # Default to True, because an LSP plugin is enabled iff it is enabled as a Sublime package. enabled=bool(s.get("enabled", True)), init_options=init_options, settings=settings, env=read_dict_setting(s, "env", {}), experimental_capabilities=s.get("experimental_capabilities"), disabled_capabilities=disabled_capabilities, file_watcher=file_watcher, path_maps=PathMap.parse(s.get("path_maps")))
def read_auto_show_diagnostics_panel_setting(settings_obj: sublime.Settings, key: str, default: str) -> str: val = settings_obj.get(key) if isinstance(val, bool): return 'always' if val else 'never' if isinstance(val, str): return val else: return default
def formatting_options(settings: sublime.Settings) -> Dict[str, Any]: # Build 4085 allows "trim_trailing_white_space_on_save" to be a string so we have to account for that in a # backwards-compatible way. trim_trailing_white_space = settings.get( "trim_trailing_white_space_on_save") not in (False, None, "none") return { # Size of a tab in spaces. "tabSize": settings.get("tab_size", 4), # Prefer spaces over tabs. "insertSpaces": settings.get("translate_tabs_to_spaces", False), # Trim trailing whitespace on a line. (since 3.15) "trimTrailingWhitespace": trim_trailing_white_space, # Insert a newline character at the end of the file if one does not exist. (since 3.15) "insertFinalNewline": settings.get("ensure_newline_at_eof_on_save", False), # Trim all newlines after the final newline at the end of the file. (sine 3.15) "trimFinalNewlines": settings.get("ensure_newline_at_eof_on_save", False) }
def _migrate_obsolete_settings(self, settings: sublime.Settings): """ Migrates setting with a root `client` key to flattened structure. Receives a `sublime.Settings` object. Returns True if settings were migrated. """ client = settings.get('client') # type: Dict if client: settings.erase('client') # Migrate old keys for key, value in client.items(): settings.set(key, value) return True return False
def get_default_line_ending(raw: Settings) -> str: from sublime import platform ending = raw.get("default_line_ending").lower() is_windows = ending == "system" and platform() == "windows" is_windows = ending == "windows" or is_windows return "crlf" if is_windows else "lf"
def on_settings_read(cls, settings: sublime.Settings): cls._user_schemas = settings.get('userSchemas', []) # Nothing has changed so don't force saving. return False
def formatting_options(settings: sublime.Settings) -> Dict[str, Any]: return { "tabSize": settings.get("tab_size", 4), "insertSpaces": settings.get("translate_tabs_to_spaces", False) }
def read_bool_setting(settings_obj: sublime.Settings, key: str, default: bool) -> bool: val = settings_obj.get(key) if isinstance(val, bool): return val else: return default
def read_int_setting(settings_obj: sublime.Settings, key: str, default: int) -> int: val = settings_obj.get(key) if isinstance(val, int): return val else: return default
def read_dict_setting(settings_obj: sublime.Settings, key: str, default: dict) -> dict: val = settings_obj.get(key) if isinstance(val, dict): return val else: return default
def read_str_setting(settings_obj: sublime.Settings, key: str, default: str) -> str: val = settings_obj.get(key) if isinstance(val, str): return val else: return default
def update(self, s: sublime.Settings) -> None: def r(name: str, default: Union[bool, int, str, list, dict]) -> None: val = s.get(name) setattr(self, name, val if isinstance(val, default.__class__) else default) r("diagnostics_additional_delay_auto_complete_ms", 0) r("diagnostics_delay_ms", 0) r("diagnostics_gutter_marker", "dot") r("diagnostics_panel_include_severity_level", 4) r("disabled_capabilities", []) r("document_highlight_style", "underline") r("log_debug", False) r("log_max_size", 8 * 1024) r("lsp_code_actions_on_save", {}) r("lsp_format_on_save", False) r("on_save_task_timeout_ms", 2000) r("only_show_lsp_completions", False) r("popup_max_characters_height", 1000) r("popup_max_characters_width", 120) r("show_code_actions", "annotation") r("show_code_lens", "annotation") r("show_code_actions_in_hover", True) r("register_trigger_chars", False) r("show_diagnostics_count_in_view_status", False) r("show_diagnostics_in_view_status", True) r("show_multiline_diagnostics_highlights", True) r("show_diagnostics_panel_on_save", 2) r("show_diagnostics_severity_level", 2) r("show_references_in_quick_panel", False) r("show_symbol_action_links", False) r("show_view_status", True) # Backwards-compatible with the bool setting log_server = s.get("log_server") if isinstance(log_server, bool): self.log_server = ["panel"] if log_server else [] elif isinstance(log_server, list): self.log_server = log_server else: self.log_server = [] # Backwards-compatible with the bool setting auto_show_diagnostics_panel = s.get("auto_show_diagnostics_panel") if isinstance(auto_show_diagnostics_panel, bool): if not auto_show_diagnostics_panel: self.show_diagnostics_panel_on_save = 0 elif isinstance(auto_show_diagnostics_panel, str): if auto_show_diagnostics_panel == "never": self.show_diagnostics_panel_on_save = 0 # Backwards-compatible with "only_show_lsp_completions" only_show_lsp_completions = s.get("only_show_lsp_completions") if isinstance(only_show_lsp_completions, bool): self.inhibit_snippet_completions = only_show_lsp_completions self.inhibit_word_completions = only_show_lsp_completions else: r("inhibit_snippet_completions", False) r("inhibit_word_completions", False) # correctness checking will happen inside diagnostics_highlight_style_flags method self.diagnostics_highlight_style = s.get( "diagnostics_highlight_style") # type: ignore # Backwards-compatible with "show_diagnostics_highlights" if s.get("show_diagnostics_highlights") is False: self.diagnostics_highlight_style = "" # Backwards-compatible with "code_action_on_save_timeout_ms" code_action_on_save_timeout_ms = s.get( "code_action_on_save_timeout_ms") if isinstance(code_action_on_save_timeout_ms, int): self.on_save_task_timeout_ms = code_action_on_save_timeout_ms set_debug_logging(self.log_debug)
def on_settings_read_internal(cls, settings: sublime.Settings) -> None: languages = settings.get('languages', None) # type: Optional[List[LanguagesDict]] if languages: settings.set('languages', cls._upgrade_languages_list(languages))
def _clear_auto_complete_triggers(self, settings: sublime.Settings) -> None: '''Remove all of our modifications to the view's "auto_complete_triggers"''' triggers = settings.get(self.AC_TRIGGERS_KEY) if isinstance(triggers, list): triggers = [t for t in triggers if self.session.config.name != t.get("server", "")] settings.set(self.AC_TRIGGERS_KEY, triggers)
def read_list_setting(settings_obj: sublime.Settings, key: str, default: list) -> list: val = settings_obj.get(key) return val if isinstance(val, list) else default
def is_applicable(cls, settings: sublime.Settings) -> bool: return settings.get('translate_tabs_to_spaces', False)