mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
Merge from master
This commit is contained in:
73
src/vs/editor/common/services/getIconClasses.ts
Normal file
73
src/vs/editor/common/services/getIconClasses.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { Schemas } from 'vs/base/common/network';
|
||||
import { DataUri, basenameOrAuthority } from 'vs/base/common/resources';
|
||||
import { URI as uri } from 'vs/base/common/uri';
|
||||
import { PLAINTEXT_MODE_ID } from 'vs/editor/common/modes/modesRegistry';
|
||||
import { IModeService } from 'vs/editor/common/services/modeService';
|
||||
import { IModelService } from 'vs/editor/common/services/modelService';
|
||||
import { FileKind } from 'vs/platform/files/common/files';
|
||||
|
||||
export function getIconClasses(modelService: IModelService, modeService: IModeService, resource: uri, fileKind?: FileKind): string[] {
|
||||
// we always set these base classes even if we do not have a path
|
||||
const classes = fileKind === FileKind.ROOT_FOLDER ? ['rootfolder-icon'] : fileKind === FileKind.FOLDER ? ['folder-icon'] : ['file-icon'];
|
||||
if (resource) {
|
||||
// Get the path and name of the resource. For data-URIs, we need to parse specially
|
||||
let name: string;
|
||||
let path: string;
|
||||
if (resource.scheme === Schemas.data) {
|
||||
const metadata = DataUri.parseMetaData(resource);
|
||||
name = metadata.get(DataUri.META_DATA_LABEL);
|
||||
path = name;
|
||||
}
|
||||
else {
|
||||
name = cssEscape(basenameOrAuthority(resource).toLowerCase());
|
||||
path = resource.path.toLowerCase();
|
||||
}
|
||||
// Folders
|
||||
if (fileKind === FileKind.FOLDER) {
|
||||
classes.push(`${name}-name-folder-icon`);
|
||||
}
|
||||
// Files
|
||||
else {
|
||||
// Name & Extension(s)
|
||||
if (name) {
|
||||
classes.push(`${name}-name-file-icon`);
|
||||
const dotSegments = name.split('.');
|
||||
for (let i = 1; i < dotSegments.length; i++) {
|
||||
classes.push(`${dotSegments.slice(i).join('.')}-ext-file-icon`); // add each combination of all found extensions if more than one
|
||||
}
|
||||
classes.push(`ext-file-icon`); // extra segment to increase file-ext score
|
||||
}
|
||||
// Configured Language
|
||||
let configuredLangId: string | null = getConfiguredLangId(modelService, resource);
|
||||
configuredLangId = configuredLangId || modeService.getModeIdByFilepathOrFirstLine(path);
|
||||
if (configuredLangId) {
|
||||
classes.push(`${cssEscape(configuredLangId)}-lang-file-icon`);
|
||||
}
|
||||
}
|
||||
}
|
||||
return classes;
|
||||
}
|
||||
|
||||
export function getConfiguredLangId(modelService: IModelService, resource: uri): string | null {
|
||||
let configuredLangId: string | null = null;
|
||||
if (resource) {
|
||||
const model = modelService.getModel(resource);
|
||||
if (model) {
|
||||
const modeId = model.getLanguageIdentifier().language;
|
||||
if (modeId && modeId !== PLAINTEXT_MODE_ID) {
|
||||
configuredLangId = modeId; // only take if the mode is specific (aka no just plain text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return configuredLangId;
|
||||
}
|
||||
|
||||
export function cssEscape(val: string): string {
|
||||
return val.replace(/\s/g, '\\$&'); // make sure to not introduce CSS classes from files that contain whitespace
|
||||
}
|
||||
Reference in New Issue
Block a user