Skip to content

Commit

Permalink
fix #7791: update mime associations on languages change
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed May 14, 2020
1 parent 55af692 commit 83f6c03
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions packages/monaco/src/browser/monaco-mime-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,47 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { MimeAssociation, MimeService } from '@theia/core/lib/browser/mime-service';
import debounce = require('lodash.debounce');
import { injectable } from 'inversify';
import { MimeAssociation, MimeService } from '@theia/core/lib/browser/mime-service';

@injectable()
export class MonacoMimeService extends MimeService {

setAssociations(associations: MimeAssociation[]): void {
monaco.mime.clearTextMimes(true);
protected associations: MimeAssociation[] = [];
protected updatingAssociations = false;

for (const association of associations) {
const mimetype = this.getMimeForMode(association.id) || `text/x-${association.id}`;
monaco.mime.registerTextMime({ id: association.id, mime: mimetype, filepattern: association.filepattern, userConfigured: true }, false);
}
constructor() {
super();
monaco.services.StaticServices.modeService.get()._onLanguagesMaybeChanged.event(() => {
if (this.updatingAssociations) {
return;
}
this.updateAssociations();
});
}

monaco.services.StaticServices.modeService.get()._onLanguagesMaybeChanged.fire(undefined);
setAssociations(associations: MimeAssociation[]): void {
this.associations = associations;
this.updateAssociations();
}

protected updateAssociations = debounce(() => {
this.updatingAssociations = true;
try {
monaco.mime.clearTextMimes(true);

for (const association of this.associations) {
const mimetype = this.getMimeForMode(association.id) || `text/x-${association.id}`;
monaco.mime.registerTextMime({ id: association.id, mime: mimetype, filepattern: association.filepattern, userConfigured: true }, false);
}

monaco.services.StaticServices.modeService.get()._onLanguagesMaybeChanged.fire(undefined);
} finally {
this.updatingAssociations = false;
}
});

protected getMimeForMode(langId: string): string | undefined {
for (const language of monaco.languages.getLanguages()) {
if (language.id === langId && language.mimetypes) {
Expand Down

0 comments on commit 83f6c03

Please sign in to comment.