Skip to content

Commit 555dc8b

Browse files
authored
[Console] SQL template with triple quote in completion (#45248) (#45342)
* SQL template with triple quote in completion * Slight update to SQL query template and bugfix for collapse/expand of request bodies * Add comment and slight update to triple quote expansion * Updated tests after changes to newline behaviour * Restore old backslash triple quote expansion behaviour
1 parent e9f4d61 commit 555dc8b

File tree

6 files changed

+55
-12
lines changed

6 files changed

+55
-12
lines changed

src/legacy/core_plugins/console/public/src/autocomplete.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,14 @@ export default function (editor) {
345345
let valueToInsert = termAsString;
346346
let templateInserted = false;
347347
if (context.addTemplate && !_.isUndefined(term.template) && !_.isNull(term.template)) {
348-
const indentedTemplateLines = utils.jsonToString(term.template, true).split('\n');
348+
let indentedTemplateLines;
349+
// In order to allow triple quoted strings in template completion we check the `__raw_`
350+
// attribute to determine whether this template should go through JSON formatting.
351+
if (term.template.__raw && term.template.value) {
352+
indentedTemplateLines = term.template.value.split('\n');
353+
} else {
354+
indentedTemplateLines = utils.jsonToString(term.template, true).split('\n');
355+
}
349356
let currentIndentation = session.getLine(context.rangeToReplace.start.row);
350357
currentIndentation = currentIndentation.match(/^\s*/)[0];
351358
for (let i = 1; i < indentedTemplateLines.length; i++) // skip first line

src/legacy/core_plugins/console/public/src/autocomplete/body_completer.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ class ScopeResolver extends SharedComponent {
127127
}
128128
function getTemplate(description) {
129129
if (description.__template) {
130+
if (description.__raw && _.isString(description.__template)) {
131+
return {
132+
// This is a special secret attribute that gets passed through to indicate that
133+
// the raw value should be passed through to the console without JSON.stringifying it
134+
// first.
135+
//
136+
// Primary use case is to allow __templates to contain extended JSON special values like
137+
// triple quotes.
138+
__raw: true,
139+
value: description.__template,
140+
};
141+
}
130142
return description.__template;
131143
} else if (description.__one_of) {
132144
return getTemplate(description.__one_of[0]);

src/legacy/core_plugins/console/public/src/utils.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,19 @@ utils.reformatData = function (data, indent) {
5959
};
6060

6161
utils.collapseLiteralStrings = function (data) {
62-
return data.replace(/"""(?:\s*\r?\n)?((?:.|\r?\n)*?)(?:\r?\n\s*)?"""/g, function (match, literal) {
63-
return JSON.stringify(literal);
64-
});
62+
const splitData = data.split(`"""`);
63+
for (let idx = 1; idx < splitData.length - 1; idx += 2) {
64+
splitData[idx] = JSON.stringify(splitData[idx]);
65+
}
66+
return splitData.join('');
6567
};
6668

6769
utils.expandLiteralStrings = function (data) {
6870
return data.replace(/("(?:\\"|[^"])*?")/g, function (match, string) {
6971
// expand things with two slashes or more
7072
if (string.split(/\\./).length > 2) {
7173
string = JSON.parse(string).replace('^\s*\n', '').replace('\n\s*^', '');
72-
const append = string.includes('\n') ? '\n' : ''; // only go multi line if the string has multiline
73-
return '"""' + append + string + append + '"""';
74+
return '"""' + string + '"""';
7475
} else {
7576
return string;
7677
}

src/legacy/core_plugins/console/public/tests/src/utils_string_collapsing.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ to you """
88
==========
99
String only 2
1010
-------------------------------------
11-
"""
11+
"""
1212
starting with new lines and ending as well
13-
"""
13+
"""
1414
-------------------------------------
15-
"starting with new lines and ending as well"
15+
"\nstarting with new lines and ending as well\n"
1616
==========
1717
Strings in requests
1818
-------------------------------------
@@ -27,8 +27,8 @@ test2
2727
}
2828
-------------------------------------
2929
{
30-
"f": { "somefield" : "test\ntest2" },
30+
"f": { "somefield" : "\ntest\ntest2\n" },
3131
"g": { "script" : "second + \"\\\";" },
3232
"h": 1,
3333
"script": "a + 2"
34-
}
34+
}

src/legacy/core_plugins/console/public/tests/src/utils_string_expanding.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Scripts in requests
33
-------------------------------------
44
{
5-
"f": { "script" : { "source": "test\ntest\\2" } },
5+
"f": { "script" : { "source": "\ntest\ntest\\2\n" } },
66
"g": { "script" : "second + \"\\\";" },
77
"f": "short with \\",
88
"h": 1,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"sql.query": {
3+
"data_autocomplete_rules": {
4+
"query": {
5+
"__template": {
6+
"__raw": true,
7+
"value": "\"\"\"\nSELECT * FROM \"TABLE\"\n\"\"\""
8+
}
9+
}
10+
},
11+
"url_params": {
12+
"format": [
13+
"csv",
14+
"json",
15+
"tsv",
16+
"txt",
17+
"yaml",
18+
"cbor",
19+
"smile"
20+
]
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)