|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +import * as vs from "vscode"; |
| 4 | +import * as as from "../analysis/analysis_server_types"; |
| 5 | +import { Analyzer } from "../analysis/analyzer"; |
| 6 | +import { isAnalyzable } from "../utils"; |
| 7 | + |
| 8 | +const DART_SHOW_FLUTTER_OUTLINE = "dart-code:showFlutterOutline"; |
| 9 | + |
| 10 | +export class FlutterOutlineProvider implements vs.TreeDataProvider<FlutterWidgetItem>, vs.Disposable { |
| 11 | + private subscriptions: vs.Disposable[] = []; |
| 12 | + private analyzer: Analyzer; |
| 13 | + private activeEditor: vs.TextEditor; |
| 14 | + private flutterOutline: as.FlutterOutlineNotification; |
| 15 | + private updateTimeout: NodeJS.Timer; |
| 16 | + private onDidChangeTreeDataEmitter: vs.EventEmitter<FlutterWidgetItem | undefined> = new vs.EventEmitter<FlutterWidgetItem | undefined>(); |
| 17 | + public readonly onDidChangeTreeData: vs.Event<FlutterWidgetItem | undefined> = this.onDidChangeTreeDataEmitter.event; |
| 18 | + |
| 19 | + constructor(analyzer: Analyzer) { |
| 20 | + this.analyzer = analyzer; |
| 21 | + |
| 22 | + this.analyzer.registerForFlutterOutline((n) => { |
| 23 | + if (this.activeEditor && n.file === this.activeEditor.document.fileName) { |
| 24 | + this.flutterOutline = n; |
| 25 | + // Delay this so if we're getting lots of updates we don't flicker. |
| 26 | + clearTimeout(this.updateTimeout); |
| 27 | + this.updateTimeout = setTimeout(() => this.update(), 500); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + this.subscriptions.push(vs.window.onDidChangeActiveTextEditor((e) => this.setTrackingFile(e))); |
| 32 | + if (vs.window.activeTextEditor) |
| 33 | + this.setTrackingFile(vs.window.activeTextEditor); |
| 34 | + } |
| 35 | + |
| 36 | + private update() { |
| 37 | + if (!this.flutterOutline || !this.activeEditor || this.flutterOutline.file !== this.activeEditor.document.fileName) |
| 38 | + return; |
| 39 | + |
| 40 | + FlutterOutlineProvider.showTree(); |
| 41 | + |
| 42 | + // Do tree... |
| 43 | + } |
| 44 | + |
| 45 | + private setTrackingFile(editor: vs.TextEditor) { |
| 46 | + if (editor && isAnalyzable(editor.document)) { |
| 47 | + this.activeEditor = editor; |
| 48 | + this.flutterOutline = null; |
| 49 | + |
| 50 | + this.analyzer.forceNotificationsFor(editor.document.fileName); |
| 51 | + } else { |
| 52 | + FlutterOutlineProvider.hideTree(); |
| 53 | + this.activeEditor = null; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public refresh(): void { |
| 58 | + this.onDidChangeTreeDataEmitter.fire(); |
| 59 | + } |
| 60 | + |
| 61 | + public getTreeItem(element: FlutterWidgetItem): vs.TreeItem { |
| 62 | + return element; |
| 63 | + } |
| 64 | + |
| 65 | + public getChildren(element?: FlutterWidgetItem): FlutterWidgetItem[] { |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + private static setTreeVisible(visible: boolean) { |
| 70 | + vs.commands.executeCommand("setContext", DART_SHOW_FLUTTER_OUTLINE, visible); |
| 71 | + } |
| 72 | + |
| 73 | + public static showTree() { this.setTreeVisible(true); } |
| 74 | + public static hideTree() { this.setTreeVisible(false); } |
| 75 | + |
| 76 | + public dispose() { |
| 77 | + this.activeEditor = null; |
| 78 | + this.subscriptions.forEach((s) => s.dispose()); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +class FlutterWidgetItem extends vs.TreeItem { |
| 83 | + constructor( |
| 84 | + public readonly label: string, |
| 85 | + public readonly depPath: string, |
| 86 | + public readonly collapsibleState?: vs.TreeItemCollapsibleState, |
| 87 | + public readonly command?: vs.Command, |
| 88 | + ) { |
| 89 | + super(label, collapsibleState); |
| 90 | + } |
| 91 | + |
| 92 | + public contextValue = "flutterWidget"; |
| 93 | +} |
0 commit comments