提交 e179ec75 编写于 作者: B Benjamin Pasero

workspace context 💄

上级 68a4cb86
......@@ -91,7 +91,7 @@ export function loadExperiments(contextService: IWorkspaceContextService, storag
const newUserDuration = 24 * 60 * 60 * 1000;
const firstSessionDate = storageService.get('telemetry.firstSessionDate');
const isNewUser = !firstSessionDate || Date.now() - Date.parse(firstSessionDate) < newUserDuration;
if (!isNewUser || !!contextService.getWorkspace()) {
if (!isNewUser || contextService.hasWorkspace()) {
showNewUserWatermark = defaultExperiments.showNewUserWatermark;
openUntitledFile = defaultExperiments.openUntitledFile;
}
......
......@@ -13,6 +13,11 @@ export const IWorkspaceContextService = createDecorator<IWorkspaceContextService
export interface IWorkspaceContextService {
_serviceBrand: any;
/**
* Returns iff the application was opened with a workspace or not.
*/
hasWorkspace(): boolean;
/**
* Provides access to the workspace object the platform is running with. This may be null if the workbench was opened
* without workspace (empty);
......@@ -72,6 +77,10 @@ export class WorkspaceContextService implements IWorkspaceContextService {
return this.workspace;
}
public hasWorkspace(): boolean {
return !!this.workspace;
}
public isInsideWorkspace(resource: URI): boolean {
if (resource && this.workspace) {
return paths.isEqualOrParent(resource.fsPath, this.workspace.resource.fsPath);
......
......@@ -85,7 +85,7 @@ export function createApiFactory(initData: IInitData, threadService: IThreadServ
const extHostMessageService = new ExtHostMessageService(threadService);
const extHostStatusBar = new ExtHostStatusBar(threadService);
const extHostOutputService = new ExtHostOutputService(threadService);
const workspacePath = contextService.getWorkspace() ? contextService.getWorkspace().resource.fsPath : undefined;
const workspacePath = contextService.hasWorkspace() ? contextService.getWorkspace().resource.fsPath : undefined;
const extHostWorkspace = new ExtHostWorkspace(threadService, workspacePath);
const extHostLanguages = new ExtHostLanguages(threadService);
......
......@@ -123,7 +123,7 @@ export class CloseFolderAction extends Action {
}
run(): TPromise<void> {
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
this.messageService.show(Severity.Info, nls.localize('noFolderOpened', "There is currently no folder opened in this instance to close."));
return TPromise.as(null);
}
......@@ -483,7 +483,7 @@ export class OpenRecentAction extends Action {
const folderPicks: IFilePickOpenEntry[] = recentFolders.map((p, index) => toPick(p, index === 0 ? { label: nls.localize('folders', "folders") } : void 0, true));
const filePicks: IFilePickOpenEntry[] = recentFiles.map((p, index) => toPick(p, index === 0 ? { label: nls.localize('files', "files"), border: true } : void 0, false));
const hasWorkspace = !!this.contextService.getWorkspace();
const hasWorkspace = this.contextService.hasWorkspace();
this.quickOpenService.pick(folderPicks.concat(...filePicks), {
autoFocus: { autoFocusFirstEntry: !hasWorkspace, autoFocusSecondEntry: hasWorkspace },
......
......@@ -133,7 +133,7 @@ function openWorkbench(environment: IWindowConfiguration, workspace: IWorkspace,
const environmentService = new EnvironmentService(environment, environment.execPath);
const contextService = new WorkspaceContextService(workspace);
const configurationService = new WorkspaceConfigurationService(contextService, environmentService);
const timerService = new TimerService((<any>window).MonacoEnvironment.timers as IInitData, !contextService.getWorkspace());
const timerService = new TimerService((<any>window).MonacoEnvironment.timers as IInitData, !contextService.hasWorkspace());
// Since the configuration service is one of the core services that is used in so many places, we initialize it
// right before startup of the workbench shell to have its data ready for consumers
......
......@@ -199,7 +199,7 @@ export class WorkbenchShell {
this.telemetryService.publicLog('workspaceLoad', {
userAgent: navigator.userAgent,
windowSize: { innerHeight: window.innerHeight, innerWidth: window.innerWidth, outerHeight: window.outerHeight, outerWidth: window.outerWidth },
emptyWorkbench: !this.contextService.getWorkspace(),
emptyWorkbench: !this.contextService.hasWorkspace(),
'workbench.filesToOpen': filesToOpen && filesToOpen.length || undefined,
'workbench.filesToCreate': filesToCreate && filesToCreate.length || undefined,
'workbench.filesToDiff': filesToDiff && filesToDiff.length || undefined,
......@@ -267,7 +267,7 @@ export class WorkbenchShell {
}, errors.onUnexpectedError);
// Storage Sevice
const disableWorkspaceStorage = this.environmentService.extensionTestsPath || (!this.contextService.getWorkspace() && !this.environmentService.isExtensionDevelopment); // without workspace or in any extension test, we use inMemory storage unless we develop an extension where we want to preserve state
const disableWorkspaceStorage = this.environmentService.extensionTestsPath || (!this.contextService.hasWorkspace() && !this.environmentService.isExtensionDevelopment); // without workspace or in any extension test, we use inMemory storage unless we develop an extension where we want to preserve state
this.storageService = instantiationService.createInstance(StorageService, window.localStorage, disableWorkspaceStorage ? inMemoryLocalStorageInstance : window.localStorage);
serviceCollection.set(IStorageService, this.storageService);
......
......@@ -382,7 +382,7 @@ export class Workbench implements IPartService {
}
// Empty workbench: some first time users will not have an untiled file; returning users will always have one
else if (!this.contextService.getWorkspace() && this.telemetryService.getExperiments().openUntitledFile) {
else if (!this.contextService.hasWorkspace() && this.telemetryService.getExperiments().openUntitledFile) {
return this.backupFileService.hasBackups().then(hasBackups => {
if (hasBackups) {
return TPromise.as([]); // do not open any empty untitled file if we have backups to restore
......@@ -508,14 +508,14 @@ export class Workbench implements IPartService {
// Sidebar visibility
this.sideBarHidden = this.storageService.getBoolean(Workbench.sidebarHiddenSettingKey, StorageScope.WORKSPACE, false);
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
this.sideBarHidden = true; // we hide sidebar in single-file-mode
}
// Panel part visibility
const panelRegistry = Registry.as<PanelRegistry>(PanelExtensions.Panels);
this.panelHidden = this.storageService.getBoolean(Workbench.panelHiddenSettingKey, StorageScope.WORKSPACE, true);
if (!this.contextService.getWorkspace() || !panelRegistry.getDefaultPanelId()) {
if (!this.contextService.hasWorkspace() || !panelRegistry.getDefaultPanelId()) {
this.panelHidden = true; // we hide panel part in single-file-mode or if there is no default panel
}
......@@ -913,7 +913,7 @@ export class Workbench implements IPartService {
}
// Apply no-workspace state as CSS class
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
this.workbench.addClass('no-workspace');
}
......
......@@ -66,7 +66,7 @@ export class DebugViewlet extends Viewlet {
super.create(parent);
this.$el = parent.div().addClass('debug-viewlet');
if (this.contextService.getWorkspace()) {
if (this.contextService.hasWorkspace()) {
const actionRunner = this.getActionRunner();
this.views = DebugViewRegistry.getDebugViews().map(viewConstructor => this.instantiationService.createInstance(
viewConstructor,
......
......@@ -430,7 +430,7 @@ export class DebugService implements debug.IDebugService {
}
public get state(): debug.State {
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
return debug.State.Disabled;
}
......@@ -456,7 +456,7 @@ export class DebugService implements debug.IDebugService {
}
public get enabled(): boolean {
return !!this.contextService.getWorkspace();
return this.contextService.hasWorkspace();
}
public focusStackFrameAndEvaluate(focusedStackFrame: debug.IStackFrame, process?: debug.IProcess): TPromise<void> {
......
......@@ -54,7 +54,7 @@ class EncodeDecodeDataUrlAction extends EmmetEditorAction {
return;
}
if (!workspaceContext.getWorkspace()) {
if (!workspaceContext.hasWorkspace()) {
const message = nls.localize('noWorkspace', "Decoding a data:URL image is only available inside a workspace folder.");
messageService.show(Severity.Info, message);
return;
......
......@@ -553,7 +553,7 @@ export class DisableForWorkspaceAction extends Action implements IExtensionActio
private update(): void {
this.enabled = false;
if (this.extension && this.workspaceContextService.getWorkspace()) {
if (this.extension && this.workspaceContextService.hasWorkspace()) {
this.enabled = this.extension.type !== LocalExtensionType.System && !this.extension.disabledGlobally && !this.extension.disabledForWorkspace;
}
}
......@@ -1001,7 +1001,7 @@ export class ShowWorkspaceRecommendedExtensionsAction extends Action {
@IWorkspaceContextService contextService: IWorkspaceContextService,
@IViewletService private viewletService: IViewletService
) {
super(id, label, null, !!contextService.getWorkspace());
super(id, label, null, contextService.hasWorkspace());
}
run(): TPromise<void> {
......@@ -1104,7 +1104,7 @@ export class ConfigureWorkspaceRecommendedExtensionsAction extends Action {
@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
@IMessageService private messageService: IMessageService
) {
super(id, label, null, !!contextService.getWorkspace());
super(id, label, null, contextService.hasWorkspace());
}
public run(event: any): TPromise<any> {
......@@ -1112,7 +1112,7 @@ export class ConfigureWorkspaceRecommendedExtensionsAction extends Action {
}
private openExtensionsFile(): TPromise<any> {
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
this.messageService.show(severity.Info, localize('ConfigureWorkspaceRecommendations.noWorkspace', 'Recommendations are only available on a workspace folder.'));
return TPromise.as(undefined);
}
......@@ -1216,7 +1216,7 @@ export class DisableAllWorkpsaceAction extends Action {
}
private update(): void {
this.enabled = !!this.workspaceContextService.getWorkspace() && this.extensionsWorkbenchService.local.some(e => e.type === LocalExtensionType.User && !e.disabledForWorkspace && !e.disabledGlobally);
this.enabled = this.workspaceContextService.hasWorkspace() && this.extensionsWorkbenchService.local.some(e => e.type === LocalExtensionType.User && !e.disabledForWorkspace && !e.disabledGlobally);
}
run(): TPromise<any> {
......@@ -1279,7 +1279,7 @@ export class EnableAllWorkpsaceAction extends Action {
}
private update(): void {
this.enabled = !!this.workspaceContextService.getWorkspace() && this.extensionsWorkbenchService.local.some(e => this.extensionEnablementService.canEnable(e.identifier) && !e.disabledGlobally && e.disabledForWorkspace);
this.enabled = this.workspaceContextService.hasWorkspace() && this.extensionsWorkbenchService.local.some(e => this.extensionEnablementService.canEnable(e.identifier) && !e.disabledGlobally && e.disabledForWorkspace);
}
run(): TPromise<any> {
......
......@@ -56,7 +56,7 @@ export class ExtensionTipsService implements IExtensionTipsService {
}
getWorkspaceRecommendations(): TPromise<string[]> {
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
return TPromise.as([]);
}
return this.fileService.resolveContent(this.contextService.toResource(paths.join('.vscode', 'extensions.json'))).then(content => {
......
......@@ -527,7 +527,7 @@ export class ExtensionsWorkbenchService implements IExtensionsWorkbenchService {
}
const globalElablement = this.extensionEnablementService.setEnablement(extension.identifier, enable, false);
if (enable && this.workspaceContextService.getWorkspace()) {
if (enable && this.workspaceContextService.hasWorkspace()) {
const workspaceEnablement = this.extensionEnablementService.setEnablement(extension.identifier, enable, true);
return TPromise.join([globalElablement, workspaceEnablement]).then(values => values[0] || values[1]);
}
......
......@@ -100,7 +100,7 @@ export class ExplorerViewlet extends Viewlet {
this.delayEditorOpeningInOpenedEditors = !!config.workbench.editor.enablePreview;
// Open editors view should always be visible in no folder workspace.
const openEditorsVisible = !this.contextService.getWorkspace() || config.explorer.openEditors.visible !== 0;
const openEditorsVisible = !this.contextService.hasWorkspace() || config.explorer.openEditors.visible !== 0;
// Create views on startup and if open editors visibility has changed #6919
if (this.openEditorsVisible !== openEditorsVisible) {
......@@ -150,7 +150,7 @@ export class ExplorerViewlet extends Viewlet {
let explorerOrEmptyView: ExplorerView | EmptyView;
// With a Workspace
if (this.contextService.getWorkspace()) {
if (this.contextService.hasWorkspace()) {
// Create a delegating editor service for the explorer to be able to delay the refresh in the opened
// editors view above. This is a workaround for being able to double click on a file to make it pinned
......
......@@ -109,7 +109,7 @@ export class FileDataSource implements IDataSource {
}
// Return if root reached
if (this.contextService.getWorkspace() && stat.resource.toString() === this.contextService.getWorkspace().resource.toString()) {
if (this.contextService.hasWorkspace() && stat.resource.toString() === this.contextService.getWorkspace().resource.toString()) {
return TPromise.as(null);
}
......
......@@ -69,7 +69,7 @@ export class CloneAction extends Action {
const clone = always(this.gitService.clone(url, result[0]), () => promise.cancel());
return clone.then(path => {
const forceNewWindow = !!this.workspaceService.getWorkspace();
const forceNewWindow = this.workspaceService.hasWorkspace();
return this.windowsService.windowOpen([path], forceNewWindow);
}).then<void>(null, e => {
......
......@@ -126,7 +126,7 @@ export class PreferencesService extends Disposable implements IPreferencesServic
}
openWorkspaceSettings(): TPromise<void> {
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
this.messageService.show(Severity.Info, nls.localize('openFolderFirst', "Open a folder first to create workspace settings"));
return;
}
......@@ -188,7 +188,7 @@ export class PreferencesService extends Disposable implements IPreferencesServic
case ConfigurationTarget.USER:
return URI.file(this.environmentService.appSettingsPath);
case ConfigurationTarget.WORKSPACE:
if (this.contextService.getWorkspace()) {
if (this.contextService.hasWorkspace()) {
return this.contextService.toResource('.vscode/settings.json');
}
}
......
......@@ -144,7 +144,7 @@ export class OpenFileHandler extends QuickOpenHandler {
private doFindResults(searchValue: string, cacheKey?: string, maxSortedResults?: number): TPromise<FileQuickOpenModel> {
const query: IQueryOptions = {
folderResources: this.contextService.getWorkspace() ? [this.contextService.getWorkspace().resource] : [],
folderResources: this.contextService.hasWorkspace() ? [this.contextService.getWorkspace().resource] : [],
extraFileResources: getOutOfWorkspaceEditorResources(this.editorGroupService, this.contextService),
filePattern: searchValue,
cacheKey: cacheKey
......@@ -186,7 +186,7 @@ export class OpenFileHandler extends QuickOpenHandler {
private cacheQuery(cacheKey: string): ISearchQuery {
const options: IQueryOptions = {
folderResources: this.contextService.getWorkspace() ? [this.contextService.getWorkspace().resource] : [],
folderResources: this.contextService.hasWorkspace() ? [this.contextService.getWorkspace().resource] : [],
extraFileResources: getOutOfWorkspaceEditorResources(this.editorGroupService, this.contextService),
filePattern: '',
cacheKey: cacheKey,
......
......@@ -221,7 +221,7 @@ export class SearchViewlet extends Viewlet {
}).getHTMLElement();
this.messages = builder.div({ 'class': 'messages' }).hide().clone();
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
this.searchWithoutFolderMessage(this.clearMessage());
}
......@@ -580,7 +580,7 @@ export class SearchViewlet extends Viewlet {
public clearSearchResults(): void {
this.viewModel.searchResult.clear();
this.showEmptyStage();
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
this.searchWithoutFolderMessage(this.clearMessage());
}
this.searchWidget.clear();
......@@ -748,7 +748,7 @@ export class SearchViewlet extends Viewlet {
let includes: IExpression = this.inputPatternIncludes.getGlob();
let options: IQueryOptions = {
folderResources: this.contextService.getWorkspace() ? [this.contextService.getWorkspace().resource] : [],
folderResources: this.contextService.hasWorkspace() ? [this.contextService.getWorkspace().resource] : [],
extraFileResources: getOutOfWorkspaceEditorResources(this.editorGroupService, this.contextService),
excludePattern: excludes,
maxResults: SearchViewlet.MAX_TEXT_RESULTS,
......@@ -882,7 +882,7 @@ export class SearchViewlet extends Viewlet {
}).on(dom.EventType.CLICK, (e: MouseEvent) => {
dom.EventHelper.stop(e, false);
if (this.contextService.getWorkspace()) {
if (this.contextService.hasWorkspace()) {
this.preferencesService.openWorkspaceSettings().done(() => null, errors.onUnexpectedError);
} else {
this.preferencesService.openGlobalSettings().done(() => null, errors.onUnexpectedError);
......@@ -890,7 +890,7 @@ export class SearchViewlet extends Viewlet {
});
}
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
this.searchWithoutFolderMessage(div);
}
} else {
......
......@@ -92,7 +92,7 @@ class AbstractTaskAction extends Action {
}
protected canRun(): boolean {
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
this.messageService.show(Severity.Info, nls.localize('AbstractTaskAction.noWorkspace', 'Tasks are only available on a workspace folder.'));
return false;
}
......@@ -197,7 +197,7 @@ abstract class OpenTaskConfigurationAction extends Action {
}
public run(event?: any): TPromise<IEditor> {
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
this.messageService.show(Severity.Info, nls.localize('ConfigureTaskRunnerAction.noWorkspace', 'Tasks are only available on a workspace folder.'));
return TPromise.as(undefined);
}
......@@ -678,7 +678,7 @@ class TaskService extends EventEmitter implements ITaskService {
private get taskSystemPromise(): TPromise<ITaskSystem> {
if (!this._taskSystemPromise) {
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
this._taskSystem = new NullTaskSystem();
this._taskSystemPromise = TPromise.as(this._taskSystem);
} else {
......
......@@ -156,7 +156,7 @@ export class WatermarkContribution implements IWorkbenchContribution {
.div({ 'class': 'watermark' });
const box = $(watermark)
.div({ 'class': 'watermark-box' });
const folder = !!this.contextService.getWorkspace();
const folder = this.contextService.hasWorkspace();
const newUser = this.telemetryService.getExperiments().showNewUserWatermark;
const selected = (newUser ? newUserEntries : (folder ? folderEntries : noFolderEntries))
.filter(entry => !('mac' in entry) || entry.mac === isMacintosh);
......
......@@ -206,7 +206,7 @@ export class ConfigurationEditingService implements IConfigurationEditingService
}
// Target cannot be workspace if no workspace opened
if (target === ConfigurationTarget.WORKSPACE && !this.contextService.getWorkspace()) {
if (target === ConfigurationTarget.WORKSPACE && !this.contextService.hasWorkspace()) {
return TPromise.as({ error: ConfigurationEditingErrorCode.ERROR_NO_WORKSPACE_OPENED });
}
......
......@@ -226,7 +226,7 @@ export class WorkspaceConfigurationService implements IWorkspaceConfigurationSer
private loadWorkspaceConfigFiles(): TPromise<{ [relativeWorkspacePath: string]: IConfigFile }> {
// Return early if we don't have a workspace
if (!this.contextService.getWorkspace()) {
if (!this.contextService.hasWorkspace()) {
return TPromise.as(Object.create(null));
}
......
......@@ -67,7 +67,7 @@ export class FileService implements IFileService {
// adjust encodings
const encodingOverride: IEncodingOverride[] = [];
encodingOverride.push({ resource: uri.file(environmentService.appSettingsHome), encoding: encoding.UTF8 });
if (this.contextService.getWorkspace()) {
if (this.contextService.hasWorkspace()) {
encodingOverride.push({ resource: uri.file(paths.join(this.contextService.getWorkspace().resource.fsPath, '.vscode')), encoding: encoding.UTF8 });
}
......
......@@ -66,6 +66,10 @@ export class TestContextService implements IWorkspaceContextService {
this.options = options || Object.create(null);
}
public hasWorkspace(): boolean {
return !!this.workspace;
}
public getWorkspace(): IWorkspace {
return this.workspace;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册