feat(def): add cross-file navigation and rename for PSC .def block templates and addresses
Add client and server support to navigate, inspect, reference, and rename block template and address symbols declared in PSC .def files: - Client: register language providers for .def (definition, hover, references, prepare/provide rename) and send the current document text with each request. - Server: parse .def files into DefDocument/DefDeclaration/DefReference, expose def-specific LSP endpoints (definition/hover/references/prepareRename/rename), and integrate .def lookups into existing Tcl hover/definition/references/rename flows so Tcl calls jump to .def declarations. - Tcl server keeps a snapshot API for .def documents (current editor content can replace file on request); only declared names in loaded .def files can be renamed. Name validation uses DEF_NAME_RE. Update README and CHANGELOG to document navigation features.
This commit is contained in:
@@ -114,6 +114,49 @@ def _name_range(node: Node, raw_name: str, *, variable_sub: bool = False) -> lsp
|
||||
)
|
||||
|
||||
|
||||
DEF_BLOCK_TEMPLATE = "block_template"
|
||||
DEF_ADDRESS = "address"
|
||||
DEF_SYMBOL_KINDS = frozenset({DEF_BLOCK_TEMPLATE, DEF_ADDRESS})
|
||||
|
||||
# NX commands taking .def names: command -> (first argument index, last index or
|
||||
# None for all following arguments, kind).
|
||||
_DEF_ARGUMENTS: dict[str, tuple[tuple[int, int | None, str], ...]] = {
|
||||
"MOM_do_template": ((0, 0, DEF_BLOCK_TEMPLATE),),
|
||||
"MOM_add_to_block_buffer": ((0, 0, DEF_BLOCK_TEMPLATE),),
|
||||
"MOM_polar_motion": ((0, 0, DEF_BLOCK_TEMPLATE),),
|
||||
"MOM_force_block": ((1, None, DEF_BLOCK_TEMPLATE),),
|
||||
"MOM_ask_address_value": ((0, 0, DEF_ADDRESS),),
|
||||
"MOM_add_to_address_buffer": ((0, 0, DEF_ADDRESS),),
|
||||
"MOM_enable_address": ((0, None, DEF_ADDRESS),),
|
||||
"MOM_disable_address": ((0, None, DEF_ADDRESS),),
|
||||
"MOM_force": ((1, None, DEF_ADDRESS),),
|
||||
"MOM_suppress": ((1, None, DEF_ADDRESS),),
|
||||
"MOM_incremental": ((1, None, DEF_ADDRESS),),
|
||||
}
|
||||
_DEFINITION_ELEMENT_KINDS = {"ADDRESS": DEF_ADDRESS, "BLOCK": DEF_BLOCK_TEMPLATE}
|
||||
|
||||
|
||||
def def_argument_kinds(command: Command) -> list[tuple[Node, str]]:
|
||||
"""Return the arguments of ``command`` that name a .def block template or address."""
|
||||
routine = _static_contents(command.routine)
|
||||
if routine in {"MOM_ask_definition_element", "MOM_has_definition_element"}:
|
||||
kind = _DEFINITION_ELEMENT_KINDS.get((_static_contents(command.args[0]) or "").upper()) if command.args else None
|
||||
return [(command.args[1], kind)] if kind and len(command.args) >= 2 else []
|
||||
result = []
|
||||
for first, last, kind in _DEF_ARGUMENTS.get(routine or "", ()):
|
||||
for position, argument in enumerate(command.args):
|
||||
if position >= first and (last is None or position <= last):
|
||||
result.append((argument, kind))
|
||||
return result
|
||||
|
||||
|
||||
def _def_name(node: Node) -> str | None:
|
||||
name = _static_contents(node)
|
||||
if not name or node.contents_pos is None or any(char.isspace() or char in "$[]{}\\\"" for char in name):
|
||||
return None
|
||||
return name
|
||||
|
||||
|
||||
def _node_range(node: Node) -> lsp.Range | None:
|
||||
if node.pos is None or node.end_pos is None:
|
||||
return None
|
||||
@@ -436,6 +479,23 @@ def build_file_symbol_index(
|
||||
if routine:
|
||||
add_proc(command.routine, routine, scope, is_definition=False)
|
||||
|
||||
for node, kind in def_argument_kinds(command):
|
||||
name = _def_name(node)
|
||||
if name is None:
|
||||
continue
|
||||
line, column = node.contents_pos
|
||||
occurrences.append(
|
||||
SymbolOccurrence(
|
||||
identity=shared(SymbolIdentity(kind=kind, name=name)),
|
||||
range=lsp.Range(
|
||||
start=lsp.Position(line=line - 1, character=column - 1),
|
||||
end=lsp.Position(line=line - 1, character=column - 1 + len(name)),
|
||||
),
|
||||
placeholder=name,
|
||||
symbol_kind=lsp.SymbolKind.Struct if kind == DEF_BLOCK_TEMPLATE else lsp.SymbolKind.Field,
|
||||
)
|
||||
)
|
||||
|
||||
declaration_nodes = _variable_declaration_nodes(command)
|
||||
declaration_ids = {id(node) for node in declaration_nodes}
|
||||
for node in declaration_nodes:
|
||||
|
||||
Reference in New Issue
Block a user