Skip to content

Commit 23f8dc0

Browse files
committed
Refactor file variables resolving process
1 parent e988311 commit 23f8dc0

2 files changed

Lines changed: 44 additions & 28 deletions

File tree

‎src/utils/decorator.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import { Telemetry } from './telemetry';
44

5-
export function trace(eventName: string) {
6-
return (target, propertyKey: string, descriptor: PropertyDescriptor) => {
5+
export function trace(eventName: string): MethodDecorator {
6+
return (target, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
77
if (descriptor === undefined) {
88
descriptor = Object.getOwnPropertyDescriptor(target, propertyKey);
99
}

‎src/utils/httpVariableProviders/fileVariableProvider.ts‎

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
'use strict';
22

33
import { TextDocument } from 'vscode';
4-
import * as Constants from "../../common/constants";
4+
import * as Constants from '../../common/constants';
55
import { ResolveErrorMessage } from '../../models/httpVariableResolveResult';
66
import { VariableType } from '../../models/variableType';
7+
import { calculateMD5Hash } from '../misc';
78
import { HttpVariableProvider, HttpVariableValue } from './httpVariableProvider';
89
import { RequestVariableProvider } from './requestVariableProvider';
910

11+
type FileVariableValue = Record<'name' | 'value', string>;
12+
1013
export 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

Comments
 (0)