|
| 1 | +import { |
| 2 | + TreeDataProvider, |
| 3 | + Disposable, |
| 4 | + TreeItem, |
| 5 | + commands, |
| 6 | + EventEmitter |
| 7 | +} from "vscode"; |
| 8 | +import { Model } from "../model"; |
| 9 | +import { ISvnPathChange, Status } from "../common/types"; |
| 10 | +import { openDiff, getIconObject, openFileRemote } from "./common"; |
| 11 | +import { dispose } from "../util"; |
| 12 | + |
| 13 | +export class BranchChangesProvider |
| 14 | + implements TreeDataProvider<ISvnPathChange>, Disposable { |
| 15 | + private _dispose: Disposable[] = []; |
| 16 | + private _onDidChangeTreeData = new EventEmitter<ISvnPathChange | undefined>(); |
| 17 | + public readonly onDidChangeTreeData = this._onDidChangeTreeData.event; |
| 18 | + |
| 19 | + constructor(private model: Model) { |
| 20 | + this._dispose.push( |
| 21 | + commands.registerCommand( |
| 22 | + "svn.branchchanges.openDiff", |
| 23 | + this.openDiffCmd, |
| 24 | + this |
| 25 | + ) |
| 26 | + ); |
| 27 | + |
| 28 | + this._dispose.push( |
| 29 | + commands.registerCommand( |
| 30 | + "svn.branchchanges.refresh", |
| 31 | + () => this._onDidChangeTreeData.fire(), |
| 32 | + this |
| 33 | + ) |
| 34 | + ); |
| 35 | + |
| 36 | + this.model.onDidChangeRepository(() => this._onDidChangeTreeData.fire()); |
| 37 | + } |
| 38 | + |
| 39 | + dispose() { |
| 40 | + dispose(this._dispose); |
| 41 | + } |
| 42 | + |
| 43 | + getTreeItem(element: ISvnPathChange): TreeItem | Thenable<TreeItem> { |
| 44 | + let iconName: string = ""; |
| 45 | + if (element.item === Status.ADDED) { |
| 46 | + iconName = "status-added"; |
| 47 | + } else if (element.item === Status.DELETED) { |
| 48 | + iconName = "status-deleted"; |
| 49 | + } else if (element.item === Status.MODIFIED) { |
| 50 | + iconName = "status-modified"; |
| 51 | + } |
| 52 | + |
| 53 | + const iconPath = getIconObject(iconName); |
| 54 | + |
| 55 | + return { |
| 56 | + label: element.newPath.toString(), |
| 57 | + command: { |
| 58 | + command: "svn.branchchanges.openDiff", |
| 59 | + title: "Open diff", |
| 60 | + arguments: [element] |
| 61 | + }, |
| 62 | + iconPath, |
| 63 | + tooltip: `${element.oldPath}@r${element.oldRevision} → ${element.newPath}@r${element.newRevision}` |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + getChildren(element?: ISvnPathChange): Promise<ISvnPathChange[]> { |
| 68 | + if (element !== undefined) { |
| 69 | + return Promise.resolve([]); |
| 70 | + } |
| 71 | + |
| 72 | + const changes: Promise<ISvnPathChange[]>[] = []; |
| 73 | + |
| 74 | + for (const repo of this.model.repositories) { |
| 75 | + changes.push(repo.getChanges()); |
| 76 | + } |
| 77 | + |
| 78 | + return Promise.all(changes).then(value => |
| 79 | + value.reduce((prev, curr) => prev.concat(curr), []) |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + public async openDiffCmd(element: ISvnPathChange) { |
| 84 | + const repo = await this.model.getRemoteRepository(element.repo); |
| 85 | + |
| 86 | + if (element.item === Status.MODIFIED) { |
| 87 | + return openDiff( |
| 88 | + repo, |
| 89 | + element.oldPath, |
| 90 | + element.oldRevision, |
| 91 | + element.newRevision, |
| 92 | + element.newPath |
| 93 | + ); |
| 94 | + } |
| 95 | + if (element.item === Status.ADDED) { |
| 96 | + return openFileRemote(repo, element.newPath, element.newRevision); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments