Skip to content

Commit

Permalink
Some changes to use of VS Code code.
Browse files Browse the repository at this point in the history
In progress.
  • Loading branch information
FernandoAscencio committed May 18, 2023
1 parent e2ae178 commit 4e7161c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ export interface SourceControlGroupFeatures {
export interface ScmRawResource {
handle: number,
sourceUri: UriComponents,
icon: IconUrl, /* icons: light, dark */
icons: (UriComponents | ThemeIcon | undefined)[], /* icons: light, dark */
tooltip: string,
strikeThrough: boolean,
faded: boolean,
Expand Down
43 changes: 22 additions & 21 deletions packages/plugin-ext/src/main/browser/scm-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { Splice } from '../../common/arrays';
import { UriComponents } from '../../common/uri-components';
import { ColorRegistry } from '@theia/core/lib/browser/color-registry';
import { PluginSharedStyle } from './plugin-shared-style';
import { IconUrl } from '../../common';
import { ThemeIcon } from '@theia/monaco-editor-core/esm/vs/platform/theme/common/themeService';

export class PluginScmResourceGroup implements ScmResourceGroup {

Expand Down Expand Up @@ -149,10 +149,12 @@ export class PluginScmProvider implements ScmProvider {
constructor(
private readonly proxy: ScmExt,
private readonly colors: ColorRegistry,
private readonly sharedStyle: PluginSharedStyle,
private readonly _handle: number,
private readonly _contextValue: string,
private readonly _label: string,
private readonly _rootUri: vscodeURI | undefined
private readonly _rootUri: vscodeURI | undefined,
private disposables: DisposableCollection
) { }

updateSourceControl(features: SourceControlProviderFeatures): void {
Expand Down Expand Up @@ -206,7 +208,7 @@ export class PluginScmProvider implements ScmProvider {
group.updateGroupLabel(label);
}

spliceGroupResourceStates(splices: ScmRawResourceSplices[], sharedStyle: PluginSharedStyle, disposables: DisposableCollection): void {
spliceGroupResourceStates(splices: ScmRawResourceSplices[]): void {
for (const splice of splices) {
const groupHandle = splice.handle;
const groupSlices = splice.splices;
Expand All @@ -223,12 +225,14 @@ export class PluginScmProvider implements ScmProvider {
for (const groupSlice of groupSlices) {
const { start, deleteCount, rawResources } = groupSlice;
const resources = rawResources.map(rawResource => {
const { handle, sourceUri, icon, tooltip, strikeThrough, faded, contextValue, command } = rawResource;
const iconClass = this.toIconClass(icon, sharedStyle, disposables);
const { handle, sourceUri, icons, tooltip, strikeThrough, faded, contextValue, command } = rawResource;
const icon = ThemeIcon.isThemeIcon(icons[0]) ? icons[0] : vscodeURI.revive(icons[0]);
const iconDark = ThemeIcon.isThemeIcon(icons[1]) ? icons[1] : vscodeURI.revive(icons[1]) || icon;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const colorVariable = (rawResource as any).colorId && this.colors.toCssVariableName((rawResource as any).colorId);
const decorations = {
icon: iconClass,
icon: this.toIconClass(icon),
iconDark: this.toIconClass(iconDark),
tooltip,
strikeThrough,
// TODO remove the letter and colorId fields when the FileDecorationProvider is applied, see https://github.com/eclipse-theia/theia/pull/8911
Expand Down Expand Up @@ -258,20 +262,17 @@ export class PluginScmProvider implements ScmProvider {
this.onDidChangeResourcesEmitter.fire();
}

private toReferencedIcon(icon: string, style: PluginSharedStyle, disposables: DisposableCollection): string {
if (icon.includes('codicon')) {
return icon;
private toIconClass(icon: vscodeURI | ThemeIcon | undefined): string {
if (!icon) {
return '';
}
const reference = style.toIconClass(icon);
disposables.push(reference);
return reference.object.iconClass;
}

private toIconClass(icons: IconUrl, style: PluginSharedStyle, disposables: DisposableCollection): string | { light: string, dark: string } {
if (typeof icons === 'object') {
return { light: this.toReferencedIcon(icons.light, style, disposables), dark: this.toReferencedIcon(icons.dark, style, disposables) };
if (ThemeIcon.isThemeIcon(icon)) {
return ThemeIcon.asClassName(icon);
}
return this.toReferencedIcon(icons, style, disposables);
console.log(icon.path);
const reference = this.sharedStyle.toIconClass(icon.path);
this.disposables.push(reference);
return reference.object.iconClass;
}

unregisterGroup(handle: number): void {
Expand Down Expand Up @@ -316,7 +317,7 @@ export class ScmMainImpl implements ScmMain {
}

async $registerSourceControl(handle: number, id: string, label: string, rootUri: UriComponents | undefined): Promise<void> {
const provider = new PluginScmProvider(this.proxy, this.colors, handle, id, label, rootUri ? vscodeURI.revive(rootUri) : undefined);
const provider = new PluginScmProvider(this.proxy, this.colors, this.sharedStyle, handle, id, label, rootUri ? vscodeURI.revive(rootUri) : undefined, this.disposables);
const repository = this.scmService.registerScmProvider(provider, {
input: {
validator: async value => {
Expand Down Expand Up @@ -382,7 +383,7 @@ export class ScmMainImpl implements ScmMain {

const provider = repository.provider as PluginScmProvider;
provider.registerGroups(groups);
provider.spliceGroupResourceStates(splices, this.sharedStyle, this.disposables);
provider.spliceGroupResourceStates(splices);
}

$updateGroup(sourceControlHandle: number, groupHandle: number, features: SourceControlGroupFeatures): void {
Expand Down Expand Up @@ -415,7 +416,7 @@ export class ScmMainImpl implements ScmMain {
}

const provider = repository.provider as PluginScmProvider;
provider.spliceGroupResourceStates(splices, this.sharedStyle, this.disposables);
provider.spliceGroupResourceStates(splices);
}

$unregisterGroup(sourceControlHandle: number, handle: number): void {
Expand Down
9 changes: 3 additions & 6 deletions packages/plugin-ext/src/plugin/plugin-icon-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { IconUrl, PluginPackage } from '../common/plugin-protocol';
import { Plugin } from '../common/plugin-api-rpc';
import { ThemeIcon } from '@theia/monaco-editor-core/esm/vs/platform/theme/common/themeService';

export type PluginIconPath = string | URI | ThemeIcon | { // TODO: check if light-dark separation is necessary
light: string | URI | ThemeIcon,
dark: string | URI | ThemeIcon
export type PluginIconPath = string | URI | {
light: string | URI,
dark: string | URI
};
export namespace PluginIconPath {
export function toUrl(iconPath: PluginIconPath | undefined, plugin: Plugin): IconUrl | undefined {
Expand All @@ -39,9 +39,6 @@ export namespace PluginIconPath {
}
export function asString(arg: string | URI | ThemeIcon, plugin: Plugin): string {
arg = arg instanceof URI && arg.scheme === 'file' ? arg.fsPath : arg;
if (ThemeIcon.isThemeIcon(arg)) {
return ThemeIcon.asClassName(arg);
}
if (typeof arg !== 'string') {
return arg.toString(true);
}
Expand Down
41 changes: 26 additions & 15 deletions packages/plugin-ext/src/plugin/scm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import * as theia from '@theia/plugin';
import { Emitter, Event } from '@theia/core/lib/common/event';
import {
IconUrl,
Plugin, PLUGIN_RPC_CONTEXT,
ScmExt,
ScmMain, ScmRawResource, ScmRawResourceGroup,
Expand All @@ -36,23 +35,23 @@ import { Splice } from '../common/arrays';
import { UriComponents } from '../common/uri-components';
import { Command } from '../common/plugin-api-rpc-model';
import { RPCProtocol } from '../common/rpc-protocol';
import { URI } from './types-impl';
import { URI, ThemeIcon } from './types-impl';
import { ScmCommandArg } from '../common/plugin-api-rpc';
import { sep } from '@theia/core/lib/common/paths';
import { ThemeIcon } from '@theia/monaco-editor-core/esm/vs/platform/theme/common/themeService';
// import { ThemeIcon } from '@theia/monaco-editor-core/esm/vs/platform/theme/common/themeService';
import { PluginIconPath } from './plugin-icon-path';
type ProviderHandle = number;
type GroupHandle = number;
type ResourceStateHandle = number;

function getIconResource(decorations?: theia.SourceControlResourceThemableDecorations): theia.Uri | ThemeIcon | undefined {
function getIconResource(decorations?: theia.SourceControlResourceThemableDecorations): UriComponents | ThemeIcon | undefined {
if (!decorations || !decorations.iconPath) {
return undefined;
} else if (typeof decorations.iconPath === 'string') {
return URI.file(decorations.iconPath);
} else if (URI.isUri(decorations.iconPath)) {
return decorations.iconPath;
} else if (ThemeIcon.isThemeIcon(decorations.iconPath)) {
} else if (ThemeIcon.is(decorations.iconPath)) {
return decorations.iconPath;
} else {
console.warn('Unexpected Source Control Resource Themable Decoration');
Expand Down Expand Up @@ -119,8 +118,8 @@ function compareResourceThemableDecorations(a: theia.SourceControlResourceThemab
return 1;
}

const aPath = typeof a.iconPath === 'string' ? a.iconPath : URI.isUri(a.iconPath) ? a.iconPath.fsPath : (a.iconPath as theia.ThemeIcon).id;
const bPath = typeof b.iconPath === 'string' ? b.iconPath : URI.isUri(b.iconPath) ? b.iconPath.fsPath : (b.iconPath as theia.ThemeIcon).id;
const aPath = typeof a.iconPath === 'string' ? a.iconPath : URI.isUri(a.iconPath) ? a.iconPath.fsPath : (a.iconPath as ThemeIcon).id;
const bPath = typeof b.iconPath === 'string' ? b.iconPath : URI.isUri(b.iconPath) ? b.iconPath.fsPath : (b.iconPath as ThemeIcon).id;
return comparePaths(aPath, bPath);
}

Expand Down Expand Up @@ -452,7 +451,11 @@ class ScmResourceGroupImpl implements theia.SourceControlResourceGroup {
this.resourceStatesMap.set(handle, r);

const sourceUri = r.resourceUri;
const icon = this.getIcon(r.decorations);

const icon = getIconResource(r.decorations);
const lightIcon = r.decorations && getIconResource(r.decorations.light) || icon;
const darkIcon = r.decorations && getIconResource(r.decorations.dark) || icon;
const icons = [this.getThemableIcon(lightIcon), this.getThemableIcon(darkIcon)];
let command: Command | undefined;

if (r.command) {
Expand All @@ -473,7 +476,7 @@ class ScmResourceGroupImpl implements theia.SourceControlResourceGroup {
// TODO remove the letter and colorId fields when the FileDecorationProvider is applied, see https://github.com/eclipse-theia/theia/pull/8911
const rawResource = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
handle, sourceUri, letter: (r as any).letter, colorId: (r as any).color.id, icon,
handle, sourceUri, letter: (r as any).letter, colorId: (r as any).color.id, icons,
tooltip, strikeThrough, faded, contextValue, command
} as ScmRawResource;

Expand Down Expand Up @@ -509,12 +512,20 @@ class ScmResourceGroupImpl implements theia.SourceControlResourceGroup {
return rawResourceSplices;
}

private getIcon(decorations: theia.SourceControlResourceDecorations | undefined): IconUrl | undefined {
const iconUri = getIconResource(decorations);
const lightIconUri = decorations && getIconResource(decorations.light) || iconUri;
const darkIconUri = decorations && getIconResource(decorations.dark) || iconUri;
const iconPair = { light: lightIconUri ? lightIconUri : '', dark: darkIconUri ? darkIconUri : '' };
return PluginIconPath.toUrl(iconUri ? iconUri : iconPair, this.plugin);
private getThemableIcon(icon: UriComponents | ThemeIcon | undefined): UriComponents | ThemeIcon | undefined {
if (!icon) {
return undefined;
} else if (ThemeIcon.is(icon)) {
return icon;
}
const uri: UriComponents = {
scheme: '',
authority: '',
path: PluginIconPath.asString(URI.revive(icon), this.plugin),
query: '',
fragment: '',
};
return uri;
}

dispose(): void {
Expand Down
3 changes: 2 additions & 1 deletion packages/scm/src/browser/scm-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export interface ScmResource {
}

export interface ScmResourceDecorations {
icon?: string | { light: string, dark: string };
icon?: string;
iconDark?: string;
tooltip?: string;
source?: string;
letter?: string;
Expand Down
6 changes: 2 additions & 4 deletions packages/scm/src/browser/scm-tree-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,8 @@ export class ScmResourceComponent extends ScmElement<ScmResourceComponent.Props>
const { model, treeNode, colors, parentPath, sourceUri, decoration, labelProvider, commandExecutor, menus, contextKeys, caption, isLightTheme } = this.props;
const resourceUri = new URI(sourceUri);

const decorationIcon = treeNode.decorations?.icon;
const themedIcon = typeof decorationIcon === 'string'
? decorationIcon
: isLightTheme ? decorationIcon?.light : decorationIcon?.dark;
const decorationIcon = treeNode.decorations;
const themedIcon = isLightTheme ? decorationIcon?.icon : decorationIcon?.iconDark;
const classNames: string[] = themedIcon ? ['decoration-icon', themedIcon] : ['status'];

const icon = labelProvider.getIcon(resourceUri);
Expand Down

0 comments on commit 4e7161c

Please sign in to comment.