|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { Disposable } from 'vs/base/common/lifecycle'; |
| 7 | +import { localize } from 'vs/nls'; |
| 8 | +import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors'; |
| 9 | +import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; |
| 10 | +import { Registry } from 'vs/platform/registry/common/platform'; |
| 11 | +import { TreeView, TreeViewPane } from 'vs/workbench/browser/parts/views/treeView'; |
| 12 | +import { Extensions, ITreeItem, ITreeViewDataProvider, ITreeViewDescriptor, IViewsRegistry, TreeItemCollapsibleState, TreeViewItemHandleArg, ViewContainer } from 'vs/workbench/common/views'; |
| 13 | +import { EDIT_SESSIONS_SCHEME, EDIT_SESSIONS_TITLE, IEditSessionsWorkbenchService } from 'vs/workbench/contrib/editSessions/common/editSessions'; |
| 14 | +import { URI } from 'vs/base/common/uri'; |
| 15 | +import { fromNow } from 'vs/base/common/date'; |
| 16 | +import { Codicon } from 'vs/base/common/codicons'; |
| 17 | +import { API_OPEN_EDITOR_COMMAND_ID } from 'vs/workbench/browser/parts/editor/editorCommands'; |
| 18 | +import { registerAction2, Action2, MenuId } from 'vs/platform/actions/common/actions'; |
| 19 | +import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; |
| 20 | +import { ICommandService } from 'vs/platform/commands/common/commands'; |
| 21 | +import { IDialogService } from 'vs/platform/dialogs/common/dialogs'; |
| 22 | + |
| 23 | +export class EditSessionsDataViews extends Disposable { |
| 24 | + constructor( |
| 25 | + container: ViewContainer, |
| 26 | + @IInstantiationService private readonly instantiationService: IInstantiationService, |
| 27 | + ) { |
| 28 | + super(); |
| 29 | + this.registerViews(container); |
| 30 | + } |
| 31 | + |
| 32 | + private registerViews(container: ViewContainer): void { |
| 33 | + const viewId = 'workbench.views.editSessions.data'; |
| 34 | + const name = localize('edit sessions data', 'All Sessions'); |
| 35 | + const treeView = this.instantiationService.createInstance(TreeView, viewId, name); |
| 36 | + treeView.showCollapseAllAction = true; |
| 37 | + treeView.showRefreshAction = true; |
| 38 | + const disposable = treeView.onDidChangeVisibility(visible => { |
| 39 | + if (visible && !treeView.dataProvider) { |
| 40 | + disposable.dispose(); |
| 41 | + treeView.dataProvider = this.instantiationService.createInstance(EditSessionDataViewDataProvider); |
| 42 | + } |
| 43 | + }); |
| 44 | + Registry.as<IViewsRegistry>(Extensions.ViewsRegistry).registerViews([<ITreeViewDescriptor>{ |
| 45 | + id: viewId, |
| 46 | + name, |
| 47 | + ctorDescriptor: new SyncDescriptor(TreeViewPane), |
| 48 | + canToggleVisibility: true, |
| 49 | + canMoveView: false, |
| 50 | + treeView, |
| 51 | + collapsed: false, |
| 52 | + order: 100, |
| 53 | + hideByDefault: true, |
| 54 | + }], container); |
| 55 | + |
| 56 | + registerAction2(class extends Action2 { |
| 57 | + constructor() { |
| 58 | + super({ |
| 59 | + id: 'workbench.editSessions.actions.resume', |
| 60 | + title: localize('workbench.editSessions.actions.resume', "Resume Edit Session"), |
| 61 | + icon: Codicon.repoPull, |
| 62 | + menu: { |
| 63 | + id: MenuId.ViewItemContext, |
| 64 | + when: ContextKeyExpr.and(ContextKeyExpr.equals('view', viewId), ContextKeyExpr.regex('viewItem', /edit-session/i)), |
| 65 | + group: 'inline' |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + async run(accessor: ServicesAccessor, handle: TreeViewItemHandleArg): Promise<void> { |
| 71 | + const editSessionId = URI.parse(handle.$treeItemHandle).path.substring(1); |
| 72 | + const commandService = accessor.get(ICommandService); |
| 73 | + await commandService.executeCommand('workbench.experimental.editSessions.actions.resumeLatest', editSessionId); |
| 74 | + await treeView.refresh(); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + registerAction2(class extends Action2 { |
| 79 | + constructor() { |
| 80 | + super({ |
| 81 | + id: 'workbench.editSessions.actions.delete', |
| 82 | + title: localize('workbench.editSessions.actions.delete', "Delete Edit Session"), |
| 83 | + icon: Codicon.trash, |
| 84 | + menu: { |
| 85 | + id: MenuId.ViewItemContext, |
| 86 | + when: ContextKeyExpr.and(ContextKeyExpr.equals('view', viewId), ContextKeyExpr.regex('viewItem', /edit-session/i)), |
| 87 | + group: 'inline' |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + async run(accessor: ServicesAccessor, handle: TreeViewItemHandleArg): Promise<void> { |
| 93 | + const editSessionId = URI.parse(handle.$treeItemHandle).path.substring(1); |
| 94 | + const dialogService = accessor.get(IDialogService); |
| 95 | + const editSessionWorkbenchService = accessor.get(IEditSessionsWorkbenchService); |
| 96 | + const result = await dialogService.confirm({ |
| 97 | + message: localize('confirm delete', 'Are you sure you want to permanently delete the edit session with ref {0}? You cannot undo this action.', editSessionId), |
| 98 | + type: 'warning', |
| 99 | + title: EDIT_SESSIONS_TITLE |
| 100 | + }); |
| 101 | + if (result.confirmed) { |
| 102 | + await editSessionWorkbenchService.delete(editSessionId); |
| 103 | + await treeView.refresh(); |
| 104 | + } |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +class EditSessionDataViewDataProvider implements ITreeViewDataProvider { |
| 111 | + constructor( |
| 112 | + @IEditSessionsWorkbenchService private readonly editSessionsWorkbenchService: IEditSessionsWorkbenchService |
| 113 | + ) { } |
| 114 | + |
| 115 | + async getChildren(element?: ITreeItem): Promise<ITreeItem[]> { |
| 116 | + if (!element) { |
| 117 | + return this.getAllEditSessions(); |
| 118 | + } |
| 119 | + |
| 120 | + const [ref, folderName, filePath] = URI.parse(element.handle).path.substring(1).split('/'); |
| 121 | + |
| 122 | + if (ref && !folderName) { |
| 123 | + return this.getEditSession(ref); |
| 124 | + } else if (ref && folderName && !filePath) { |
| 125 | + return this.getEditSessionFolderContents(ref, folderName); |
| 126 | + } |
| 127 | + |
| 128 | + return []; |
| 129 | + } |
| 130 | + |
| 131 | + private async getAllEditSessions(): Promise<ITreeItem[]> { |
| 132 | + const allEditSessions = await this.editSessionsWorkbenchService.list(); |
| 133 | + return allEditSessions.map((session) => { |
| 134 | + const resource = URI.from({ scheme: EDIT_SESSIONS_SCHEME, authority: 'remote-session-content', path: `/${session.ref}` }); |
| 135 | + return { |
| 136 | + handle: resource.toString(), |
| 137 | + collapsibleState: TreeItemCollapsibleState.Collapsed, |
| 138 | + label: { label: session.ref }, |
| 139 | + description: fromNow(session.created, true), |
| 140 | + themeIcon: Codicon.repo, |
| 141 | + contextValue: `edit-session` |
| 142 | + }; |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + private async getEditSession(ref: string): Promise<ITreeItem[]> { |
| 147 | + const data = await this.editSessionsWorkbenchService.read(ref); |
| 148 | + |
| 149 | + if (!data) { |
| 150 | + return []; |
| 151 | + } |
| 152 | + |
| 153 | + return data.editSession.folders.map((folder) => { |
| 154 | + const resource = URI.from({ scheme: EDIT_SESSIONS_SCHEME, authority: 'remote-session-content', path: `/${data.ref}/${folder.name}` }); |
| 155 | + return { |
| 156 | + handle: resource.toString(), |
| 157 | + collapsibleState: TreeItemCollapsibleState.Collapsed, |
| 158 | + label: { label: folder.name }, |
| 159 | + themeIcon: Codicon.folder |
| 160 | + }; |
| 161 | + }); |
| 162 | + } |
| 163 | + |
| 164 | + private async getEditSessionFolderContents(ref: string, folderName: string): Promise<ITreeItem[]> { |
| 165 | + const data = await this.editSessionsWorkbenchService.read(ref); |
| 166 | + |
| 167 | + if (!data) { |
| 168 | + return []; |
| 169 | + } |
| 170 | + |
| 171 | + return (data.editSession.folders.find((folder) => folder.name === folderName)?.workingChanges ?? []).map((change) => { |
| 172 | + const resource = URI.from({ scheme: EDIT_SESSIONS_SCHEME, authority: 'remote-session-content', path: `/${data.ref}/${folderName}/${change.relativeFilePath}` }); |
| 173 | + return { |
| 174 | + handle: resource.toString(), |
| 175 | + resourceUri: resource, |
| 176 | + collapsibleState: TreeItemCollapsibleState.None, |
| 177 | + label: { label: change.relativeFilePath }, |
| 178 | + themeIcon: Codicon.file, |
| 179 | + command: { |
| 180 | + id: API_OPEN_EDITOR_COMMAND_ID, |
| 181 | + title: localize('open file', 'Open File'), |
| 182 | + arguments: [resource, undefined, undefined] |
| 183 | + } |
| 184 | + }; |
| 185 | + }); |
| 186 | + } |
| 187 | +} |
0 commit comments