Merge from master

This commit is contained in:
Raj Musuku
2019-02-21 17:56:04 -08:00
parent 5a146e34fa
commit 666ae11639
11482 changed files with 119352 additions and 255574 deletions

View File

@@ -2,10 +2,10 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as paths from 'vs/base/common/paths';
import * as strings from 'vs/base/common/strings';
import * as arrays from 'vs/base/common/arrays';
import { match } from 'vs/base/common/glob';
export const MIME_TEXT = 'text/plain';
@@ -106,7 +106,7 @@ export function clearTextMimes(onlyUserConfigured?: boolean): void {
/**
* Given a file, return the best matching mime type for it
*/
export function guessMimeTypes(path: string, firstLine?: string): string[] {
export function guessMimeTypes(path: string, firstLine?: string, skipUserAssociations: boolean = false): string[] {
if (!path) {
return [MIME_UNKNOWN];
}
@@ -114,10 +114,12 @@ export function guessMimeTypes(path: string, firstLine?: string): string[] {
path = path.toLowerCase();
const filename = paths.basename(path);
// 1.) User configured mappings have highest priority
const configuredMime = guessMimeTypeByPath(path, filename, userRegisteredAssociations);
if (configuredMime) {
return [configuredMime, MIME_TEXT];
if (!skipUserAssociations) {
// 1.) User configured mappings have highest priority
const configuredMime = guessMimeTypeByPath(path, filename, userRegisteredAssociations);
if (configuredMime) {
return [configuredMime, MIME_TEXT];
}
}
// 2.) Registered mappings have middle priority
@@ -137,10 +139,10 @@ export function guessMimeTypes(path: string, firstLine?: string): string[] {
return [MIME_UNKNOWN];
}
function guessMimeTypeByPath(path: string, filename: string, associations: ITextMimeAssociationItem[]): string {
let filenameMatch: ITextMimeAssociationItem;
let patternMatch: ITextMimeAssociationItem;
let extensionMatch: ITextMimeAssociationItem;
function guessMimeTypeByPath(path: string, filename: string, associations: ITextMimeAssociationItem[]): string | null {
let filenameMatch: ITextMimeAssociationItem | null = null;
let patternMatch: ITextMimeAssociationItem | null = null;
let extensionMatch: ITextMimeAssociationItem | null = null;
// We want to prioritize associations based on the order they are registered so that the last registered
// association wins over all other. This is for https://github.com/Microsoft/vscode/issues/20074
@@ -155,9 +157,9 @@ function guessMimeTypeByPath(path: string, filename: string, associations: IText
// Longest pattern match
if (association.filepattern) {
if (!patternMatch || association.filepattern.length > patternMatch.filepattern.length) {
if (!patternMatch || association.filepattern.length > patternMatch.filepattern!.length) {
const target = association.filepatternOnPath ? path : filename; // match on full path if pattern contains path separator
if (match(association.filepatternLowercase, target)) {
if (match(association.filepatternLowercase!, target)) {
patternMatch = association;
}
}
@@ -165,8 +167,8 @@ function guessMimeTypeByPath(path: string, filename: string, associations: IText
// Longest extension match
if (association.extension) {
if (!extensionMatch || association.extension.length > extensionMatch.extension.length) {
if (strings.endsWith(filename, association.extensionLowercase)) {
if (!extensionMatch || association.extension.length > extensionMatch.extension!.length) {
if (strings.endsWith(filename, association.extensionLowercase!)) {
extensionMatch = association;
}
}
@@ -191,7 +193,7 @@ function guessMimeTypeByPath(path: string, filename: string, associations: IText
return null;
}
function guessMimeTypeByFirstline(firstLine: string): string {
function guessMimeTypeByFirstline(firstLine: string): string | null {
if (strings.startsWithUTF8BOM(firstLine)) {
firstLine = firstLine.substr(1);
}
@@ -225,19 +227,24 @@ export function isUnspecific(mime: string[] | string): boolean {
return mime.length === 1 && isUnspecific(mime[0]);
}
/**
* Returns a suggestion for the filename by the following logic:
* 1. If a relevant extension exists and is an actual filename extension (starting with a dot), suggest the prefix appended by the first one.
* 2. Otherwise, if there are other extensions, suggest the first one.
* 3. Otherwise, suggest the prefix.
*/
export function suggestFilename(langId: string, prefix: string): string {
for (let i = 0; i < registeredAssociations.length; i++) {
const association = registeredAssociations[i];
if (association.userConfigured) {
continue; // only support registered ones
}
const extensions = registeredAssociations
.filter(assoc => !assoc.userConfigured && assoc.extension && assoc.id === langId)
.map(assoc => assoc.extension);
const extensionsWithDotFirst = arrays.coalesce(extensions)
.filter(assoc => strings.startsWith(assoc, '.'));
if (association.id === langId && association.extension) {
return prefix + association.extension;
}
if (extensionsWithDotFirst.length > 0) {
return prefix + extensionsWithDotFirst[0];
}
return prefix; // without any known extension, just return the prefix
return extensions[0] || prefix;
}
interface MapExtToMediaMimes {
@@ -297,4 +304,4 @@ const mapExtToMediaMimes: MapExtToMediaMimes = {
export function getMediaMime(path: string): string | undefined {
const ext = paths.extname(path);
return mapExtToMediaMimes[ext.toLowerCase()];
}
}