Skip to content

Commit

Permalink
Move painless lang support to @kbn/monaco (#81010)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth authored Oct 22, 2020
1 parent 8f9e09b commit 6ec817b
Show file tree
Hide file tree
Showing 23 changed files with 237 additions and 339 deletions.
20 changes: 20 additions & 0 deletions packages/kbn-monaco/src/esql/constants.ts
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 const ID = 'esql';
23 changes: 23 additions & 0 deletions packages/kbn-monaco/src/esql/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/

import { ID } from './constants';
import { lexerRules } from './lexer_rules';

export const EsqlLang = { ID, lexerRules };
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
* under the License.
*/

import { monaco } from '../../monaco';

export const ID = 'esql';
import { monaco } from '../../monaco_imports';

const brackets = [
{ open: '[', close: ']', token: 'delimiter.square' },
Expand Down
20 changes: 20 additions & 0 deletions packages/kbn-monaco/src/esql/lexer_rules/index.ts
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 { lexerRules } from './esql';
6 changes: 5 additions & 1 deletion packages/kbn-monaco/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
* under the License.
*/

export { monaco } from './monaco';
// global setup for supported languages
import './register_globals';

export { monaco } from './monaco_imports';
export { XJsonLang } from './xjson';
export { PainlessLang } from './painless';

/* eslint-disable-next-line @kbn/eslint/module_migration */
import * as BarePluginApi from 'monaco-editor/esm/vs/editor/editor.api';
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions packages/kbn-monaco/src/painless/constants.ts
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 const ID = 'painless';
23 changes: 23 additions & 0 deletions packages/kbn-monaco/src/painless/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/

import { ID } from './constants';
import { lexerRules } from './lexer_rules';

export const PainlessLang = { ID, lexerRules };
20 changes: 20 additions & 0 deletions packages/kbn-monaco/src/painless/lexer_rules/index.ts
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 { lexerRules } from './painless';
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,9 @@
* under the License.
*/

import { monaco } from '../../monaco';
import { monaco } from '../../monaco_imports';

export const ID = 'painless';

/**
* Extends the default type for a Monarch language so we can use
* attribute references (like @keywords to reference the keywords list)
* in the defined tokenizer
*/
interface Language extends monaco.languages.IMonarchLanguage {
export interface Language extends monaco.languages.IMonarchLanguage {
default: string;
brackets: any;
keywords: string[];
Expand All @@ -41,8 +34,7 @@ interface Language extends monaco.languages.IMonarchLanguage {
}

export const lexerRules = {
default: 'invalid',
tokenPostfix: '',
default: '',
// painless does not use < >, so we define our own
brackets: [
['{', '}', 'delimiter.curly'],
Expand Down Expand Up @@ -136,9 +128,9 @@ export const lexerRules = {
},
],
// whitespace
[/[ \t\r\n]+/, { token: 'whitespace' }],
[/[ \t\r\n]+/, '@whitespace'],
// comments
[/\/\*/, 'comment', '@comment'],
// [/\/\*/, 'comment', '@comment'],
[/\/\/.*$/, 'comment'],
// brackets
[/[{}()\[\]]/, '@brackets'],
Expand Down Expand Up @@ -168,7 +160,6 @@ export const lexerRules = {
// strings single quoted
[/'([^'\\]|\\.)*$/, 'string.invalid'], // string without termination
[/'/, 'string', '@string_sq'],
[/"""/, { token: 'punctuation.end_triple_quote', nextEmbedded: '@pop' }],
],
comment: [
[/[^\/*]+/, 'comment'],
Expand All @@ -189,6 +180,3 @@ export const lexerRules = {
],
},
} as Language;

monaco.languages.register({ id: ID });
monaco.languages.setMonarchTokensProvider(ID, lexerRules);
55 changes: 55 additions & 0 deletions packages/kbn-monaco/src/register_globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.
*/

import { XJsonLang } from './xjson';
import { PainlessLang } from './painless';
import { EsqlLang } from './esql';
import { monaco } from './monaco_imports';
// @ts-ignore
import xJsonWorkerSrc from '!!raw-loader!../target/public/xjson.editor.worker.js';
// @ts-ignore
import defaultWorkerSrc from '!!raw-loader!../target/public/default.editor.worker.js';

/**
* Register languages and lexer rules
*/
monaco.languages.register({ id: XJsonLang.ID });
monaco.languages.setMonarchTokensProvider(XJsonLang.ID, XJsonLang.lexerRules);
monaco.languages.setLanguageConfiguration(XJsonLang.ID, XJsonLang.languageConfiguration);
monaco.languages.register({ id: PainlessLang.ID });
monaco.languages.setMonarchTokensProvider(PainlessLang.ID, PainlessLang.lexerRules);
monaco.languages.register({ id: EsqlLang.ID });
monaco.languages.setMonarchTokensProvider(EsqlLang.ID, EsqlLang.lexerRules);

/**
* Create web workers by language ID
*/
const mapLanguageIdToWorker: { [key: string]: any } = {
[XJsonLang.ID]: xJsonWorkerSrc,
};

// @ts-ignore
window.MonacoEnvironment = {
getWorker: (module: string, languageId: string) => {
const workerSrc = mapLanguageIdToWorker[languageId] || defaultWorkerSrc;

const blob = new Blob([workerSrc], { type: 'application/javascript' });
return new Worker(URL.createObjectURL(blob));
},
};
3 changes: 2 additions & 1 deletion packages/kbn-monaco/src/xjson/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
*/
import './language';
import { ID } from './constants';
import { lexerRules, languageConfiguration } from './lexer_rules';

export const XJsonLang = { ID };
export const XJsonLang = { ID, lexerRules, languageConfiguration };
22 changes: 1 addition & 21 deletions packages/kbn-monaco/src/xjson/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,12 @@

// This file contains a lot of single setup logic for registering a language globally

import { monaco } from '../monaco';
import { monaco } from '../monaco_imports';
import { WorkerProxyService } from './worker_proxy_service';
import { registerLexerRules } from './lexer_rules';
import { ID } from './constants';
// @ts-ignore
import workerSrc from '!!raw-loader!../../target/public/xjson.editor.worker.js';

const wps = new WorkerProxyService();

// Register rules against shared monaco instance.
registerLexerRules(monaco);

// In future we will need to make this map languages to workers using "id" and/or "label" values
// that get passed in. Also this should not live inside the "xjson" dir directly. We can update this
// once we have another worker.
// @ts-ignore
window.MonacoEnvironment = {
getWorker: (module: string, languageId: string) => {
if (languageId === ID) {
// In kibana we will probably build this once and then load with raw-loader
const blob = new Blob([workerSrc], { type: 'application/javascript' });
return new Worker(URL.createObjectURL(blob));
}
},
};

monaco.languages.onLanguage(ID, async () => {
return wps.setup();
});
Expand Down
15 changes: 1 addition & 14 deletions packages/kbn-monaco/src/xjson/lexer_rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,4 @@
* under the License.
*/

/* eslint-disable-next-line @kbn/eslint/module_migration */
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import * as xJson from './xjson';
import * as esql from './esql';
import * as painless from './painless';

export const registerLexerRules = (m: typeof monaco) => {
m.languages.register({ id: xJson.ID });
m.languages.setMonarchTokensProvider(xJson.ID, xJson.lexerRules);
m.languages.register({ id: painless.ID });
m.languages.setMonarchTokensProvider(painless.ID, painless.lexerRules);
m.languages.register({ id: esql.ID });
m.languages.setMonarchTokensProvider(esql.ID, esql.lexerRules);
};
export { lexerRules, languageConfiguration } from './xjson';
15 changes: 3 additions & 12 deletions packages/kbn-monaco/src/xjson/lexer_rules/xjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@
* under the License.
*/

import { monaco } from '../../monaco';
import { ID } from '../constants';
import './painless';
import './esql';
import { monaco } from '../../monaco_imports';

import { globals } from './shared';

export { ID };

export const lexerRules: monaco.languages.IMonarchLanguage = {
...(globals as any),

Expand Down Expand Up @@ -124,11 +119,7 @@ export const lexerRules: monaco.languages.IMonarchLanguage = {
},
};

monaco.languages.register({
id: ID,
});
monaco.languages.setMonarchTokensProvider(ID, lexerRules);
monaco.languages.setLanguageConfiguration(ID, {
export const languageConfiguration: monaco.languages.LanguageConfiguration = {
brackets: [
['{', '}'],
['[', ']'],
Expand All @@ -138,4 +129,4 @@ monaco.languages.setLanguageConfiguration(ID, {
{ open: '[', close: ']' },
{ open: '"', close: '"' },
],
});
};
2 changes: 1 addition & 1 deletion packages/kbn-monaco/src/xjson/worker_proxy_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { ParseResult } from './grammar';
import { monaco } from '../monaco';
import { monaco } from '../monaco_imports';
import { XJsonWorker } from './worker';
import { ID } from './constants';

Expand Down
Loading

0 comments on commit 6ec817b

Please sign in to comment.