From 2959fcde6aa9ff2058ad13cef8a5265ddb5f58a4 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Mon, 14 Mar 2022 19:07:36 +0100 Subject: [PATCH] Fixes #143567: Use directly language ids --- .../common/services/languagesAssociations.ts | 45 +++++++++++++------ .../common/services/languagesRegistry.ts | 6 +-- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/vs/editor/common/services/languagesAssociations.ts b/src/vs/editor/common/services/languagesAssociations.ts index 58ad7c3719c..4912ff8c7cf 100644 --- a/src/vs/editor/common/services/languagesAssociations.ts +++ b/src/vs/editor/common/services/languagesAssociations.ts @@ -10,6 +10,7 @@ import { basename, posix } from 'vs/base/common/path'; import { DataUri } from 'vs/base/common/resources'; import { startsWithUTF8BOM } from 'vs/base/common/strings'; import { URI } from 'vs/base/common/uri'; +import { PLAINTEXT_LANGUAGE_ID } from 'vs/editor/common/languages/modesRegistry'; export interface ILanguageAssociation { readonly id: string; @@ -119,11 +120,27 @@ export function clearConfiguredLanguageAssociations(): void { userRegisteredAssociations = []; } +interface IdAndMime { + id: string; + mime: string; +} + /** * Given a file, return the best matching mime types for it * based on the registered language associations. */ export function getMimeTypes(resource: URI | null, firstLine?: string): string[] { + return getAssociations(resource, firstLine).map(item => item.mime); +} + +/** + * @see `getMimeTypes` + */ +export function getLanguageIds(resource: URI | null, firstLine?: string): string[] { + return getAssociations(resource, firstLine).map(item => item.id); +} + +function getAssociations(resource: URI | null, firstLine?: string): IdAndMime[] { let path: string | undefined; if (resource) { switch (resource.scheme) { @@ -141,7 +158,7 @@ export function getMimeTypes(resource: URI | null, firstLine?: string): string[] } if (!path) { - return [Mimes.unknown]; + return [{ id: 'unknown', mime: Mimes.unknown }]; } path = path.toLowerCase(); @@ -149,29 +166,29 @@ export function getMimeTypes(resource: URI | null, firstLine?: string): string[] const filename = basename(path); // 1.) User configured mappings have highest priority - const configuredLanguage = getMimeByPath(path, filename, userRegisteredAssociations); + const configuredLanguage = getAssociationByPath(path, filename, userRegisteredAssociations); if (configuredLanguage) { - return [configuredLanguage, Mimes.text]; + return [configuredLanguage, { id: PLAINTEXT_LANGUAGE_ID, mime: Mimes.text }]; } // 2.) Registered mappings have middle priority - const registeredLanguage = getMimeByPath(path, filename, nonUserRegisteredAssociations); + const registeredLanguage = getAssociationByPath(path, filename, nonUserRegisteredAssociations); if (registeredLanguage) { - return [registeredLanguage, Mimes.text]; + return [registeredLanguage, { id: PLAINTEXT_LANGUAGE_ID, mime: Mimes.text }]; } // 3.) Firstline has lowest priority if (firstLine) { - const firstlineLanguage = getMimeByFirstline(firstLine); + const firstlineLanguage = getAssociationByFirstline(firstLine); if (firstlineLanguage) { - return [firstlineLanguage, Mimes.text]; + return [firstlineLanguage, { id: PLAINTEXT_LANGUAGE_ID, mime: Mimes.text }]; } } - return [Mimes.unknown]; + return [{ id: 'unknown', mime: Mimes.unknown }]; } -function getMimeByPath(path: string, filename: string, associations: ILanguageAssociationItem[]): string | undefined { +function getAssociationByPath(path: string, filename: string, associations: ILanguageAssociationItem[]): ILanguageAssociationItem | undefined { let filenameMatch: ILanguageAssociationItem | undefined = undefined; let patternMatch: ILanguageAssociationItem | undefined = undefined; let extensionMatch: ILanguageAssociationItem | undefined = undefined; @@ -209,23 +226,23 @@ function getMimeByPath(path: string, filename: string, associations: ILanguageAs // 1.) Exact name match has second highest priority if (filenameMatch) { - return filenameMatch.mime; + return filenameMatch; } // 2.) Match on pattern if (patternMatch) { - return patternMatch.mime; + return patternMatch; } // 3.) Match on extension comes next if (extensionMatch) { - return extensionMatch.mime; + return extensionMatch; } return undefined; } -function getMimeByFirstline(firstLine: string): string | undefined { +function getAssociationByFirstline(firstLine: string): ILanguageAssociationItem | undefined { if (startsWithUTF8BOM(firstLine)) { firstLine = firstLine.substr(1); } @@ -242,7 +259,7 @@ function getMimeByFirstline(firstLine: string): string | undefined { const matches = firstLine.match(association.firstline); if (matches && matches.length > 0) { - return association.mime; + return association; } } } diff --git a/src/vs/editor/common/services/languagesRegistry.ts b/src/vs/editor/common/services/languagesRegistry.ts index 138fe2e8bb5..01b58ce512c 100644 --- a/src/vs/editor/common/services/languagesRegistry.ts +++ b/src/vs/editor/common/services/languagesRegistry.ts @@ -3,12 +3,11 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { coalesce } from 'vs/base/common/arrays'; import { onUnexpectedError } from 'vs/base/common/errors'; import { Emitter, Event } from 'vs/base/common/event'; import { Disposable } from 'vs/base/common/lifecycle'; import { compareIgnoreCase, regExpLeadsToEndlessLoop } from 'vs/base/common/strings'; -import { clearPlatformLanguageAssociations, getMimeTypes, registerPlatformLanguageAssociation } from 'vs/editor/common/services/languagesAssociations'; +import { clearPlatformLanguageAssociations, getLanguageIds, registerPlatformLanguageAssociation } from 'vs/editor/common/services/languagesAssociations'; import { URI } from 'vs/base/common/uri'; import { ILanguageIdCodec, LanguageId } from 'vs/editor/common/languages'; import { ModesRegistry, PLAINTEXT_LANGUAGE_ID } from 'vs/editor/common/languages/modesRegistry'; @@ -362,7 +361,6 @@ export class LanguagesRegistry extends Disposable { if (!resource && !firstLine) { return []; } - const mimeTypes = getMimeTypes(resource, firstLine); - return coalesce(mimeTypes.map(mimeType => this.getLanguageIdByMimeType(mimeType))); + return getLanguageIds(resource, firstLine); } }