Skip to content

Commit

Permalink
Handle multiple monaco editor instances for Painless lang (#85834) (#…
Browse files Browse the repository at this point in the history
…85962)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
alisonelizabeth and kibanamachine authored Dec 15, 2020
1 parent bcafd22 commit b3acc4b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
38 changes: 27 additions & 11 deletions packages/kbn-monaco/src/painless/diagnostics_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,41 @@ export class DiagnosticsAdapter {
constructor(private worker: WorkerAccessor) {
const onModelAdd = (model: monaco.editor.IModel): void => {
let handle: any;
model.onDidChangeContent(() => {
// Every time a new change is made, wait 500ms before validating
clearTimeout(handle);
handle = setTimeout(() => this.validate(model.uri), 500);
});

this.validate(model.uri);
if (model.getModeId() === ID) {
model.onDidChangeContent(() => {
// Do not validate if the language ID has changed
if (model.getModeId() !== ID) {
return;
}

// Every time a new change is made, wait 500ms before validating
clearTimeout(handle);
handle = setTimeout(() => this.validate(model.uri), 500);
});

model.onDidChangeLanguage(({ newLanguage }) => {
// Reset the model markers if the language ID has changed and is no longer "painless"
if (newLanguage !== ID) {
return monaco.editor.setModelMarkers(model, ID, []);
}
});

this.validate(model.uri);
}
};
monaco.editor.onDidCreateModel(onModelAdd);
monaco.editor.getModels().forEach(onModelAdd);
}

private async validate(resource: monaco.Uri): Promise<void> {
const worker = await this.worker(resource);
const errorMarkers = await worker.getSyntaxErrors();

const model = monaco.editor.getModel(resource);
const errorMarkers = await worker.getSyntaxErrors(resource.toString());

// Set the error markers and underline them with "Error" severity
monaco.editor.setModelMarkers(model!, ID, errorMarkers.map(toDiagnostics));
if (errorMarkers) {
const model = monaco.editor.getModel(resource);
// Set the error markers and underline them with "Error" severity
monaco.editor.setModelMarkers(model!, ID, errorMarkers.map(toDiagnostics));
}
}
}
16 changes: 10 additions & 6 deletions packages/kbn-monaco/src/painless/worker/painless_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ export class PainlessWorker {
this._ctx = ctx;
}

private getTextDocument(): string {
const model = this._ctx.getMirrorModels()[0];
return model.getValue();
private getTextDocument(modelUri: string): string | undefined {
const model = this._ctx.getMirrorModels().find((m) => m.uri.toString() === modelUri);

return model?.getValue();
}

public async getSyntaxErrors() {
const code = this.getTextDocument();
return parseAndGetSyntaxErrors(code);
public async getSyntaxErrors(modelUri: string) {
const code = this.getTextDocument(modelUri);

if (code) {
return parseAndGetSyntaxErrors(code);
}
}

public provideAutocompleteSuggestions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const fieldsConfig: FieldsConfig = {

export const Script: FormFieldsComponent = ({ initialFieldValues }) => {
const [showId, setShowId] = useState(() => !!initialFieldValues?.id);
const [scriptLanguage, setScriptLanguage] = useState<string>('plaintext');
const [scriptLanguage, setScriptLanguage] = useState<string>(PainlessLang.ID);

const [{ fields }] = useFormData({ watch: 'fields.lang' });

Expand Down

0 comments on commit b3acc4b

Please sign in to comment.