forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.ts
More file actions
53 lines (47 loc) · 2.13 KB
/
helper.ts
File metadata and controls
53 lines (47 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { inject, injectable } from 'inversify';
import * as path from 'path';
import { Uri } from 'vscode';
import { ExecutionInfo, IConfigurationService, IFormattingSettings, Product } from '../common/types';
import { IServiceContainer } from '../ioc/types';
import { FormatterId, FormatterSettingsPropertyNames, IFormatterHelper } from './types';
@injectable()
export class FormatterHelper implements IFormatterHelper {
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) {}
public translateToId(formatter: Product): FormatterId {
switch (formatter) {
case Product.autopep8:
return 'autopep8';
case Product.black:
return 'black';
case Product.yapf:
return 'yapf';
default: {
throw new Error(`Unrecognized Formatter '${formatter}'`);
}
}
}
public getSettingsPropertyNames(formatter: Product): FormatterSettingsPropertyNames {
const id = this.translateToId(formatter);
return {
argsName: `${id}Args` as keyof IFormattingSettings,
pathName: `${id}Path` as keyof IFormattingSettings
};
}
public getExecutionInfo(formatter: Product, customArgs: string[], resource?: Uri): ExecutionInfo {
const settings = this.serviceContainer.get<IConfigurationService>(IConfigurationService).getSettings(resource);
const names = this.getSettingsPropertyNames(formatter);
const execPath = settings.formatting[names.pathName] as string;
let args: string[] = Array.isArray(settings.formatting[names.argsName])
? (settings.formatting[names.argsName] as string[])
: [];
args = args.concat(customArgs);
let moduleName: string | undefined;
// If path information is not available, then treat it as a module,
if (path.basename(execPath) === execPath) {
moduleName = execPath;
}
return { execPath, moduleName, args, product: formatter };
}
}