Skip to content

Commit 039d8d2

Browse files
authored
Attempt to use existing line endings when replacing a file's contents post-compile (intersystems-community#1567)
1 parent a4e89ee commit 039d8d2

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/commands/compile.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
cspAppsForUri,
2020
CurrentBinaryFile,
2121
currentFile,
22-
CurrentFile,
2322
currentFileFromContent,
2423
CurrentTextFile,
2524
exportedUris,
@@ -231,7 +230,16 @@ export async function loadChanges(files: (CurrentTextFile | CurrentBinaryFile)[]
231230
const content = await api.getDoc(file.name, file.uri).then((data) => data.result.content);
232231
exportedUris.add(file.uri.toString()); // Set optimistically
233232
await vscode.workspace.fs
234-
.writeFile(file.uri, Buffer.isBuffer(content) ? content : new TextEncoder().encode(content.join("\n")))
233+
.writeFile(
234+
file.uri,
235+
Buffer.isBuffer(content)
236+
? content
237+
: new TextEncoder().encode(
238+
content.join(
239+
((<CurrentTextFile>file)?.eol ?? vscode.EndOfLine.LF) == vscode.EndOfLine.CRLF ? "\r\n" : "\n"
240+
)
241+
)
242+
)
235243
.then(undefined, (e) => {
236244
// Save failed, so remove this URI from the set
237245
exportedUris.delete(file.uri.toString());
@@ -251,12 +259,15 @@ export async function loadChanges(files: (CurrentTextFile | CurrentBinaryFile)[]
251259
);
252260
}
253261

254-
export async function compile(docs: CurrentFile[], flags?: string): Promise<any> {
262+
export async function compile(docs: (CurrentTextFile | CurrentBinaryFile)[], flags?: string): Promise<any> {
255263
const wsFolder = vscode.workspace.getWorkspaceFolder(docs[0].uri);
256264
const conf = vscode.workspace.getConfiguration("objectscript", wsFolder || docs[0].uri);
257265
flags = flags || conf.get("compileFlags");
258266
const api = new AtelierAPI(docs[0].uri);
259267
const docNames = docs.map((d) => d.name);
268+
// Determine the line ending to use for other documents affected
269+
// by compilation so we don't need to read their contents
270+
const eol = (<CurrentTextFile>docs.find((d) => (<CurrentTextFile>d)?.eol))?.eol ?? vscode.EndOfLine.LF;
260271
return vscode.window
261272
.withProgress(
262273
{
@@ -284,9 +295,11 @@ export async function compile(docs: CurrentFile[], flags?: string): Promise<any>
284295
name: f.name,
285296
uri: u,
286297
uniqueId: `${wsFolder.name}:${f.name}`,
287-
// These two keys aren't used by loadChanges()
298+
eol,
299+
// These three keys aren't used by loadChanges()
288300
workspaceFolder: wsFolder.name,
289301
fileName: u.fsPath,
302+
content: "",
290303
});
291304
});
292305
});
@@ -416,7 +429,7 @@ export async function namespaceCompile(askFlags = false): Promise<any> {
416429
}
417430

418431
async function importFiles(files: vscode.Uri[], noCompile = false) {
419-
const toCompile: CurrentFile[] = [];
432+
const toCompile: (CurrentTextFile | CurrentBinaryFile)[] = [];
420433
const rateLimiter = new RateLimiter(50);
421434
await Promise.allSettled<void>(
422435
files.map((uri) =>

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
12971297
wsEdit.replace(
12981298
uri,
12991299
new vscode.Range(0, 0, newContent.content.length + 1, 0),
1300-
newContent.content.join("\n"),
1300+
newContent.content.join(newContent.eol == vscode.EndOfLine.CRLF ? "\r\n" : "\n"),
13011301
{
13021302
label: "ObjectScript autoAdjustName",
13031303
needsConfirmation: false,

src/providers/FileSystemProvider/FileSystemProvider.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ export function generateFileContent(
6868
uri: vscode.Uri,
6969
fileName: string,
7070
sourceContent: Uint8Array
71-
): { content: string[]; enc: boolean } {
71+
): { content: string[]; enc: boolean; eol: vscode.EndOfLine } {
7272
const sourceLines = sourceContent.length ? new TextDecoder().decode(sourceContent).split("\n") : [];
73+
const eol = sourceLines.length && sourceLines[0].slice(-1) == "\r" ? vscode.EndOfLine.CRLF : vscode.EndOfLine.LF;
7374
const fileExt = fileName.split(".").pop().toLowerCase();
7475
const csp = fileName.startsWith("/");
7576
if (fileExt === "cls" && !csp) {
@@ -108,6 +109,7 @@ export function generateFileContent(
108109
return {
109110
content,
110111
enc: false,
112+
eol,
111113
};
112114
} else if (["int", "inc", "mac"].includes(fileExt) && !csp) {
113115
if (sourceLines.length && notIsfs(uri) && (fileName.includes(path.sep) || fileName.includes(" "))) {
@@ -116,6 +118,7 @@ export function generateFileContent(
116118
return {
117119
content: sourceLines,
118120
enc: false,
121+
eol,
119122
};
120123
} else {
121124
sourceLines.shift();
@@ -137,12 +140,14 @@ export function generateFileContent(
137140
return {
138141
content: [`ROUTINE ${routineName} ${routineType}`, ...sourceLines],
139142
enc: false,
143+
eol,
140144
};
141145
}
142146
}
143147
return {
144148
content: base64EncodeContent(Buffer.from(sourceContent)),
145149
enc: true,
150+
eol,
146151
};
147152
}
148153

0 commit comments

Comments
 (0)