Skip to content

Commit

Permalink
Speed up registerTextMime (fixes microsoft#37521)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Nov 7, 2017
1 parent 636be80 commit 968934c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/vs/base/common/mime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let userRegisteredAssociations: ITextMimeAssociationItem[] = [];
/**
* Associate a text mime to the registry.
*/
export function registerTextMime(association: ITextMimeAssociation): void {
export function registerTextMime(association: ITextMimeAssociation, warnOnOverwrite = false): void {

// Register
const associationItem = toTextMimeAssociationItem(association);
Expand All @@ -49,7 +49,7 @@ export function registerTextMime(association: ITextMimeAssociation): void {
}

// Check for conflicts unless this is a user configured association
if (!associationItem.userConfigured) {
if (warnOnOverwrite && !associationItem.userConfigured) {
registeredAssociations.forEach(a => {
if (a.mime === associationItem.mime || a.userConfigured) {
return; // same mime or userConfigured is ok
Expand Down
17 changes: 10 additions & 7 deletions src/vs/editor/common/services/languagesRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ export class LanguagesRegistry {
private _nameMap: { [name: string]: LanguageIdentifier; };
private _lowercaseNameMap: { [name: string]: LanguageIdentifier; };

constructor(useModesRegistry = true) {
private _warnOnOverwrite: boolean;

constructor(useModesRegistry = true, warnOnOverwrite = false) {
this._nextLanguageId = 1;
this._languages = {};
this._mimeTypesMap = {};
this._nameMap = {};
this._lowercaseNameMap = {};
this._languageIds = [];
this._warnOnOverwrite = warnOnOverwrite;

if (useModesRegistry) {
this._registerLanguages(ModesRegistry.getLanguages());
Expand Down Expand Up @@ -100,10 +103,10 @@ export class LanguagesRegistry {
this._languages[langId] = resolvedLanguage;
}

LanguagesRegistry._mergeLanguage(resolvedLanguage, lang);
this._mergeLanguage(resolvedLanguage, lang);
}

private static _mergeLanguage(resolvedLanguage: IResolvedLanguage, lang: ILanguageExtensionPoint): void {
private _mergeLanguage(resolvedLanguage: IResolvedLanguage, lang: ILanguageExtensionPoint): void {
const langId = lang.id;

let primaryMime: string = null;
Expand All @@ -124,21 +127,21 @@ export class LanguagesRegistry {

if (Array.isArray(lang.extensions)) {
for (let extension of lang.extensions) {
mime.registerTextMime({ id: langId, mime: primaryMime, extension: extension });
mime.registerTextMime({ id: langId, mime: primaryMime, extension: extension }, this._warnOnOverwrite);
resolvedLanguage.extensions.push(extension);
}
}

if (Array.isArray(lang.filenames)) {
for (let filename of lang.filenames) {
mime.registerTextMime({ id: langId, mime: primaryMime, filename: filename });
mime.registerTextMime({ id: langId, mime: primaryMime, filename: filename }, this._warnOnOverwrite);
resolvedLanguage.filenames.push(filename);
}
}

if (Array.isArray(lang.filenamePatterns)) {
for (let filenamePattern of lang.filenamePatterns) {
mime.registerTextMime({ id: langId, mime: primaryMime, filepattern: filenamePattern });
mime.registerTextMime({ id: langId, mime: primaryMime, filepattern: filenamePattern }, this._warnOnOverwrite);
}
}

Expand All @@ -150,7 +153,7 @@ export class LanguagesRegistry {
try {
let firstLineRegex = new RegExp(firstLineRegexStr);
if (!strings.regExpLeadsToEndlessLoop(firstLineRegex)) {
mime.registerTextMime({ id: langId, mime: primaryMime, firstline: firstLineRegex });
mime.registerTextMime({ id: langId, mime: primaryMime, firstline: firstLineRegex }, this._warnOnOverwrite);
}
} catch (err) {
// Most likely, the regex was bad
Expand Down
4 changes: 2 additions & 2 deletions src/vs/editor/common/services/modeServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export class ModeServiceImpl implements IModeService {
private readonly _onDidCreateMode: Emitter<IMode> = new Emitter<IMode>();
public readonly onDidCreateMode: Event<IMode> = this._onDidCreateMode.event;

constructor() {
constructor(warnOnOverwrite = false) {
this._instantiatedModes = {};

this._registry = new LanguagesRegistry();
this._registry = new LanguagesRegistry(true, warnOnOverwrite);
}

protected _onReady(): TPromise<boolean> {
Expand Down
6 changes: 4 additions & 2 deletions src/vs/workbench/services/mode/common/workbenchModeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ModesRegistry } from 'vs/editor/common/modes/modesRegistry';
import { ILanguageExtensionPoint, IValidLanguageExtensionPoint } from 'vs/editor/common/services/modeService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ModeServiceImpl } from 'vs/editor/common/services/modeServiceImpl';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';

export const languagesExtPoint: IExtensionPoint<ILanguageExtensionPoint[]> = ExtensionsRegistry.registerExtensionPoint<ILanguageExtensionPoint[]>('languages', [], {
description: nls.localize('vscode.extension.contributes.languages', 'Contributes language declarations.'),
Expand Down Expand Up @@ -84,9 +85,10 @@ export class WorkbenchModeServiceImpl extends ModeServiceImpl {

constructor(
@IExtensionService extensionService: IExtensionService,
@IConfigurationService configurationService: IConfigurationService
@IConfigurationService configurationService: IConfigurationService,
@IEnvironmentService environmentService: IEnvironmentService
) {
super();
super(environmentService.verbose || environmentService.isExtensionDevelopment || !environmentService.isBuilt);
this._configurationService = configurationService;
this._extensionService = extensionService;

Expand Down

0 comments on commit 968934c

Please sign in to comment.