This commit is contained in:
@@ -125,10 +125,46 @@ def document_diagnostic(params: lsp.DocumentDiagnosticParams):
|
||||
|
||||
@LSP_SERVER.feature(lsp.TEXT_DOCUMENT_COMPLETION)
|
||||
def on_completion(params: lsp.CompletionParams) -> lsp.CompletionList:
|
||||
_ = LSP_SERVER.workspace.get_text_document(params.text_document.uri)
|
||||
from tools.variable_index import build_variable_index
|
||||
from tools.completion_items import BUILTIN_VAR_LABELS
|
||||
|
||||
doc = LSP_SERVER.workspace.get_text_document(params.text_document.uri)
|
||||
|
||||
# Base items
|
||||
poco = [item for items in LSP_SERVER.poco_completion.values() for item in items]
|
||||
items = standard_items.tcl_keyword_list + standard_items.nx_procs + standard_items.nx_variables + poco
|
||||
return lsp.CompletionList(is_incomplete=False, items=items)
|
||||
base_items = standard_items.tcl_keyword_list + standard_items.nx_procs + standard_items.nx_variables + poco
|
||||
|
||||
# Build variable index from current document
|
||||
globals_set, procs_locals, proc_ranges = build_variable_index(doc.source)
|
||||
|
||||
# Always include globals (excluding built-ins)
|
||||
dynamic_items = []
|
||||
for name in sorted(globals_set):
|
||||
if name not in BUILTIN_VAR_LABELS:
|
||||
dynamic_items.append(lsp.CompletionItem(label=name, kind=lsp.CompletionItemKind.Variable))
|
||||
|
||||
# Include proc-local variables when cursor is inside that proc
|
||||
pos = params.position
|
||||
if pos is not None:
|
||||
for pr in proc_ranges:
|
||||
if pr.start_line <= pos.line <= (pr.end_line or pr.start_line):
|
||||
for name in sorted(procs_locals.get(pr.name, set())):
|
||||
# Exclude built-ins and globals to avoid duplication
|
||||
if name not in BUILTIN_VAR_LABELS and name not in globals_set:
|
||||
dynamic_items.append(lsp.CompletionItem(label=name, kind=lsp.CompletionItemKind.Variable))
|
||||
break
|
||||
|
||||
# Merge with de-duplication for variables only
|
||||
merged: list[lsp.CompletionItem] = []
|
||||
seen_var_labels: set[str] = set()
|
||||
for it in base_items + dynamic_items:
|
||||
if getattr(it, "kind", None) == lsp.CompletionItemKind.Variable:
|
||||
if it.label in seen_var_labels:
|
||||
continue
|
||||
seen_var_labels.add(it.label)
|
||||
merged.append(it)
|
||||
|
||||
return lsp.CompletionList(is_incomplete=False, items=merged)
|
||||
|
||||
|
||||
# @LSP_SERVER.feature(lsp.TEXT_DOCUMENT_DOCUMENT_SYMBOL)
|
||||
|
||||
Reference in New Issue
Block a user