Skip to content

Commit

Permalink
refactor: tidy codes
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Jul 12, 2024
1 parent 885797a commit 6c26af2
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions plugin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
import shutil
import sys
from collections import defaultdict
import weakref
from dataclasses import dataclass
from pathlib import Path
from typing import Any, cast
Expand Down Expand Up @@ -47,7 +47,7 @@ class LspBasedpyrightPlugin(NpmClientHandler):
server_version = ""
"""The version of the language server."""

window_attrs: defaultdict[WindowId, WindowAttr] = defaultdict(WindowAttr)
window_attrs: weakref.WeakKeyDictionary[sublime.Window, WindowAttr] = weakref.WeakKeyDictionary()
"""Per-window attributes. I.e., per-session attributes."""

@classmethod
Expand All @@ -65,6 +65,20 @@ def should_ignore(cls, view: sublime.View) -> bool:
return True
return False

@classmethod
def can_start(
cls,
window: sublime.Window,
initiating_view: sublime.View,
workspace_folders: list[WorkspaceFolder],
configuration: ClientConfig,
) -> str | None:
if message := super().can_start(window, initiating_view, workspace_folders, configuration):
return message

cls.window_attrs.setdefault(window, WindowAttr())
return None

def on_settings_changed(self, settings: DottedDict) -> None:
super().on_settings_changed(settings)

Expand Down Expand Up @@ -92,7 +106,7 @@ def on_pre_start(

cls.server_version = cls.parse_server_version()
cls.update_venv_info(configuration.settings, workspace_folders, window=window)
if venv_info := cls.window_attrs[window.id()].venv_info:
if venv_info := cls.window_attrs[window].venv_info:
log_info(f"Using python executable: {venv_info.python_executable}")
configuration.settings.set("python.pythonPath", str(venv_info.python_executable))
return None
Expand Down Expand Up @@ -151,13 +165,12 @@ def copy_overwrite_dirs(cls) -> None:
def update_status_bar_text(self, extra_variables: dict[str, Any] | None = None) -> None:
if not (session := self.weaksession()):
return
window_id = session.window.id()

variables: dict[str, Any] = {
"server_version": self.server_version,
}

if venv_info := self.window_attrs[window_id].venv_info:
if venv_info := self.window_attrs[session.window].venv_info:
variables["venv"] = {
"finder_name": venv_info.meta.finder_name,
"python_version": venv_info.python_version,
Expand Down Expand Up @@ -251,8 +264,7 @@ def update_venv_info(
*,
window: sublime.Window,
) -> None:
window_id = window.id()
window_attr = cls.window_attrs[window_id]
window_attr = cls.window_attrs[window]

def _update_venv_info() -> None:
window_attr.venv_info = None
Expand Down

0 comments on commit 6c26af2

Please sign in to comment.