import { inject , injectable } from 'inversify' ; import { CancellationToken , ConfigurationChangeEvent , Event , FileSystemWatcher , GlobPattern , TextDocument , Uri , window , workspace , WorkspaceConfiguration , WorkspaceFolder , WorkspaceFoldersChangeEvent } from 'vscode' ; import { IApplicationShell , IWorkspaceService } from './types' ; @ injectable ( ) export class WorkspaceService implements IWorkspaceService { constructor ( @ inject ( IApplicationShell ) private shell ) { } public get onDidChangeConfiguration ( ) : Event < ConfigurationChangeEvent > { return workspace . onDidChangeConfiguration ; } public get rootPath ( ) : string | undefined { return Array . isArray ( workspace . workspaceFolders ) ? workspace . workspaceFolders [ 0 ] . uri . fsPath : undefined ; } public get workspaceFolders ( ) : WorkspaceFolder [ ] | undefined { return workspace . workspaceFolders ; } public get onDidChangeWorkspaceFolders ( ) : Event < WorkspaceFoldersChangeEvent > { return workspace . onDidChangeWorkspaceFolders ; } public get hasWorkspaceFolders ( ) { return Array . isArray ( workspace . workspaceFolders ) && workspace . workspaceFolders . length > 0 ; } public getConfiguration ( section ? , resource ? ) { return workspace . getConfiguration ( section , resource ) ; } public getWorkspaceFolder ( uri ) { return workspace . getWorkspaceFolder ( uri ) ; } public asRelativePath ( pathOrUri , includeWorkspaceFolder ? ) { return workspace . asRelativePath ( pathOrUri , includeWorkspaceFolder ) ; } public createFileSystemWatcher ( globPattern , ignoreCreateEvents ? , ignoreChangeEvents ? , ignoreDeleteEvents ? ) { return workspace . createFileSystemWatcher ( globPattern , ignoreCreateEvents , ignoreChangeEvents , ignoreDeleteEvents ) ; } public findFiles ( include , exclude ? , maxResults ? , token ? ) { return workspace . findFiles ( include , exclude , maxResults , token ) ; } public get onDidSaveTextDocument ( ) : Event < TextDocument > { return workspace . onDidSaveTextDocument ; } public async selectWorkspaceFolder ( ) { if ( ! this . hasWorkspaceFolders ) { await this . shell . showErrorMessage ( 'Please open a workspace folder' ) ; return ; } else if ( workspace . workspaceFolders ! . length === 1 ) { return workspace . workspaceFolders ! [ 0 ] ; } else { return window . showWorkspaceFolderPick ( { placeHolder : 'Select a workspace folder' } ) ; } } }