-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
72 lines (57 loc) · 2.11 KB
/
plugin.py
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
67
68
69
70
71
72
from __future__ import annotations
import os
from collections.abc import Callable
import sublime
from LSP.plugin import Session
from lsp_utils import ApiWrapperInterface, NpmClientHandler, request_handler
from .data_types import CustomDataChangedNotification, CustomDataRequest
assert __package__
def plugin_loaded() -> None:
LspHtmlPlugin.setup()
def plugin_unloaded() -> None:
LspHtmlPlugin.cleanup()
class LspHtmlPlugin(NpmClientHandler):
package_name = __package__
server_directory = "language-server"
server_binary_path = os.path.join(
server_directory,
"html-language-features",
"server",
"out",
"node",
"htmlServerNodeMain.js",
)
@classmethod
def required_node_version(cls) -> str:
return ">=14"
@classmethod
def should_ignore(cls, view: sublime.View) -> bool:
return bool(
# SublimeREPL views
view.settings().get("repl")
# syntax test files
or os.path.basename(view.file_name() or "").startswith("syntax_test")
)
def on_ready(self, api: ApiWrapperInterface) -> None:
session = self.weaksession()
if not session:
return
self.resolve_custom_data_paths(session)
def resolve_custom_data_paths(self, session: Session) -> None:
custom_data_paths: list[str] = session.config.settings.get("html.customData")
resolved_custom_data_paths: list[str] = []
for folder in session.get_workspace_folders():
resolved_custom_data_paths.extend(os.path.abspath(os.path.join(folder.path, p)) for p in custom_data_paths)
session.send_notification(CustomDataChangedNotification.create(resolved_custom_data_paths))
@request_handler(CustomDataRequest.Type)
def on_custom_data_content_async(
self,
params: CustomDataRequest.Params,
respond: Callable[[CustomDataRequest.Response], None],
) -> None:
(file_path,) = params
try:
with open(file_path, encoding="utf-8") as fd:
respond(fd.read())
except Exception:
respond("")