|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import { getLocation } from 'jsonc-parser'; |
| 3 | +import { IFeature } from '../feature'; |
| 4 | +import { ILogger } from '../logging'; |
| 5 | + |
| 6 | +export class PuppetModuleHoverFeature implements IFeature { |
| 7 | + constructor(public context: vscode.ExtensionContext, public logger: ILogger) { |
| 8 | + let selector = [{ language: 'json', scheme: '*', pattern: '**/metadata.json' }]; |
| 9 | + context.subscriptions.push(vscode.languages.registerHoverProvider(selector, new PuppetModuleHoverProvider(logger))); |
| 10 | + } |
| 11 | + |
| 12 | + dispose() {} |
| 13 | +} |
| 14 | + |
| 15 | +export class PuppetModuleHoverProvider implements vscode.HoverProvider { |
| 16 | + constructor(public logger: ILogger) {} |
| 17 | + |
| 18 | + provideHover( |
| 19 | + document: vscode.TextDocument, |
| 20 | + position: vscode.Position, |
| 21 | + token: vscode.CancellationToken |
| 22 | + ): Thenable<vscode.Hover> | null { |
| 23 | + const offset = document.offsetAt(position); |
| 24 | + const location = getLocation(document.getText(), offset); |
| 25 | + |
| 26 | + if (location.isAtPropertyKey) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (location.path[0] !== 'dependencies') { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + if (location.path[2] !== 'name') { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const range = document.getWordRangeAtPosition(position); |
| 39 | + const word = document.getText(range); |
| 40 | + |
| 41 | + this.logger.debug('Metadata hover info found ' + word + ' module'); |
| 42 | + |
| 43 | + let name = word |
| 44 | + .replace('"', '') |
| 45 | + .replace('"', '') |
| 46 | + .replace('/', '-'); |
| 47 | + |
| 48 | + return this.getModuleInfo(name).then(function(result: any) { |
| 49 | + let msg: string[] = []; |
| 50 | + msg.push(`### ${result.slug}`); |
| 51 | + |
| 52 | + const releaseDate = new Date(result.releases[0].created_at); |
| 53 | + const dateformat = require('dateformat'); |
| 54 | + msg.push(`\nLatest version: ${result.releases[0].version} (${dateformat(releaseDate, 'dS mmmm yyyy')})`); |
| 55 | + |
| 56 | + if (result.endorsement !== null) { |
| 57 | + const endorsementCapitalized = result.endorsement.charAt(0).toUpperCase() + result.endorsement.slice(1); |
| 58 | + msg.push(`\nEndorsement: ${endorsementCapitalized}`); |
| 59 | + } |
| 60 | + |
| 61 | + msg.push(`\nOwner: ${result.owner.slug}`); |
| 62 | + |
| 63 | + const forgeUri = `https://forge.puppet.com/${result.owner.username}/${result.name}`; |
| 64 | + msg.push(`\nForge: \[${forgeUri}\](${forgeUri})\n`); |
| 65 | + |
| 66 | + if (result.homepage_url !== null) { |
| 67 | + msg.push(`\nProject: \[${result.homepage_url}\](${result.homepage_url})\n`); |
| 68 | + } |
| 69 | + |
| 70 | + let md = msg.join('\n'); |
| 71 | + |
| 72 | + return Promise.resolve(new vscode.Hover(new vscode.MarkdownString(md), range)); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + private getModuleInfo(name: string) { |
| 77 | + var options = { |
| 78 | + url: `https://forgeapi.puppet.com/v3/modules/${name}?exclude_fields=readme%20changelog%20license%20reference` |
| 79 | + }; |
| 80 | + return new Promise(function(resolve, reject) { |
| 81 | + const request = require('request'); |
| 82 | + request.get(options, function(err, resp, body) { |
| 83 | + if (err) { |
| 84 | + reject(err); |
| 85 | + } else { |
| 86 | + resolve(JSON.parse(body)); |
| 87 | + } |
| 88 | + }); |
| 89 | + }); |
| 90 | + } |
| 91 | +} |
0 commit comments