Skip to content

Commit

Permalink
chore: show vscode error if user tried to open non existing file
Browse files Browse the repository at this point in the history
  • Loading branch information
nagy-nabil committed Mar 17, 2024
1 parent ce7594d commit 95edce9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/commands/ui.show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ function assignListeners(state: WorkspaceState, fsEdirorUri: vscode.Uri): void {
async (tdoc) => {
if (tdoc.document.fileName.includes(storageFileName)) {
await tdoc.document.save();
await state.setstore(
tdoc.document
.getText()
.split("\n")
.filter((val) => val !== "")
);
const newFilePaths = tdoc.document
.getText()
.split("\n")
.map((v) => v.trim())
.filter((val) => val !== "");
await state.setStore(newFilePaths);
}
}
);
Expand Down
36 changes: 29 additions & 7 deletions src/workspaceState.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import * as vscode from "vscode";
import { storeKey } from "./config";
import { pathToFileURL } from "url";
import * as path from "path";

/**
* class that would hold the state for the current workspace, like opened editors[marked] update the state, manage context.workspaceState, fs store
*/
export class WorkspaceState {
// todo is it better to use only map? instead of array and set
private _vsStore!: string[];
// will be used to fast check marked files so need to be always synced with _editors
/**
* will be used to fast check marked files so need to be always synced with _editors
*/
private _editorsSet!: Set<string>;
private _context: vscode.ExtensionContext;
private _fsStorgePath: vscode.Uri; // folder path not file
/**
* folder path not file
*/
private _fsStorgePath: vscode.Uri;

constructor(context: vscode.ExtensionContext, fsStoragePath: vscode.Uri) {
this._context = context;
Expand All @@ -19,7 +26,7 @@ export class WorkspaceState {
this.vsStore = this._context.workspaceState.get(storeKey) || [];
}

// todo take textdocument from input
// TODO: take textdocument from input
async addMark(uri: vscode.Uri) {
if (this._editorsSet.has(uri.fsPath)) {
console.log("didnt saved new Mark because i already have it");
Expand Down Expand Up @@ -64,7 +71,7 @@ export class WorkspaceState {
* maybe activiting GC way too much but whatever
* @param newContent
*/
async setstore(newContent: string[]) {
async setStore(newContent: string[]) {
this.vsStore = newContent;
await this._updateWorkspaceStore();
}
Expand All @@ -75,11 +82,26 @@ export class WorkspaceState {
* @returns
*/
async changeEditor(id: number) {
if (id >= this._vsStore.length) {
if (id < 0 || id >= this.vsStore.length) {
console.log("no access out of scope");
return;
}
const doc = await vscode.workspace.openTextDocument(this._vsStore[id]);
await vscode.window.showTextDocument(doc);
try {
const doc = await vscode.workspace.openTextDocument(
this.vsStore[id]
);
await vscode.window.showTextDocument(doc);
} catch (err) {
console.error(
"🪵 [workspaceState.ts:98] ~ token ~ \x1b[0;32me\x1b[0m = ",
err
);
vscode.window.showErrorMessage(
err instanceof Error
? err.message
: `cannot open file ${this.vsStore[id]}`
);
}
return;
}

Expand Down

0 comments on commit 95edce9

Please sign in to comment.