Skip to content

Commit be18e1a

Browse files
authored
Merge pull request #368 from slothy-optimizer/robust-multi-line
More robust multi-line comment handling
2 parents e9cacf5 + 45631f2 commit be18e1a

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

slothy/helper.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@ class SourceLine:
4848
def _extract_comments_from_text(self):
4949
if "//" not in self._raw:
5050
return
51-
s = list(self._raw.split("//"))
51+
# Don't split block comments (they contain markers and may have // in content)
52+
if _NEWLINE_MARKER in self._raw:
53+
s = self._raw.split("//", 1)
54+
else:
55+
s = self._raw.split("//")
5256
self._raw = s[0]
53-
self._comments += map(str.lstrip, s[1:])
57+
# Preserve whitespace for block comments (with markers), lstrip others
58+
self._comments += [c if _NEWLINE_MARKER in c else c.lstrip() for c in s[1:]]
5459
self._trim_comments()
5560

5661
def _extract_indentation_from_text(self):
@@ -96,7 +101,10 @@ def tag_callback(g):
96101
return s
97102

98103
def _strip_comments(self):
99-
self._comments = list(map(str.lstrip, self._comments))
104+
# Preserve whitespace for block comments (with markers), lstrip others
105+
self._comments = [
106+
c if _NEWLINE_MARKER in c else c.lstrip() for c in self._comments
107+
]
100108

101109
def _trim_comments(self):
102110
self._strip_comments()
@@ -246,8 +254,7 @@ def format_comment(s):
246254
if _NEWLINE_MARKER in s:
247255
# Restore as multi-line /* */ comment
248256
lines = s.split(_NEWLINE_MARKER)
249-
lines = [line.strip() for line in lines]
250-
return "/* " + "\n ".join(lines) + " */"
257+
return "/*" + "\n".join(lines) + "*/"
251258
elif s.startswith(_BLOCK_COMMENT_MARKER):
252259
# Restore as single-line /* */ comment
253260
return f"/* {s[len(_BLOCK_COMMENT_MARKER):]} */"
@@ -375,9 +382,8 @@ def replace_comment(match):
375382
content = match.group(1)
376383
if "\n" in content:
377384
# Multi-line /* */ comment - mark newlines
378-
content = content.replace("\n", f" {_NEWLINE_MARKER} ")
379-
content = " ".join(content.split())
380-
return f"// {content}" if content else ""
385+
content = content.replace("\n", _NEWLINE_MARKER)
386+
return f"//{content}" if content.strip() else ""
381387
else:
382388
# Single-line /* */ comment - mark with prefix
383389
content = content.strip()

0 commit comments

Comments
 (0)