Skip to content

Commit 4bdb4c9

Browse files
committed
tidy: remove duplicate comments
1 parent 97d7906 commit 4bdb4c9

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

marimo/_convert/comment_preserver.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ def wrapper(*args: object, **kwargs: object) -> list[str]:
7373
original_sources = args[0]
7474

7575
# Merge comments back into transformed sources
76-
return self._merge_comments(
77-
original_sources, transformed_sources
78-
)
76+
result = self._merge_comments(original_sources, transformed_sources)
77+
78+
# Update our internal comment data to track only the clean transformed sources
79+
# This clears old comments that no longer apply
80+
self._update_comments_for_transformed_sources(transformed_sources)
81+
82+
return result
7983

8084
return wrapper
8185

@@ -138,20 +142,37 @@ def _apply_comments_to_source(
138142
if target_line_idx < 0:
139143
continue
140144

145+
# Select the best comment for this line (line comments take precedence)
146+
line_comment = None
147+
inline_comment = None
148+
141149
for comment in line_comments:
142-
comment_text = comment.text
143-
if comment.col > 0 and target_line_idx < len(original_lines):
150+
if comment.col == 0: # Line comment (starts at column 0)
151+
line_comment = comment
152+
break # Line comment takes precedence, no need to check others
153+
else: # Inline comment
154+
inline_comment = comment
155+
156+
# Prefer line comment over inline comment
157+
chosen_comment = line_comment if line_comment else inline_comment
158+
159+
if chosen_comment:
160+
comment_text = chosen_comment.text
161+
if chosen_comment.col > 0 and target_line_idx < len(original_lines):
144162
# Inline comment - append to the line if not already present
145163
current_line = result_lines[target_line_idx]
146-
if not current_line.rstrip().endswith(
147-
comment_text.rstrip()
148-
):
164+
if not current_line.rstrip().endswith(comment_text.rstrip()):
149165
result_lines[target_line_idx] = (
150166
current_line.rstrip() + " " + comment_text
151167
)
152168
elif target_line_idx >= 0 and comment_text not in result_lines:
153-
# Standalone comment - insert above the line if not already
154-
# present
169+
# Standalone comment - insert above the line if not already present
155170
result_lines.insert(target_line_idx, comment_text)
156171

157172
return "\n".join(result_lines)
173+
174+
def _update_comments_for_transformed_sources(self, sources: list[str]) -> None:
175+
"""Update internal comment data to track the transformed sources."""
176+
self.sources = sources
177+
self.comments_by_source = {}
178+
self._extract_all_comments()

tests/_convert/snapshots/convert_comments_preservation.py.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ def _():
6363
@app.cell
6464
def _(np):
6565
# Cell 7: Duplicate import with different comment
66-
# This should be deduplicated but preserve one of the comments # Import numpy again with different comment
6766
# This should be deduplicated but preserve one of the comments
68-
# Create array
6967
arr = np.array([1, 2, 3]) # Import numpy again with different comment # Create array
7068
return
7169

0 commit comments

Comments
 (0)