@@ -56,6 +56,10 @@ export class TerraformTipsProvider implements CompletionItemProvider {
56
56
position : Position ;
57
57
token : CancellationToken ;
58
58
resourceType : string | null = null ;
59
+ private extensionPath : string ;
60
+ constructor ( extensionPath : string ) {
61
+ this . extensionPath = extensionPath ;
62
+ }
59
63
60
64
public async provideCompletionItems ( document : TextDocument , position : Position , token : CancellationToken , context : TerraformCompletionContext ) : Promise < CompletionItem [ ] > {
61
65
this . document = document ;
@@ -108,7 +112,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
108
112
let resourceName = parts [ 1 ] ;
109
113
try {
110
114
// async load resource config
111
- const tips = await loadResource ( ) ;
115
+ const tips = await loadResource ( this . extensionPath ) ;
112
116
const resources = tips . resource ;
113
117
let attrs = resources [ resourceType ] . attrs ;
114
118
let result = _ . map ( attrs , o => {
@@ -146,7 +150,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
146
150
// load options
147
151
try {
148
152
// async load resource config
149
- const tips = await loadResource ( ) ;
153
+ const tips = await loadResource ( this . extensionPath ) ;
150
154
const name = lineBeforeEqualSign ;
151
155
const resources = tips . resource ;
152
156
const argStrs = this . findArgByName ( resources [ this . resourceType ] . args , name ) ;
@@ -155,7 +159,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
155
159
this . resourceType = "" ;
156
160
return ( options ) . length ? options : [ ] ;
157
161
} catch ( error ) {
158
- console . error ( `Can not load resource from json. error:[${ error } ]` ) ;
162
+ console . error ( `Can not load resource from json when loading options . error:[${ error } ]` ) ;
159
163
}
160
164
}
161
165
this . resourceType = "" ;
@@ -173,12 +177,12 @@ export class TerraformTipsProvider implements CompletionItemProvider {
173
177
const resourceType = this . getResourceTypeFromLine ( line ) ;
174
178
try {
175
179
// async load resource config
176
- const tips = await loadResource ( ) ;
180
+ const tips = await loadResource ( this . extensionPath ) ;
177
181
const resources = tips . resource ;
178
182
const ret = this . getItemsForArgs ( resources [ resourceType ] . args , resourceType ) ;
179
183
return ret ;
180
184
} catch ( error ) {
181
- console . error ( `Can not load resource from json. error:[${ error } ]` ) ;
185
+ console . error ( `Can not load resource from json when loading argument . error:[${ error } ]` ) ;
182
186
return [ ] ;
183
187
}
184
188
}
@@ -298,7 +302,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
298
302
// handle resource
299
303
try {
300
304
// async load resource config
301
- const tips = await loadResource ( ) ;
305
+ const tips = await loadResource ( this . extensionPath ) ;
302
306
const resources = tips . resource ;
303
307
let possibleResources = _ . filter ( _ . keys ( resources ) , k => {
304
308
if ( regex . test ( k ) ) {
@@ -308,7 +312,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
308
312
} ) ;
309
313
return possibleResources ;
310
314
} catch ( error ) {
311
- console . error ( `Can not load resource from json. error:[${ error } ]` ) ;
315
+ console . error ( `Can not load resource from json when loading resource type . error:[${ error } ]` ) ;
312
316
return [ ] ;
313
317
}
314
318
}
@@ -357,7 +361,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
357
361
}
358
362
359
363
const changes = event . contentChanges [ 0 ] ;
360
- if ( changes . text === TIPS_OPTIONS_TRIGGER_CHARACTER ) {
364
+ if ( changes && changes . text === TIPS_OPTIONS_TRIGGER_CHARACTER ) {
361
365
const position = activeEditor . selection . active ;
362
366
const resourceType = this . findResourceType ( event . document , position ) ;
363
367
@@ -370,9 +374,15 @@ export class TerraformTipsProvider implements CompletionItemProvider {
370
374
}
371
375
372
376
async function sortJsonFiles ( dir : string ) {
373
- const files = fs . readdirSync ( dir ) ;
374
- const jsonFiles = files . filter ( file => path . extname ( file ) === '.json' && versionPattern . test ( file ) ) ;
375
- // const jsonFiles: string[] = ["v1.81.50.json", "v1.81.54.json"]; // debug
377
+ let jsonFiles : string [ ] ;
378
+ try {
379
+ const files = fs . readdirSync ( dir ) ;
380
+ jsonFiles = files . filter ( file => path . extname ( file ) === '.json' && versionPattern . test ( file ) ) ;
381
+ // const jsonFiles: string[] = ["v1.81.50.json", "v1.81.54.json"]; // debug data
382
+ } catch ( error ) {
383
+ console . error ( `read dir failed. error:[${ error } ]` ) ;
384
+ return null ;
385
+ }
376
386
377
387
// import files
378
388
const versions = await Promise . all ( jsonFiles . map ( async file => {
@@ -388,11 +398,12 @@ async function sortJsonFiles(dir: string) {
388
398
389
399
// sort with version desc
390
400
versions . sort ( ( a , b ) => compareVersions ( b . version , a . version ) ) ;
391
-
392
401
return versions ;
393
402
}
394
403
395
404
function compareVersions ( a , b ) {
405
+ if ( a && ! b ) { return 1 ; }
406
+ if ( ! a && b ) { return - 1 ; }
396
407
if ( a === 'latest' ) { return 1 ; }
397
408
if ( b === 'latest' ) { return - 1 ; }
398
409
const aParts = a . split ( '.' ) . map ( Number ) ;
@@ -410,7 +421,7 @@ function compareVersions(a, b) {
410
421
}
411
422
412
423
// load resource config from json files based on the appropriate version
413
- async function loadResource ( ) : Promise < Tips > {
424
+ async function loadResource ( extPath : string ) : Promise < Tips > {
414
425
let tfVersion : string ;
415
426
const cwd = workspaceUtils . getActiveEditorPath ( ) ;
416
427
if ( ! cwd ) {
@@ -424,24 +435,30 @@ async function loadResource(): Promise<Tips> {
424
435
if ( match ) {
425
436
tfVersion = match [ 1 ] ;
426
437
} else {
438
+ // gives the latest JSON if not tf provider version found
427
439
tfVersion = LATEST_VERSION ;
428
440
}
429
- console . log ( `tf provider version:[${ tfVersion } ],cwd:[${ cwd } ]` ) ; //like: 1.81.54
441
+ console . log ( `tf provider version:[${ tfVersion } ], cwd:[${ cwd } ]` ) ;
430
442
} ) . catch ( error => {
431
443
console . error ( `execute terraform version failed: ${ error } ` ) ;
432
444
} ) ;
433
445
434
- const tipsDir = path . join ( __dirname , '..' , 'config' , 'tips' ) ;
435
- const tipFiles = await sortJsonFiles ( tipsDir ) ;
436
446
let result : Tips | null = null ;
447
+ const tipsDir = path . join ( extPath , 'config' , 'tips' ) ;
448
+ const tipFiles = await sortJsonFiles ( tipsDir ) ;
437
449
438
450
tipFiles . some ( file => {
439
451
if ( compareVersions ( tfVersion , file . version ) >= 0 ) {
440
- console . log ( `loaded json version:${ file . version } ` ) ;
441
452
result = file . json as Tips ;
442
453
return true ;
443
454
}
455
+ // gives the latest JSON if not one JSON files matched
456
+ result = file . json as Tips ;
444
457
return false ;
445
458
} ) ;
459
+
460
+ console . log ( `Loaded json. tf version:[${ tfVersion } ], json version:[${ result . version } ]` ) ;
461
+ // vscode.window.showInformationMessage(`Loaded json. tf version:[${tfVersion}], json version:[${result.version}]`);
462
+
446
463
return result ;
447
464
}
0 commit comments