Skip to content

Commit

Permalink
[8.16] [Console] Add highlighting for painless language (elastic#202695
Browse files Browse the repository at this point in the history
…) (elastic#202951)

# Backport

This will backport the following commits from `main` to `8.16`:
- [[Console] Add highlighting for painless language
(elastic#202695)](elastic#202695)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Elena
Stoeva","email":"59341489+ElenaStoeva@users.noreply.github.com"},"sourceCommit":{"committedDate":"2024-12-04T14:48:16Z","message":"[Console]
Add highlighting for painless language (elastic#202695)\n\nCloses
https://github.com/elastic/kibana/issues/201672?reload=1?reload=1\r\n\r\n##
Summary\r\n\r\nThis PR adds existing painless rules to the console
language so that the\r\npainless scripts are correctly highlighted. We
are adding a painless\r\nstarting rule that matches a string
`\"*_script\"`, `\"inline\"`, or\r\n`\"source\"`, followed by a triple
quote, in order to prevent clashing\r\nwith the existing rules for
triple-quote strings.\r\n\r\nExample request with a
script:\r\n\r\n```\r\nPOST _ingest/pipeline/_simulate\r\n{\r\n
\"pipeline\": { \r\n \"processors\": [\r\n {\r\n \"script\": {\r\n
\"description\": \"Extract 'tags' from 'env' field\",\r\n \"lang\":
\"painless\",\r\n \"source\": \"\"\"\r\n String[] envSplit =
ctx['env'].splitOnToken(params['delimiter']);\r\n ArrayList tags = new
ArrayList();\r\n tags.add(envSplit[params['position']].trim());\r\n
ctx['tags'] = tags;\r\n \"\"\",\r\n \"params\": {\r\n \"delimiter\":
\"-\",\r\n \"position\": 1\r\n }\r\n }\r\n }\r\n ]\r\n },\r\n \"docs\":
[\r\n {\r\n \"_source\": {\r\n \"env\": \"es01-prod\"\r\n }\r\n }\r\n
]\r\n}\r\n```\r\n\r\n<img width=\"1049\" alt=\"Screenshot 2024-12-03 at
12 02
52\"\r\nsrc=\"https://github.com/user-attachments/assets/fb249953-a998-40c0-9775-3474e15b5de2\">","sha":"2fcd323927e42b4e7648f2aaa1a38dd3c7ea3900","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Console","Team:Kibana
Management","release_note:skip","v9.0.0","backport:prev-minor","v8.16.0","v8.17.0"],"title":"[Console]
Add highlighting for painless
language","number":202695,"url":"https://github.com/elastic/kibana/pull/202695","mergeCommit":{"message":"[Console]
Add highlighting for painless language (elastic#202695)\n\nCloses
https://github.com/elastic/kibana/issues/201672?reload=1?reload=1\r\n\r\n##
Summary\r\n\r\nThis PR adds existing painless rules to the console
language so that the\r\npainless scripts are correctly highlighted. We
are adding a painless\r\nstarting rule that matches a string
`\"*_script\"`, `\"inline\"`, or\r\n`\"source\"`, followed by a triple
quote, in order to prevent clashing\r\nwith the existing rules for
triple-quote strings.\r\n\r\nExample request with a
script:\r\n\r\n```\r\nPOST _ingest/pipeline/_simulate\r\n{\r\n
\"pipeline\": { \r\n \"processors\": [\r\n {\r\n \"script\": {\r\n
\"description\": \"Extract 'tags' from 'env' field\",\r\n \"lang\":
\"painless\",\r\n \"source\": \"\"\"\r\n String[] envSplit =
ctx['env'].splitOnToken(params['delimiter']);\r\n ArrayList tags = new
ArrayList();\r\n tags.add(envSplit[params['position']].trim());\r\n
ctx['tags'] = tags;\r\n \"\"\",\r\n \"params\": {\r\n \"delimiter\":
\"-\",\r\n \"position\": 1\r\n }\r\n }\r\n }\r\n ]\r\n },\r\n \"docs\":
[\r\n {\r\n \"_source\": {\r\n \"env\": \"es01-prod\"\r\n }\r\n }\r\n
]\r\n}\r\n```\r\n\r\n<img width=\"1049\" alt=\"Screenshot 2024-12-03 at
12 02
52\"\r\nsrc=\"https://github.com/user-attachments/assets/fb249953-a998-40c0-9775-3474e15b5de2\">","sha":"2fcd323927e42b4e7648f2aaa1a38dd3c7ea3900"}},"sourceBranch":"main","suggestedTargetBranches":["8.16","8.17"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/202695","number":202695,"mergeCommit":{"message":"[Console]
Add highlighting for painless language (elastic#202695)\n\nCloses
https://github.com/elastic/kibana/issues/201672?reload=1?reload=1\r\n\r\n##
Summary\r\n\r\nThis PR adds existing painless rules to the console
language so that the\r\npainless scripts are correctly highlighted. We
are adding a painless\r\nstarting rule that matches a string
`\"*_script\"`, `\"inline\"`, or\r\n`\"source\"`, followed by a triple
quote, in order to prevent clashing\r\nwith the existing rules for
triple-quote strings.\r\n\r\nExample request with a
script:\r\n\r\n```\r\nPOST _ingest/pipeline/_simulate\r\n{\r\n
\"pipeline\": { \r\n \"processors\": [\r\n {\r\n \"script\": {\r\n
\"description\": \"Extract 'tags' from 'env' field\",\r\n \"lang\":
\"painless\",\r\n \"source\": \"\"\"\r\n String[] envSplit =
ctx['env'].splitOnToken(params['delimiter']);\r\n ArrayList tags = new
ArrayList();\r\n tags.add(envSplit[params['position']].trim());\r\n
ctx['tags'] = tags;\r\n \"\"\",\r\n \"params\": {\r\n \"delimiter\":
\"-\",\r\n \"position\": 1\r\n }\r\n }\r\n }\r\n ]\r\n },\r\n \"docs\":
[\r\n {\r\n \"_source\": {\r\n \"env\": \"es01-prod\"\r\n }\r\n }\r\n
]\r\n}\r\n```\r\n\r\n<img width=\"1049\" alt=\"Screenshot 2024-12-03 at
12 02
52\"\r\nsrc=\"https://github.com/user-attachments/assets/fb249953-a998-40c0-9775-3474e15b5de2\">","sha":"2fcd323927e42b4e7648f2aaa1a38dd3c7ea3900"}},{"branch":"8.16","label":"v8.16.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.17","label":"v8.17.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Elena Stoeva <59341489+ElenaStoeva@users.noreply.github.com>
  • Loading branch information
kibanamachine and ElenaStoeva authored Dec 4, 2024
1 parent 8dc8fd4 commit dde4ef3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
71 changes: 71 additions & 0 deletions packages/kbn-monaco/src/console/lexer_rules/nested_painless.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { lexerRules as painlessLexerRules } from '../../painless/lexer_rules';

/*
* This rule is used inside json root to start a painless highlighting sequence
*/
export const buildPainlessStartRule = (painlessRoot: string = 'painless_root') => {
return [
/("(?:[^"]*_)?script"|"inline"|"source")(\s*?)(:)(\s*?)(""")/,
[
'variable',
'whitespace',
'punctuation.colon',
'whitespace',
{
token: 'punctuation',
next: `@${painlessRoot}`,
},
],
];
};

/*
* This function creates a group of rules needed for painless highlighting in console.
* It reuses the lexer rules from the "painless" language, but since not all rules are referenced in the root
* tokenizer and to avoid conflicts with existing console rules, only selected rules are used.
*/
export const buildPainlessRules = (painlessRoot: string = 'painless_root') => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { root, comment, string_dq, string_sq } = painlessLexerRules.tokenizer;
return {
[painlessRoot]: [
// the rule to end painless highlighting and get back to the previous tokenizer state
[
/"""/,
{
token: 'punctuation',
next: '@pop',
},
],
...root,
],
comment,
string_dq,
string_sq,
};
};

/*
* These language attributes need to be added to the console language definition for painless tokenizer
* to work correctly.
*/
export const painlessLanguageAttributes = {
keywords: painlessLexerRules.keywords,
primitives: painlessLexerRules.primitives,
constants: painlessLexerRules.constants,
operators: painlessLexerRules.operators,
symbols: painlessLexerRules.symbols,
digits: painlessLexerRules.digits,
octaldigits: painlessLexerRules.octaldigits,
binarydigits: painlessLexerRules.binarydigits,
hexdigits: painlessLexerRules.hexdigits,
};
14 changes: 14 additions & 0 deletions packages/kbn-monaco/src/console/lexer_rules/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
*/

import { buildSqlRules, buildSqlStartRule, sqlLanguageAttributes } from './nested_sql';
import {
buildPainlessRules,
buildPainlessStartRule,
painlessLanguageAttributes,
} from './nested_painless';
import { monaco } from '../../..';
import { globals } from '../../common/lexer_rules';
import { buildXjsonRules } from '../../xjson/lexer_rules/xjson';
Expand All @@ -16,11 +21,13 @@ export const consoleSharedLanguageConfiguration: monaco.languages.LanguageConfig
brackets: [
['{', '}'],
['[', ']'],
['(', ')'],
['"""', '"""\n'],
],
autoClosingPairs: [
{ open: '{', close: '}' },
{ open: '[', close: ']' },
{ open: '(', close: ')' },
{ open: '"', close: '"' },
{ open: '"""', close: '"""' },
],
Expand Down Expand Up @@ -100,17 +107,22 @@ xjsonRules.json_root = [
matchToken('variable.template', /("\${\w+}")/),
// @ts-expect-error include a rule to start sql highlighting
buildSqlStartRule(),
// @ts-expect-error include a rule to start painless highlighting
buildPainlessStartRule(),
...xjsonRules.json_root,
];

const sqlRules = buildSqlRules();
const painlessRules = buildPainlessRules();
/*
Lexer rules that are shared between the Console editor and the Console output panel.
*/
export const consoleSharedLexerRules: monaco.languages.IMonarchLanguage = {
...(globals as any),
defaultToken: 'invalid',
...sqlLanguageAttributes,
...painlessLanguageAttributes,
keywords: [...sqlLanguageAttributes.keywords, ...painlessLanguageAttributes.keywords],
tokenizer: {
root: [
// warning comment
Expand Down Expand Up @@ -138,5 +150,7 @@ export const consoleSharedLexerRules: monaco.languages.IMonarchLanguage = {
...xjsonRules,
// include sql rules
...sqlRules,
// include painless rules
...painlessRules,
},
};

0 comments on commit dde4ef3

Please sign in to comment.