|
| 1 | +import * as path from 'path'; |
| 2 | +import { |
| 3 | + commands, |
| 4 | + Event, |
| 5 | + EventEmitter, |
| 6 | + ProviderResult, |
| 7 | + ThemeIcon, |
| 8 | + TreeDataProvider, |
| 9 | + TreeItem, |
| 10 | + TreeItemCollapsibleState, |
| 11 | + Uri, |
| 12 | + ViewColumn, |
| 13 | + window, |
| 14 | + workspace, |
| 15 | +} from 'vscode'; |
| 16 | +import { RequestType } from 'vscode-languageclient'; |
| 17 | +import { ConnectionHandler } from '../handler'; |
| 18 | +import { reporter } from '../telemetry'; |
| 19 | + |
| 20 | +class PuppetfileDependencyItem extends TreeItem { |
| 21 | + constructor( |
| 22 | + public readonly name: string, |
| 23 | + public readonly version: string, |
| 24 | + public readonly start_line: number, |
| 25 | + public readonly end_line: number, |
| 26 | + public readonly collapsibleState: TreeItemCollapsibleState, |
| 27 | + public readonly children?: Array<[string, PuppetfileDependencyItem]>, |
| 28 | + ) { |
| 29 | + super(name, collapsibleState); |
| 30 | + if (children) { |
| 31 | + this.iconPath = ThemeIcon.Folder; |
| 32 | + } else { |
| 33 | + this.iconPath = new ThemeIcon('package'); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + get tooltip(): string { |
| 38 | + return `${this.name}-${this.version}`; |
| 39 | + } |
| 40 | + |
| 41 | + get description(): string { |
| 42 | + return this.version; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +class PuppetfileDependency { |
| 47 | + constructor( |
| 48 | + public readonly name: string, |
| 49 | + public readonly version: string, |
| 50 | + public readonly start_line: number, |
| 51 | + public readonly end_line: number, |
| 52 | + ) { |
| 53 | + // |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +interface PuppetfileDependencyResponse { |
| 58 | + dependencies: PuppetfileDependency[]; |
| 59 | + error: string[]; |
| 60 | +} |
| 61 | + |
| 62 | +export class PuppetfileProvider implements TreeDataProvider<PuppetfileDependencyItem> { |
| 63 | + private _onDidChangeTreeData: EventEmitter<PuppetfileDependencyItem | undefined> = new EventEmitter< |
| 64 | + PuppetfileDependencyItem | undefined |
| 65 | + >(); |
| 66 | + readonly onDidChangeTreeData: Event<PuppetfileDependencyItem | undefined>; |
| 67 | + |
| 68 | + constructor(protected handler: ConnectionHandler) { |
| 69 | + this.onDidChangeTreeData = this._onDidChangeTreeData.event; |
| 70 | + commands.registerCommand('puppet.refreshPuppetfileDependencies', () => { |
| 71 | + reporter.sendTelemetryEvent('puppet.refreshPuppetfileDependencies'); |
| 72 | + this.refresh(); |
| 73 | + }); |
| 74 | + commands.registerCommand('puppet.goToPuppetfileDefinition', (puppetModule: PuppetfileDependencyItem) => { |
| 75 | + reporter.sendTelemetryEvent('puppet.goToPuppetfileDefinition'); |
| 76 | + |
| 77 | + const workspaceFolder = workspace.workspaceFolders[0].uri; |
| 78 | + const puppetfile = path.join(workspaceFolder.fsPath, 'Puppetfile'); |
| 79 | + workspace.openTextDocument(puppetfile).then((doc) => { |
| 80 | + const line = doc.lineAt(+puppetModule.start_line); |
| 81 | + window.showTextDocument(doc, { |
| 82 | + preserveFocus: true, |
| 83 | + preview: false, |
| 84 | + selection: line.range, |
| 85 | + viewColumn: ViewColumn.Active, |
| 86 | + }); |
| 87 | + }); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + refresh(): void { |
| 92 | + this._onDidChangeTreeData.fire(null); |
| 93 | + } |
| 94 | + |
| 95 | + getTreeItem(element: PuppetfileDependencyItem): TreeItem | Thenable<PuppetfileDependencyItem> { |
| 96 | + return element; |
| 97 | + } |
| 98 | + |
| 99 | + getChildren(element?: PuppetfileDependencyItem): Promise<PuppetfileDependencyItem[]> { |
| 100 | + if (element) { |
| 101 | + return Promise.resolve(element.children.map((e) => e[1])); |
| 102 | + } else { |
| 103 | + return this.getPuppetfileDependenciesFromLanguageServer(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private async getPuppetfileDependenciesFromLanguageServer(): Promise<PuppetfileDependencyItem[]> { |
| 108 | + await this.handler.languageClient.onReady(); |
| 109 | + |
| 110 | + const fileUri = Uri.file(path.join(workspace.workspaceFolders[0].uri.fsPath, 'Puppetfile')); |
| 111 | + /* |
| 112 | + We use openTextDocument here because we need to parse whether or not a user has opened a |
| 113 | + Puppetfile or not. This triggers onDidOpen notification which sends the content of the Puppetfile |
| 114 | + to the Puppet Language Server which caches the content for puppetfile-resolver to parse |
| 115 | + */ |
| 116 | + return workspace.openTextDocument(fileUri).then(async () => { |
| 117 | + const results = await this.handler.languageClient.sendRequest( |
| 118 | + new RequestType<never, PuppetfileDependencyResponse, void, void>('puppetfile/getDependencies'), |
| 119 | + { |
| 120 | + uri: fileUri.toString(), |
| 121 | + }, |
| 122 | + ); |
| 123 | + |
| 124 | + reporter.sendTelemetryEvent('puppetfileView'); |
| 125 | + |
| 126 | + if (results.error) { |
| 127 | + window.showErrorMessage(`${results.error}`); |
| 128 | + } |
| 129 | + |
| 130 | + const list = results.dependencies.map((d) => { |
| 131 | + return new PuppetfileDependencyItem(d.name, d.version, d.start_line, d.end_line, TreeItemCollapsibleState.None); |
| 132 | + }); |
| 133 | + |
| 134 | + return list; |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 139 | + getParent?(element: PuppetfileDependencyItem): ProviderResult<PuppetfileDependencyItem> { |
| 140 | + throw new Error('Method not implemented.'); |
| 141 | + } |
| 142 | +} |
0 commit comments