Repository navigation
Expand file tree
/
Copy pathvariableUtility.ts
More file actions
127 lines (105 loc) · 5.54 KB
/
Copy pathvariableUtility.ts
File metadata and controls
127 lines (105 loc) · 5.54 KB
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
import { Position, Range, TextDocument } from 'vscode';
import * as Constants from '../common/constants';
import { Selector } from './selector';
export class VariableUtility {
private static readonly environmentOrFileVariableReferenceRegex = /\{{2}[^{}]+\}{2}/;
private static readonly fileVariableDefinitionRegex = /@([^\s=]+)\s*=/;
private static readonly requestVariableReferenceRegex = /\{{2}(\w+)\.(response|request)?(\.body(\..*?)?|\.headers(\.[\w-]+)?)?\}{2}/;
private static readonly partialRequestVariableReferenceRegex = /\{{2}(\w+)\.(.*?)?\}{2}/;
public static getEnvironmentOrFileVariableReferenceNameRange(document: TextDocument, position: Position): Range | undefined {
const wordRange = document.getWordRangeAtPosition(position, this.environmentOrFileVariableReferenceRegex);
if (!wordRange) {
return undefined;
}
// Remove leading and trailing curly braces
const start = wordRange.start.with({ character: wordRange.start.character + 2 });
const end = wordRange.end.with({ character: wordRange.end.character - 2 });
return wordRange.with(start, end);
}
public static getFileVariableDefinitionNameRange(document: TextDocument, position: Position): Range | undefined {
const wordRange = document.getWordRangeAtPosition(position, this.fileVariableDefinitionRegex);
if (!wordRange) {
return undefined;
}
const fullName = document.getText(wordRange);
const index = fullName.search(/[\s=]/);
// Adjust the range from the character after @ until the whitespaces
const start = wordRange.start.with({ character: wordRange.start.character + 1 });
const end = wordRange.start.with({ character: wordRange.start.character + index });
return wordRange.with(start, end);
}
public static getRequestVariableReferenceNameRange(document: TextDocument, position: Position): Range | undefined {
const wordRange = document.getWordRangeAtPosition(position, this.requestVariableReferenceRegex);
if (!wordRange) {
return undefined;
}
const fullName = document.getText(wordRange);
const index = fullName.search(/\./);
// Adjust the range from the character after {{ until the dot
const start = wordRange.start.with({ character: wordRange.start.character + 2 });
const end = wordRange.start.with({ character: wordRange.start.character + index });
return wordRange.with(start, end);
}
public static getRequestVariableReferencePathRange(document: TextDocument, position: Position): Range | undefined {
const wordRange = document.getWordRangeAtPosition(position, this.requestVariableReferenceRegex);
if (!wordRange) {
return undefined;
}
// Remove leading and trailing curly braces
const start = wordRange.start.with({ character: wordRange.start.character + 2 });
const end = wordRange.end.with({ character: wordRange.end.character - 2 });
return wordRange.with(start, end);
}
public static getPartialRequestVariableReferencePathRange(document: TextDocument, position: Position): Range | undefined {
const wordRange = document.getWordRangeAtPosition(position, this.partialRequestVariableReferenceRegex);
if (!wordRange) {
return undefined;
}
// Remove leading and trailing curly braces
const start = wordRange.start.with({ character: wordRange.start.character + 2 });
const end = wordRange.end.with({ character: wordRange.end.character - 2 });
return wordRange.with(start, end);
}
public static getFileVariableDefinitionRanges(lines: string[], variable: string): Range[] {
const locations: Range[] = [];
for (const [index, line] of lines.entries()) {
let match: RegExpExecArray | null;
if ((match = Constants.FileVariableDefinitionRegex.exec(line)) && match[1] === variable) {
const startPos = line.indexOf(`@${variable}`);
const endPos = startPos + variable.length + 1;
locations.push(new Range(index, startPos, index, endPos));
}
}
return locations;
}
public static getRequestVariableDefinitionRanges(lines: string[], variable: string): Range[] {
const locations: Range[] = [];
for (const [index, line] of lines.entries()) {
let match: RegExpExecArray | null;
if ((match = Constants.RequestVariableDefinitionRegex.exec(line)) && match[1] === variable) {
const startPos = line.indexOf(`${variable}`);
const endPos = startPos + variable.length + 1;
locations.push(new Range(index, startPos, index, endPos));
}
}
return locations;
}
public static getFileVariableReferenceRanges(lines: string[], variable: string): Range[] {
const locations: Range[] = [];
const regex = new RegExp(`{{${variable}}}`, 'g');
for (const [index, line] of lines.entries()) {
if (Selector.isCommentLine(line)) {
continue;
}
regex.lastIndex = 0;
let match: RegExpExecArray | null;
while (match = regex.exec(line)) {
const startPos = match.index + 2;
const endPos = startPos + variable.length;
locations.push(new Range(index, startPos, index, endPos));
regex.lastIndex = match.index + 1;
}
}
return locations;
}
}