This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
/
VscodeExtensionConfig.ts
373 lines (337 loc) · 11 KB
/
VscodeExtensionConfig.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import { injectable } from 'inversify';
import { TypescriptGenerationOptions } from 'typescript-parser';
import { workspace, WorkspaceConfiguration } from 'vscode';
import { ExtensionConfig, ResolverConfig } from '../common/config';
import { CodeOutlineConfig } from '../common/config/CodeOutlineConfig';
import { ResolverMode } from '../common/enums';
import { ImportGroup, ImportGroupSetting, ImportGroupSettingParser, RemainImportGroup } from './import-grouping';
const sectionKey = 'typescriptHero';
/**
* Configuration class for TypeScript Hero
* Contains all exposed config endpoints.
*
* @export
* @class VscodeExtensionConfig
*/
@injectable()
export class VscodeExtensionConfig implements ExtensionConfig {
private resolverConfig: ResolverConfig = new VscodeResolverConfig();
private codeOutlineConfig: CodeOutlineConfig = new VscodeCodeOutlineConfig();
private get workspaceSection(): WorkspaceConfiguration {
return workspace.getConfiguration(sectionKey);
}
/**
* The actual log level.
*
* @readonly
* @type {string}
* @memberof VscodeExtensionConfig
*/
public get verbosity(): string {
return this.workspaceSection.get<string>('verbosity') || 'Warning';
}
/**
* Configuration object for the resolver extension.
*
* @readonly
* @type {ResolverConfig}
* @memberof VscodeExtensionConfig
*/
public get resolver(): ResolverConfig {
return this.resolverConfig;
}
/**
* Configuration object for the code outline extension.
*
* @readonly
* @type {CodeOutlineConfig}
* @memberof VscodeExtensionConfig
*/
public get codeOutline(): CodeOutlineConfig {
return this.codeOutlineConfig;
}
/**
* Completion sort mode
*
* @readonly
* @type {'default'|'bottom'}
* @memberof VscodeExtensionConfig
*/
public get completionSortMode(): 'default' | 'bottom' {
return this.workspaceSection.get<'default' | 'bottom'>('completionSortMode') || 'default';
}
}
/**
* Configuration class for the resolver extension.
*
* @class VscodeResolverConfig
*/
class VscodeResolverConfig implements ResolverConfig {
private get workspaceSection(): WorkspaceConfiguration {
return workspace.getConfiguration(sectionKey);
}
/**
* Defines, if there should be a space between the brace and the import specifiers.
* {Symbol} vs { Symbol }
*
* @readonly
* @type {boolean}
* @memberof VscodeResolverConfig
*/
public get insertSpaceBeforeAndAfterImportBraces(): boolean {
const value = this.workspaceSection.get<boolean>('resolver.insertSpaceBeforeAndAfterImportBraces');
return value !== undefined ? value : true;
}
/**
* Defines, if there should be a semicolon at the end of a statement.
* import Symbol from 'symbol' vs import Symbol from 'symbol';
*
* @readonly
* @type {boolean}
* @memberof VscodeResolverConfig
*/
public get insertSemicolons(): boolean {
const value = this.workspaceSection.get<boolean>('resolver.insertSemicolons');
return value !== undefined ? value : true;
}
/**
* Defines the quote style (' or ").
*
* @readonly
* @type {string}
* @memberof VscodeResolverConfig
*/
public get stringQuoteStyle(): string {
return this.workspaceSection.get<string>('resolver.stringQuoteStyle') || `'`;
}
/**
* Array of string that are excluded from indexing (e.g. build, out, node_modules).
* If those parts are found after the workspace path is striped away, the file is ignored.
*
* @readonly
* @type {string[]}
* @memberof VscodeResolverConfig
*/
public get ignorePatterns(): string[] {
return this.workspaceSection.get<string[]>('resolver.ignorePatterns') || [
'build',
'out',
'dist',
];
}
/**
* A length number after which the import is transformed into a multiline import.
*
* @readonly
* @type {number}
* @memberof VscodeResolverConfig
*/
public get multiLineWrapThreshold(): number {
return this.workspaceSection.get<number>('resolver.multiLineWrapThreshold') || 125;
}
/**
* If a multiline named import should contain the last trailing comma.
*
* @readonly
* @type {boolean}
* @memberof VscodeResolverConfig
*
* @example
* import {
* Foo,
* Bar, <<
* } from 'whatever';
*/
public get multiLineTrailingComma(): boolean {
const value = this.workspaceSection.get<boolean>('resolver.multiLineTrailingComma');
return value !== undefined ? value : true;
}
/**
* Defines, if sorting is obligatory during organize imports
*
* @readonly
* @type {boolean}
* @memberof ResolverConfig
*/
public get disableImportSorting(): boolean {
const value = this.workspaceSection.get<boolean>('resolver.disableImportsSorting');
return value !== undefined ? value : false;
}
/**
* Defines, if removal unsed is obligatory during organize imports
*
* @readonly
* @type {boolean}
* @memberof ResolverConfig
*/
public get disableImportRemovalOnOrganize(): boolean {
const value = this.workspaceSection.get<boolean>('resolver.disableImportRemovalOnOrganize');
return value !== undefined ? value : false;
}
/**
* Returns the tab size that is configured in vscode.
*
* @readonly
* @type {number}
* @memberof VscodeResolverConfig
*/
public get tabSize(): number {
return workspace.getConfiguration().get<number>('editor.tabSize') || 4;
}
/**
* Returns the list of imports that should be ignored during organize import feature.
*
* @readonly
* @type {string[]}
* @memberof VscodeResolverConfig
*/
public get ignoreImportsForOrganize(): string[] {
return this.workspaceSection.get<string[]>('resolver.ignoreImportsForOrganize') || [];
}
/**
* Returns the configured import groups. On a parsing error, the default is used.
*
* @type {ImportGroup[]}
* @memberof VscodeResolverConfig
*/
public get importGroups(): ImportGroup[] {
const groups = this.workspaceSection.get<ImportGroupSetting[]>('resolver.importGroups');
let importGroups: ImportGroup[] = [];
try {
if (groups) {
importGroups = groups.map(g => ImportGroupSettingParser.parseSetting(g));
} else {
importGroups = ImportGroupSettingParser.default;
}
} catch (e) {
importGroups = ImportGroupSettingParser.default;
}
if (!importGroups.some(i => i instanceof RemainImportGroup)) {
importGroups.push(new RemainImportGroup());
}
return importGroups;
}
/**
* All information that are needed to print an import.
*
* @readonly
* @type {TypescriptGenerationOptions}
* @memberof VscodeResolverConfig
*/
public get generationOptions(): TypescriptGenerationOptions {
return {
eol: this.insertSemicolons ? ';' : '',
multiLineWrapThreshold: this.multiLineWrapThreshold,
multiLineTrailingComma: this.multiLineTrailingComma,
spaceBraces: this.insertSpaceBeforeAndAfterImportBraces,
stringQuoteStyle: this.stringQuoteStyle,
tabSize: this.tabSize,
};
}
/**
* Current mode of the resolver.
*
* @readonly
* @type {ResolverMode}
* @memberof VscodeResolverConfig
*/
public get resolverMode(): ResolverMode {
const mode = this.workspaceSection.get<string>('resolver.resolverMode', 'TypeScript');
return ResolverMode[mode] || ResolverMode.TypeScript;
}
/**
* Returns a list of file globs for the actual set resolver mode.
*
* @example `TypeScript`
* Will return: ['\*\*\/\*.ts', '\*\*\/\*.tsx']
*
* @example `ES6`
* Will return: ['\*\*\/\*.js', '\*\*\/\*.jsx']
*
* @type {string[]}
* @memberof VscodeResolverConfig
*/
public get resolverModeFileGlobs(): string[] {
const mode = this.resolverMode;
const globs: string[] = [];
if (mode === ResolverMode.TypeScript || mode === ResolverMode.Both) {
globs.push('**/*.ts');
globs.push('**/*.tsx');
}
if (mode === ResolverMode.ES6 || mode === ResolverMode.Both) {
globs.push('**/*.js');
globs.push('**/*.jsx');
}
return globs;
}
/**
* Returns a list of usable languages for the set resolver mode.
*
* @example `TypeScript`
* Will return: ['typescript', 'typescriptreact']
*
* @readonly
* @type {string[]}
* @memberof VscodeResolverConfig
*/
public get resolverModeLanguages(): string[] {
const mode = this.resolverMode;
const languages: string[] = [];
if (mode === ResolverMode.TypeScript || mode === ResolverMode.Both) {
languages.push('typescript');
languages.push('typescriptreact');
}
if (mode === ResolverMode.ES6 || mode === ResolverMode.Both) {
languages.push('javascript');
languages.push('javascriptreact');
}
return languages;
}
/**
* Defines if typescript hero tries to organize your imports of a
* file as soon as the file would be saved.
* Is a combination between the editor.formatOnSave and the resolver settings.
*
* @readonly
* @type {boolean}
* @memberof VscodeResolverConfig
*/
public get organizeOnSave(): boolean {
const typescriptHeroValue = this.workspaceSection.get<boolean>('resolver.organizeOnSave', true);
const editorValue = workspace.getConfiguration().get('editor.formatOnSave', false);
return typescriptHeroValue && editorValue;
}
/**
* Defines if typescript hero should ask the user for default specifiers or duplicate specifier aliases.
* If true, tsh does ask the user.
*
* @readonly
* @type {boolean}
* @memberof VscodeResolverConfig
*/
public get promptForSpecifiers(): boolean {
return this.workspaceSection.get<boolean>('resolver.promptForSpecifiers', false);
}
}
/**
* Configuration interface for the code outline feature.
*
* @class VscodeCodeOutlineConfig
* @implements {CodeOutlineConfig}
*/
class VscodeCodeOutlineConfig implements CodeOutlineConfig {
private get workspaceSection(): WorkspaceConfiguration {
return workspace.getConfiguration(sectionKey);
}
/**
* Defined if the code outline feature is enabled or not.
*
* @readonly
* @type {boolean}
* @memberof VscodeCodeOutlineConfig
*/
public get outlineEnabled(): boolean {
const value = this.workspaceSection.get<boolean>('codeOutline.enabled');
return value !== undefined ? value : true;
}
}