Skip to content

Commit

Permalink
First iteration of PoC for SQL highlighting and better completion
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Nov 22, 2019
1 parent 67a3489 commit fd7cde0
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/legacy/core_plugins/console/np_ready/public/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'brace';
import 'brace/ext/language_tools';
import 'brace/ext/searchbox';
import 'brace/mode/json';
import 'brace/mode/sql';
import 'brace/mode/text';

/* eslint-disable @kbn/eslint/no-restricted-paths */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ export default function({
score: 0,
context,
};
// we only need out custom insertMatch behavior for the body
// we only need our custom insertMatch behavior for the body
if (context.autoCompleteType === 'body') {
defaults.completer = {
insertMatch() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

// @ts-ignore
import { ConstantComponent } from './constant_component';

export class FullRequestComponent extends ConstantComponent {
private readonly name: string;
constructor(name: string, parent: any, private readonly template: string) {
super(name, parent);
this.name = name;
}

getTerms() {
return [{ name: this.name, snippet: this.template }];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
SimpleParamComponent,
} from '.';

import { FullRequestComponent } from './full_request_component';

/**
* @param parametrizedComponentFactories a dict of the following structure
* that will be used as a fall back for pattern parameters (i.e.: {indices})
Expand Down Expand Up @@ -54,6 +56,9 @@ export class UrlPatternMatcher {
endpoint.methods.forEach((method) => {
let c;
let activeComponent = this[method].rootComponent;
if (endpoint.template) {
new FullRequestComponent(pattern + '[body]', activeComponent, endpoint.template);
}
const endpointComponents = endpoint.url_components || {};
const partList = pattern.split('/');
_.each(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@
const ace = require('brace');
import { addToRules } from './x_json_highlight_rules';

export function addEOL(tokens, reg, nextIfEOL, normalNext) {
if (typeof reg === 'object') {
reg = reg.source;
}
return [
{ token: tokens.concat(['whitespace']), regex: reg + '(\\s*)$', next: nextIfEOL },
{ token: tokens, regex: reg, next: normalNext }
];
}

export function mergeTokens(/* ... */) {
return [].concat.apply([], arguments);
}

const oop = ace.acequire('ace/lib/oop');
const { TextHighlightRules } = ace.acequire('ace/mode/text_highlight_rules');
export function InputHighlightRules() {
function mergeTokens(/* ... */) {
return [].concat.apply([], arguments);
}

function addEOL(tokens, reg, nextIfEOL, normalNext) {
if (typeof reg === 'object') {
reg = reg.source;
}
return [
{ token: tokens.concat(['whitespace']), regex: reg + '(\\s*)$', next: nextIfEOL },
{ token: tokens, regex: reg, next: normalNext }
];
}

// regexp must not have capturing parentheses. Use (?:) instead.
// regexps are ordered -> the first match is used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* under the License.
*/

import ace from 'brace';

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

Expand All @@ -31,6 +34,13 @@ const jsonRules = function (root) {
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 @@ -127,4 +137,9 @@ export function addToRules(otherRules, embedUnder) {
regex: '"""',
next: 'pop',
}]);
otherRules.embedRules(SqlHighlightRules, 'sql-', [{
token: 'punctuation.end_triple_quote',
regex: '"""',
next: 'pop',
}]);
}
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.split(`"""`);
const splitData = data.replace(/"""sql/, `"""`).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 @@ -4,7 +4,7 @@
"query": {
"__template": {
"__raw": true,
"value": "\"\"\"\nSELECT * FROM \"TABLE\"\n\"\"\""
"value": "\"\"\"sql\nSELECT * FROM \"TABLE\"\n\"\"\""
}
}
},
Expand All @@ -18,6 +18,7 @@
"cbor",
"smile"
]
}
},
"template": "_sql?format=json\n{\n\"query\":\n \"\"\"sql\n SELECT * FROM \"TABLE\"\n \"\"\"\n}\n"
}
}

0 comments on commit fd7cde0

Please sign in to comment.