Skip to content

Commit f2a242e

Browse files
committed
Address bneradt's review: wire SESSION_VARS into kg and LSP
kg_visitor.py was missing sessionVarSection dispatch in visitSection, causing hrw4u-kg to silently drop SESSION_VARS blocks. lsp/strings.py only detected VARS { ... } for declaration mode, leaving session-scoped variables without hover/type metadata in hrw4u-lsp.
1 parent c1a0183 commit f2a242e

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

tools/hrw4u/src/kg_visitor.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ def visitSection(self, ctx) -> None:
242242
with self.debug_context("visitSection"):
243243
if ctx.varSection():
244244
return self.visitVarSection(ctx.varSection())
245+
if ctx.sessionVarSection():
246+
return self.visitSessionVarSection(ctx.sessionVarSection())
245247

246248
if ctx.name is None:
247249
return
@@ -294,6 +296,25 @@ def visitVarSection(self, ctx) -> None:
294296

295297
self.current_section_id = old_section_id
296298

299+
def visitSessionVarSection(self, ctx) -> None:
300+
with self.debug_context("visitSessionVarSection"):
301+
vars_section_id = self._add_node(
302+
"VarSection", {
303+
"scope": "session",
304+
"line_start": ctx.start.line if ctx.start else None,
305+
"line_end": ctx.stop.line if ctx.stop else None
306+
}, f"{self.filename}:SESSION_VARS")
307+
308+
if self.current_file_id:
309+
self._add_edge(self.current_file_id, vars_section_id, "CONTAINS")
310+
311+
old_section_id = self.current_section_id
312+
self.current_section_id = vars_section_id
313+
314+
self.visit(ctx.variables())
315+
316+
self.current_section_id = old_section_id
317+
297318
def visitVariableDecl(self, ctx) -> None:
298319
with self.debug_context("visitVariableDecl"):
299320
if ctx.name is None or ctx.typeName is None:

tools/hrw4u/src/lsp/strings.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ def parse_variable_declarations(text: str) -> Dict[str, VariableDeclaration]:
212212
if not stripped or stripped.startswith('//') or stripped.startswith('#'):
213213
continue
214214

215-
# Check for VARS section start
216-
if stripped == 'VARS {' or stripped.startswith('VARS {'):
215+
# Check for VARS or SESSION_VARS section start
216+
if stripped == 'VARS {' or stripped.startswith('VARS {') or \
217+
stripped == 'SESSION_VARS {' or stripped.startswith('SESSION_VARS {'):
217218
in_vars_section = True
218219
brace_count = 1
219220
continue

tools/hrw4u/tests/test_coverage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,14 @@ def test_rewrite_inline_fallback_quoted(self):
366366

367367
def test_get_var_declarations_empty(self):
368368
r = self._resolver()
369-
assert r.get_var_declarations() == []
369+
assert r.get_var_declarations() == ([], [])
370370

371371
def test_get_var_declarations_after_state_tag(self):
372372
r = self._resolver()
373373
r._handle_state_tag("STATE-FLAG", "0")
374-
decls = r.get_var_declarations()
375-
assert len(decls) == 1
376-
assert "bool" in decls[0]
374+
txn_decls, ssn_decls = r.get_var_declarations()
375+
assert len(txn_decls) == 1
376+
assert "bool" in txn_decls[0]
377377

378378
def test_percent_to_ident_invalid(self):
379379
r = self._resolver()

0 commit comments

Comments
 (0)