From 8b4d424ddaa6859f6c214d8d328c469ae40350d7 Mon Sep 17 00:00:00 2001 From: Joyce Er Date: Fri, 7 Apr 2023 16:31:50 +0000 Subject: [PATCH] Allow opening diffs from Cloud Changes view --- .../editSessions/browser/editSessionsViews.ts | 54 ++++++++++++++++--- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/src/vs/workbench/contrib/editSessions/browser/editSessionsViews.ts b/src/vs/workbench/contrib/editSessions/browser/editSessionsViews.ts index b0a73c1b0c6e6..21eb04f24d73c 100644 --- a/src/vs/workbench/contrib/editSessions/browser/editSessionsViews.ts +++ b/src/vs/workbench/contrib/editSessions/browser/editSessionsViews.ts @@ -10,7 +10,7 @@ import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiati import { Registry } from 'vs/platform/registry/common/platform'; import { TreeView, TreeViewPane } from 'vs/workbench/browser/parts/views/treeView'; import { Extensions, ITreeItem, ITreeViewDataProvider, ITreeViewDescriptor, IViewsRegistry, TreeItemCollapsibleState, TreeViewItemHandleArg, ViewContainer } from 'vs/workbench/common/views'; -import { EDIT_SESSIONS_DATA_VIEW_ID, EDIT_SESSIONS_SCHEME, EDIT_SESSIONS_SHOW_VIEW, EDIT_SESSIONS_TITLE, IEditSessionsStorageService } from 'vs/workbench/contrib/editSessions/common/editSessions'; +import { ChangeType, EDIT_SESSIONS_DATA_VIEW_ID, EDIT_SESSIONS_SCHEME, EDIT_SESSIONS_SHOW_VIEW, EDIT_SESSIONS_TITLE, IEditSessionsStorageService } from 'vs/workbench/contrib/editSessions/common/editSessions'; import { URI } from 'vs/base/common/uri'; import { fromNow } from 'vs/base/common/date'; import { Codicon } from 'vs/base/common/codicons'; @@ -19,6 +19,10 @@ import { registerAction2, Action2, MenuId } from 'vs/platform/actions/common/act import { ContextKeyExpr, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey'; import { ICommandService } from 'vs/platform/commands/common/commands'; import { IDialogService } from 'vs/platform/dialogs/common/dialogs'; +import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace'; +import { joinPath } from 'vs/base/common/resources'; +import { IFileService } from 'vs/platform/files/common/files'; +import { basename } from 'vs/base/common/path'; const EDIT_SESSIONS_COUNT_KEY = 'editSessionsCount'; const EDIT_SESSIONS_COUNT_CONTEXT_KEY = new RawContextKey(EDIT_SESSIONS_COUNT_KEY, 0); @@ -170,7 +174,9 @@ class EditSessionDataViewDataProvider implements ITreeViewDataProvider { constructor( @IEditSessionsStorageService private readonly editSessionsStorageService: IEditSessionsStorageService, - @IContextKeyService private readonly contextKeyService: IContextKeyService + @IContextKeyService private readonly contextKeyService: IContextKeyService, + @IWorkspaceContextService private readonly workspaceContextService: IWorkspaceContextService, + @IFileService private readonly fileService: IFileService, ) { this.editSessionsCount = EDIT_SESSIONS_COUNT_CONTEXT_KEY.bindTo(this.contextKeyService); } @@ -247,20 +253,52 @@ class EditSessionDataViewDataProvider implements ITreeViewDataProvider { return []; } - return (data.editSession.folders.find((folder) => folder.name === folderName)?.workingChanges ?? []).map((change) => { - const resource = URI.from({ scheme: EDIT_SESSIONS_SCHEME, authority: 'remote-session-content', path: `/${data.ref}/${folderName}/${change.relativeFilePath}` }); + const currentWorkspaceFolder = this.workspaceContextService.getWorkspace().folders.find((folder) => folder.name === folderName); + const editSessionFolder = data.editSession.folders.find((folder) => folder.name === folderName); + + if (!editSessionFolder) { + return []; + } + + return Promise.all(editSessionFolder.workingChanges.map(async (change) => { + const cloudChangeUri = URI.from({ scheme: EDIT_SESSIONS_SCHEME, authority: 'remote-session-content', path: `/${data.ref}/${folderName}/${change.relativeFilePath}` }); + + if (currentWorkspaceFolder?.uri) { + // find the corresponding file in the workspace + const localCopy = joinPath(currentWorkspaceFolder.uri, change.relativeFilePath); + if (change.type === ChangeType.Addition && await this.fileService.exists(localCopy)) { + return { + handle: cloudChangeUri.toString(), + resourceUri: cloudChangeUri, + collapsibleState: TreeItemCollapsibleState.None, + label: { label: change.relativeFilePath }, + themeIcon: Codicon.file, + command: { + id: 'vscode.diff', + title: localize('compare changes', 'Compare Changes'), + arguments: [ + localCopy, + cloudChangeUri, + `${basename(change.relativeFilePath)} (${localize('local copy', 'Local Copy')} \u2194 ${localize('cloud changes', 'Cloud Changes')})`, + undefined + ] + } + }; + } + } + return { - handle: resource.toString(), - resourceUri: resource, + handle: cloudChangeUri.toString(), + resourceUri: cloudChangeUri, collapsibleState: TreeItemCollapsibleState.None, label: { label: change.relativeFilePath }, themeIcon: Codicon.file, command: { id: API_OPEN_EDITOR_COMMAND_ID, title: localize('open file', 'Open File'), - arguments: [resource, undefined, undefined] + arguments: [cloudChangeUri, undefined, undefined] } }; - }); + })); } }