Skip to content

Commit

Permalink
Consolidate all language definitions used in monaco within the @kbn/m…
Browse files Browse the repository at this point in the history
…onaco package (elastic#208950)

## Summary

This PR was created in response to
elastic#208858, it migrates all existing
language definitions within the `@kbn/code-editor` package into the
`@kbn/monaco` package to provide a separation of concern for logic that
doesn't particularly relate to the configurations for the code editor
UI.

With this change, all supported languages are ingested from the
`@kbn/monaco` package where they will be domiciled from henceforth, and
in turn fix the issue that was discovered relating to the way the
language definitions within `@kbn/code-editor` get registered as a side
effect.

With this change, to add support for a new language, said language
should be defined within `@kbn/monaco`, and registered in
`languages/index.ts`.

<!--
### Checklist

Check the PR satisfies following conditions. 

Reviewers should verify this PR satisfies this list as well.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

Does this PR introduce any risks? For example, consider risks like hard
to test bugs, performance regression, potential of data loss.

Describe the risk, its severity, and mitigation for each identified
risk. Invite stakeholders and evaluate how to proceed before merging.

- [ ] [See some risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
- [ ] ...

-->

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
eokoneyo and kibanamachine authored Feb 18, 2025
1 parent a67154c commit ff0470f
Show file tree
Hide file tree
Showing 170 changed files with 569 additions and 446 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
//

import React, { useCallback } from 'react';
import { monaco, XJsonLang } from '@kbn/monaco';
import { monaco } from '@kbn/monaco';

import {
CodeEditor as KibanaReactCodeEditor,
type CodeEditorProps as KibanaReactCodeEditorProps,
MarkdownLang,
MARKDOWN_LANG_ID,
XJSON_LANG_ID,
} from '@kbn/code-editor';

type Props = Pick<KibanaReactCodeEditorProps, 'aria-label' | 'value' | 'onChange'>;
Expand Down Expand Up @@ -103,7 +104,7 @@ export const CodeEditor = ({ onChange, type, isReadOnly, name, ...props }: CodeE
return (
<KibanaReactCodeEditor
{...{ onChange, editorDidMount, options, ...props }}
languageId={type === 'json' ? XJsonLang.ID : MarkdownLang}
languageId={type === 'json' ? XJSON_LANG_ID : MARKDOWN_LANG_ID}
width="100%"
/>
);
Expand Down
31 changes: 4 additions & 27 deletions src/platform/packages/shared/kbn-monaco/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,18 @@

import './src/register_globals';

export {
monaco,
cssConf,
cssLanguage,
markdownConf,
markdownLanguage,
yamlConf,
yamlLanguage,
} from './src/monaco_imports';
export { XJsonLang } from './src/xjson';
export { SQLLang } from './src/sql';
export { ESQL_LANG_ID, ESQL_DARK_THEME_ID, ESQL_LIGHT_THEME_ID, ESQLLang } from './src/esql';
export { monaco } from './src/monaco_imports';

export type { ESQLCallbacks } from '@kbn/esql-validation-autocomplete';

export * from './src/painless';
/* eslint-disable-next-line @kbn/eslint/module_migration */
import * as BarePluginApi from 'monaco-editor/esm/vs/editor/editor.api';
export { YAML_LANG_ID, configureMonacoYamlSchema } from './src/yaml';

import { registerLanguage } from './src/helpers';
export * from './src/languages';

export { BarePluginApi, registerLanguage };
export { BarePluginApi };
export * from './src/types';

export {
CONSOLE_LANG_ID,
CONSOLE_OUTPUT_LANG_ID,
CONSOLE_THEME_ID,
getParsedRequestsProvider,
ConsoleParsedRequestsProvider,
createOutputParser,
} from './src/console';

export type { ParsedRequest } from './src/console';

export {
defaultThemesResolvers,
CODE_EDITOR_DEFAULT_THEME_ID,
Expand Down
2 changes: 1 addition & 1 deletion src/platform/packages/shared/kbn-monaco/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"license": "Elastic License 2.0 OR AGPL-3.0-only OR SSPL-1.0",
"scripts": {
"build:antlr4:painless": "antlr -Dlanguage=TypeScript ./src/painless/antlr/painless_lexer.g4 ./src/painless/antlr/painless_parser.g4 && node ./scripts/fix_generated_antlr.js painless",
"build:antlr4:painless": "antlr -Dlanguage=TypeScript ./src/languages/painless/antlr/painless_lexer.g4 ./src/languages/painless/antlr/painless_parser.g4 && node ./scripts/fix_generated_antlr.js painless",
"prebuild:antlr4": "brew bundle --file=./scripts/antlr4_tools/brewfile",
"build:antlr4": "yarn run build:antlr4:painless && npm run build:antlr4:esql"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const log = ora('Updating generated antlr grammar').start();
const SUPPORTED_FOLDERS = ['painless', 'esql'];

function execute(folder) {
const generatedAntlrFolder = join(__dirname, '..', 'src', folder, 'antlr');
const generatedAntlrFolder = join(__dirname, '..', 'src', 'languages', folder, 'antlr');

const generatedAntlrFolderContents = readdirSync(generatedAntlrFolder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export const CODE_EDITOR_DEFAULT_THEME_ID = 'codeEditorDefaultTheme';
export const CODE_EDITOR_TRANSPARENT_THEME_ID = 'codeEditorTransparentTheme';
export const CODE_EDITOR_DEFAULT_THEME_ID = 'codeEditorDefaultTheme' as const;
export const CODE_EDITOR_TRANSPARENT_THEME_ID = 'codeEditorTransparentTheme' as const;
49 changes: 0 additions & 49 deletions src/platform/packages/shared/kbn-monaco/src/console/index.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/platform/packages/shared/kbn-monaco/src/console/language.ts

This file was deleted.

8 changes: 6 additions & 2 deletions src/platform/packages/shared/kbn-monaco/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
import { monaco } from './monaco_imports';
import type { LangModuleType, CustomLangModuleType } from './types';

export function registerLanguage(language: LangModuleType | CustomLangModuleType) {
export function registerLanguage(language: LangModuleType | CustomLangModuleType, force = false) {
const { ID, lexerRules, languageConfiguration, foldingRangeProvider } = language;

if (!force && monaco.languages.getLanguages().some((lang) => lang.id === ID)) {
return;
}

monaco.languages.register({ id: ID });

if ('languageThemeResolver' in language) {
Expand All @@ -33,7 +37,7 @@ export function registerLanguage(language: LangModuleType | CustomLangModuleType
}

if ('onLanguage' in language) {
await language.onLanguage();
await language.onLanguage?.();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { ConsoleWorkerProxyService } from './console_worker_proxy';
import { CONSOLE_LANG_ID } from './constants';
import { monaco } from '../monaco_imports';
import { monaco } from '../../monaco_imports';

/*
* This setup function runs when the Console language is registered into the Monaco editor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { ConsoleWorkerProxyService } from './console_worker_proxy';
import { ParsedRequest } from './types';
import { monaco } from '../monaco_imports';
import { monaco } from '../../monaco_imports';

/*
* This class is a helper interface that is used in the Console plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { monaco } from '../monaco_imports';
import { monaco } from '../../monaco_imports';
import { CONSOLE_LANG_ID } from './constants';
import { ConsoleParserResult, ConsoleWorkerDefinition } from './types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export const CONSOLE_LANG_ID = 'console';
export const CONSOLE_OUTPUT_LANG_ID = 'consoleOutput';
export const CONSOLE_POSTFIX = '.console';
export const CONSOLE_LANG_ID = 'console' as const;
export const CONSOLE_OUTPUT_LANG_ID = 'consoleOutput' as const;
export const CONSOLE_POSTFIX = '.console' as const;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

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

export const foldingRangeProvider: monaco.languages.FoldingRangeProvider = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

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

const getOpeningLineRegex = (openingMarker: string) => {
// Opening parentheses can only be preceded by a colon or nothing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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".
*/

/**
* This import registers the Console monaco language contribution
*/
import './language';

export { CONSOLE_LANG_ID, CONSOLE_OUTPUT_LANG_ID } from './constants';

export type { ParsedRequest } from './types';
export {
getParsedRequestsProvider,
ConsoleLang,
ConsoleOutputLang,
CONSOLE_THEME_ID,
CONSOLE_OUTPUT_THEME_ID,
} from './language';
export { ConsoleParsedRequestsProvider } from './console_parsed_requests_provider';

export { createOutputParser } from './output_parser';
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 { setupConsoleErrorsProvider } from './console_errors_provider';
import { ConsoleWorkerProxyService } from './console_worker_proxy';
import { monaco } from '../../monaco_imports';
import { CONSOLE_LANG_ID, CONSOLE_OUTPUT_LANG_ID } from './constants';
import { ConsoleParsedRequestsProvider } from './console_parsed_requests_provider';
import { buildConsoleTheme } from './theme';
import type { LangModuleType } from '../../types';

const workerProxyService = new ConsoleWorkerProxyService();

import {
lexerRules,
languageConfiguration,
consoleOutputLexerRules,
consoleOutputLanguageConfiguration,
} from './lexer_rules';
import { foldingRangeProvider } from './folding_range_provider';

/**
* @description This language definition is used for the console input panel
*/
export const ConsoleLang: LangModuleType = {
ID: CONSOLE_LANG_ID,
lexerRules,
languageConfiguration,
foldingRangeProvider,
onLanguage: () => {
workerProxyService.setup();
setupConsoleErrorsProvider(workerProxyService);
},
languageThemeResolver: buildConsoleTheme,
};

/**
* @description This language definition is used for the console output panel
*/
export const ConsoleOutputLang: LangModuleType = {
ID: CONSOLE_OUTPUT_LANG_ID,
lexerRules: consoleOutputLexerRules,
languageConfiguration: consoleOutputLanguageConfiguration,
foldingRangeProvider,
};

// Theme id is the same as lang id, as we register only one theme resolver that's color mode aware
export const CONSOLE_THEME_ID = CONSOLE_LANG_ID;

// console output theme is the same as console theme
export const CONSOLE_OUTPUT_THEME_ID = CONSOLE_THEME_ID;

export const getParsedRequestsProvider = (model: monaco.editor.ITextModel | null) => {
return new ConsoleParsedRequestsProvider(workerProxyService, model);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
matchToken,
matchTokens,
} from './shared';
import { monaco } from '../../monaco_imports';
import { monaco } from '../../../monaco_imports';

export const languageConfiguration: monaco.languages.LanguageConfiguration = {
...consoleSharedLanguageConfiguration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
consoleSharedLexerRules,
matchTokensWithEOL,
} from './shared';
import { monaco } from '../../monaco_imports';
import { monaco } from '../../../monaco_imports';

export const consoleOutputLanguageConfiguration: monaco.languages.LanguageConfiguration = {
...consoleSharedLanguageConfiguration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
painlessLanguageAttributes,
} from './nested_painless';
import { buildEsqlRules, buildEsqlStartRule, esqlLanguageAttributes } from './nested_esql';
import { monaco } from '../../..';
import { globals } from '../../common/lexer_rules';
import { monaco } from '../../../..';
import { globals } from '../../../common/lexer_rules';
import { buildXjsonRules } from '../../xjson/lexer_rules/xjson';

export const consoleSharedLanguageConfiguration: monaco.languages.LanguageConfiguration = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/

import { makeHighContrastColor, type UseEuiTheme } from '@elastic/eui';
import { defaultThemesResolvers, CODE_EDITOR_DEFAULT_THEME_ID } from '../code_editor';
import { themeRuleGroupBuilderFactory } from '../common/theme';
import { defaultThemesResolvers, CODE_EDITOR_DEFAULT_THEME_ID } from '../../code_editor';
import { themeRuleGroupBuilderFactory } from '../../common/theme';

const buildRuleGroup = themeRuleGroupBuilderFactory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export const ID = 'yaml';
export const ID = 'css' as const;
Loading

0 comments on commit ff0470f

Please sign in to comment.