Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic plumbing for Flutter Outline.
See #605.
- Loading branch information
Showing
5 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"use strict"; | ||
|
||
import * as vs from "vscode"; | ||
import * as as from "../analysis/analysis_server_types"; | ||
import { Analyzer } from "../analysis/analyzer"; | ||
import { isAnalyzable } from "../utils"; | ||
|
||
const DART_SHOW_FLUTTER_OUTLINE = "dart-code:showFlutterOutline"; | ||
|
||
export class FlutterOutlineProvider implements vs.TreeDataProvider<FlutterWidgetItem>, vs.Disposable { | ||
private subscriptions: vs.Disposable[] = []; | ||
private analyzer: Analyzer; | ||
private activeEditor: vs.TextEditor; | ||
private flutterOutline: as.FlutterOutlineNotification; | ||
private updateTimeout: NodeJS.Timer; | ||
private onDidChangeTreeDataEmitter: vs.EventEmitter<FlutterWidgetItem | undefined> = new vs.EventEmitter<FlutterWidgetItem | undefined>(); | ||
public readonly onDidChangeTreeData: vs.Event<FlutterWidgetItem | undefined> = this.onDidChangeTreeDataEmitter.event; | ||
|
||
constructor(analyzer: Analyzer) { | ||
this.analyzer = analyzer; | ||
|
||
this.analyzer.registerForFlutterOutline((n) => { | ||
if (this.activeEditor && n.file === this.activeEditor.document.fileName) { | ||
this.flutterOutline = n; | ||
// Delay this so if we're getting lots of updates we don't flicker. | ||
clearTimeout(this.updateTimeout); | ||
this.updateTimeout = setTimeout(() => this.update(), 500); | ||
} | ||
}); | ||
|
||
this.subscriptions.push(vs.window.onDidChangeActiveTextEditor((e) => this.setTrackingFile(e))); | ||
if (vs.window.activeTextEditor) | ||
this.setTrackingFile(vs.window.activeTextEditor); | ||
} | ||
|
||
private update() { | ||
if (!this.flutterOutline || !this.activeEditor || this.flutterOutline.file !== this.activeEditor.document.fileName) | ||
return; | ||
|
||
FlutterOutlineProvider.showTree(); | ||
|
||
// Do tree... | ||
} | ||
|
||
private setTrackingFile(editor: vs.TextEditor) { | ||
if (editor && isAnalyzable(editor.document)) { | ||
this.activeEditor = editor; | ||
this.flutterOutline = null; | ||
|
||
this.analyzer.forceNotificationsFor(editor.document.fileName); | ||
} else { | ||
FlutterOutlineProvider.hideTree(); | ||
this.activeEditor = null; | ||
} | ||
} | ||
|
||
public refresh(): void { | ||
this.onDidChangeTreeDataEmitter.fire(); | ||
} | ||
|
||
public getTreeItem(element: FlutterWidgetItem): vs.TreeItem { | ||
return element; | ||
} | ||
|
||
public getChildren(element?: FlutterWidgetItem): FlutterWidgetItem[] { | ||
return []; | ||
} | ||
|
||
private static setTreeVisible(visible: boolean) { | ||
vs.commands.executeCommand("setContext", DART_SHOW_FLUTTER_OUTLINE, visible); | ||
} | ||
|
||
public static showTree() { this.setTreeVisible(true); } | ||
public static hideTree() { this.setTreeVisible(false); } | ||
|
||
public dispose() { | ||
this.activeEditor = null; | ||
this.subscriptions.forEach((s) => s.dispose()); | ||
} | ||
} | ||
|
||
class FlutterWidgetItem extends vs.TreeItem { | ||
constructor( | ||
public readonly label: string, | ||
public readonly depPath: string, | ||
public readonly collapsibleState?: vs.TreeItemCollapsibleState, | ||
public readonly command?: vs.Command, | ||
) { | ||
super(label, collapsibleState); | ||
} | ||
|
||
public contextValue = "flutterWidget"; | ||
} |