Skip to content

Commit 8a62e44

Browse files
authored
Insert file stub snippet when creating a new class or routine using client-side editing (intersystems-community#1681)
1 parent 530376c commit 8a62e44

File tree

2 files changed

+46
-73
lines changed

2 files changed

+46
-73
lines changed

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,11 +1503,6 @@
15031503
"type": "boolean",
15041504
"default": false
15051505
},
1506-
"objectscript.autoAdjustName": {
1507-
"markdownDescription": "Automatically modify the class name or ROUTINE header of a file in a client-side workspace folder to match the file's path when copying or moving a file. Uses the `#objectscript.export#` settings to determine the new name.",
1508-
"type": "boolean",
1509-
"default": false
1510-
},
15111506
"objectscript.compileOnSave": {
15121507
"description": "Automatically compile an InterSystems file when saved in the editor.",
15131508
"type": "boolean",

src/extension.ts

Lines changed: 46 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export const cspLangId = "objectscript-csp";
2828
export const outputLangId = "vscode-objectscript-output";
2929

3030
import * as url from "url";
31-
import path = require("path");
3231
import {
3332
importAndCompile,
3433
importFolder as importFileOrFolder,
@@ -41,13 +40,7 @@ import {
4140
importXMLFiles,
4241
} from "./commands/compile";
4342
import { deleteExplorerItems } from "./commands/delete";
44-
import {
45-
exportAll,
46-
exportCurrentFile,
47-
exportDocumentsToXMLFile,
48-
exportExplorerItems,
49-
getCategory,
50-
} from "./commands/export";
43+
import { exportAll, exportCurrentFile, exportDocumentsToXMLFile, exportExplorerItems } from "./commands/export";
5144
import { serverActions } from "./commands/serverActions";
5245
import { subclass } from "./commands/subclass";
5346
import { superclass } from "./commands/superclass";
@@ -86,7 +79,7 @@ import { ObjectScriptDebugAdapterDescriptorFactory } from "./debug/debugAdapterF
8679
import { ObjectScriptConfigurationProvider } from "./debug/debugConfProvider";
8780
import { ProjectsExplorerProvider } from "./explorer/projectsExplorer";
8881
import { ObjectScriptExplorerProvider, registerExplorerOpen } from "./explorer/explorer";
89-
import { FileSystemProvider, generateFileContent } from "./providers/FileSystemProvider/FileSystemProvider";
82+
import { FileSystemProvider } from "./providers/FileSystemProvider/FileSystemProvider";
9083
import { WorkspaceSymbolProvider } from "./providers/WorkspaceSymbolProvider";
9184
import {
9285
connectionTarget,
@@ -96,8 +89,6 @@ import {
9689
terminalWithDocker,
9790
notNull,
9891
currentFile,
99-
workspaceFolderOfUri,
100-
uriOfWorkspaceFolder,
10192
isUnauthenticated,
10293
notIsfs,
10394
handleError,
@@ -1439,64 +1430,52 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
14391430
sendCommandTelemetryEvent("exportCurrentFile");
14401431
exportCurrentFile();
14411432
}),
1442-
vscode.workspace.onDidCreateFiles((e: vscode.FileCreateEvent) => {
1443-
return Promise.all(
1444-
e.files
1445-
// Only attempt to adjust the names of classes and routines that are
1446-
// not server-side files and were not created due to an export
1447-
.filter((f) => notIsfs(f) && isClassOrRtn(f) && !exportedUris.has(f.toString()))
1448-
.map(async (uri) => {
1449-
// Determine the file name
1450-
const workspace = workspaceFolderOfUri(uri);
1451-
if (!workspace) {
1452-
// No workspace folders are open
1453-
return;
1454-
}
1455-
// Need to wait in case file was created using "Save As..."
1456-
// because in that case the file gets created without
1457-
// content, and then the content is written in after that
1458-
await new Promise((resolve) => setTimeout(resolve, 100));
1459-
const sourceContent = await vscode.workspace.fs.readFile(uri);
1460-
if (
1461-
sourceContent.length &&
1462-
!vscode.workspace.getConfiguration("objectscript").get<boolean>("autoAdjustName")
1463-
) {
1464-
// Don't modify a file with content unless the user opts in
1465-
return;
1466-
}
1467-
const workspacePath = uriOfWorkspaceFolder(workspace).fsPath;
1468-
const filePathNoWorkspaceArr = uri.fsPath.replace(workspacePath + path.sep, "").split(path.sep);
1469-
const { folder, addCategory } = config("export", workspace);
1470-
const expectedFolder = typeof folder === "string" && folder.length ? folder : null;
1471-
const expectedFolderArr = expectedFolder ? expectedFolder.split(path.sep) : [];
1472-
if (
1473-
expectedFolder !== null &&
1474-
filePathNoWorkspaceArr.slice(0, expectedFolderArr.length).join(path.sep) === expectedFolder
1475-
) {
1476-
filePathNoWorkspaceArr.splice(0, expectedFolderArr.length);
1477-
}
1478-
const expectedCat = addCategory ? getCategory(uri.fsPath, addCategory) : null;
1479-
if (expectedCat !== null && filePathNoWorkspaceArr[0] === expectedCat) {
1480-
filePathNoWorkspaceArr.shift();
1481-
}
1482-
const fileName = filePathNoWorkspaceArr.join(".");
1483-
// Generate the new content
1484-
const newContent = generateFileContent(uri, fileName, sourceContent);
1485-
// Write the new content to the file
1486-
const wsEdit = new vscode.WorkspaceEdit();
1487-
wsEdit.replace(
1488-
uri,
1489-
new vscode.Range(0, 0, newContent.content.length + 1, 0),
1490-
newContent.content.join(newContent.eol == vscode.EndOfLine.CRLF ? "\r\n" : "\n"),
1433+
vscode.workspace.onDidCreateFiles((e: vscode.FileCreateEvent) =>
1434+
e.files
1435+
// Attempt to fill in stub content for classes and routines that
1436+
// are not server-side files and were not created due to an export
1437+
.filter((f) => notIsfs(f) && isClassOrRtn(f) && !exportedUris.has(f.toString()))
1438+
.forEach(async (uri) => {
1439+
// Need to wait in case file was created using "Save As..."
1440+
// because in that case the file gets created without
1441+
// content, and then the content is written in after that
1442+
await new Promise((resolve) => setTimeout(resolve, 100));
1443+
const sourceContent = await vscode.workspace.fs.readFile(uri);
1444+
if (sourceContent.length) {
1445+
// Don't modify a file with content
1446+
return;
1447+
}
1448+
// Generate the new content
1449+
const fileExt = uri.path.split(".").pop().toLowerCase();
1450+
const newContent =
1451+
fileExt == "cls"
1452+
? ["Class $1 Extends %RegisteredObject", "{", "// $0", "}", ""]
1453+
: [
1454+
`ROUTINE $1${fileExt == "int" ? " [Type=INT]" : fileExt == "inc" ? " [Type=INC]" : ""}`,
1455+
`${fileExt == "int" ? ";" : "#;"} $0`,
1456+
"",
1457+
];
1458+
// Make the edit
1459+
const wsEdit = new vscode.WorkspaceEdit();
1460+
wsEdit.set(uri, [
1461+
[
1462+
vscode.SnippetTextEdit.insert(
1463+
new vscode.Position(0, 0),
1464+
// Only use CRLF as the line termination string if we are certain the file is on a Windows file system
1465+
new vscode.SnippetString(
1466+
newContent.join(uri.scheme == "file" && process.platform == "win32" ? "\r\n" : "\n")
1467+
)
1468+
),
14911469
{
1492-
label: "ObjectScript autoAdjustName",
1470+
label: `New ObjectScript ${fileExt == "cls" ? "class" : fileExt == "inc" ? "include file" : "routine"}`,
1471+
iconPath: iscIcon,
14931472
needsConfirmation: false,
1494-
}
1495-
);
1496-
await vscode.workspace.applyEdit(wsEdit);
1497-
})
1498-
);
1499-
}),
1473+
},
1474+
],
1475+
]);
1476+
vscode.workspace.applyEdit(wsEdit);
1477+
})
1478+
),
15001479
vscode.workspace.onDidCloseTextDocument((doc: vscode.TextDocument) => {
15011480
const uri: string = doc.uri.toString();
15021481
const idx: number = openedClasses.indexOf(uri);
@@ -1953,7 +1932,6 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
19531932
languageServerVersion: languageServerExt?.packageJSON.version,
19541933
serverManagerVersion: smExt?.packageJSON.version,
19551934
"config.explorer.alwaysShowServerCopy": String(conf.get("explorer.alwaysShowServerCopy")),
1956-
"config.autoAdjustName": String(conf.get("autoAdjustName")),
19571935
"config.autoShowTerminal": String(conf.get("autoShowTerminal")),
19581936
"config.suppressCompileMessages": String(conf.get("suppressCompileMessages")),
19591937
"config.suppressCompileErrorMessages": String(conf.get("suppressCompileErrorMessages")),

0 commit comments

Comments
 (0)