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
68 changes: 57 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 All @@ -15,6 +16,30 @@ def plugin_unloaded() -> None:
LspPyrightPlugin.cleanup()


def deflate_dict(d: Dict[str, Any], sep: str = ".", prefix: str = "") -> Dict[str, Any]:
jfcherng marked this conversation as resolved.
Show resolved Hide resolved
"""
Deflated a nested dict into a single-level dict.

Converts `{"a":{"b":{"c":"d"}}}`` into `{"a.b.c":"d"}`.

:param d: The source dict
:param sep: The key separator
:param prefix: The key prefix
"""

d_new = {}

for k, v in d.items():
prefix_next = (prefix + sep + k) if prefix else k

if isinstance(v, dict):
d_new.update(deflate_dict(v, sep, prefix_next))
else:
d_new[prefix_next] = v

return d_new


class LspPyrightPlugin(NpmClientHandler):
package_name = __package__
server_directory = "language-server"
Expand All @@ -35,20 +60,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 = deflate_dict(settings.get())
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 +97,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