Skip to content

Commit 3a9eb37

Browse files
Merge pull request #867 from isc-bsaviano/fix-864
Generate content when a new local class or routine is created
2 parents fa895b7 + 33c6832 commit 3a9eb37

File tree

2 files changed

+73
-43
lines changed

2 files changed

+73
-43
lines changed

src/extension.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const schemas = [
1818
export const filesystemSchemas = [FILESYSTEM_SCHEMA, FILESYSTEM_READONLY_SCHEMA];
1919

2020
import * as url from "url";
21+
import path = require("path");
2122
import {
2223
importAndCompile,
2324
importFolder as importFileOrFolder,
@@ -27,7 +28,7 @@ import {
2728
compileOnly,
2829
} from "./commands/compile";
2930
import { deleteExplorerItems } from "./commands/delete";
30-
import { exportAll, exportCurrentFile, exportExplorerItems } from "./commands/export";
31+
import { exportAll, exportCurrentFile, exportExplorerItems, getCategory } from "./commands/export";
3132
import { serverActions } from "./commands/serverActions";
3233
import { subclass } from "./commands/subclass";
3334
import { superclass } from "./commands/superclass";
@@ -66,7 +67,7 @@ import { ObjectScriptDebugAdapterDescriptorFactory } from "./debug/debugAdapterF
6667
import { ObjectScriptConfigurationProvider } from "./debug/debugConfProvider";
6768
import { ObjectScriptExplorerProvider, registerExplorerOpen } from "./explorer/explorer";
6869
import { WorkspaceNode } from "./explorer/models/workspaceNode";
69-
import { FileSystemProvider } from "./providers/FileSystemProvider/FileSystemProvider";
70+
import { FileSystemProvider, generateFileContent } from "./providers/FileSystemProvider/FileSystemProvider";
7071
import { WorkspaceSymbolProvider } from "./providers/WorkspaceSymbolProvider";
7172
import {
7273
connectionTarget,
@@ -78,6 +79,8 @@ import {
7879
currentFile,
7980
InputBoxManager,
8081
isImportableLocalFile,
82+
workspaceFolderOfUri,
83+
uriOfWorkspaceFolder,
8184
} from "./utils";
8285
import { ObjectScriptDiagnosticProvider } from "./providers/ObjectScriptDiagnosticProvider";
8386
import { DocumentRangeFormattingEditProvider } from "./providers/DocumentRangeFormattingEditProvider";
@@ -938,6 +941,33 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
938941
DocumaticPreviewPanel.create(context.extensionUri)
939942
),
940943
vscode.commands.registerCommand("vscode-objectscript.exportCurrentFile", exportCurrentFile),
944+
vscode.workspace.onDidCreateFiles((e: vscode.FileCreateEvent) =>
945+
Promise.all(
946+
e.files
947+
.filter((f) => !filesystemSchemas.includes(f.scheme))
948+
.filter((f) => ["cls", "inc", "int", "mac"].includes(f.path.split(".").pop().toLowerCase()))
949+
.map(async (f) => {
950+
// Determine the file name
951+
const workspace = workspaceFolderOfUri(f);
952+
const workspacePath = uriOfWorkspaceFolder(workspace).fsPath;
953+
const filePathNoWorkspaceArr = f.fsPath.replace(workspacePath + path.sep, "").split(path.sep);
954+
const { folder, addCategory } = config("export", workspace);
955+
const expectedFolder = typeof folder === "string" && folder.length ? folder : null;
956+
if (expectedFolder !== null && filePathNoWorkspaceArr[0] === expectedFolder) {
957+
filePathNoWorkspaceArr.shift();
958+
}
959+
const expectedCat = addCategory ? getCategory(f.fsPath, addCategory) : null;
960+
if (expectedCat !== null && filePathNoWorkspaceArr[0] === expectedCat) {
961+
filePathNoWorkspaceArr.shift();
962+
}
963+
const fileName = filePathNoWorkspaceArr.join(".");
964+
// Generate the new content
965+
const newContent = generateFileContent(fileName, Buffer.from(await vscode.workspace.fs.readFile(f)));
966+
// Write the new content to the file
967+
return vscode.workspace.fs.writeFile(f, new TextEncoder().encode(newContent.content.join("\n")));
968+
})
969+
)
970+
),
941971

942972
/* Anything we use from the VS Code proposed API */
943973
...proposed

src/providers/FileSystemProvider/FileSystemProvider.ts

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,46 @@ declare function setTimeout(callback: (...args: any[]) => void, ms: number, ...a
1414

1515
export type Entry = File | Directory;
1616

17+
export function generateFileContent(fileName: string, sourceContent: Buffer): { content: string[]; enc: boolean } {
18+
const sourceLines = sourceContent.toString().split("\n");
19+
const fileExt = fileName.split(".").pop().toLowerCase();
20+
if (fileExt === "cls") {
21+
const className = fileName.split(".").slice(0, -1).join(".");
22+
const content: string[] = [];
23+
const preamble: string[] = [];
24+
25+
// If content was provided (e.g. when copying a file), use all lines except for
26+
// the Class x.y one. Replace that with one to match fileName.
27+
while (sourceLines.length > 0) {
28+
const nextLine = sourceLines.shift();
29+
if (nextLine.startsWith("Class ")) {
30+
content.push(...preamble, `Class ${className}`, ...sourceLines);
31+
break;
32+
}
33+
preamble.push(nextLine);
34+
}
35+
if (content.length === 0) {
36+
content.push(`Class ${className}`, "{", "}");
37+
}
38+
return {
39+
content,
40+
enc: false,
41+
};
42+
} else if (["int", "inc", "mac"].includes(fileExt)) {
43+
sourceLines.shift();
44+
const routineName = fileName.split(".").slice(0, -1).join(".");
45+
const routineType = `[ type = ${fileExt}]`;
46+
return {
47+
content: [`ROUTINE ${routineName} ${routineType}`, ...sourceLines],
48+
enc: false,
49+
};
50+
}
51+
return {
52+
content: [sourceContent.toString("base64")],
53+
enc: true,
54+
};
55+
}
56+
1757
export class FileSystemProvider implements vscode.FileSystemProvider {
1858
private superRoot = new Directory("", "");
1959

@@ -151,46 +191,6 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
151191
});
152192
}
153193

154-
private generateFileContent(fileName: string, sourceContent: Buffer): { content: string[]; enc: boolean } {
155-
const sourceLines = sourceContent.toString().split("\n");
156-
const fileExt = fileName.split(".").pop().toLowerCase();
157-
if (fileExt === "cls") {
158-
const className = fileName.split(".").slice(0, -1).join(".");
159-
const content: string[] = [];
160-
const preamble: string[] = [];
161-
162-
// If content was provided (e.g. when copying a file), use all lines except for
163-
// the Class x.y one. Replace that with one to match fileName.
164-
while (sourceLines.length > 0) {
165-
const nextLine = sourceLines.shift();
166-
if (nextLine.startsWith("Class ")) {
167-
content.push(...preamble, `Class ${className}`, ...sourceLines);
168-
break;
169-
}
170-
preamble.push(nextLine);
171-
}
172-
if (content.length === 0) {
173-
content.push(`Class ${className}`, "{", "}");
174-
}
175-
return {
176-
content,
177-
enc: false,
178-
};
179-
} else if (["int", "inc", "mac"].includes(fileExt)) {
180-
sourceLines.shift();
181-
const routineName = fileName.split(".").slice(0, -1).join(".");
182-
const routineType = `[ type = ${fileExt}]`;
183-
return {
184-
content: [`ROUTINE ${routineName} ${routineType}`, ...sourceLines],
185-
enc: false,
186-
};
187-
}
188-
return {
189-
content: [sourceContent.toString("base64")],
190-
enc: true,
191-
};
192-
}
193-
194194
public writeFile(
195195
uri: vscode.Uri,
196196
content: Buffer,
@@ -250,7 +250,7 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
250250
}
251251
// File doesn't exist on the server, and we are allowed to create it.
252252
// Create content (typically a stub).
253-
const newContent = this.generateFileContent(fileName, content);
253+
const newContent = generateFileContent(fileName, content);
254254

255255
// Write it to the server
256256
return api

0 commit comments

Comments
 (0)