Path: blob/main/extensions/copilot/src/util/vs/base/common/themables.ts
13405 views
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'12/*---------------------------------------------------------------------------------------------3* Copyright (c) Microsoft Corporation. All rights reserved.4* Licensed under the MIT License. See License.txt in the project root for license information.5*--------------------------------------------------------------------------------------------*/67import { Codicon } from './codicons';89export type ColorIdentifier = string;1011export type IconIdentifier = string;1213export interface ThemeColor {14id: string;15}1617export namespace ThemeColor {18export function isThemeColor(obj: unknown): obj is ThemeColor {19return !!obj && typeof obj === 'object' && typeof (<ThemeColor>obj).id === 'string';20}21}2223export function themeColorFromId(id: ColorIdentifier) {24return { id };25}262728export interface ThemeIcon {29readonly id: string;30readonly color?: ThemeColor;31}3233export namespace ThemeIcon {34export const iconNameSegment = '[A-Za-z0-9]+';35export const iconNameExpression = '[A-Za-z0-9-]+';36export const iconModifierExpression = '~[A-Za-z]+';37export const iconNameCharacter = '[A-Za-z0-9~-]';3839const ThemeIconIdRegex = new RegExp(`^(${iconNameExpression})(${iconModifierExpression})?$`);4041export function asClassNameArray(icon: ThemeIcon): string[] {42const match = ThemeIconIdRegex.exec(icon.id);43if (!match) {44return asClassNameArray(Codicon.error);45}46const [, id, modifier] = match;47const classNames = ['codicon', 'codicon-' + id];48if (modifier) {49classNames.push('codicon-modifier-' + modifier.substring(1));50}51return classNames;52}5354export function asClassName(icon: ThemeIcon): string {55return asClassNameArray(icon).join(' ');56}5758export function asCSSSelector(icon: ThemeIcon): string {59return '.' + asClassNameArray(icon).join('.');60}6162export function isThemeIcon(obj: unknown): obj is ThemeIcon {63return !!obj && typeof obj === 'object' && typeof (<ThemeIcon>obj).id === 'string' && (typeof (<ThemeIcon>obj).color === 'undefined' || ThemeColor.isThemeColor((<ThemeIcon>obj).color));64}6566const _regexFromString = new RegExp(`^\\$\\((${ThemeIcon.iconNameExpression}(?:${ThemeIcon.iconModifierExpression})?)\\)$`);6768export function fromString(str: string): ThemeIcon | undefined {69const match = _regexFromString.exec(str);70if (!match) {71return undefined;72}73const [, name] = match;74return { id: name };75}7677export function fromId(id: string): ThemeIcon {78return { id };79}8081export function modify(icon: ThemeIcon, modifier: 'disabled' | 'spin' | undefined): ThemeIcon {82let id = icon.id;83const tildeIndex = id.lastIndexOf('~');84if (tildeIndex !== -1) {85id = id.substring(0, tildeIndex);86}87if (modifier) {88id = `${id}~${modifier}`;89}90return { id };91}9293export function getModifier(icon: ThemeIcon): string | undefined {94const tildeIndex = icon.id.lastIndexOf('~');95if (tildeIndex !== -1) {96return icon.id.substring(tildeIndex + 1);97}98return undefined;99}100101export function isEqual(ti1: ThemeIcon, ti2: ThemeIcon): boolean {102return ti1.id === ti2.id && ti1.color?.id === ti2.color?.id;103}104105/**106* Returns whether specified icon is defined and has 'file' ID.107*/108export function isFile(icon: ThemeIcon | undefined): boolean {109return icon?.id === Codicon.file.id;110}111112/**113* Returns whether specified icon is defined and has 'folder' ID.114*/115export function isFolder(icon: ThemeIcon | undefined): boolean {116return icon?.id === Codicon.folder.id;117}118}119120121