Skip to content

Commit

Permalink
inject colors and shape settings from ide
Browse files Browse the repository at this point in the history
  • Loading branch information
asoee committed Dec 28, 2024
1 parent 97218fa commit cfe93f9
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions packages/cursorless-jetbrains/src/ide/JetbrainsHats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export class JetbrainsHats implements Hats {
private client: JetbrainsClient;
enabledHatStyles: HatStyleMap;
private enabledHatShapes = ["default"];
private hatShapePenalties: Map<string, number> = new Map([["default", 0]]);
private enabledHatColors = ["default"];
private hatColorPenalties: Map<string, number> = new Map([["default", 0]]);

constructor(client: JetbrainsClient) {
this.client = client;
Expand All @@ -50,6 +53,30 @@ export class JetbrainsHats implements Hats {
this.hatStyleChangedNotifier.notifyListeners(this.enabledHatStyles);
}

setHatShapePenalties(hatShapePenalties: Map<string, number>): void {
// supplied map is a json map, and not a typescript map, so convert it to typed map
this.hatShapePenalties = new Map<string, number>(
Object.entries(hatShapePenalties),
);
this.enabledHatStyles = this.generateHatStyles();
this.hatStyleChangedNotifier.notifyListeners(this.enabledHatStyles);
}

setEnabledHatColors(enabledHatColors: string[]): void {
this.enabledHatColors = enabledHatColors;
this.enabledHatStyles = this.generateHatStyles();
this.hatStyleChangedNotifier.notifyListeners(this.enabledHatStyles);
}

setHatColorPenalties(hatColorPenalties: Map<string, number>): void {
// supplied map is a json map, and not a typescript map, so convert it to typed map
this.hatColorPenalties = new Map<string, number>(
Object.entries(hatColorPenalties),
);
this.enabledHatStyles = this.generateHatStyles();
this.hatStyleChangedNotifier.notifyListeners(this.enabledHatStyles);
}

toJetbransHatRanges(hatRanges: HatRange[]): JetbrainsHatRange[] {
return hatRanges.map((range) => {
return {
Expand All @@ -62,10 +89,10 @@ export class JetbrainsHats implements Hats {

private generateHatStyles(): HatStyleMap {
const res = new Map<string, HatStyleInfo>();
for (const color of HAT_COLORS) {
const colorPenalty = color === "default" ? 0 : 1;
for (const color of this.enabledHatColors) {
const colorPenalty = this.getColorPenalty(color);
for (const shape of this.enabledHatShapes) {
const shapePenalty = shape === "default" ? 0 : 2;
const shapePenalty = this.getShapePenalty(shape);
let styleName: string;
if (shape === "default") {
styleName = color;
Expand All @@ -78,6 +105,24 @@ export class JetbrainsHats implements Hats {
return Object.fromEntries(res);
}

private getShapePenalty(shape: string) {
let shapePenalty = this.hatShapePenalties.get(shape);
if (shapePenalty == null) {
shapePenalty = shape === "default" ? 0 : 2;
} else {
shapePenalty = shape === "default" ? shapePenalty : shapePenalty + 1;
}
return shapePenalty;
}

private getColorPenalty(color: string) {
let colorPenalty = this.hatColorPenalties.get(color);
if (colorPenalty == null) {
colorPenalty = color === "default" ? 0 : 1;
}
return colorPenalty;
}

onDidChangeEnabledHatStyles(listener: Listener<[HatStyleMap]>): Disposable {
return this.hatStyleChangedNotifier.registerListener(listener);
}
Expand Down

0 comments on commit cfe93f9

Please sign in to comment.