Skip to content

Commit

Permalink
[Painless] Language Service (#60612)
Browse files Browse the repository at this point in the history
* Added language service

* Use the correct monaco instance and add wordwise operations

* Remove plugin context initializer for now
  • Loading branch information
jloleysens authored Mar 20, 2020
1 parent 4506714 commit ec81bf6
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 203 deletions.
2 changes: 2 additions & 0 deletions packages/kbn-ui-shared-deps/monaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import 'monaco-editor/esm/vs/base/worker/defaultWorkerFactory';
import 'monaco-editor/esm/vs/editor/browser/controller/coreCommands.js';
import 'monaco-editor/esm/vs/editor/browser/widget/codeEditorWidget.js';

import 'monaco-editor/esm/vs/editor/contrib/wordOperations/wordOperations.js'; // Needed for word-wise char navigation

import 'monaco-editor/esm/vs/editor/contrib/suggest/suggestController.js'; // Needed for suggestions
import 'monaco-editor/esm/vs/editor/contrib/hover/hover.js'; // Needed for hover
import 'monaco-editor/esm/vs/editor/contrib/parameterHints/parameterHints.js'; // Needed for signature
Expand Down
195 changes: 0 additions & 195 deletions x-pack/plugins/painless_lab/public/application/register_painless.ts

This file was deleted.

5 changes: 2 additions & 3 deletions x-pack/plugins/painless_lab/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
*/

import './styles/_index.scss';
import { PluginInitializerContext } from 'src/core/public';
import { PainlessLabUIPlugin } from './plugin';

export function plugin(ctx: PluginInitializerContext) {
return new PainlessLabUIPlugin(ctx);
export function plugin() {
return new PainlessLabUIPlugin();
}
7 changes: 7 additions & 0 deletions x-pack/plugins/painless_lab/public/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export { monacoPainlessLang } from './monaco_painless_lang';
174 changes: 174 additions & 0 deletions x-pack/plugins/painless_lab/public/lib/monaco_painless_lang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import * as monaco from 'monaco-editor';

/**
* 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 {
default: string;
brackets: any;
keywords: string[];
symbols: RegExp;
escapes: RegExp;
digits: RegExp;
primitives: string[];
octaldigits: RegExp;
binarydigits: RegExp;
constants: string[];
operators: string[];
}

export const monacoPainlessLang = {
default: '',
// painless does not use < >, so we define our own
brackets: [
['{', '}', 'delimiter.curly'],
['[', ']', 'delimiter.square'],
['(', ')', 'delimiter.parenthesis'],
],
keywords: [
'if',
'in',
'else',
'while',
'do',
'for',
'continue',
'break',
'return',
'new',
'try',
'catch',
'throw',
'this',
'instanceof',
],
primitives: ['void', 'boolean', 'byte', 'short', 'char', 'int', 'long', 'float', 'double', 'def'],
constants: ['true', 'false'],
operators: [
'=',
'>',
'<',
'!',
'~',
'?',
'?:',
'?.',
':',
'==',
'===',
'<=',
'>=',
'!=',
'!==',
'&&',
'||',
'++',
'--',
'+',
'-',
'*',
'/',
'&',
'|',
'^',
'%',
'<<',
'>>',
'>>>',
'+=',
'-=',
'*=',
'/=',
'&=',
'|=',
'^=',
'%=',
'<<=',
'>>=',
'>>>=',
'->',
'::',
'=~',
'==~',
],
symbols: /[=><!~?:&|+\-*\/^%]+/,
escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
digits: /\d+(_+\d+)*/,
octaldigits: /[0-7]+(_+[0-7]+)*/,
binarydigits: /[0-1]+(_+[0-1]+)*/,
hexdigits: /[[0-9a-fA-F]+(_+[0-9a-fA-F]+)*/,
tokenizer: {
root: [
// identifiers and keywords
[
/[a-zA-Z_][\w]*/,
{
cases: {
'@keywords': 'keyword',
'@primitives': 'type',
'@constants': 'constant',
'@default': 'identifier',
},
},
],
// whitespace
[/[ \t\r\n]+/, '@whitespace'],
// comments
// [/\/\*/, 'comment', '@comment'],
[/\/\/.*$/, 'comment'],
// brackets
[/[{}()\[\]]/, '@brackets'],
// operators
[
/@symbols/,
{
cases: {
'@operators': 'operators',
'@default': '',
},
},
],
// numbers
[/(@digits)[eE]([\-+]?(@digits))?[fFdD]?/, 'number.float'],
[/(@digits)\.(@digits)([eE][\-+]?(@digits))?[fFdD]?/, 'number.float'],
[/0[xX](@hexdigits)[Ll]?/, 'number.hex'],
[/0(@octaldigits)[Ll]?/, 'number.octal'],
[/0[bB](@binarydigits)[Ll]?/, 'number.binary'],
[/(@digits)[fFdD]/, 'number.float'],
[/(@digits)[lL]?/, 'number'],
// delimiter: after numbers due to conflict with decimals and dot
[/[;,.]/, 'delimiter'],
// strings double quoted
[/"([^"\\]|\\.)*$/, 'string.invalid'], // string without termination
[/"/, 'string', '@string_dq'],
// strings single quoted
[/'([^'\\]|\\.)*$/, 'string.invalid'], // string without termination
[/'/, 'string', '@string_sq'],
],
comment: [
[/[^\/*]+/, 'comment'],
[/\*\//, 'comment', '@pop'],
[/[\/*]/, 'comment'],
],
string_dq: [
[/[^\\"]+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/"/, 'string', '@pop'],
],
string_sq: [
[/[^\\']+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/'/, 'string', '@pop'],
],
},
} as Language;
Loading

0 comments on commit ec81bf6

Please sign in to comment.