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

fix(format): only format json when skipDocStringsFormat is falsy #395

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
fix(format): only format json when skipDocStringsFormat is falsy
nicojs authored Jun 17, 2021
commit bd9494877af6359bc09fdf25e0ff8f0bae84017f
70 changes: 36 additions & 34 deletions gserver/src/format.ts
Original file line number Diff line number Diff line change
@@ -171,47 +171,49 @@ function formatTables(text) {
}


function formatJson(textBody: string, indent: string) {
let rxTextBlock = /^\s*"""([\s\S.]*?)"""/gm;
let rxQuoteBegin = /"""/g;
function formatJson(textBody: string, indent: string, settings: Settings) {
if (!settings.cucumberautocomplete.skipDocStringsFormat) {
let rxTextBlock = /^\s*"""([\s\S.]*?)"""/gm;
let rxQuoteBegin = /"""/g;

let textArr = textBody.match(rxTextBlock);
let textArr = textBody.match(rxTextBlock);

if (textArr === null) {
return textBody;
}

for (let txt of textArr) {
let preJson = txt.replace(rxQuoteBegin, '');
let taggedMap = {};
let taggedTexts;
while ((taggedTexts = /<.*?>/g.exec(preJson)) !== null) {
taggedTexts.forEach(function (tag, index) {
let uuid = createUUID();

taggedMap[uuid] = tag;
preJson = preJson.replace(tag, uuid);
});
}
if (!isJson(preJson)) {
continue;
if (textArr === null) {
return textBody;
}

let rxIndentTotal = /^([\s\S]*?)"""/;
let textIndentTotal = txt.match(rxIndentTotal);
let textIndent = textIndentTotal[0].replace('"""', '').replace(/\n/g, '');
for (let txt of textArr) {
let preJson = txt.replace(rxQuoteBegin, '');
let taggedMap = {};
let taggedTexts;
while ((taggedTexts = /<.*?>/g.exec(preJson)) !== null) {
taggedTexts.forEach(function (tag, index) {
let uuid = createUUID();

let jsonTxt = JSON.stringify(JSON.parse(preJson), null, indent);
jsonTxt = '\n"""\n' + jsonTxt + '\n"""';
jsonTxt = jsonTxt.replace(/^/gm, textIndent);
taggedMap[uuid] = tag;
preJson = preJson.replace(tag, uuid);
});
}
if (!isJson(preJson)) {
continue;
}

let rxIndentTotal = /^([\s\S]*?)"""/;
let textIndentTotal = txt.match(rxIndentTotal);
let textIndent = textIndentTotal[0].replace('"""', '').replace(/\n/g, '');

// Restore tagged json
for (let uuid in taggedMap) {
if (taggedMap.hasOwnProperty(uuid)) {
jsonTxt = jsonTxt.replace(uuid, taggedMap[uuid]);
let jsonTxt = JSON.stringify(JSON.parse(preJson), null, indent);
jsonTxt = '\n"""\n' + jsonTxt + '\n"""';
jsonTxt = jsonTxt.replace(/^/gm, textIndent);

// Restore tagged json
for (let uuid in taggedMap) {
if (taggedMap.hasOwnProperty(uuid)) {
jsonTxt = jsonTxt.replace(uuid, taggedMap[uuid]);
}
}
textBody = textBody.replace(txt, jsonTxt);
}
textBody = textBody.replace(txt, jsonTxt);
}
return textBody;
}
@@ -243,7 +245,7 @@ export function format(indent: string, text: string, settings: Settings): string
text = formatTables(text);

// JSON beautifier
text = formatJson(text, indent);
text = formatJson(text, indent, settings);

return text;