11'use strict' ;
22
33import { TextDocument } from 'vscode' ;
4- import * as Constants from " ../../common/constants" ;
4+ import * as Constants from ' ../../common/constants' ;
55import { ResolveErrorMessage } from '../../models/httpVariableResolveResult' ;
66import { VariableType } from '../../models/variableType' ;
7+ import { calculateMD5Hash } from '../misc' ;
78import { HttpVariableProvider , HttpVariableValue } from './httpVariableProvider' ;
89import { RequestVariableProvider } from './requestVariableProvider' ;
910
11+ type FileVariableValue = Record < 'name' | 'value' , string > ;
12+
1013export class FileVariableProvider implements HttpVariableProvider {
1114 private static _instance : FileVariableProvider ;
1215
@@ -28,6 +31,10 @@ export class FileVariableProvider implements HttpVariableProvider {
2831 RequestVariableProvider . Instance ,
2932 ] ;
3033
34+ private readonly cache = new Map < string , FileVariableValue [ ] > ( ) ;
35+
36+ private readonly fileMD5Hash = new Map < string , string > ( ) ;
37+
3138 private constructor ( ) {
3239 }
3340
@@ -44,42 +51,51 @@ export class FileVariableProvider implements HttpVariableProvider {
4451 if ( ! variable ) {
4552 return { name, error : ResolveErrorMessage . FileVariableNotExist } ;
4653 } else {
47- return variable ;
54+ const value = await this . processFileVariableValue ( document , variable . value ) ;
55+ return { name, value } ;
4856 }
4957 }
5058
51- public getAll ( document : TextDocument ) : Promise < HttpVariableValue [ ] > {
52- return this . getFileVariables ( document ) ;
59+ public async getAll ( document : TextDocument ) : Promise < HttpVariableValue [ ] > {
60+ const variables = await this . getFileVariables ( document ) ;
61+ return await Promise . all ( variables . map (
62+ async ( { name, value } ) => {
63+ const parsedValue = await this . processFileVariableValue ( document , value ) ;
64+ return { name, value : parsedValue } ;
65+ } ) ) ;
5366 }
5467
55- private async getFileVariables ( document : TextDocument ) : Promise < HttpVariableValue [ ] > {
68+ private async getFileVariables ( document : TextDocument ) : Promise < FileVariableValue [ ] > {
69+ const file = document . uri . toString ( ) ;
5670 const fileContent = document . getText ( ) ;
57- const variables : HttpVariableValue [ ] = [ ] ;
58-
59- const regex = new RegExp ( Constants . FileVariableDefinitionRegex , 'mg' ) ;
60-
61- let match : RegExpExecArray ;
62- while ( match = regex . exec ( fileContent ) ) {
63- const key = match [ 1 ] ;
64- const originalValue = await ( this . processFileVariableValue ( document , match [ 2 ] ) ) ;
65- let value = "" ;
66- let isPrevCharEscape = false ;
67- for ( const currentChar of originalValue ) {
68- if ( isPrevCharEscape ) {
69- isPrevCharEscape = false ;
70- value += this . escapee . get ( currentChar ) || currentChar ;
71- } else {
72- if ( currentChar === "\\" ) {
73- isPrevCharEscape = true ;
74- continue ;
71+ const fileHash = calculateMD5Hash ( fileContent ) ;
72+ if ( ! this . cache . has ( file ) || fileHash !== this . fileMD5Hash . get ( file ) ) {
73+ const regex = new RegExp ( Constants . FileVariableDefinitionRegex , 'mg' ) ;
74+ const variables = new Map < string , FileVariableValue > ( ) ;
75+ let match : RegExpExecArray ;
76+ while ( match = regex . exec ( fileContent ) ) {
77+ const [ , key , originalValue ] = match ;
78+ let value = "" ;
79+ let isPrevCharEscape = false ;
80+ for ( const currentChar of originalValue ) {
81+ if ( isPrevCharEscape ) {
82+ isPrevCharEscape = false ;
83+ value += this . escapee . get ( currentChar ) || currentChar ;
84+ } else {
85+ if ( currentChar === "\\" ) {
86+ isPrevCharEscape = true ;
87+ continue ;
88+ }
89+ value += currentChar ;
7590 }
76- value += currentChar ;
7791 }
92+ variables . set ( key , { name : key , value } ) ;
7893 }
79- variables . push ( { name : key , value } ) ;
94+ this . cache . set ( file , [ ...variables . values ( ) ] ) ;
95+ this . fileMD5Hash . set ( file , fileHash ) ;
8096 }
8197
82- return variables ;
98+ return this . cache . get ( file ) ;
8399 }
84100
85101 private async processFileVariableValue ( document : TextDocument , value : string ) : Promise < string > {
0 commit comments