Skip to content

Commit 448cb4d

Browse files
committed
WIP - Trying to match comments above a variable to it too
1 parent ef7df1d commit 448cb4d

File tree

6 files changed

+19
-6
lines changed

6 files changed

+19
-6
lines changed

src/plcdoc/interpreter.py

+3
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ def get_comment(self) -> Optional[str]:
369369
return None
370370

371371
big_block: str = block_comment.text
372+
elif hasattr(self._model, "comments_above") and self._model.comments_above is not None:
373+
# Comment was put above it instead of inline
374+
big_block: str = self._model.comments_above[-1].text
372375
else:
373376
return None
374377

src/plcdoc/st_declaration.tx

+4-1
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,12 @@ VariableListType:
157157
Single variable declaration
158158

159159
Unfortunately, it is possible to define multiple variables inline - those are ignored for now
160+
161+
Inline comments after the variable are matched, but comments above it also. Because of priority
162+
comments above would have been caught by the preceding variable if relevant.
160163
*/
161164
Variable:
162-
CommentAny*
165+
comments_above=CommentAny*
163166
name=ID
164167
(',' ID)*
165168
(address=Address)?

tests/plc_code/FB_Comments.txt

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ VAR_INPUT // Ignored
1212
someInput : LREAL; // Important
1313
// Ignored
1414
otherInput : BOOL; // Important
15+
// Important
16+
lastInput : INT;
1517
END_VAR
1618
// Ignored
1719
VAR_OUTPUT

tests/roots/test-plc-autodoc/src_plc/AutoFunction.TcPOU

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ FUNCTION AutoFunction : LREAL
1111
VAR_INPUT
1212
input : LREAL; // This is an in-code description of some variable
1313
other_arg : UDINT;
14+
15+
// This is a comment above "above"
16+
above : BOOL;
1417
END_VAR
1518
VAR
1619
END_VAR

tests/test_plc_autodoc.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_autodoc_function(app, status, warning):
7070

7171
actual = do_autodoc(app, "plc:function", "AutoFunction")
7272

73-
assert ".. plc:function:: AutoFunction(input, other_arg)" in actual
73+
assert ".. plc:function:: AutoFunction(input, other_arg, above)" in actual
7474

7575
expected_end = [
7676
" Short description of the function.",
@@ -80,10 +80,11 @@ def test_autodoc_function(app, status, warning):
8080
"",
8181
" :var_input LREAL input: This is an in-code description of some variable",
8282
" :var_input UDINT other_arg:",
83+
' :var_input BOOL above: This is a comment above "above"',
8384
"",
8485
]
8586

86-
assert expected_end == actual[-8:]
87+
assert list(actual)[-9:] == expected_end
8788

8889

8990
@pytest.mark.sphinx("html", testroot="plc-autodoc")

tests/test_st_grammar.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ def test_grammar_comments(meta_model):
168168
filepath = os.path.realpath(tests_dir + "/plc_code/" + filename)
169169
try:
170170
model = meta_model.model_from_file(filepath)
171-
except:
172-
pytest.fail(f"Error when analyzing the file `{filename}`")
171+
except TextXSyntaxError as err:
172+
pytest.fail(f"Error when analyzing the file `{filename}`: {err}")
173173
else:
174174
assert model is not None
175175
assert model.functions and not model.types
@@ -181,7 +181,7 @@ def test_grammar_comments(meta_model):
181181
"VAR_INPUT",
182182
"VAR_OUTPUT",
183183
"VAR",
184-
"VAR",
184+
"VAR",
185185
]
186186
assert [l.constant for l in fb.lists] == [False, False, True, False]
187187

@@ -190,6 +190,7 @@ def test_grammar_comments(meta_model):
190190
fb.comments[-1].text,
191191
fb.lists[0].variables[0].comment.text,
192192
fb.lists[0].variables[1].comment.text,
193+
fb.lists[0].variables[2].comments_above[0].text,
193194
fb.lists[1].variables[0].comment.text,
194195
fb.lists[2].variables[0].comment.text,
195196
fb.lists[3].variables[0].comment.text,

0 commit comments

Comments
 (0)