Skip to content

Commit 2df81f9

Browse files
committed
Fix: Line endings when editing profiles
1 parent 8f62aac commit 2df81f9

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/features/terminal/shells/common/editUtils.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { isWindows } from '../../../../common/utils/platformUtils';
2+
13
export function hasStartupCode(content: string, start: string, end: string, keys: string[]): boolean {
2-
// Normalize line endings to \n
34
const normalizedContent = content.replace(/\r\n/g, '\n');
45
const startIndex = normalizedContent.indexOf(start);
56
const endIndex = normalizedContent.indexOf(end);
@@ -10,45 +11,46 @@ export function hasStartupCode(content: string, start: string, end: string, keys
1011
return contentBetween.length > 0 && keys.every((key) => contentBetween.includes(key));
1112
}
1213

13-
export function insertStartupCode(content: string, start: string, end: string, code: string): string {
14-
// Detect line ending style from content (default to \n if cannot determine)
15-
const lineEnding = content.includes('\r\n') ? '\r\n' : '\n';
14+
function getLineEndings(content: string): string {
15+
if (content.includes('\r\n')) {
16+
return '\r\n';
17+
} else if (content.includes('\n')) {
18+
return '\n';
19+
}
20+
return isWindows() ? '\r\n' : '\n';
21+
}
1622

17-
// Normalize line endings to \n for processing
23+
export function insertStartupCode(content: string, start: string, end: string, code: string): string {
24+
let lineEnding = getLineEndings(content);
1825
const normalizedContent = content.replace(/\r\n/g, '\n');
26+
1927
const startIndex = normalizedContent.indexOf(start);
2028
const endIndex = normalizedContent.indexOf(end);
2129

2230
let result: string;
2331
if (startIndex !== -1 && endIndex !== -1 && startIndex < endIndex) {
24-
// Both markers exist in correct order
2532
result =
2633
normalizedContent.substring(0, startIndex + start.length) +
2734
'\n' +
2835
code +
2936
'\n' +
3037
normalizedContent.substring(endIndex);
3138
} else if (startIndex !== -1) {
32-
// Only start marker exists - truncate everything after the start marker
3339
result = normalizedContent.substring(0, startIndex + start.length) + '\n' + code + '\n' + end + '\n';
3440
} else {
35-
// No markers or only end marker exists
3641
result = normalizedContent + '\n' + start + '\n' + code + '\n' + end + '\n';
3742
}
3843

39-
// Restore original line ending style
4044
if (lineEnding === '\r\n') {
4145
result = result.replace(/\n/g, '\r\n');
4246
}
4347
return result;
4448
}
4549

4650
export function removeStartupCode(content: string, start: string, end: string): string {
47-
// Detect line ending style from content (default to \n if cannot determine)
48-
const lineEnding = content.includes('\r\n') ? '\r\n' : '\n';
49-
50-
// Normalize line endings to \n for processing
51+
let lineEnding = getLineEndings(content);
5152
const normalizedContent = content.replace(/\r\n/g, '\n');
53+
5254
const startIndex = normalizedContent.indexOf(start);
5355
const endIndex = normalizedContent.indexOf(end);
5456

@@ -67,7 +69,6 @@ export function removeStartupCode(content: string, start: string, end: string):
6769
result = before + after;
6870
}
6971

70-
// Restore original line ending style
7172
if (lineEnding === '\r\n') {
7273
result = result.replace(/\n/g, '\r\n');
7374
}

src/test/features/terminal/shells/common/editUtils.unit.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as assert from 'assert';
2+
import { isWindows } from '../../../../../common/utils/platformUtils';
23
import {
34
hasStartupCode,
45
insertStartupCode,
@@ -80,9 +81,10 @@ suite('Shell Edit Utils', () => {
8081
const start = '# START';
8182
const end = '# END';
8283
const code = 'new code';
84+
const lineEndings = isWindows() ? '\r\n' : '\n';
8385

8486
const result = insertStartupCode(content, start, end, code);
85-
const expected = 'existing content\n# START\nnew code\n# END\n';
87+
const expected = `existing content${lineEndings}# START${lineEndings}new code${lineEndings}# END${lineEndings}`;
8688

8789
assert.strictEqual(result, expected);
8890
});

0 commit comments

Comments
 (0)