Skip to content

Commit 701306e

Browse files
authored
Don't overwrite file name if the new name couldn't be determined (intersystems-community#1253)
1 parent b759a83 commit 701306e

File tree

1 file changed

+50
-33
lines changed

1 file changed

+50
-33
lines changed

src/providers/FileSystemProvider/FileSystemProvider.ts

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,49 +32,66 @@ export function generateFileContent(
3232
const csp = fileName.startsWith("/");
3333
if (fileExt === "cls" && !csp) {
3434
const className = fileName.split(".").slice(0, -1).join(".");
35-
const content: string[] = [];
35+
let content: string[] = [];
3636
const preamble: string[] = [];
3737

38-
// If content was provided (e.g. when copying a file), use all lines except for
39-
// the Class x.y one. Replace that with one to match fileName.
40-
while (sourceLines.length > 0) {
41-
const nextLine = sourceLines.shift();
42-
if (nextLine.startsWith("Class ")) {
43-
const classLine = nextLine.split(" ");
44-
classLine[1] = className;
45-
content.push(...preamble, classLine.join(" "), ...sourceLines);
46-
break;
38+
if (sourceLines.length) {
39+
if (uri.scheme == "file" && (fileName.includes(path.sep) || fileName.includes(" "))) {
40+
// We couldn't resolve a class name from the file path,
41+
// so keep the source text unchanged.
42+
content = sourceLines;
43+
} else {
44+
// Use all lines except for the Class x.y one.
45+
// Replace that with one to match fileName.
46+
while (sourceLines.length > 0) {
47+
const nextLine = sourceLines.shift();
48+
if (nextLine.startsWith("Class ")) {
49+
const classLine = nextLine.split(" ");
50+
classLine[1] = className;
51+
content.push(...preamble, classLine.join(" "), ...sourceLines);
52+
break;
53+
}
54+
preamble.push(nextLine);
55+
}
4756
}
48-
preamble.push(nextLine);
49-
}
50-
if (content.length === 0) {
51-
content.push(`Class ${className} Extends %RegisteredObject`, "{", "}");
57+
} else {
58+
content = [`Class ${className} Extends %RegisteredObject`, "{", "}"];
5259
}
60+
5361
return {
5462
content,
5563
enc: false,
5664
};
5765
} else if (["int", "inc", "mac"].includes(fileExt) && !csp) {
58-
sourceLines.shift();
59-
const routineName = fileName.split(".").slice(0, -1).join(".");
60-
const routineType = fileExt != "mac" ? `[Type=${fileExt.toUpperCase()}]` : "";
61-
if (sourceLines.length === 0 && fileExt !== "inc") {
62-
const languageId = fileExt === "mac" ? "objectscript" : "objectscript-int";
63-
64-
// Labels cannot contain dots
65-
const firstLabel = routineName.replaceAll(".", "");
66-
67-
// Be smart about whether to use a Tab or a space between label and comment.
68-
// Doing this will help autodetect to do the right thing.
69-
const lineStart = vscode.workspace.getConfiguration("editor", { languageId, uri }).get("insertSpaces")
70-
? " "
71-
: "\t";
72-
sourceLines.push(`${firstLabel}${lineStart};`);
66+
if (sourceLines.length && uri.scheme == "file" && (fileName.includes(path.sep) || fileName.includes(" "))) {
67+
// We couldn't resolve a routine name from the file path,
68+
// so keep the source text unchanged.
69+
return {
70+
content: sourceLines,
71+
enc: false,
72+
};
73+
} else {
74+
sourceLines.shift();
75+
const routineName = fileName.split(".").slice(0, -1).join(".");
76+
const routineType = fileExt != "mac" ? `[Type=${fileExt.toUpperCase()}]` : "";
77+
if (sourceLines.length === 0 && fileExt !== "inc") {
78+
const languageId = fileExt === "mac" ? "objectscript" : "objectscript-int";
79+
80+
// Labels cannot contain dots
81+
const firstLabel = routineName.replaceAll(".", "");
82+
83+
// Be smart about whether to use a Tab or a space between label and comment.
84+
// Doing this will help autodetect to do the right thing.
85+
const lineStart = vscode.workspace.getConfiguration("editor", { languageId, uri }).get("insertSpaces")
86+
? " "
87+
: "\t";
88+
sourceLines.push(`${firstLabel}${lineStart};`);
89+
}
90+
return {
91+
content: [`ROUTINE ${routineName} ${routineType}`, ...sourceLines],
92+
enc: false,
93+
};
7394
}
74-
return {
75-
content: [`ROUTINE ${routineName} ${routineType}`, ...sourceLines],
76-
enc: false,
77-
};
7895
}
7996
return {
8097
content: [sourceContent.toString("base64")],

0 commit comments

Comments
 (0)