|
| 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