|
| 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 | +"use strict"; |
| 7 | + |
| 8 | +import { |
| 9 | + window, |
| 10 | + workspace, |
| 11 | + Uri, |
| 12 | + Disposable, |
| 13 | + Event, |
| 14 | + EventEmitter, |
| 15 | + DecorationData, |
| 16 | + DecorationProvider, |
| 17 | + ThemeColor |
| 18 | +} from "vscode"; |
| 19 | +import { Repository } from "./repository"; |
| 20 | +import { Model } from "./model"; |
| 21 | +import { debounce } from "./decorators"; |
| 22 | +import { filterEvent, isDescendant } from "./util"; |
| 23 | +import { SvnResourceGroup } from "./resource"; |
| 24 | +import { Status } from "./svn"; |
| 25 | +import * as path from "path"; |
| 26 | + |
| 27 | +class SvnIgnoreDecorationProvider implements DecorationProvider { |
| 28 | + private readonly _onDidChangeDecorations = new EventEmitter<Uri[]>(); |
| 29 | + readonly onDidChangeDecorations: Event<Uri[]> = this._onDidChangeDecorations |
| 30 | + .event; |
| 31 | + |
| 32 | + private checkIgnoreQueue = new Map< |
| 33 | + string, |
| 34 | + { resolve: (status: boolean) => void; reject: (err: any) => void } |
| 35 | + >(); |
| 36 | + private disposables: Disposable[] = []; |
| 37 | + |
| 38 | + constructor(private repository: Repository) { |
| 39 | + this.disposables.push( |
| 40 | + window.registerDecorationProvider(this), |
| 41 | + repository.onDidChangeStatus(_ => this._onDidChangeDecorations.fire()) |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + dispose(): void { |
| 46 | + this.disposables.forEach(d => d.dispose()); |
| 47 | + this.checkIgnoreQueue.clear(); |
| 48 | + } |
| 49 | + |
| 50 | + provideDecoration(uri: Uri): Promise<DecorationData | undefined> { |
| 51 | + return new Promise<boolean>((resolve, reject) => { |
| 52 | + this.checkIgnoreQueue.set(uri.fsPath, { resolve, reject }); |
| 53 | + this.checkIgnoreSoon(); |
| 54 | + }).then(ignored => { |
| 55 | + if (ignored) { |
| 56 | + return <DecorationData>{ |
| 57 | + priority: 3, |
| 58 | + color: new ThemeColor("gitDecoration.ignoredResourceForeground") |
| 59 | + }; |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + @debounce(500) |
| 65 | + private checkIgnoreSoon(): void { |
| 66 | + const queue = new Map(this.checkIgnoreQueue.entries()); |
| 67 | + this.checkIgnoreQueue.clear(); |
| 68 | + |
| 69 | + const ignored = this.repository.statusIgnored; |
| 70 | + const external = this.repository.statusExternal; |
| 71 | + |
| 72 | + const files = ignored.map(stat => |
| 73 | + path.join(this.repository.workspaceRoot, stat.path) |
| 74 | + ); |
| 75 | + |
| 76 | + files.push( |
| 77 | + ...external.map(stat => |
| 78 | + path.join(this.repository.workspaceRoot, stat.path) |
| 79 | + ) |
| 80 | + ); |
| 81 | + |
| 82 | + for (const [key, value] of queue.entries()) { |
| 83 | + value.resolve(files.some(file => isDescendant(file, key))); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +class SvnDecorationProvider implements DecorationProvider { |
| 89 | + private readonly _onDidChangeDecorations = new EventEmitter<Uri[]>(); |
| 90 | + readonly onDidChangeDecorations: Event<Uri[]> = this._onDidChangeDecorations |
| 91 | + .event; |
| 92 | + |
| 93 | + private disposables: Disposable[] = []; |
| 94 | + private decorations = new Map<string, DecorationData>(); |
| 95 | + |
| 96 | + constructor(private repository: Repository) { |
| 97 | + this.disposables.push( |
| 98 | + window.registerDecorationProvider(this), |
| 99 | + // repository.onDidRunOperation(this.onDidRunOperation, this) |
| 100 | + repository.onDidChangeStatus(this.onDidRunOperation, this) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + private onDidRunOperation(): void { |
| 105 | + let newDecorations = new Map<string, DecorationData>(); |
| 106 | + this.collectDecorationData(this.repository.changes, newDecorations); |
| 107 | + this.collectDecorationData(this.repository.unversioned, newDecorations); |
| 108 | + this.collectDecorationData(this.repository.conflicts, newDecorations); |
| 109 | + |
| 110 | + this.repository.changelists.forEach((group, changelist) => { |
| 111 | + this.collectDecorationData(group, newDecorations); |
| 112 | + }); |
| 113 | + |
| 114 | + let uris: Uri[] = []; |
| 115 | + newDecorations.forEach((value, uriString) => { |
| 116 | + if (this.decorations.has(uriString)) { |
| 117 | + this.decorations.delete(uriString); |
| 118 | + } else { |
| 119 | + uris.push(Uri.parse(uriString)); |
| 120 | + } |
| 121 | + }); |
| 122 | + this.decorations.forEach((value, uriString) => { |
| 123 | + uris.push(Uri.parse(uriString)); |
| 124 | + }); |
| 125 | + this.decorations = newDecorations; |
| 126 | + this._onDidChangeDecorations.fire(uris); |
| 127 | + } |
| 128 | + |
| 129 | + private collectDecorationData( |
| 130 | + group: SvnResourceGroup, |
| 131 | + bucket: Map<string, DecorationData> |
| 132 | + ): void { |
| 133 | + group.resourceStates.forEach(r => { |
| 134 | + if (r.resourceDecoration) { |
| 135 | + bucket.set(r.resourceUri.toString(), r.resourceDecoration); |
| 136 | + } |
| 137 | + }); |
| 138 | + } |
| 139 | + |
| 140 | + provideDecoration(uri: Uri): DecorationData | undefined { |
| 141 | + return this.decorations.get(uri.toString()); |
| 142 | + } |
| 143 | + |
| 144 | + dispose(): void { |
| 145 | + this.disposables.forEach(d => d.dispose()); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +export class SvnDecorations { |
| 150 | + private configListener: Disposable; |
| 151 | + private modelListener: Disposable[] = []; |
| 152 | + private providers = new Map<Repository, Disposable>(); |
| 153 | + |
| 154 | + constructor(private model: Model) { |
| 155 | + // this.configListener = workspace.onDidChangeConfiguration( |
| 156 | + // e => e.affectsConfiguration("svn.decorations.enabled") && this.update() |
| 157 | + // ); |
| 158 | + this.update(); |
| 159 | + } |
| 160 | + |
| 161 | + private update(): void { |
| 162 | + const enabled = workspace |
| 163 | + .getConfiguration() |
| 164 | + .get<boolean>("svn.decorations.enabled"); |
| 165 | + if (enabled) { |
| 166 | + this.enable(); |
| 167 | + } else { |
| 168 | + this.disable(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private enable(): void { |
| 173 | + this.modelListener = []; |
| 174 | + this.model.onDidOpenRepository( |
| 175 | + this.onDidOpenRepository, |
| 176 | + this, |
| 177 | + this.modelListener |
| 178 | + ); |
| 179 | + // this.model.onDidCloseRepository( |
| 180 | + // this.onDidCloseRepository, |
| 181 | + // this, |
| 182 | + // this.modelListener |
| 183 | + // ); |
| 184 | + this.model.repositories.forEach(this.onDidOpenRepository, this); |
| 185 | + } |
| 186 | + |
| 187 | + private disable(): void { |
| 188 | + this.modelListener.forEach(d => d.dispose()); |
| 189 | + this.providers.forEach(value => value.dispose()); |
| 190 | + this.providers.clear(); |
| 191 | + } |
| 192 | + |
| 193 | + private onDidOpenRepository(repository: Repository): void { |
| 194 | + const provider = new SvnDecorationProvider(repository); |
| 195 | + const ignoreProvider = new SvnIgnoreDecorationProvider(repository); |
| 196 | + this.providers.set(repository, Disposable.from(provider, ignoreProvider)); |
| 197 | + } |
| 198 | + |
| 199 | + private onDidCloseRepository(repository: Repository): void { |
| 200 | + const provider = this.providers.get(repository); |
| 201 | + if (provider) { |
| 202 | + provider.dispose(); |
| 203 | + this.providers.delete(repository); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + dispose(): void { |
| 208 | + this.configListener.dispose(); |
| 209 | + this.modelListener.forEach(d => d.dispose()); |
| 210 | + this.providers.forEach(value => value.dispose); |
| 211 | + this.providers.clear(); |
| 212 | + } |
| 213 | +} |
0 commit comments