import { Disposable } from '../utils/dispose' ; import * as vscode from 'vscode' ; import * as fs from 'fs' ; import * as path from 'path' ; import { PathUtil } from '../utils/pathUtil' ; description export class WorkspaceManager extends Disposable { private _workspace : vscode . WorkspaceFolder | undefined ; constructor ( ) { super ( ) ; if ( this . numPaths == 1 ) { this . _workspace = this . firstListedWorkspace ; } } public get workspace ( ) : vscode . WorkspaceFolder | undefined { return this . _workspace ; } public get workspacePath ( ) : string | undefined { return this . workspace ?. uri . fsPath ; } public get workspaceURI ( ) : vscode . Uri | undefined { return this . _workspace ?. uri ; } public get workspacePathname ( ) : string { return this . workspace ?. name ?? '' ; } public get numPaths ( ) : number { return vscode . workspace . workspaceFolders ?. length ?? 0 ; } description public get firstListedWorkspace ( ) : vscode . WorkspaceFolder | undefined { return vscode . workspace . workspaceFolders ?. [ 0 ] ; } public get workspaces ( ) { return vscode . workspace . workspaceFolders ; } description param { string } path returns public absPathInDefaultWorkspace ( path ) { return this . workspacePath ? PathUtil . PathBeginsWith ( path , this . workspacePath ) : false ; } description param { string } path returns { boolean } public absPathInAnyWorkspace ( file ) { if ( this . workspaces ) { for ( const i in this . workspaces ) { if ( PathUtil . PathBeginsWith ( file , this . workspaces [ i ] . uri . fsPath ) ) { return true ; } } } return false ; } description param { string } path returns { boolean } public pathExistsRelativeToDefaultWorkspace ( file ) { if ( ! this . workspacePath ) { return false ; } const fullPath = path . join ( this . workspacePath , file ) ; return fs . existsSync ( fullPath ) ; } description param { string } path returns { boolean } public pathExistsRelativeToAnyWorkspace ( file ) { if ( this . workspaces ) { for ( const i in this . workspaces ) { if ( fs . existsSync ( path . join ( this . workspaces [ i ] . uri . fsPath , file ) ) ) { return true ; } } } return false ; } description param { string } path returns { string } public getFileRelativeToDefaultWorkspace ( path ) { const workspaceFolder = this . workspacePath ; if ( workspaceFolder && this . absPathInDefaultWorkspace ( path ) ) { return PathUtil . ConvertToUnixPath ( path . substr ( workspaceFolder . length ) ) ; } else { return undefined ; } } }