add goto
/ build_and_publish (release) Successful in 31s

This commit is contained in:
Christoph Brandau
2025-08-11 18:40:06 +02:00
parent 0a26cb5c0f
commit 72f894b05d
2 changed files with 188 additions and 0 deletions
+72
View File
@@ -444,6 +444,77 @@ def hover(params: lsp.HoverParams) -> lsp.Hover:
return None
@LSP_SERVER.feature(lsp.TEXT_DOCUMENT_DEFINITION)
def goto_definition(params: lsp.DefinitionParams):
"""Provide go-to-definition locations for Tcl procs.
Strategy:
- Find the token under the cursor.
- If it matches a custom proc collected in proc_signatures, locate its declaration
by searching the current document first, then other indexed files.
- Return a Location pointing to the proc name in its declaration line.
"""
doc = LSP_SERVER.workspace.get_text_document(params.text_document.uri)
pos = params.position
try:
line = doc.lines[pos.line]
except IndexError:
return None
# Identify token under cursor
token = None
for m in re.finditer(r"\b\w+\b", line):
if m.start() <= pos.character <= m.end():
token = m.group(0)
break
if not token:
return None
# Helper to search a single source text for a proc declaration
def find_decl_in_source(source_text: str, uri: str) -> Optional[lsp.Location]:
lines = source_text.split("\n")
pattern = re.compile(r"^\s*proc\s+" + re.escape(token) + r"\b")
for i, ln in enumerate(lines):
m = pattern.match(ln)
if m:
start_char = ln.find(token)
if start_char < 0:
start_char = max(m.end() - len(token), 0)
start = lsp.Position(i, start_char)
end = lsp.Position(i, start_char + len(token))
return lsp.Location(uri=uri, range=lsp.Range(start=start, end=end))
return None
# 1) Search in current document
loc = find_decl_in_source(doc.source, doc.uri)
if loc:
return loc
# 2) Search in indexed files from proc_signatures
# Build list of candidate files that declare this token as a proc
candidate_files: List[str] = []
for file_path, procs in LSP_SERVER.proc_signatures.items():
if token in procs:
candidate_files.append(file_path)
for fp in candidate_files:
uri = pathlib.Path(fp).as_uri()
# Try to get from workspace if available; else read from disk
try:
other_doc = LSP_SERVER.workspace.get_text_document(uri)
source = other_doc.source
except Exception:
try:
source = pathlib.Path(fp).read_text(encoding="utf-8")
except Exception:
continue
loc = find_decl_in_source(source, uri)
if loc:
return loc
return None
# **********************************************************
# Linting features end here
# **********************************************************
@@ -508,6 +579,7 @@ def initialize(params: lsp.InitializeParams) -> lsp.InitializeResult:
capabilities=lsp.ServerCapabilities(
document_formatting_provider=GLOBAL_SETTINGS.get("formatter", True),
semantic_tokens_provider=lsp.SemanticTokensOptions(legend=semantic_tokens_legend, full=True, range=False),
definition_provider=True,
)
)