diff --git a/src/commands/ui.show.ts b/src/commands/ui.show.ts index ac76476..17e365a 100644 --- a/src/commands/ui.show.ts +++ b/src/commands/ui.show.ts @@ -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); } } ); diff --git a/src/workspaceState.ts b/src/workspaceState.ts index bc18cac..7dcd275 100644 --- a/src/workspaceState.ts +++ b/src/workspaceState.ts @@ -1,5 +1,7 @@ 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 @@ -7,10 +9,15 @@ import { storeKey } from "./config"; 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; 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; @@ -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"); @@ -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(); } @@ -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; }