Skip to content

Commit

Permalink
Fix the issue of automatic closure when reopening the file and proper…
Browse files Browse the repository at this point in the history
…ly implement the status bar

Signed-off-by: Shubh <[email protected]>
  • Loading branch information
shubhisroking committed Sep 1, 2023
1 parent d05fc48 commit 8f4f182
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ import * as vscode from "vscode";

class StreamerMode {
private isEnabled = false;
private statusBarItem: vscode.StatusBarItem;

constructor() {
this.statusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left,
100
);
this.statusBarItem.command = "vscode-streamer-mode.toggle";
this.updateStatusBarItem();
this.statusBarItem.show();
vscode.workspace
.getConfiguration()
.update(
Expand All @@ -22,6 +30,7 @@ class StreamerMode {
this.isEnabled,
vscode.ConfigurationTarget.Global
);
this.updateStatusBarItem();
vscode.window.showInformationMessage(
`Streamer mode is now ${this.isEnabled ? "enabled" : "disabled"}.`
);
Expand All @@ -30,29 +39,30 @@ class StreamerMode {
public getEnabled() {
return this.isEnabled;
}

private updateStatusBarItem() {
this.statusBarItem.text = `Streamer Mode: ${
this.isEnabled ? "Enabled" : "Disabled"
}`;
}
}

class SensitiveFile {
private readonly fileExtensions: Set<string>;
private readonly regex: RegExp;

constructor(fileExtensions: string[]) {
this.fileExtensions = new Set(
fileExtensions.map((ext) => ext.toLowerCase())
);
this.fileExtensions = new Set(fileExtensions);
this.regex = new RegExp(`\\.(${fileExtensions.join("|")})$`, "i");
}

public isSensitive(document: vscode.TextDocument): boolean {
const extension = this.getFileExtension(document.fileName);
return this.fileExtensions.has(extension);
public isSensitiveFile(document: vscode.TextDocument): boolean {
return this.fileExtensions.has(this.getFileExtension(document.fileName));
}

private getFileExtension(fileName: string): string {
const lastDotIndex = fileName.lastIndexOf(".");
return lastDotIndex === -1
? ""
: fileName.slice(lastDotIndex + 1).toLowerCase();
return lastDotIndex === -1 ? "" : fileName.slice(lastDotIndex + 1);
}
}

Expand All @@ -75,27 +85,14 @@ export function activate(context: vscode.ExtensionContext) {
"pfx",
]);

// Create a status bar item
const statusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left,
100
);
statusBarItem.text = "Streamer Mode: Disabled";
statusBarItem.command = "vscode-streamer-mode.toggle";
statusBarItem.show();

context.subscriptions.push(
vscode.commands.registerCommand("vscode-streamer-mode.toggle", () => {
streamerMode.toggle();
// Update the status bar item text
statusBarItem.text = `Streamer Mode: ${
streamerMode.getEnabled() ? "Enabled" : "Disabled"
}`;
})
);

vscode.workspace.onDidOpenTextDocument((document) => {
if (sensitiveFile.isSensitive(document) && streamerMode.getEnabled()) {
if (sensitiveFile.isSensitiveFile(document) && streamerMode.getEnabled()) {
if (!shouldKeepEditorOpen) {
vscode.window
.showWarningMessage(
Expand Down

0 comments on commit 8f4f182

Please sign in to comment.