|
1 | 1 | import * as vscode from 'vscode'; |
2 | 2 | import * as _ from 'lodash'; |
| 3 | +import { IChildLogger } from '@vscode-logging/logger'; |
| 4 | +import { getClassLogger } from './logger/logger-wrapper'; |
| 5 | + |
3 | 6 |
|
4 | 7 | export class Contributors { |
5 | | - public static async getSnippet(contributerInfo: any) { |
6 | | - const contributorId = _.get(contributerInfo, "contributorId"); |
7 | | - const extension = Contributors.getContributorExtension(contributorId); |
| 8 | + private readonly logger: IChildLogger; |
| 9 | + |
| 10 | + constructor() { |
| 11 | + this.logger = getClassLogger("Contributors"); |
| 12 | + } |
| 13 | + |
| 14 | + public async getSnippet(contributorInfo: any) { |
| 15 | + const contributorId = _.get(contributorInfo, "contributorId"); |
| 16 | + const extension = this.getContributorExtension(contributorId); |
8 | 17 | if (extension) { |
9 | 18 | try { |
10 | 19 | const api = await this.getApiPromise(extension as vscode.Extension<any>); |
11 | | - const snippetContext = _.get(contributerInfo, "context"); |
| 20 | + const snippetContext = _.get(contributorInfo, "context"); |
12 | 21 | const snippets = api.getCodeSnippets(snippetContext); |
13 | | - const snippetName = _.get(contributerInfo, "snippetName"); |
| 22 | + const snippetName = _.get(contributorInfo, "snippetName"); |
14 | 23 | return snippets.get(snippetName); |
15 | 24 | } catch (error) { |
16 | 25 | const errorMessage = _.get(error, "stack", _.get(error, "message", error)); |
17 | | - console.error(errorMessage); |
18 | | - // TODO: Add Logger.error |
| 26 | + this.logger.error(`Could not get '${contributorId}' snippet`, errorMessage); |
19 | 27 | } |
20 | 28 | } |
21 | 29 | } |
22 | 30 |
|
23 | | - private static getApiPromise(extension: vscode.Extension<any>) { |
24 | | - return (extension.isActive ? extension.exports : extension.activate()); |
| 31 | + private getApiPromise(extension: vscode.Extension<any>): Thenable<any> { |
| 32 | + return (extension.isActive ? Promise.resolve(extension.exports) : extension.activate()); |
25 | 33 | } |
26 | 34 |
|
27 | | - private static getContributorExtension(contributorId: string) { |
| 35 | + private getContributorExtension(contributorId: string) { |
28 | 36 | return _.find(vscode.extensions.all, (extension: vscode.Extension<any>) => { |
29 | 37 | const extensionDependencies: string[] = _.get(extension, "packageJSON.extensionDependencies"); |
30 | 38 | if (_.includes(extensionDependencies, "saposs.code-snippet")) { |
31 | | - if (contributorId === Contributors.getExtensionId(extension)) { |
| 39 | + if (contributorId === this.getExtensionId(extension)) { |
32 | 40 | return extension; |
33 | 41 | } |
34 | 42 | } |
| 43 | + |
| 44 | + this.logger.warn(`Extension '${contributorId}' could not be found.`); |
35 | 45 | }); |
36 | 46 | } |
37 | 47 |
|
38 | | - private static getExtensionId(extension: vscode.Extension<any>) { |
| 48 | + private getExtensionId(extension: vscode.Extension<any>) { |
39 | 49 | const extensionName: string = _.get(extension, "packageJSON.name"); |
40 | 50 | const extensionPublisher: string = _.get(extension, "packageJSON.publisher"); |
41 | 51 | return `${extensionPublisher}.${extensionName}`; |
|
0 commit comments