Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: utilize latest lsp_utils #33

Merged
merged 11 commits into from
Nov 28, 2020
44 changes: 33 additions & 11 deletions plugin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
import sublime
import sys

from LSP.plugin import __version__ as lsp_version
from LSP.plugin import DottedDict
from LSP.plugin.core.typing import Any, Dict, List, Optional, Tuple
from lsp_utils import NpmClientHandler
from sublime_lib import ActivityIndicator
import os
import sublime
import sys


def plugin_loaded() -> None:
Expand Down Expand Up @@ -35,20 +36,27 @@ def minimum_node_version(cls) -> Tuple[int, int, int]:

@classmethod
def on_settings_read(cls, settings: sublime.Settings) -> bool:
""" Only needed for ST 3 """

super().on_settings_read(settings)

if settings.get("dev_environment") == "sublime_text":
if lsp_version < (1, 0, 0) and cls.get_dev_environment() == "sublime_text":
server_settings = settings.get("settings", {}) # type: Dict[str, Any]

# add package dependencies into "python.analysis.extraPaths"
extraPaths = server_settings.get("python.analysis.extraPaths", []) # type: List[str]
extraPaths.extend(cls.find_package_dependency_dirs())
server_settings["python.analysis.extraPaths"] = extraPaths

cls.inject_extra_paths_st(server_settings)
settings.set("settings", server_settings)

return False

def on_settings_changed(self, settings: DottedDict) -> None:
""" Only works for ST 4 """

super().on_settings_changed(settings)

if self.get_dev_environment() == "sublime_text":
server_settings = settings.get() # type: Dict[str, Any]
self.inject_extra_paths_st(server_settings)
settings.update(server_settings)

def on_ready(self, api) -> None:
api.on_notification("pyright/beginProgress", self.handle_begin_progress)
api.on_notification("pyright/endProgress", self.handle_end_progress)
Expand All @@ -65,6 +73,20 @@ def handle_end_progress(self, params) -> None:
def handle_report_progress(self, params: List[str]) -> None:
self._start_indicator("{}: {}".format(self.package_name, "; ".join(params)))

@classmethod
def get_dev_environment(cls, settings: Optional[sublime.Settings] = None) -> str:
if not settings:
settings = sublime.load_settings(cls.package_name + ".sublime-settings")

return str(settings.get("dev_environment"))

@classmethod
def inject_extra_paths_st(cls, server_settings: Dict[str, Any]) -> None:
# add package dependencies into "python.analysis.extraPaths"
extraPaths = server_settings.get("python.analysis.extraPaths", []) # type: List[str]
extraPaths.extend(cls.find_package_dependency_dirs())
server_settings["python.analysis.extraPaths"] = extraPaths
jfcherng marked this conversation as resolved.
Show resolved Hide resolved

@staticmethod
def find_package_dependency_dirs() -> List[str]:
dep_dirs = sys.path.copy()
Expand Down