Skip to content

Commit

Permalink
Move Ace XJSON lexer-rules, worker and utils to es_ui_shared (#57563)
Browse files Browse the repository at this point in the history
* Move mode and lexer rules to shared space

* Update searchprofiler mode rules

* Moved x-json worker out of searchprofiler and into es_ui_shared (for use in Watcher)
Renamed ace/mode -> ace/modes

* Moved collapse and expand literal string functions to es_ui_shared

* Fix some imports
Enable Watcher editor to parse XJson using XJsonMode

* Fix imports

Import JSON highlight rules in XJSONHighlight rules

* Move console_lang, fix Jest tests

Exporting console_lang through the es_ui_shared/public barrel caused the XJsonMode to imported to a index_management too and any other plugin that may
import something from that directory. console_lang was moved to it's own top level directory es_ui_shared/console_lang.

We also included a mock for tests using XJsonMode to import from console_lang.

* Fixed OSS Jest tests

Console Jest tests were still failing because they did not mock out the raw-loader imported worker file

* Expand triple quotes in editor

Upon entering the advanced watcher creation view, the JSON should be scanned for triple quote expansion

* Bring all editors themes in line

Editors had github theme, which diverged from the textmate theme used in Console and SearchProfiler

* Added XJSON mode to simulate alternative input editor

Slight refactor to the logic for using XJSON mode. Created an adhoc hook for wrapping the
logic of useState and moved lanugage util imports there too to reduce the number of imports
in both the watcher form and simulate tabs in advanced creation.

* Moved x-json worker to x-pack

x-json worker is currently only used inside of x-pack. Also testing
for if this will fix the CI/prod build

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
jloleysens and elasticmachine authored Feb 17, 2020
1 parent 26fdc4a commit 3d7bae4
Show file tree
Hide file tree
Showing 51 changed files with 591 additions and 417 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
useRequestReadContext,
} from '../../../../contexts';

import * as utils from '../../../../../lib/utils/utils';
import { expandLiteralStrings } from '../../../../../../../es_ui_shared/console_lang/lib';

import { subscribeResizeChecker } from '../subscribe_console_resize_checker';
import { applyCurrentSettings } from './apply_editor_settings';
Expand Down Expand Up @@ -67,7 +67,7 @@ function EditorOutputUI() {
editor.update(
data
.map(d => d.response.value as string)
.map(readOnlySettings.tripleQuotes ? utils.expandLiteralStrings : a => a)
.map(readOnlySettings.tripleQuotes ? expandLiteralStrings : a => a)
.join('\n')
);
} else if (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
* under the License.
*/

import * as utils from '../../../lib/utils/utils';
import { extractDeprecationMessages } from '../../../lib/utils';
import { collapseLiteralStrings } from '../../../../../es_ui_shared/console_lang/lib';
// @ts-ignore
import * as es from '../../../lib/es/es';
import { BaseResponseType } from '../../../types/common';
import { BaseResponseType } from '../../../types';

export interface EsRequestArgs {
requests: any;
Expand Down Expand Up @@ -73,7 +74,7 @@ export function sendRequestToES(args: EsRequestArgs): Promise<ESRequestResult[]>
const req = requests.shift();
const esPath = req.url;
const esMethod = req.method;
let esData = utils.collapseLiteralStrings(req.data.join('\n'));
let esData = collapseLiteralStrings(req.data.join('\n'));
if (esData) {
esData += '\n';
} // append a new line for bulk requests.
Expand All @@ -97,7 +98,7 @@ export function sendRequestToES(args: EsRequestArgs): Promise<ESRequestResult[]>

const warnings = xhr.getResponseHeader('warning');
if (warnings) {
const deprecationMessages = utils.extractDeprecationMessages(warnings);
const deprecationMessages = extractDeprecationMessages(warnings);
value = deprecationMessages.join('\n') + '\n' + value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import ace from 'brace';
import { workerModule } from './worker';

const oop = ace.acequire('ace/lib/oop');
const TextMode = ace.acequire('ace/mode/text').Mode;
Expand All @@ -29,7 +30,6 @@ const WorkerClient = ace.acequire('ace/worker/worker_client').WorkerClient;
const AceTokenizer = ace.acequire('ace/tokenizer').Tokenizer;

const HighlightRules = require('./input_highlight_rules').InputHighlightRules;
import { workerModule } from './worker';

export function Mode() {
this.$tokenizer = new AceTokenizer(new HighlightRules().getRules());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

const ace = require('brace');
import { addToRules } from './x_json_highlight_rules';
import { addXJsonToRules } from '../../../../../../es_ui_shared/console_lang';

export function addEOL(tokens, reg, nextIfEOL, normalNext) {
if (typeof reg === 'object') {
Expand Down Expand Up @@ -101,7 +101,7 @@ export function InputHighlightRules() {
),
};

addToRules(this);
addXJsonToRules(this);

if (this.constructor === InputHighlightRules) {
this.normalizeRules();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@

const ace = require('brace');
import 'brace/mode/json';
import { addToRules } from './x_json_highlight_rules';
import { addXJsonToRules } from '../../../../../../es_ui_shared/console_lang';

const oop = ace.acequire('ace/lib/oop');
const JsonHighlightRules = ace.acequire('ace/mode/json_highlight_rules').JsonHighlightRules;

export function OutputJsonHighlightRules() {
this.$rules = {};

addToRules(this, 'start');
addXJsonToRules(this, 'start');

this.$rules.start.unshift(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@
*/

import ace from 'brace';
import { ScriptHighlightRules } from '../../../../../../es_ui_shared/console_lang';

const oop = ace.acequire('ace/lib/oop');
const TextMode = ace.acequire('ace/mode/text').Mode;
const MatchingBraceOutdent = ace.acequire('ace/mode/matching_brace_outdent').MatchingBraceOutdent;
const CstyleBehaviour = ace.acequire('ace/mode/behaviour/cstyle').CstyleBehaviour;
const CStyleFoldMode = ace.acequire('ace/mode/folding/cstyle').FoldMode;
//const WorkerClient = ace.acequire('ace/worker/worker_client').WorkerClient;
ace.acequire('ace/tokenizer');

const ScriptHighlightRules = require('./script_highlight_rules').ScriptHighlightRules;

export function ScriptMode() {
this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour();
Expand Down Expand Up @@ -57,19 +55,4 @@ oop.inherits(ScriptMode, TextMode);
this.autoOutdent = function(state, doc, row) {
this.$outdent.autoOutdent(doc, row);
};

// this.createWorker = function (session) {
// const worker = new WorkerClient(['ace', 'sense_editor'], 'sense_editor/mode/worker', 'SenseWorker', 'sense_editor/mode/worker');
// worker.attachToDocument(session.getDocument());

// worker.on('error', function (e) {
// session.setAnnotations([e.data]);
// });

// worker.on('ok', function (anno) {
// session.setAnnotations(anno.data);
// });

// return worker;
// };
}.call(ScriptMode.prototype));
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

export declare const workerModule: { id: string; src: string };
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import $ from 'jquery';
import _ from 'lodash';

import { create } from '../create';
import { collapseLiteralStrings } from '../../../../../../es_ui_shared/console_lang/lib';
const editorInput1 = require('./editor_input1.txt');
const utils = require('../../../../lib/utils/utils');

describe('Editor', () => {
let input;
Expand Down Expand Up @@ -331,7 +331,7 @@ describe('Editor', () => {
const expected = {
method: 'POST',
url: '_search',
data: [utils.collapseLiteralStrings(simpleRequest.data)],
data: [collapseLiteralStrings(simpleRequest.data)],
};

compareRequest(request, expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import _ from 'lodash';
import RowParser from '../../../lib/row_parser';
import * as utils from '../../../lib/utils/utils';
import { collapseLiteralStrings } from '../../../../../es_ui_shared/console_lang/lib';
import * as utils from '../../../lib/utils';

// @ts-ignore
import * as es from '../../../lib/es/es';

Expand Down Expand Up @@ -480,7 +482,7 @@ export class SenseEditor {
let ret = 'curl -X' + esMethod + ' "' + url + '"';
if (esData && esData.length) {
ret += " -H 'Content-Type: application/json' -d'\n";
const dataAsString = utils.collapseLiteralStrings(esData.join('\n'));
const dataAsString = collapseLiteralStrings(esData.join('\n'));
// since Sense doesn't allow single quote json string any single qoute is within a string.
ret += dataAsString.replace(/'/g, '\\"');
if (esData.length > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* specific language governing permissions and limitations
* under the License.
*/

import '../../application/models/sense_editor/sense_editor.test.mocks';

import $ from 'jquery';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
// @ts-ignore
} from '../kb/kb';

import * as utils from '../utils/utils';
import * as utils from '../utils';

// @ts-ignore
import { populateContext } from './engine';
Expand Down
Loading

0 comments on commit 3d7bae4

Please sign in to comment.