diff --git a/plugin/core/constants.py b/plugin/core/constants.py index beae8df1c..bca52cff2 100644 --- a/plugin/core/constants.py +++ b/plugin/core/constants.py @@ -12,6 +12,10 @@ SublimeKind = Tuple[int, str, str] +ST_CACHE_PATH = sublime.cache_path() +ST_INSTALLED_PACKAGES_PATH = sublime.installed_packages_path() +ST_PACKAGES_PATH = sublime.packages_path() +ST_PLATFORM = sublime.platform() ST_VERSION = int(sublime.version()) diff --git a/plugin/core/logging.py b/plugin/core/logging.py index ebaeb3629..2ff74386c 100644 --- a/plugin/core/logging.py +++ b/plugin/core/logging.py @@ -1,8 +1,8 @@ from __future__ import annotations +from .constants import ST_PACKAGES_PATH from typing import Any import traceback import inspect -import sublime log_debug = False @@ -26,7 +26,7 @@ def trace() -> None: return previous_frame = current_frame.f_back file_name, line_number, function_name, _, _ = inspect.getframeinfo(previous_frame) # type: ignore - file_name = file_name[len(sublime.packages_path()) + len("/LSP/"):] + file_name = file_name[len(ST_PACKAGES_PATH) + len("/LSP/"):] debug(f"TRACE {function_name:<32} {file_name}:{line_number}") diff --git a/plugin/core/open.py b/plugin/core/open.py index a9a695f50..097efc937 100644 --- a/plugin/core/open.py +++ b/plugin/core/open.py @@ -1,4 +1,5 @@ from __future__ import annotations +from .constants import ST_PLATFORM from .constants import ST_VERSION from .logging import exception_log from .promise import Promise @@ -133,11 +134,7 @@ def center_selection(v: sublime.View, r: Range) -> sublime.View: window = v.window() if window: window.focus_view(v) - if int(sublime.version()) >= 4124: - v.show_at_center(selection.begin(), animate=False) - else: - # TODO: remove later when a stable build lands - v.show_at_center(selection.begin()) # type: ignore + v.show_at_center(selection.begin(), animate=False) return v @@ -155,9 +152,9 @@ def open_externally(uri: str, take_focus: bool) -> bool: """ try: # TODO: handle take_focus - if sublime.platform() == "windows": + if ST_PLATFORM == "windows": os.startfile(uri) # type: ignore - elif sublime.platform() == "osx": + elif ST_PLATFORM == "osx": subprocess.check_call(("/usr/bin/open", uri)) else: # linux subprocess.check_call(("xdg-open", uri)) diff --git a/plugin/core/transports.py b/plugin/core/transports.py index 1ea3cbbf7..40868ff27 100644 --- a/plugin/core/transports.py +++ b/plugin/core/transports.py @@ -1,4 +1,5 @@ from __future__ import annotations +from .constants import ST_PLATFORM from .logging import exception_log, debug from .types import TCP_CONNECT_TIMEOUT from .types import TransportConfig @@ -310,7 +311,7 @@ def kill_all_subprocesses() -> None: def _fixup_startup_args(args: list[str]) -> Any: startupinfo = None - if sublime.platform() == "windows": + if ST_PLATFORM == "windows": startupinfo = subprocess.STARTUPINFO() # type: ignore startupinfo.dwFlags |= subprocess.SW_HIDE | subprocess.STARTF_USESHOWWINDOW # type: ignore executable_arg = args[0] diff --git a/plugin/core/url.py b/plugin/core/url.py index 1e9f09418..75c3f6235 100644 --- a/plugin/core/url.py +++ b/plugin/core/url.py @@ -1,4 +1,6 @@ from __future__ import annotations +from .constants import ST_INSTALLED_PACKAGES_PATH +from .constants import ST_PACKAGES_PATH from typing import Any from typing_extensions import deprecated from urllib.parse import urljoin @@ -15,10 +17,10 @@ def filename_to_uri(file_name: str) -> str: """ Convert a file name obtained from view.file_name() into an URI """ - prefix = sublime.installed_packages_path() + prefix = ST_INSTALLED_PACKAGES_PATH if file_name.startswith(prefix): return _to_resource_uri(file_name, prefix) - prefix = sublime.packages_path() + prefix = ST_PACKAGES_PATH if file_name.startswith(prefix) and not os.path.exists(file_name): return _to_resource_uri(file_name, prefix) path = pathname2url(file_name) diff --git a/plugin/core/views.py b/plugin/core/views.py index 8fc644570..654fabef7 100644 --- a/plugin/core/views.py +++ b/plugin/core/views.py @@ -1,5 +1,6 @@ from __future__ import annotations from .constants import CODE_ACTION_KINDS +from .constants import ST_CACHE_PATH from .constants import SUBLIME_KIND_SCOPES from .constants import SublimeKind from .css import css as lsp_css @@ -115,13 +116,13 @@ def get_storage_path() -> str: - on Windows: %LocalAppData%/Sublime Text - on Linux: ~/.cache/sublime-text """ - return os.path.abspath(os.path.join(sublime.cache_path(), "..", "Package Storage")) + return os.path.abspath(os.path.join(ST_CACHE_PATH, "..", "Package Storage")) def extract_variables(window: sublime.Window) -> dict[str, str]: variables = window.extract_variables() variables["storage_path"] = get_storage_path() - variables["cache_path"] = sublime.cache_path() + variables["cache_path"] = ST_CACHE_PATH variables["temp_dir"] = tempfile.gettempdir() variables["home"] = os.path.expanduser('~') return variables diff --git a/plugin/locationpicker.py b/plugin/locationpicker.py index d4ccc01d5..97d5f73b0 100644 --- a/plugin/locationpicker.py +++ b/plugin/locationpicker.py @@ -1,4 +1,5 @@ from __future__ import annotations +from .core.constants import ST_PACKAGES_PATH from .core.constants import SublimeKind from .core.logging import debug from .core.protocol import DocumentUri @@ -52,7 +53,7 @@ def open_basic_file( else: prefix = 'res:/Packages' # Note: keep in sync with core/url.py#_to_resource_uri assert uri.startswith(prefix) - filename = sublime.packages_path() + url2pathname(uri[len(prefix):]) + filename = ST_PACKAGES_PATH + url2pathname(uri[len(prefix):]) # Window.open_file can only focus and scroll to a location in a resource file if it is already opened if not session.window.find_open_file(filename): return None