| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Logger} from 'isl-server/src/logger'; |
| b69ab31 | | | 9 | import type {Disposable, Event} from 'vscode'; |
| b69ab31 | | | 10 | import type {SaplingResourceGroup, VSCodeRepo} from './VSCodeRepo'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import { |
| b69ab31 | | | 13 | EventEmitter, |
| b69ab31 | | | 14 | ThemeIcon, |
| b69ab31 | | | 15 | Uri, |
| b69ab31 | | | 16 | window, |
| b69ab31 | | | 17 | type FileDecoration, |
| b69ab31 | | | 18 | type FileDecorationProvider, |
| b69ab31 | | | 19 | } from 'vscode'; |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | export default class SaplingFileDecorationProvider implements FileDecorationProvider { |
| b69ab31 | | | 22 | private readonly _onDidChangeDecorations = new EventEmitter<Uri[]>(); |
| b69ab31 | | | 23 | readonly onDidChangeFileDecorations: Event<Uri[]> = this._onDidChangeDecorations.event; |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | private disposables: Disposable[] = []; |
| b69ab31 | | | 26 | private decorations = new Map<string, FileDecoration>(); |
| b69ab31 | | | 27 | |
| b69ab31 | | | 28 | constructor( |
| b69ab31 | | | 29 | private repository: VSCodeRepo, |
| b69ab31 | | | 30 | private logger: Logger, |
| b69ab31 | | | 31 | ) { |
| b69ab31 | | | 32 | this.disposables.push( |
| b69ab31 | | | 33 | window?.registerFileDecorationProvider?.(this), |
| b69ab31 | | | 34 | repository.repo.subscribeToUncommittedChanges(this.onDidRunStatus.bind(this)), |
| b69ab31 | | | 35 | repository.repo.onChangeConflictState(this.onDidRunStatus.bind(this)), |
| b69ab31 | | | 36 | ); |
| b69ab31 | | | 37 | this.onDidRunStatus(); |
| b69ab31 | | | 38 | } |
| b69ab31 | | | 39 | |
| b69ab31 | | | 40 | private onDidRunStatus(): void { |
| b69ab31 | | | 41 | const newDecorations = new Map<string, FileDecoration>(); |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | const resourceGroups = this.repository.getResourceGroups() ?? {}; |
| b69ab31 | | | 44 | for (const key of Object.keys(resourceGroups) as (keyof typeof resourceGroups)[]) { |
| b69ab31 | | | 45 | this.collectDecorationData(resourceGroups[key], newDecorations); |
| b69ab31 | | | 46 | } |
| b69ab31 | | | 47 | |
| b69ab31 | | | 48 | const uris = new Set([...this.decorations.keys()].concat([...newDecorations.keys()])); |
| b69ab31 | | | 49 | this.decorations = newDecorations; |
| b69ab31 | | | 50 | this._onDidChangeDecorations.fire([...uris.values()].map(value => Uri.parse(value, true))); |
| b69ab31 | | | 51 | } |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | private collectDecorationData( |
| b69ab31 | | | 54 | group: SaplingResourceGroup, |
| b69ab31 | | | 55 | bucket: Map<string, FileDecoration>, |
| b69ab31 | | | 56 | ): void { |
| b69ab31 | | | 57 | for (const r of group.resourceStates) { |
| b69ab31 | | | 58 | const decoration = r.decorations; |
| b69ab31 | | | 59 | if (decoration) { |
| b69ab31 | | | 60 | bucket.set(r.resourceUri.toString(), { |
| b69ab31 | | | 61 | badge: r.status, |
| b69ab31 | | | 62 | color: decoration.iconPath instanceof ThemeIcon ? decoration.iconPath.color : undefined, |
| b69ab31 | | | 63 | }); |
| b69ab31 | | | 64 | } |
| b69ab31 | | | 65 | } |
| b69ab31 | | | 66 | } |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | provideFileDecoration(uri: Uri): FileDecoration | undefined { |
| b69ab31 | | | 69 | return this.decorations.get(uri.toString()); |
| b69ab31 | | | 70 | } |
| b69ab31 | | | 71 | |
| b69ab31 | | | 72 | dispose(): void { |
| b69ab31 | | | 73 | this.disposables.forEach(d => d?.dispose()); |
| b69ab31 | | | 74 | } |
| b69ab31 | | | 75 | } |