Files
nx_post_support/server/src/common/load_data.py
T

154 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import json
import lsprotocol.types as lsp
import pathlib
KIND_MAP = {
"function": lsp.CompletionItemKind.Function,
"method": lsp.CompletionItemKind.Method,
"class": lsp.CompletionItemKind.Class,
"variable": lsp.CompletionItemKind.Variable,
"field": lsp.CompletionItemKind.Field,
"module": lsp.CompletionItemKind.Module,
"namespace": lsp.CompletionItemKind.Class,
"keyword": lsp.CompletionItemKind.Keyword,
}
class StandardCompletionItems:
def __init__(self):
self.__json_data: dict = self.__load_json()
self.__tcl_keyword_list = self.__load_tcl_keyword()
self.__nx_procs = self.__load_nx_procs()
self.__nx_variables = self.__load_nx_variables()
self.__custom_functions = list[lsp.CompletionItem]
@property
def json_data(self):
return self.__json_data
@property
def tcl_keyword_list(self):
return self.__tcl_keyword_list
@property
def nx_procs(self):
return self.__nx_procs
@property
def nx_variables(self):
return self.__nx_variables
@property
def custom_functions(self) -> list[lsp.CompletionItem]:
return self.__custom_functions
@custom_functions.setter
def custom_functions(self, value: lsp.CompletionItem):
self.__custom_functions.append(value)
def __load_json(self) -> dict:
with open(
pathlib.Path(__file__).parent.joinpath("completion_list.json"), "r"
) as f:
data = json.load(f)
return data
def __load_tcl_keyword(self) -> list[lsp.CompletionItem]:
data = self.json_data
items = []
for i in data.get("tcl", []):
items.append(
lsp.CompletionItem(
label=i.get("label", ""),
kind=KIND_MAP.get(i.get("kind", ""), lsp.CompletionItemKind.Text),
)
)
return items
def __load_nx_variables(self):
data = self.json_data
items = []
for var in data.get("mom_variables", []):
label = var.get("label", "")
kind_str = var.get("kind", "").lower()
kind_enum = KIND_MAP.get(kind_str, lsp.CompletionItemKind.Text)
doc_md = f"""\
**MOM variable**
{label}
**Description**
{var.get("description", "")}
**Possible Values:**
{var.get("possible_values", "any")}
**Data type**
{var.get("data_type", "")}
"""
items.append(
lsp.CompletionItem(
label=label,
kind=kind_enum,
detail=f"{label}",
documentation=lsp.MarkupContent(
kind=lsp.MarkupKind.Markdown, value=doc_md
),
)
)
return items
def __load_nx_procs(self) -> list[lsp.CompletionItem]:
data = self.json_data
items = []
for proc in data.get("MOM_procs", []):
label = proc.get("label", "")
kind_str = proc.get("kind", "").lower()
kind_enum = KIND_MAP.get(kind_str, lsp.CompletionItemKind.Text)
parameters = proc.get("parameters", [])
param_lines = (
"\n".join(f"- `{p['name']}`: {p['desc']}" for p in parameters)
or "_None_"
)
example_data = proc.get("example", [])
example_md = "\n".join(f"{line}" for line in example_data)
returns_data = proc.get("returns", ["None"])
returns_md = "\n".join(f"- {line}" for line in returns_data)
doc_md = f"""\
### 📘 {label}
**Purpose**
{proc.get("description", "No description available.")}
**Format**
`{proc.get("format", label)}`
**Parameters**
{param_lines}
**Return value**
{returns_md}
**Example**
```tcl
{example_md}"""
items.append(
lsp.CompletionItem(
label=label,
kind=kind_enum,
detail=f"{label} {proc.get('description', '').split('.')[0]}",
documentation=lsp.MarkupContent(
kind=lsp.MarkupKind.Markdown, value=doc_md
),
)
)
return items
standard_items = StandardCompletionItems()