Skip to content

Attempt to use existing line endings when replacing a file's contents post-compile #1567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/commands/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
cspAppsForUri,
CurrentBinaryFile,
currentFile,
CurrentFile,
currentFileFromContent,
CurrentTextFile,
exportedUris,
Expand Down Expand Up @@ -231,7 +230,16 @@ export async function loadChanges(files: (CurrentTextFile | CurrentBinaryFile)[]
const content = await api.getDoc(file.name, file.uri).then((data) => data.result.content);
exportedUris.add(file.uri.toString()); // Set optimistically
await vscode.workspace.fs
.writeFile(file.uri, Buffer.isBuffer(content) ? content : new TextEncoder().encode(content.join("\n")))
.writeFile(
file.uri,
Buffer.isBuffer(content)
? content
: new TextEncoder().encode(
content.join(
((<CurrentTextFile>file)?.eol ?? vscode.EndOfLine.LF) == vscode.EndOfLine.CRLF ? "\r\n" : "\n"
)
)
)
.then(undefined, (e) => {
// Save failed, so remove this URI from the set
exportedUris.delete(file.uri.toString());
Expand All @@ -251,12 +259,15 @@ export async function loadChanges(files: (CurrentTextFile | CurrentBinaryFile)[]
);
}

export async function compile(docs: CurrentFile[], flags?: string): Promise<any> {
export async function compile(docs: (CurrentTextFile | CurrentBinaryFile)[], flags?: string): Promise<any> {
const wsFolder = vscode.workspace.getWorkspaceFolder(docs[0].uri);
const conf = vscode.workspace.getConfiguration("objectscript", wsFolder || docs[0].uri);
flags = flags || conf.get("compileFlags");
const api = new AtelierAPI(docs[0].uri);
const docNames = docs.map((d) => d.name);
// Determine the line ending to use for other documents affected
// by compilation so we don't need to read their contents
const eol = (<CurrentTextFile>docs.find((d) => (<CurrentTextFile>d)?.eol))?.eol ?? vscode.EndOfLine.LF;
return vscode.window
.withProgress(
{
Expand Down Expand Up @@ -284,9 +295,11 @@ export async function compile(docs: CurrentFile[], flags?: string): Promise<any>
name: f.name,
uri: u,
uniqueId: `${wsFolder.name}:${f.name}`,
// These two keys aren't used by loadChanges()
eol,
// These three keys aren't used by loadChanges()
workspaceFolder: wsFolder.name,
fileName: u.fsPath,
content: "",
});
});
});
Expand Down Expand Up @@ -416,7 +429,7 @@ export async function namespaceCompile(askFlags = false): Promise<any> {
}

async function importFiles(files: vscode.Uri[], noCompile = false) {
const toCompile: CurrentFile[] = [];
const toCompile: (CurrentTextFile | CurrentBinaryFile)[] = [];
const rateLimiter = new RateLimiter(50);
await Promise.allSettled<void>(
files.map((uri) =>
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
wsEdit.replace(
uri,
new vscode.Range(0, 0, newContent.content.length + 1, 0),
newContent.content.join("\n"),
newContent.content.join(newContent.eol == vscode.EndOfLine.CRLF ? "\r\n" : "\n"),
{
label: "ObjectScript autoAdjustName",
needsConfirmation: false,
Expand Down
7 changes: 6 additions & 1 deletion src/providers/FileSystemProvider/FileSystemProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ export function generateFileContent(
uri: vscode.Uri,
fileName: string,
sourceContent: Uint8Array
): { content: string[]; enc: boolean } {
): { content: string[]; enc: boolean; eol: vscode.EndOfLine } {
const sourceLines = sourceContent.length ? new TextDecoder().decode(sourceContent).split("\n") : [];
const eol = sourceLines.length && sourceLines[0].slice(-1) == "\r" ? vscode.EndOfLine.CRLF : vscode.EndOfLine.LF;
const fileExt = fileName.split(".").pop().toLowerCase();
const csp = fileName.startsWith("/");
if (fileExt === "cls" && !csp) {
Expand Down Expand Up @@ -108,6 +109,7 @@ export function generateFileContent(
return {
content,
enc: false,
eol,
};
} else if (["int", "inc", "mac"].includes(fileExt) && !csp) {
if (sourceLines.length && notIsfs(uri) && (fileName.includes(path.sep) || fileName.includes(" "))) {
Expand All @@ -116,6 +118,7 @@ export function generateFileContent(
return {
content: sourceLines,
enc: false,
eol,
};
} else {
sourceLines.shift();
Expand All @@ -137,12 +140,14 @@ export function generateFileContent(
return {
content: [`ROUTINE ${routineName} ${routineType}`, ...sourceLines],
enc: false,
eol,
};
}
}
return {
content: base64EncodeContent(Buffer.from(sourceContent)),
enc: true,
eol,
};
}

Expand Down