Handle multiple monaco editor instances for Painless lang (#85834)

This commit is contained in:
Alison Goryachev 2020-12-15 09:37:21 -05:00 committed by GitHub
parent 9ce6331ead
commit 579ead8eaf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 18 deletions

View file

@ -32,13 +32,28 @@ 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);
@ -46,11 +61,12 @@ export class DiagnosticsAdapter {
private async validate(resource: monaco.Uri): Promise<void> {
const worker = await this.worker(resource);
const errorMarkers = await worker.getSyntaxErrors();
const errorMarkers = await worker.getSyntaxErrors(resource.toString());
const model = monaco.editor.getModel(resource);
// 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));
}
}
}

View file

@ -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(

View file

@ -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' });