Skip to content

Commit

Permalink
Extract more global constants (#2565)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwortmann authored Dec 9, 2024
1 parent 8b7afad commit 5cff692
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 15 deletions.
4 changes: 4 additions & 0 deletions plugin/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())


Expand Down
4 changes: 2 additions & 2 deletions plugin/core/logging.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}")


Expand Down
11 changes: 4 additions & 7 deletions plugin/core/open.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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


Expand All @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion plugin/core/transports.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]
Expand Down
6 changes: 4 additions & 2 deletions plugin/core/url.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions plugin/core/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion plugin/locationpicker.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5cff692

Please sign in to comment.