Skip to content

Commit 7edb74c

Browse files
authored
[7.9] [Monaco] Refactor the way XJSON grammar checker gets registered (#75160) (#75291)
* [Monaco] Refactor the way XJSON grammar checker gets registered (#75160) * Refactor the way XJSON grammar checker gets registered - avoid registering multiple model add listeners - remove regsiterGrammarChecker from public API! - fix getWorker handler to register XJSON only for XJSON models * remove unused import * updateAnnos -> updateAnnotations # Conflicts: # x-pack/plugins/ingest_pipelines/public/application/components/pipeline_processors_editor/components/manage_processor_form/field_components/xjson_editor.tsx * remove use of registerGrammarChecker in XJsonEditor
1 parent 1673947 commit 7edb74c

File tree

6 files changed

+56
-44
lines changed

6 files changed

+56
-44
lines changed

packages/kbn-monaco/src/xjson/grammar.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ export enum AnnoTypes {
2222
warning = 'warning',
2323
}
2424

25+
export type Parser = ReturnType<typeof createParser>;
26+
27+
export interface Annotation {
28+
name?: string;
29+
type: AnnoTypes;
30+
text: string;
31+
at: number;
32+
}
33+
34+
export interface ParseResult {
35+
annotations: Annotation[];
36+
}
37+
2538
/* eslint-disable */
2639

2740
export const createParser = () => {

packages/kbn-monaco/src/xjson/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
* under the License.
1818
*/
1919

20-
import { registerGrammarChecker } from './language';
21-
20+
/**
21+
* This import registers the XJSON monaco language contribution
22+
*/
23+
import './language';
2224
import { ID } from './constants';
2325

24-
export const XJsonLang = { registerGrammarChecker, ID };
26+
export const XJsonLang = { ID };

packages/kbn-monaco/src/xjson/language.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ const wps = new WorkerProxyService();
3232
registerLexerRules(monaco);
3333

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

@@ -47,15 +50,19 @@ monaco.languages.onLanguage(ID, async () => {
4750
});
4851

4952
const OWNER = 'XJSON_GRAMMAR_CHECKER';
50-
export const registerGrammarChecker = (editor: monaco.editor.IEditor) => {
53+
54+
export const registerGrammarChecker = () => {
5155
const allDisposables: monaco.IDisposable[] = [];
5256

53-
const updateAnnos = async () => {
54-
const { annotations } = await wps.getAnnos();
55-
const model = editor.getModel() as monaco.editor.ITextModel | null;
56-
if (!model) {
57+
const updateAnnotations = async (model: monaco.editor.IModel): Promise<void> => {
58+
if (model.isDisposed()) {
5759
return;
5860
}
61+
const parseResult = await wps.getAnnos(model.uri);
62+
if (!parseResult) {
63+
return;
64+
}
65+
const { annotations } = parseResult;
5966
monaco.editor.setModelMarkers(
6067
model,
6168
OWNER,
@@ -74,19 +81,21 @@ export const registerGrammarChecker = (editor: monaco.editor.IEditor) => {
7481
};
7582

7683
const onModelAdd = (model: monaco.editor.IModel) => {
77-
allDisposables.push(
78-
model.onDidChangeContent(async () => {
79-
updateAnnos();
80-
})
81-
);
84+
if (model.getModeId() === ID) {
85+
allDisposables.push(
86+
model.onDidChangeContent(async () => {
87+
updateAnnotations(model);
88+
})
89+
);
8290

83-
updateAnnos();
91+
updateAnnotations(model);
92+
}
8493
};
85-
8694
allDisposables.push(monaco.editor.onDidCreateModel(onModelAdd));
87-
monaco.editor.getModels().forEach(onModelAdd);
8895
return () => {
8996
wps.stop();
9097
allDisposables.forEach((d) => d.dispose());
9198
};
9299
};
100+
101+
registerGrammarChecker();

packages/kbn-monaco/src/xjson/worker/xjson_worker.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@
1919

2020
/* eslint-disable-next-line @kbn/eslint/module_migration */
2121
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
22-
import { createParser } from '../grammar';
22+
import { createParser, Parser, ParseResult } from '../grammar';
2323

2424
export class XJsonWorker {
2525
constructor(private ctx: monaco.worker.IWorkerContext) {}
26-
private parser: any;
26+
private parser: Parser | undefined;
2727

28-
async parse() {
28+
async parse(modelUri: string): Promise<ParseResult | undefined> {
2929
if (!this.parser) {
3030
this.parser = createParser();
3131
}
32-
const [model] = this.ctx.getMirrorModels();
33-
return this.parser(model.getValue());
32+
const model = this.ctx.getMirrorModels().find((m) => m.uri.toString() === modelUri);
33+
if (model) {
34+
return this.parser(model.getValue());
35+
}
3436
}
3537
}

packages/kbn-monaco/src/xjson/worker_proxy_service.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,21 @@
1717
* under the License.
1818
*/
1919

20-
import { AnnoTypes } from './grammar';
20+
import { ParseResult } from './grammar';
2121
import { monaco } from '../monaco';
2222
import { XJsonWorker } from './worker';
2323
import { ID } from './constants';
2424

25-
export interface Annotation {
26-
name?: string;
27-
type: AnnoTypes;
28-
text: string;
29-
at: number;
30-
}
31-
32-
export interface AnnotationsResponse {
33-
annotations: Annotation[];
34-
}
35-
3625
export class WorkerProxyService {
3726
private worker: monaco.editor.MonacoWebWorker<XJsonWorker> | undefined;
3827

39-
public async getAnnos(): Promise<AnnotationsResponse> {
28+
public async getAnnos(modelUri: monaco.Uri): Promise<ParseResult | undefined> {
4029
if (!this.worker) {
4130
throw new Error('Worker Proxy Service has not been setup!');
4231
}
43-
await this.worker.withSyncedResources(monaco.editor.getModels().map(({ uri }) => uri));
32+
await this.worker.withSyncedResources([modelUri]);
4433
const proxy = await this.worker.getProxy();
45-
return proxy.parse();
34+
return proxy.parse(modelUri.toString());
4635
}
4736

4837
public setup() {

x-pack/plugins/ingest_pipelines/public/application/components/pipeline_processors_editor/components/processor_settings_form/field_components/xjson_editor.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ export const XJsonEditor: FunctionComponent<Props> = ({ field, editorProps }) =>
5353
<CodeEditor
5454
value={xJson}
5555
languageId={XJsonLang.ID}
56-
editorDidMount={(m) => {
57-
XJsonLang.registerGrammarChecker(m);
58-
}}
5956
options={{ minimap: { enabled: false } }}
6057
onChange={onChange}
6158
{...(editorProps as any)}

0 commit comments

Comments
 (0)