Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added FileHeader support with @@ syntax #762

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const AzureClouds: { [key: string]: { aad: string, arm: string, armAudien

export const CommentIdentifiersRegex: RegExp = /^\s*(#|\/{2})/;

export const FileVariableDefinitionRegex: RegExp = /^\s*@([^\s=]+)\s*=\s*(.*?)\s*$/;
export const FileVariableDefinitionRegex: RegExp = /^\s*@([^\s=@]+)\s*=\s*(.*?)\s*$/;

export const RequestVariableDefinitionWithNameRegexFactory = (name: string, flags?: string): RegExp =>
new RegExp(`^\\s*(?:#{1,}|\\/{2,})\\s+@name\\s+(${name})\\s*$`, flags);
Expand All @@ -70,4 +70,6 @@ export const RequestVariableDefinitionRegex: RegExp = RequestVariableDefinitionW

export const NoteCommentRegex = /^\s*(?:#{1,}|\/{2,})\s*@note\s*$/m;

export const LineSplitterRegex: RegExp = /\r?\n/g;
export const LineSplitterRegex: RegExp = /\r?\n/g;

export const FileHeaderDefinitionRegex: RegExp = /^\s*@@(.*?)\s*$/;
44 changes: 40 additions & 4 deletions src/utils/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface RequestRangeOptions {
ignoreEmptyLine?: boolean;
ignoreFileVariableDefinitionLine?: boolean;
ignoreResponseRange?: boolean;
ignoreFileHeaderVariableDefinitionLine?: boolean;
}

export interface SelectedRequest {
Expand Down Expand Up @@ -67,6 +68,7 @@ export class Selector {
ignoreEmptyLine: true,
ignoreFileVariableDefinitionLine: true,
ignoreResponseRange: true,
ignoreFileHeaderVariableDefinitionLine: true,
...options};
const requestRanges: [number, number][] = [];
const delimitedLines = this.getDelimiterRows(lines);
Expand All @@ -84,7 +86,8 @@ export class Selector {

if (options.ignoreCommentLine && this.isCommentLine(startLine)
|| options.ignoreEmptyLine && this.isEmptyLine(startLine)
|| options.ignoreFileVariableDefinitionLine && this.isFileVariableDefinitionLine(startLine)) {
|| options.ignoreFileVariableDefinitionLine && this.isFileVariableDefinitionLine(startLine)
|| options.ignoreFileHeaderVariableDefinitionLine && this.isFileHeaderDefinitionLine(startLine)) {
start++;
continue;
}
Expand Down Expand Up @@ -134,9 +137,32 @@ export class Selector {
return Constants.NoteCommentRegex.test(text);
}

public static isFileHeaderDefinitionLine(line: string): boolean {
return Constants.FileHeaderDefinitionRegex.test(line);
}

public static getFileHeaderLines(lines: string[]): string[] | undefined {
const result: string[] = new Array<string>();
lines.forEach(line => {
const m = Constants.FileHeaderDefinitionRegex.exec(line);
if (m) {
result.push(m[1]);
}
});
return result;
}

private static getDelimitedText(fullText: string, currentLine: number): string | null {
const lines: string[] = fullText.split(Constants.LineSplitterRegex);
const delimiterLineNumbers: number[] = this.getDelimiterRows(lines);

let fileHeaders ;
if (delimiterLineNumbers.length === 0) {
fileHeaders = Selector.getFileHeaderLines(lines);
} else {
fileHeaders = Selector.getFileHeaderLines(lines.slice(0, currentLine));
}

if (delimiterLineNumbers.length === 0) {
return fullText;
}
Expand All @@ -146,22 +172,32 @@ export class Selector {
return null;
}

let requestLines: Array<string> | undefined;

if (currentLine < delimiterLineNumbers[0]) {
return lines.slice(0, delimiterLineNumbers[0]).join(EOL);
requestLines = lines.slice(0, delimiterLineNumbers[0]);
}

if (currentLine > delimiterLineNumbers[delimiterLineNumbers.length - 1]) {
return lines.slice(delimiterLineNumbers[delimiterLineNumbers.length - 1] + 1).join(EOL);
requestLines = lines.slice(delimiterLineNumbers[delimiterLineNumbers.length - 1] + 1);
}

for (let index = 0; index < delimiterLineNumbers.length - 1; index++) {
const start = delimiterLineNumbers[index];
const end = delimiterLineNumbers[index + 1];
if (start < currentLine && currentLine < end) {
return lines.slice(start + 1, end).join(EOL);
requestLines = lines.slice(start + 1, end);
}
}

if (requestLines) {
if (fileHeaders) {
requestLines.splice(1, 0, ...fileHeaders);
}

return requestLines.join(EOL);
}

return null;
}

Expand Down