Skip to content

Commit ff972c0

Browse files
authored
Add FileDecoration for Generated files (#1035)
1 parent 4f6f7a2 commit ff972c0

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

docs/SettingsReference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ The extensions in the InterSystems ObjectScript Extension Pack provide many sett
7676
| `"objectscript.serverSideEditing"` | Allow editing code directly on the server after opening it from ObjectScript Explorer. | `boolean` | `false` | |
7777
| `"objectscript.serverSourceControl .disableOtherActionTriggers"` | Prevent server-side source control 'other action' triggers from firing. | `boolean` | `false` | |
7878
| `"objectscript.showExplorer"` | Show the ObjectScript Explorer view. | `boolean` | `true` | |
79+
| `"objectscript.showGeneratedFileDecorations"` | Controls whether a badge is shown in the file explorer and open editors view for generated files. | `boolean` | `true` | |
7980
| `"objectscript.studioActionDebugOutput"` | Log in JSON format the action that VS Code should perform as requested by the server. | `boolean` | `false` | Actions will be logged to the `ObjectScript` Output channel. |
8081
| `"objectscript.suppressCompileErrorMessages"` | Suppress popup messages about errors during compile, but still focus on Output view. | `boolean` | `false` | |
8182
| `"objectscript.suppressCompileMessages"` | Suppress popup messages about successful compile. | `boolean` | `true` | |

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,11 @@
14161416
"description": "Automatically save a client-side InterSystems file on the server when saved in the editor.",
14171417
"type": "boolean",
14181418
"default": true
1419+
},
1420+
"objectscript.showGeneratedFileDecorations": {
1421+
"description": "Controls whether a badge is shown in the file explorer and open editors view for generated files.",
1422+
"type": "boolean",
1423+
"default": true
14191424
}
14201425
}
14211426
},

src/extension.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import {
114114
import { NodeBase } from "./explorer/models/nodeBase";
115115
import { loadStudioColors, loadStudioSnippets } from "./commands/studioMigration";
116116
import { newFile, NewFileType } from "./commands/newFile";
117+
import { FileDecorationProvider } from "./providers/FileDecorationProvider";
117118

118119
const packageJson = vscode.extensions.getExtension(extensionId).packageJSON;
119120
const extensionVersion = packageJson.version;
@@ -737,6 +738,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
737738

738739
openedClasses = workspaceState.get("openedClasses") ?? [];
739740

741+
// Create this here so we can fire its event
742+
const fileDecorationProvider = new FileDecorationProvider();
743+
740744
context.subscriptions.push(
741745
reporter,
742746
panel,
@@ -1182,6 +1186,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
11821186
newFile(NewFileType.BusinessService)
11831187
),
11841188
vscode.commands.registerCommand("vscode-objectscript.newFile.dtl", () => newFile(NewFileType.DTL)),
1189+
vscode.window.registerFileDecorationProvider(fileDecorationProvider),
1190+
vscode.workspace.onDidOpenTextDocument((doc) => !doc.isUntitled && fileDecorationProvider.emitter.fire(doc.uri)),
11851191
vscode.commands.registerCommand("vscode-objectscript.importLocalFilesServerSide", (wsFolderUri) => {
11861192
if (
11871193
wsFolderUri instanceof vscode.Uri &&
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import * as vscode from "vscode";
2+
import { currentFile } from "../utils";
3+
4+
export class FileDecorationProvider implements vscode.FileDecorationProvider {
5+
private _genBadge = String.fromCharCode(9965); // Gear
6+
private _genTooltip = "Generated";
7+
onDidChangeFileDecorations: vscode.Event<vscode.Uri>;
8+
9+
emitter = new vscode.EventEmitter<vscode.Uri>();
10+
11+
public constructor() {
12+
this.onDidChangeFileDecorations = this.emitter.event;
13+
}
14+
15+
provideFileDecoration(uri: vscode.Uri): vscode.FileDecoration | undefined {
16+
let result: vscode.FileDecoration | undefined = undefined;
17+
18+
// Only provide decorations for files that are open and not untitled
19+
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() == uri.toString());
20+
if (
21+
doc != undefined &&
22+
!doc.isUntitled &&
23+
["objectscript", "objectscript-class", "objectscript-int", "objectscript-macros"].includes(doc.languageId) &&
24+
vscode.workspace
25+
.getConfiguration("objectscript", vscode.workspace.getWorkspaceFolder(uri))
26+
.get<boolean>("showGeneratedFileDecorations")
27+
) {
28+
// Use the file's contents to check if it's generated
29+
if (doc.languageId == "objectscript-class") {
30+
for (let line = 0; line < doc.lineCount; line++) {
31+
const lineText = doc.lineAt(line).text;
32+
if (lineText.startsWith("Class ")) {
33+
const clsMatch = lineText.match(/[[, ]{1}GeneratedBy *= *([^,\] ]+)/i);
34+
if (clsMatch) {
35+
// This class is generated
36+
result = new vscode.FileDecoration(this._genBadge, `${this._genTooltip} by ${clsMatch[1]}`);
37+
}
38+
break;
39+
}
40+
}
41+
} else {
42+
const firstLine = doc.lineAt(0).text;
43+
if (firstLine.startsWith("ROUTINE ") && firstLine.includes("Generated]")) {
44+
// This routine is generated
45+
let tooltip = this._genTooltip;
46+
const file = currentFile(doc)?.name;
47+
if (file) {
48+
const macMatch = file.match(/^(.*)\.G[0-9]+\.mac$/);
49+
const intMatch = file.match(/^(.*)\.[0-9]+\.int$/);
50+
if (macMatch) {
51+
tooltip += ` by ${macMatch[1]}.cls`;
52+
} else if (intMatch) {
53+
tooltip += ` by ${intMatch[1]}.cls`;
54+
} else if (doc.languageId == "objectscript-int") {
55+
tooltip += ` by ${file.slice(0, -3)}mac`;
56+
}
57+
}
58+
result = new vscode.FileDecoration(this._genBadge, tooltip);
59+
}
60+
}
61+
}
62+
63+
return result;
64+
}
65+
}

0 commit comments

Comments
 (0)