Skip to content

Commit

Permalink
Alternate implementation (no """sql markers)
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Dec 4, 2019
1 parent fd7cde0 commit 468eff2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ export function mergeTokens(/* ... */) {

const oop = ace.acequire('ace/lib/oop');
const { TextHighlightRules } = ace.acequire('ace/mode/text_highlight_rules');
export function InputHighlightRules() {

export function InputHighlightRules() {
// regexp must not have capturing parentheses. Use (?:) instead.
// regexps are ordered -> the first match is used
/*jshint -W015 */
this.$rules = {
'start-sql': [
{ token: 'whitespace', regex: '\\s+' },
{ token: 'paren.lparen', regex: '{', next: 'json-sql', push: true },
{ regex: '', next: 'start' }
],
'start': mergeTokens([
{ 'token': 'warning', 'regex': '#!.*$' },
{ token: 'comment', regex: /^#.*$/ },
Expand All @@ -66,6 +71,7 @@ export function InputHighlightRules() {
addEOL(['whitespace'], /(\s+)/, 'start', 'url')
),
'url': mergeTokens(
addEOL(['url.part'], /(_sql)/, 'start-sql', 'url-sql'),
addEOL(['url.part'], /([^?\/,\s]+)/, 'start'),
addEOL(['url.comma'], /(,)/, 'start'),
addEOL(['url.slash'], /(\/)/, 'start'),
Expand All @@ -75,7 +81,17 @@ export function InputHighlightRules() {
addEOL(['url.param', 'url.equal', 'url.value'], /([^&=]+)(=)([^&]*)/, 'start'),
addEOL(['url.param'], /([^&=]+)/, 'start'),
addEOL(['url.amp'], /(&)/, 'start')
)
),
'url-sql': mergeTokens(
addEOL(['url.comma'], /(,)/, 'start-sql'),
addEOL(['url.slash'], /(\/)/, 'start-sql'),
addEOL(['url.questionmark'], /(\?)/, 'start-sql', 'urlParams-sql')
),
'urlParams-sql': mergeTokens(
addEOL(['url.param', 'url.equal', 'url.value'], /([^&=]+)(=)([^&]*)/, 'start-sql'),
addEOL(['url.param'], /([^&=]+)/, 'start-sql'),
addEOL(['url.amp'], /(&)/, 'start-sql')
),
};

addToRules(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,22 @@
*/

import ace from 'brace';
const _ = require('lodash');

const { SqlHighlightRules } = ace.acequire('ace/mode/sql_highlight_rules');
const _ = require('lodash');
const ScriptHighlightRules = require('./script_highlight_rules').ScriptHighlightRules;
const { ScriptHighlightRules } = require('./script_highlight_rules');

const jsonRules = function (root) {
root = root ? root : 'json';
const rules = {};
rules[root] = [
const xJsonRules = [
{
token: ['variable', 'whitespace', 'ace.punctuation.colon', 'whitespace', 'punctuation.start_triple_quote'],
regex: '("(?:[^"]*_)?script"|"inline"|"source")(\\s*?)(:)(\\s*?)(""")',
next: 'script-start',
merge: false,
push: true
},
{
token: 'punctuation.start_triple_quote',
regex: '"""sql',
next: 'sql-start',
merge: false,
push: true
},
{
token: 'variable', // single line
regex: '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]\\s*(?=:)'
Expand Down Expand Up @@ -116,6 +109,16 @@ const jsonRules = function (root) {
regex: '.+?'
}
];

rules[root] = xJsonRules;
rules[root + '-sql'] = [{
token: ['variable', 'whitespace', 'ace.punctuation.colon', 'whitespace', 'punctuation.start_triple_quote'],
regex: '("query")(\\s*?)(:)(\\s*?)(""")',
next: 'sql-start',
merge: false,
push: true
}].concat(xJsonRules);

rules.string_literal = [
{
token: 'punctuation.end_triple_quote',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function RowParser(editor) {
return MODE.BETWEEN_REQUESTS;
} // shouldn't really happen

if (mode !== 'start') {
if (mode !== 'start' && mode !== 'start-sql') {
return MODE.IN_REQUEST;
}
let line = (session.getLine(row) || '').trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ utils.reformatData = function (data, indent) {
};

utils.collapseLiteralStrings = function (data) {
const splitData = data.replace(/"""sql/, `"""`).split(`"""`);
const splitData = data.split(`"""`);
for (let idx = 1; idx < splitData.length - 1; idx += 2) {
splitData[idx] = JSON.stringify(splitData[idx]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import 'brace';
import 'brace/ext/language_tools';
import 'brace/ext/searchbox';
import 'brace/mode/json';
import 'brace/mode/sql';
import 'brace/mode/text';

jest.mock('../../../../np_ready/public/application', () => ({ legacyBackDoorToSettings: () => {}, }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"query": {
"__template": {
"__raw": true,
"value": "\"\"\"sql\nSELECT * FROM \"TABLE\"\n\"\"\""
"value": "\"\"\"\nSELECT * FROM \"TABLE\"\n\"\"\""
}
}
},
Expand All @@ -19,6 +19,6 @@
"smile"
]
},
"template": "_sql?format=json\n{\n\"query\":\n \"\"\"sql\n SELECT * FROM \"TABLE\"\n \"\"\"\n}\n"
"template": "_sql?format=json\n{\n \"query\": \"\"\"\n SELECT * FROM \"${1:TABLE}\"\n \"\"\"\n}\n"
}
}

0 comments on commit 468eff2

Please sign in to comment.