add first proc doc

This commit is contained in:
Christoph Brandau
2025-08-11 15:59:09 +02:00
parent 27e2b3bf6e
commit bc745622b9
4 changed files with 291 additions and 25 deletions
@@ -0,0 +1,103 @@
import os
import sys
from pathlib import Path
# Ensure server/src is on the path for imports
THIS_DIR = Path(__file__).parent
SRC_DIR = THIS_DIR.parent.parent / "src"
if str(SRC_DIR) not in sys.path:
sys.path.insert(0, str(SRC_DIR))
import lsprotocol.types as lsp # type: ignore
from tools.proc_docs import build_proc_docs, is_proc_declaration_position
from lsp_server import LSP_SERVER, hover # type: ignore
def test_build_proc_docs_extracts_block_and_tags():
source = (
"#____________________________________________________________________________________________\n"
"# <Documentation>\n"
"# This procedure creates a new directory if it does not exist.\n"
"# <Arguments>\n"
"# directory\n"
"#\tThe full pathname of the directory to be created.\n"
"# <Returnvalue>\n"
"# 0 - directory created or already exists\n"
"# 1 - error\n"
"#______________________________________________________________________________________________\n"
"proc LIB_FH_create_directory {directory} {\n"
" return 0\n"
"}\n"
)
from tools.parser import CustomParser
tree = CustomParser().parse(source)
docs = build_proc_docs(tree, source)
assert "LIB_FH_create_directory" in docs
md = docs["LIB_FH_create_directory"]
# Tags become markdown headings
assert "### Documentation" in md
assert "### Arguments" in md
assert "### Return value" in md
# Content preserved
assert "creates a new directory" in md
def test_hover_shows_doc_on_usage_but_not_on_declaration(tmp_path: Path):
# Build a TCL file with a documented proc and a usage
source_lines = [
"#_________________________________________________________________________________________________",
"# <Documentation>",
"# Function to delete the file",
"#_________________________________________________________________________________________________",
"proc SERVICE_remove_file {file} {",
" if {![SERVICE_check_file_exists $file]} {return}",
" MOM_remove_file $file",
"}",
"",
"proc SERVICE_check_file_exists {file} {",
" if {[file exists $file]} {return 1}",
" return 0",
"}",
"",
"# usage below",
"SERVICE_remove_file \"C:/tmp/x\"",
]
source = "\n".join(source_lines)
# Register document with server
uri = Path(tmp_path / "test.tcl").as_uri()
LSP_SERVER.workspace.put_text_document(
lsp.TextDocumentItem(uri=uri, language_id="tcl", version=1, text=source)
)
# Force server to parse and build proc docs
doc = LSP_SERVER.workspace.get_text_document(uri)
LSP_SERVER.update_poco_completion_for_file(doc)
# 1) Hover on usage -> should return docs
usage_line = source_lines.index("SERVICE_remove_file \"C:/tmp/x\"")
char_index = source_lines[usage_line].find("SERVICE_remove_file") + 5 # inside the token
params = lsp.HoverParams(
text_document=lsp.TextDocumentIdentifier(uri=uri),
position=lsp.Position(line=usage_line, character=char_index),
)
result = hover(params)
assert result is not None
assert "Function to delete the file" in result.contents.value # type: ignore[attr-defined]
# 2) Hover on declaration name -> should be None
decl_line = source_lines.index("proc SERVICE_remove_file {file} {")
decl_char = source_lines[decl_line].find("SERVICE_remove_file") + 2
assert is_proc_declaration_position(source, decl_line, decl_char)
params_decl = lsp.HoverParams(
text_document=lsp.TextDocumentIdentifier(uri=uri),
position=lsp.Position(line=decl_line, character=decl_char),
)
none_result = hover(params_decl)
assert none_result is None