Skip to content

Commit aa9d1ac

Browse files
committed
More VS Code mangling while in CI/CD pipeline.
1 parent edac93e commit aa9d1ac

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

bin/cleanup-text.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,36 @@ def em_dash_replacer(match):
9797
return text
9898

9999

100-
def ensure_single_newline(text: str) -> str:
100+
def handle_newlines(text: str, no_newline: bool = False) -> str:
101101
"""
102-
Ensure the text ends with exactly one newline character. Used for all text files.
102+
Handle newline at EOF based on flags and environment.
103+
104+
Args:
105+
text (str): The text to process
106+
no_newline (bool): If True, don't add any newlines
107+
108+
Returns:
109+
str: Text with appropriate newline handling
103110
"""
104-
return text.rstrip('\r\n') + '\n'
111+
if no_newline:
112+
return text # Leave exactly as is
113+
114+
# Check if running inside VS Code extension host (but not CI/CD pipeline)
115+
vscode_extension = False
116+
process_title = os.environ.get('VSCODE_PROCESS_TITLE', '')
117+
app_insights = os.environ.get('APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL', '')
118+
if process_title.startswith('extension-host') and app_insights != 'true':
119+
vscode_extension = True
120+
121+
# Only add newline if there isn't one already
122+
if not text.endswith('\n'):
123+
text += '\n'
124+
125+
# Add extra newline if running in VS Code extension host (to compensate for stripping)
126+
if vscode_extension:
127+
text += '\n'
128+
129+
return text
105130

106131

107132
def main():
@@ -157,23 +182,7 @@ def main():
157182
# No files provided: filter mode (STDIN to STDOUT)
158183
raw = sys.stdin.read()
159184
cleaned = clean_text(raw, preserve_invisible=args.invisible)
160-
161-
# Check if running inside VS Code extension host
162-
vscode_extension = False
163-
process_title = os.environ.get('VSCODE_PROCESS_TITLE', '')
164-
if process_title.startswith('extension-host'):
165-
vscode_extension = True
166-
167-
# Handle newline at EOF based on -n/--no-newline
168-
if not args.no_newline:
169-
# Only add newline if there isn't one already
170-
if not cleaned.endswith('\n'):
171-
cleaned += '\n'
172-
# Add extra newline if running in VS Code extension host (only when we added the first one)
173-
if vscode_extension:
174-
cleaned += '\n'
175-
# If --no-newline is specified, leave the file exactly as is (no changes to newlines)
176-
185+
cleaned = handle_newlines(cleaned, args.no_newline)
177186
sys.stdout.write(cleaned)
178187
return
179188

@@ -198,11 +207,7 @@ def main():
198207
with open(tmpfile, "r", encoding="utf-8", errors="replace") as f:
199208
raw = f.read()
200209
cleaned = clean_text(raw, preserve_invisible=args.invisible)
201-
# Add or suppress newline at EOF based on -n/--no-newline
202-
if not args.no_newline:
203-
cleaned = ensure_single_newline(cleaned)
204-
else:
205-
cleaned = cleaned.rstrip('\r\n')
210+
cleaned = handle_newlines(cleaned, args.no_newline)
206211
with open(infile, "w", encoding="utf-8") as f:
207212
f.write(cleaned)
208213
print(f"[✓] Cleaned (in-place): {infile}")
@@ -215,11 +220,7 @@ def main():
215220
with open(infile, "r", encoding="utf-8", errors="replace") as f:
216221
raw = f.read()
217222
cleaned = clean_text(raw, preserve_invisible=args.invisible)
218-
# Add or suppress newline at EOF based on -n/--no-newline
219-
if not args.no_newline:
220-
cleaned = ensure_single_newline(cleaned)
221-
else:
222-
cleaned = cleaned.rstrip('\r\n')
223+
cleaned = handle_newlines(cleaned, args.no_newline)
223224

224225
if args.output:
225226
if args.output == '-':

0 commit comments

Comments
 (0)