|
| 1 | +import { |
| 2 | + TreeItem, |
| 3 | + TreeItemCollapsibleState, |
| 4 | + ThemeIcon, |
| 5 | + TreeDataProvider, |
| 6 | + Event, |
| 7 | + EventEmitter, |
| 8 | + commands, |
| 9 | + ProviderResult, |
| 10 | + window, |
| 11 | +} from 'vscode'; |
| 12 | +import { RequestType, RequestType0 } from 'vscode-languageclient'; |
| 13 | +import { ConnectionHandler } from '../handler'; |
| 14 | +import { PuppetVersionRequest } from '../messages'; |
| 15 | + |
| 16 | +class PuppetFact extends TreeItem { |
| 17 | + constructor( |
| 18 | + public readonly label: string, |
| 19 | + private value: string, |
| 20 | + public readonly collapsibleState: TreeItemCollapsibleState, |
| 21 | + public readonly children?: Array<[string, PuppetFact]> |
| 22 | + ) { |
| 23 | + super(label, collapsibleState); |
| 24 | + if (children) { |
| 25 | + this.iconPath = ThemeIcon.Folder; |
| 26 | + } else { |
| 27 | + this.iconPath = ThemeIcon.File; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + get tooltip(): string { |
| 32 | + return `${this.label}-${this.value}`; |
| 33 | + } |
| 34 | + |
| 35 | + get description(): string { |
| 36 | + return this.value; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +interface PuppetFactResponse { |
| 41 | + facts: string; |
| 42 | + error: string; |
| 43 | +} |
| 44 | + |
| 45 | +export class PuppetFactsProvider implements TreeDataProvider<PuppetFact> { |
| 46 | + private elements: Array<[string, PuppetFact]> = []; |
| 47 | + private _onDidChangeTreeData: EventEmitter<PuppetFact | undefined> = new EventEmitter<PuppetFact | undefined>(); |
| 48 | + readonly onDidChangeTreeData: Event<PuppetFact | undefined> = this._onDidChangeTreeData.event; |
| 49 | + |
| 50 | + constructor(protected handler: ConnectionHandler) { |
| 51 | + commands.registerCommand('puppet.refreshFacts', () => this.refresh()); |
| 52 | + } |
| 53 | + |
| 54 | + refresh(): void { |
| 55 | + this._onDidChangeTreeData.fire(); |
| 56 | + } |
| 57 | + |
| 58 | + getTreeItem(element: PuppetFact): TreeItem | Thenable<PuppetFact> { |
| 59 | + return element; |
| 60 | + } |
| 61 | + |
| 62 | + getChildren(element?: PuppetFact): Promise<PuppetFact[]> { |
| 63 | + if (element) { |
| 64 | + return Promise.resolve(element.children.map((e) => e[1])); |
| 65 | + } else { |
| 66 | + return this.getFactsFromLanguageServer(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private async getFactsFromLanguageServer(): Promise<PuppetFact[]> { |
| 71 | + /* |
| 72 | + this is problematic because we both store this and return the value |
| 73 | + but this allows us to cache the info for quick expands of the node. |
| 74 | + if we didn't cache, we would have to call out for each expand and getting |
| 75 | + facts is slow. |
| 76 | + */ |
| 77 | + await this.handler.languageClient.onReady(); |
| 78 | + const results = await this.handler.languageClient.sendRequest( |
| 79 | + new RequestType0<PuppetFactResponse, void, void>('puppet/getFacts') |
| 80 | + ); |
| 81 | + this.elements = this.toList(results.facts); |
| 82 | + return this.elements.map((e) => e[1]); |
| 83 | + } |
| 84 | + |
| 85 | + getParent?(element: PuppetFact): ProviderResult<PuppetFact> { |
| 86 | + throw new Error('Method not implemented.'); |
| 87 | + } |
| 88 | + |
| 89 | + toList(data: any): Array<[string, PuppetFact]> { |
| 90 | + let things: Array<[string, PuppetFact]> = []; |
| 91 | + |
| 92 | + for (let key of Object.keys(data)) { |
| 93 | + let value = data[key]; |
| 94 | + if (Object.prototype.toString.call(value) === '[object Object]') { |
| 95 | + let children = this.toList(value); |
| 96 | + const item = new PuppetFact(key, value, TreeItemCollapsibleState.Collapsed, children); |
| 97 | + things.push([key, item]); |
| 98 | + } else { |
| 99 | + things.push([key, new PuppetFact(key, value.toString(), TreeItemCollapsibleState.None)]); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return things; |
| 104 | + } |
| 105 | +} |
0 commit comments