Skip to content

Commit e03c0e3

Browse files
Improve the MAC and INT server-side new file stubs (intersystems-community#1218)
1 parent 3fadb63 commit e03c0e3

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

src/extension.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,17 +1048,17 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
10481048
vscode.workspace.onDidCreateFiles((e: vscode.FileCreateEvent) =>
10491049
Promise.all(
10501050
e.files
1051-
.filter((f) => !filesystemSchemas.includes(f.scheme))
1052-
.filter((f) => ["cls", "inc", "int", "mac"].includes(f.path.split(".").pop().toLowerCase()))
1053-
.map(async (f) => {
1051+
.filter((uri) => !filesystemSchemas.includes(uri.scheme))
1052+
.filter((uri) => ["cls", "inc", "int", "mac"].includes(uri.path.split(".").pop().toLowerCase()))
1053+
.map(async (uri) => {
10541054
// Determine the file name
1055-
const workspace = workspaceFolderOfUri(f);
1055+
const workspace = workspaceFolderOfUri(uri);
10561056
if (!workspace) {
10571057
// No workspace folders are open
10581058
return null;
10591059
}
10601060
const workspacePath = uriOfWorkspaceFolder(workspace).fsPath;
1061-
const filePathNoWorkspaceArr = f.fsPath.replace(workspacePath + path.sep, "").split(path.sep);
1061+
const filePathNoWorkspaceArr = uri.fsPath.replace(workspacePath + path.sep, "").split(path.sep);
10621062
const { folder, addCategory } = config("export", workspace);
10631063
const expectedFolder = typeof folder === "string" && folder.length ? folder : null;
10641064
const expectedFolderArr = expectedFolder.split(path.sep);
@@ -1068,15 +1068,15 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
10681068
) {
10691069
filePathNoWorkspaceArr.splice(0, expectedFolderArr.length);
10701070
}
1071-
const expectedCat = addCategory ? getCategory(f.fsPath, addCategory) : null;
1071+
const expectedCat = addCategory ? getCategory(uri.fsPath, addCategory) : null;
10721072
if (expectedCat !== null && filePathNoWorkspaceArr[0] === expectedCat) {
10731073
filePathNoWorkspaceArr.shift();
10741074
}
10751075
const fileName = filePathNoWorkspaceArr.join(".");
10761076
// Generate the new content
1077-
const newContent = generateFileContent(fileName, Buffer.from(await vscode.workspace.fs.readFile(f)));
1077+
const newContent = generateFileContent(uri, fileName, Buffer.from(await vscode.workspace.fs.readFile(uri)));
10781078
// Write the new content to the file
1079-
return vscode.workspace.fs.writeFile(f, new TextEncoder().encode(newContent.content.join("\n")));
1079+
return vscode.workspace.fs.writeFile(uri, new TextEncoder().encode(newContent.content.join("\n")));
10801080
})
10811081
)
10821082
),

src/providers/FileSystemProvider/FileSystemProvider.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ declare function setTimeout(callback: (...args: any[]) => void, ms: number, ...a
2121

2222
export type Entry = File | Directory;
2323

24-
export function generateFileContent(fileName: string, sourceContent: Buffer): { content: string[]; enc: boolean } {
24+
export function generateFileContent(
25+
uri: vscode.Uri,
26+
fileName: string,
27+
sourceContent: Buffer
28+
): { content: string[]; enc: boolean } {
2529
const sourceLines = sourceContent.toString().split("\n");
2630
const fileExt = fileName.split(".").pop().toLowerCase();
2731
const csp = fileName.startsWith("/");
@@ -53,6 +57,19 @@ export function generateFileContent(fileName: string, sourceContent: Buffer): {
5357
sourceLines.shift();
5458
const routineName = fileName.split(".").slice(0, -1).join(".");
5559
const routineType = fileExt != "mac" ? `[Type=${fileExt.toUpperCase()}]` : "";
60+
if (sourceLines.length === 0 && fileExt !== "inc") {
61+
const languageId = fileExt === "mac" ? "objectscript" : "objectscript-int";
62+
63+
// Labels cannot contain dots
64+
const firstLabel = routineName.replaceAll(".", "");
65+
66+
// Be smart about whether to use a Tab or a space between label and comment.
67+
// Doing this will help autodetect to do the right thing.
68+
const lineStart = vscode.workspace.getConfiguration("editor", { languageId, uri }).get("insertSpaces")
69+
? " "
70+
: "\t";
71+
sourceLines.push(`${firstLabel}${lineStart};`);
72+
}
5673
return {
5774
content: [`ROUTINE ${routineName} ${routineType}`, ...sourceLines],
5875
enc: false,
@@ -354,7 +371,7 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
354371
}
355372
// File doesn't exist on the server, and we are allowed to create it.
356373
// Create content (typically a stub, unless the write-phase of a copy operation).
357-
const newContent = generateFileContent(fileName, content);
374+
const newContent = generateFileContent(uri, fileName, content);
358375

359376
// Write it to the server
360377
return api
@@ -388,6 +405,12 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
388405
this._lookupAsFile(uri).then(() => {
389406
this._fireSoon({ type: vscode.FileChangeType.Changed, uri });
390407
});
408+
409+
// Ask to put cursor at start of 3rd line.
410+
// For CLS stub this will be where properties and methods need to be inserted.
411+
// For MAC and INT stubs there is no 3rd line, so cursor goes to the end of the 2nd, which is 1st of actual routine and is ready for comment text.
412+
const editor = await vscode.window.showTextDocument(uri);
413+
editor.selection = new vscode.Selection(2, 0, 2, 0);
391414
});
392415
}
393416
);
@@ -555,7 +578,11 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
555578
const newCsp = newParams.has("csp") && ["", "1"].includes(newParams.get("csp"));
556579
const newFileName = newCsp ? newUri.path : newUri.path.slice(1).replace(/\//g, ".");
557580
// Generate content for the new file
558-
const newContent = generateFileContent(newFileName, Buffer.from(await vscode.workspace.fs.readFile(oldUri)));
581+
const newContent = generateFileContent(
582+
newUri,
583+
newFileName,
584+
Buffer.from(await vscode.workspace.fs.readFile(oldUri))
585+
);
559586
if (newFileStat) {
560587
// We're overwriting an existing file so prompt the user to check it out
561588
await fireOtherStudioAction(OtherStudioAction.AttemptedEdit, newUri);

0 commit comments

Comments
 (0)