-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.py
More file actions
66 lines (54 loc) · 2 KB
/
plugin.py
File metadata and controls
66 lines (54 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from __future__ import annotations
import functools
import os
import urllib.parse
import urllib.request
from collections.abc import Callable
from typing import final
import sublime
from LSP.plugin import ClientConfig
from lsp_utils import NpmClientHandler
from typing_extensions import override
def plugin_loaded() -> None:
LspYamlPlugin.setup()
def plugin_unloaded() -> None:
LspYamlPlugin.cleanup()
@final
class LspYamlPlugin(NpmClientHandler):
package_name = str(__package__)
server_directory = "language-server"
server_binary_path = os.path.join(
server_directory,
"node_modules",
"yaml-language-server",
"bin",
"yaml-language-server",
)
@override
@classmethod
def is_applicable(cls, view: sublime.View, config: ClientConfig) -> bool:
return bool(
super().is_applicable(view, config)
# REPL views (https://github.com/sublimelsp/LSP-pyright/issues/343)
and not view.settings().get("repl")
)
@override
def on_open_uri_async(self, uri: str, callback: Callable[[str, str, str], None]) -> bool:
def run_blocking() -> None:
title = urllib.parse.urldefrag(uri).url
try:
# TODO: Make async!
parsed = urllib.parse.urlparse(uri)
http_url = urllib.parse.unquote(parsed.fragment)
with urllib.request.urlopen(http_url) as f:
content = f.read().decode("utf-8").replace("\r", "")
if syntax_obj := sublime.find_syntax_for_file(urllib.parse.urlparse(http_url).path):
syntax = syntax_obj.path
except Exception as ex:
content = f"Error: {ex}"
syntax = "Packages/Text/Plain text.tmLanguage"
sublime.set_timeout_async(functools.partial(callback, title, content, syntax))
if not uri.startswith("json-schema:"):
return False
sublime.set_timeout_async(run_blocking)
return True