@@ -56,6 +56,10 @@ export class TerraformTipsProvider implements CompletionItemProvider {
5656 position : Position ;
5757 token : CancellationToken ;
5858 resourceType : string | null = null ;
59+ private extensionPath : string ;
60+ constructor ( extensionPath : string ) {
61+ this . extensionPath = extensionPath ;
62+ }
5963
6064 public async provideCompletionItems ( document : TextDocument , position : Position , token : CancellationToken , context : TerraformCompletionContext ) : Promise < CompletionItem [ ] > {
6165 this . document = document ;
@@ -108,7 +112,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
108112 let resourceName = parts [ 1 ] ;
109113 try {
110114 // async load resource config
111- const tips = await loadResource ( ) ;
115+ const tips = await loadResource ( this . extensionPath ) ;
112116 const resources = tips . resource ;
113117 let attrs = resources [ resourceType ] . attrs ;
114118 let result = _ . map ( attrs , o => {
@@ -146,7 +150,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
146150 // load options
147151 try {
148152 // async load resource config
149- const tips = await loadResource ( ) ;
153+ const tips = await loadResource ( this . extensionPath ) ;
150154 const name = lineBeforeEqualSign ;
151155 const resources = tips . resource ;
152156 const argStrs = this . findArgByName ( resources [ this . resourceType ] . args , name ) ;
@@ -155,7 +159,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
155159 this . resourceType = "" ;
156160 return ( options ) . length ? options : [ ] ;
157161 } 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 } ]` ) ;
159163 }
160164 }
161165 this . resourceType = "" ;
@@ -173,12 +177,12 @@ export class TerraformTipsProvider implements CompletionItemProvider {
173177 const resourceType = this . getResourceTypeFromLine ( line ) ;
174178 try {
175179 // async load resource config
176- const tips = await loadResource ( ) ;
180+ const tips = await loadResource ( this . extensionPath ) ;
177181 const resources = tips . resource ;
178182 const ret = this . getItemsForArgs ( resources [ resourceType ] . args , resourceType ) ;
179183 return ret ;
180184 } 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 } ]` ) ;
182186 return [ ] ;
183187 }
184188 }
@@ -298,7 +302,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
298302 // handle resource
299303 try {
300304 // async load resource config
301- const tips = await loadResource ( ) ;
305+ const tips = await loadResource ( this . extensionPath ) ;
302306 const resources = tips . resource ;
303307 let possibleResources = _ . filter ( _ . keys ( resources ) , k => {
304308 if ( regex . test ( k ) ) {
@@ -308,7 +312,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
308312 } ) ;
309313 return possibleResources ;
310314 } 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 } ]` ) ;
312316 return [ ] ;
313317 }
314318 }
@@ -357,7 +361,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
357361 }
358362
359363 const changes = event . contentChanges [ 0 ] ;
360- if ( changes . text === TIPS_OPTIONS_TRIGGER_CHARACTER ) {
364+ if ( changes && changes . text === TIPS_OPTIONS_TRIGGER_CHARACTER ) {
361365 const position = activeEditor . selection . active ;
362366 const resourceType = this . findResourceType ( event . document , position ) ;
363367
@@ -370,9 +374,15 @@ export class TerraformTipsProvider implements CompletionItemProvider {
370374}
371375
372376async 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+ }
376386
377387 // import files
378388 const versions = await Promise . all ( jsonFiles . map ( async file => {
@@ -388,11 +398,12 @@ async function sortJsonFiles(dir: string) {
388398
389399 // sort with version desc
390400 versions . sort ( ( a , b ) => compareVersions ( b . version , a . version ) ) ;
391-
392401 return versions ;
393402}
394403
395404function compareVersions ( a , b ) {
405+ if ( a && ! b ) { return 1 ; }
406+ if ( ! a && b ) { return - 1 ; }
396407 if ( a === 'latest' ) { return 1 ; }
397408 if ( b === 'latest' ) { return - 1 ; }
398409 const aParts = a . split ( '.' ) . map ( Number ) ;
@@ -410,7 +421,7 @@ function compareVersions(a, b) {
410421}
411422
412423// 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 > {
414425 let tfVersion : string ;
415426 const cwd = workspaceUtils . getActiveEditorPath ( ) ;
416427 if ( ! cwd ) {
@@ -424,24 +435,30 @@ async function loadResource(): Promise<Tips> {
424435 if ( match ) {
425436 tfVersion = match [ 1 ] ;
426437 } else {
438+ // gives the latest JSON if not tf provider version found
427439 tfVersion = LATEST_VERSION ;
428440 }
429- console . log ( `tf provider version:[${ tfVersion } ],cwd:[${ cwd } ]` ) ; //like: 1.81.54
441+ console . log ( `tf provider version:[${ tfVersion } ], cwd:[${ cwd } ]` ) ;
430442 } ) . catch ( error => {
431443 console . error ( `execute terraform version failed: ${ error } ` ) ;
432444 } ) ;
433445
434- const tipsDir = path . join ( __dirname , '..' , 'config' , 'tips' ) ;
435- const tipFiles = await sortJsonFiles ( tipsDir ) ;
436446 let result : Tips | null = null ;
447+ const tipsDir = path . join ( extPath , 'config' , 'tips' ) ;
448+ const tipFiles = await sortJsonFiles ( tipsDir ) ;
437449
438450 tipFiles . some ( file => {
439451 if ( compareVersions ( tfVersion , file . version ) >= 0 ) {
440- console . log ( `loaded json version:${ file . version } ` ) ;
441452 result = file . json as Tips ;
442453 return true ;
443454 }
455+ // gives the latest JSON if not one JSON files matched
456+ result = file . json as Tips ;
444457 return false ;
445458 } ) ;
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+
446463 return result ;
447464}
0 commit comments