-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_comment_context.py
More file actions
387 lines (293 loc) · 11.4 KB
/
cpp_comment_context.py
File metadata and controls
387 lines (293 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
from __future__ import annotations
from typing import Any
COMMENT_CONTEXT_SCHEMA = "cpp.comment_context.v1"
LINE_COMMENT_PREFIXES = ("//", "///", "//!")
def format_source_lines(lines: list[str], start_line: int, end_line: int) -> str:
start_line = max(1, start_line)
end_line = min(len(lines), end_line)
if start_line > end_line:
return ""
return "\n".join(
f"{line_no:04d}: {lines[line_no - 1]}"
for line_no in range(start_line, end_line + 1)
)
def comment_text_from_source(source: str) -> str:
if not source:
return ""
result: list[str] = []
for line in source.splitlines():
# Strip the generated `0001: ` prefix if present.
if len(line) >= 6 and line[:4].isdigit() and line[4:6] == ": ":
result.append(line[6:])
else:
result.append(line)
return "\n".join(result)
def _strip_bom(text: str) -> str:
return text.removeprefix("\ufeff")
def _is_global_module_fragment_line(line: str) -> bool:
return _strip_bom(line).strip() == "module;"
def _is_blank(line: str) -> bool:
return not line.strip()
def _is_line_comment(line: str) -> bool:
stripped = line.lstrip()
return stripped.startswith("//")
def _looks_like_block_comment_line(line: str) -> bool:
stripped = line.lstrip()
return stripped.startswith("/*") or stripped.startswith("*") or "*/" in stripped
def _has_block_comment_end(line: str) -> bool:
return "*/" in line
def _has_block_comment_start(line: str) -> bool:
return "/*" in line
def _is_code_before_block_start(line: str) -> bool:
index = line.find("/*")
if index < 0:
return False
return bool(line[:index].strip())
def _is_code_after_block_end(line: str) -> bool:
index = line.rfind("*/")
if index < 0:
return False
return bool(line[index + 2 :].strip())
def _empty_leading_result(
*,
relative_path: str,
target_start_line: int,
target_end_line: int,
) -> dict[str, Any]:
return {
"schema": COMMENT_CONTEXT_SCHEMA,
"relativePath": relative_path,
"targetStartLine": target_start_line,
"targetEndLine": target_end_line,
"hasComment": False,
"commentStartLine": None,
"commentEndLine": None,
"source": "",
"text": "",
}
def _empty_header_result(*, relative_path: str) -> dict[str, Any]:
return {
"schema": COMMENT_CONTEXT_SCHEMA,
"relativePath": relative_path,
"hasComment": False,
"commentStartLine": None,
"commentEndLine": None,
"source": "",
"text": "",
}
def _collect_block_comment_ending_at(
*,
lines: list[str],
end_index: int,
min_index: int,
) -> tuple[int, int] | None:
index = end_index
while index >= min_index:
line = lines[index]
if _has_block_comment_start(line):
# Leading comments must not be attached when the block starts after
# real code on the same line, e.g. `int x; /* comment */`.
if _is_code_before_block_start(line):
return None
# The line that closes the block must also not contain following
# code. This keeps the extractor conservative.
if _is_code_after_block_end(lines[end_index]):
return None
return index, end_index
# Inside a block comment, middle lines usually start with `*` or are
# blank. Anything that looks like real code means this is not a clean
# leading block comment.
if not _looks_like_block_comment_line(line) and not _is_blank(line):
return None
index -= 1
return None
def extract_leading_comment(
*,
lines: list[str],
relative_path: str,
target_start_line: int,
target_end_line: int,
max_lines: int = 20,
allow_blank_gap: bool = True,
max_blank_gap: int = 2,
) -> dict[str, Any]:
"""Extract the exact leading comment range before a known source range.
This intentionally does not index comments. It only inspects the original
source lines immediately before a symbol/data declaration on demand.
V1 is conservative:
- line-comment blocks are collected when directly above the target
- block comments are collected when the block ends directly above the target
- optional blank gap is allowed only between comment and target
- comments separated from the target by real code are not attached
"""
if target_start_line <= 1 or not lines:
return _empty_leading_result(
relative_path=relative_path,
target_start_line=target_start_line,
target_end_line=target_end_line,
)
max_lines = max(1, max_lines)
index = min(len(lines), target_start_line - 1) - 1
blank_gap = 0
if allow_blank_gap:
while index >= 0 and _is_blank(lines[index]) and blank_gap < max_blank_gap:
blank_gap += 1
index -= 1
if index < 0:
return _empty_leading_result(
relative_path=relative_path,
target_start_line=target_start_line,
target_end_line=target_end_line,
)
min_index = max(0, index - max_lines + 1)
if _is_line_comment(lines[index]):
end_index = index
while index >= min_index and _is_line_comment(lines[index]):
index -= 1
start_index = index + 1
source = format_source_lines(lines, start_index + 1, end_index + 1)
return {
"schema": COMMENT_CONTEXT_SCHEMA,
"relativePath": relative_path,
"targetStartLine": target_start_line,
"targetEndLine": target_end_line,
"hasComment": True,
"commentStartLine": start_index + 1,
"commentEndLine": end_index + 1,
"source": source,
"text": comment_text_from_source(source),
}
if _has_block_comment_end(lines[index]) and _looks_like_block_comment_line(lines[index]):
block_range = _collect_block_comment_ending_at(
lines=lines,
end_index=index,
min_index=min_index,
)
if block_range is not None:
start_index, end_index = block_range
source = format_source_lines(lines, start_index + 1, end_index + 1)
return {
"schema": COMMENT_CONTEXT_SCHEMA,
"relativePath": relative_path,
"targetStartLine": target_start_line,
"targetEndLine": target_end_line,
"hasComment": True,
"commentStartLine": start_index + 1,
"commentEndLine": end_index + 1,
"source": source,
"text": comment_text_from_source(source),
}
if _has_block_comment_start(lines[index]) and _has_block_comment_end(lines[index]):
if not _is_code_before_block_start(lines[index]) and not _is_code_after_block_end(lines[index]):
source = format_source_lines(lines, index + 1, index + 1)
return {
"schema": COMMENT_CONTEXT_SCHEMA,
"relativePath": relative_path,
"targetStartLine": target_start_line,
"targetEndLine": target_end_line,
"hasComment": True,
"commentStartLine": index + 1,
"commentEndLine": index + 1,
"source": source,
"text": comment_text_from_source(source),
}
return _empty_leading_result(
relative_path=relative_path,
target_start_line=target_start_line,
target_end_line=target_end_line,
)
def _collect_line_comment_header(
*,
lines: list[str],
start_index: int,
max_index: int,
) -> tuple[int, int]:
index = start_index
while index <= max_index and _is_line_comment(lines[index]):
index += 1
return start_index, index - 1
def _collect_block_comment_header(
*,
lines: list[str],
start_index: int,
max_index: int,
) -> tuple[int, int] | None:
if not _has_block_comment_start(lines[start_index]):
return None
if _is_code_before_block_start(lines[start_index]):
return None
index = start_index
while index <= max_index:
if _has_block_comment_end(lines[index]):
if _is_code_after_block_end(lines[index]):
return None
return start_index, index
index += 1
return None
def extract_file_header_comment(
*,
lines: list[str],
relative_path: str,
max_lines: int = 120,
) -> dict[str, Any]:
"""Extract a file-header comment from the beginning of a file.
V1 returns only the first initial comment block. It skips leading blank
lines, then stops at the first non-comment code line. A blank line after the
first comment block terminates the header.
"""
if not lines:
return _empty_header_result(relative_path=relative_path)
max_index = min(len(lines), max(1, max_lines)) - 1
index = 0
while index <= max_index and _is_blank(lines[index]):
index += 1
# C++20 module implementation/interface files often start with a global
# module fragment:
#
# module;
# // file-level/module-level rationale comment
# #include "stdafx.h"
#
# Treat `module;` as a permitted prefix for file-header extraction. The
# returned header range still contains only the comment lines, not `module;`.
if index <= max_index and _is_global_module_fragment_line(lines[index]):
index += 1
while index <= max_index and _is_blank(lines[index]):
index += 1
if index > max_index:
return _empty_header_result(relative_path=relative_path)
if _is_line_comment(lines[index]):
start_index, end_index = _collect_line_comment_header(
lines=lines,
start_index=index,
max_index=max_index,
)
source = format_source_lines(lines, start_index + 1, end_index + 1)
return {
"schema": COMMENT_CONTEXT_SCHEMA,
"relativePath": relative_path,
"hasComment": True,
"commentStartLine": start_index + 1,
"commentEndLine": end_index + 1,
"source": source,
"text": comment_text_from_source(source),
}
if _has_block_comment_start(lines[index]):
block_range = _collect_block_comment_header(
lines=lines,
start_index=index,
max_index=max_index,
)
if block_range is not None:
start_index, end_index = block_range
source = format_source_lines(lines, start_index + 1, end_index + 1)
return {
"schema": COMMENT_CONTEXT_SCHEMA,
"relativePath": relative_path,
"hasComment": True,
"commentStartLine": start_index + 1,
"commentEndLine": end_index + 1,
"source": source,
"text": comment_text_from_source(source),
}
return _empty_header_result(relative_path=relative_path)