Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate all language definitions used in monaco within the @kbn/monaco package #208950

Merged
Merged
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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to forbid importing monaco from @kbn/monaco and instead ask importing it from code_editor? (I don't know, just curious)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree, this is something worth exploring I created an issue for this.

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