Better handling of auto formatting

This commit is contained in:
Boaz Leskes 2016-12-09 21:15:16 +01:00
parent 795fcc0cdd
commit cf6e0b1563
6 changed files with 41 additions and 26 deletions

View file

@ -131,7 +131,7 @@ export function initializeInput($el, $actionsEl, $copyAsCurlEl, output) {
var req = requests.shift();
var es_path = req.url;
var es_method = req.method;
var es_data = req.data.join("\n");
var es_data = utils.collapseLiteralStrings(req.data.join("\n"));
if (es_data) {
es_data += "\n";
} //append a new line for bulk requests.
@ -171,7 +171,7 @@ export function initializeInput($el, $actionsEl, $copyAsCurlEl, output) {
if (mode === null || mode === "application/json") {
// assume json - auto pretty
try {
value = utils.expandScriptsToLiterals(JSON.stringify(JSON.parse(value), null, 2));
value = utils.expandLiteralStrings(JSON.stringify(JSON.parse(value), null, 2));
}
catch (e) {

View file

@ -141,7 +141,7 @@ function SenseEditor($el) {
}
parsed_req.data = formatted_data.data;
editor.replaceRequestRange(parsed_req, req_range, indent);
editor.replaceRequestRange(parsed_req, req_range);
}
});
});
@ -165,8 +165,8 @@ function SenseEditor($el) {
};
editor.replaceRequestRange = function (newRequest, requestRange, autoExpandScripts) {
var text = utils.textFromRequest(newRequest, autoExpandScripts);
editor.replaceRequestRange = function (newRequest, requestRange) {
var text = utils.textFromRequest(newRequest);
if (requestRange) {
var pos = editor.getCursorPosition();
editor.getSession().replace(requestRange, text);
@ -318,8 +318,7 @@ function SenseEditor($el) {
dataEndPos.row, dataEndPos.column
);
var data = editor.getSession().getTextRange(bodyRange);
data = utils.collapseLiteralStrings(data.trim());
request.data.push(data);
request.data.push(data.trim());
bodyStartRow = dataEndPos.row + 1;
}
@ -548,8 +547,9 @@ function SenseEditor($el) {
var ret = 'curl -X' + es_method + ' "' + url + '"';
if (es_data && es_data.length) {
ret += " -d'\n";
var data_as_string = utils.collapseLiteralStrings(es_data.join("\n"))
// since Sense doesn't allow single quote json string any single qoute is within a string.
ret += es_data.join("\n").replace(/'/g, '\\"');
ret += data_as_string.replace(/'/g, '\\"');
if (es_data.length > 1) {
ret += "\n";
} // end with a new line

View file

@ -1,13 +1,10 @@
var utils = {};
utils.textFromRequest = function (request, autoExpandScripts) {
utils.textFromRequest = function (request) {
var data = request.data;
if (typeof data != "string") {
data = data.join("\n");
}
if (autoExpandScripts || typeof autoExpandScripts === "undefined" ) {
data = utils.expandScriptsToLiterals(data);
}
return request.method + " " + request.url + "\n" + data;
};
@ -21,7 +18,10 @@ utils.reformatData = function (data, indent) {
for (var i = 0; i < data.length; i++) {
var cur_doc = data[i];
try {
var new_doc = utils.jsonToString(JSON.parse(cur_doc), indent ? 2 : 0);
var new_doc = utils.jsonToString(JSON.parse(utils.collapseLiteralStrings(cur_doc)), indent ? 2 : 0);
if (indent) {
new_doc = utils.expandLiteralStrings(new_doc);
}
changed = changed || new_doc != cur_doc;
formatted_data.push(new_doc);
}
@ -38,14 +38,21 @@ utils.reformatData = function (data, indent) {
};
utils.collapseLiteralStrings = function (data) {
return data.replace(/"""\n?((?:.|\n)*?)"""/g,function (match, literal) {
return JSON.stringify(literal.trim());
return data.replace(/"""(?:\s*\n)?((?:.|\n)*?)(?:\n\s*)?"""/g,function (match, literal) {
return JSON.stringify(literal);
});
}
utils.expandScriptsToLiterals = function (data) {
return data.replace(/("(?:script|inline)"\s*?:)[\s\n\r]*?(".+?\\.+?[^\\]")/g, function (match, tag, string) {
return tag + ' """\n' + JSON.parse(string).trim() + '\n"""';
utils.expandLiteralStrings = function (data) {
return data.replace(/("(?:\\"|[^"])*?")/g, function (match, string) {
// expand things with two slashes or more
if (string.split(/\\./).length > 2) {
string = JSON.parse(string).replace("^\s*\n", "").replace("\n\s*^", "");
var append = string.includes("\n") ? "\n" : ""; // only go multi line if the string has multiline
return '"""' + append + string + append + '"""';
} else {
return string;
}
});
}

View file

@ -4,7 +4,15 @@ String only 1
""" hello
to you """
-------------------------------------
"hello\nto you"
" hello\nto you "
==========
String only 2
-------------------------------------
"""
startning with new lines and ending as well
"""
-------------------------------------
"startning with new lines and ending as well"
==========
Strings in requests
-------------------------------------
@ -13,7 +21,7 @@ Strings in requests
test
test2
""" },
"g": { "script" : """second + "\"; """ },
"g": { "script" : """second + "\";""" },
"h": 1,
"script": "a + 2"
}

View file

@ -2,8 +2,9 @@
Scripts in requests
-------------------------------------
{
"f": { "script" : { "inline": "test\ntest2" } },
"f": { "script" : { "inline": "test\ntest\\2" } },
"g": { "script" : "second + \"\\\";" },
"f": "short with \\",
"h": 1,
"script": "a + 2"
}
@ -11,11 +12,10 @@ Scripts in requests
{
"f": { "script" : { "inline": """
test
test2
test\2
""" } },
"g": { "script" : """
second + "\";
""" },
"g": { "script" : """second + "\";""" },
"f": "short with \\",
"h": 1,
"script": "a + 2"
}

View file

@ -31,6 +31,6 @@ _.each(expandingTests.split(/^=+$/m), function (fixture) {
expanded = fixture[2].trim();
test("Literal expand - " + name, function () {
deepEqual(utils.expandScriptsToLiterals(collapsed), expanded);
deepEqual(utils.expandLiteralStrings(collapsed), expanded);
});
});