Skip to content

Commit 38f51fd

Browse files
remove token and grapheme splitter singleton
1 parent ed8f043 commit 38f51fd

5 files changed

Lines changed: 32 additions & 35 deletions

File tree

packages/cursorless-engine/src/core/HatAllocator.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Disposable, Hats, IDE, TokenHat } from "@cursorless/common";
2-
import tokenGraphemeSplitter from "../singletons/tokenGraphemeSplitter.singleton";
2+
import type { TokenGraphemeSplitter } from "../tokenGraphemeSplitter";
33
import { allocateHats } from "../util/allocateHats";
44
import type { IndividualHatMap } from "./IndividualHatMap";
55
import { DecorationDebouncer } from "../util/DecorationDebouncer";
@@ -13,6 +13,7 @@ export class HatAllocator {
1313

1414
constructor(
1515
private ide: IDE,
16+
private tokenGraphemeSplitter: TokenGraphemeSplitter,
1617
private hats: Hats,
1718
private context: Context,
1819
) {
@@ -42,7 +43,7 @@ export class HatAllocator {
4243
ide.onDidChangeTextEditorVisibleRanges(debouncer.run),
4344
// Re-draw hats on grapheme splitting algorithm change in case they
4445
// changed their token hat splitting setting.
45-
tokenGraphemeSplitter(ide).registerAlgorithmChangeListener(debouncer.run),
46+
tokenGraphemeSplitter.registerAlgorithmChangeListener(debouncer.run),
4647

4748
debouncer,
4849
);
@@ -60,15 +61,13 @@ export class HatAllocator {
6061
// Forced graphemes won't have been normalized
6162
forceTokenHats = forceTokenHats?.map((tokenHat) => ({
6263
...tokenHat,
63-
grapheme: tokenGraphemeSplitter(this.ide).normalizeGrapheme(
64-
tokenHat.grapheme,
65-
),
64+
grapheme: this.tokenGraphemeSplitter.normalizeGrapheme(tokenHat.grapheme),
6665
}));
6766

6867
const tokenHats = this.hats.isEnabled
6968
? allocateHats({
7069
ide: this.ide,
71-
tokenGraphemeSplitter: tokenGraphemeSplitter(this.ide),
70+
tokenGraphemeSplitter: this.tokenGraphemeSplitter,
7271
enabledHatStyles: this.hats.enabledHatStyles,
7372
forceTokenHats,
7473
oldTokenHats: activeMap.tokenHats,

packages/cursorless-engine/src/core/HatTokenMapImpl.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
ReadOnlyHatMap,
77
TokenHat,
88
} from "@cursorless/common";
9+
import type { TokenGraphemeSplitter } from "../tokenGraphemeSplitter";
910
import type { Debug } from "./Debug";
1011
import { HatAllocator } from "./HatAllocator";
1112
import { IndividualHatMap } from "./IndividualHatMap";
@@ -39,20 +40,30 @@ export class HatTokenMapImpl implements HatTokenMap {
3940

4041
constructor(
4142
private ide: IDE,
43+
tokenGraphemeSplitter: TokenGraphemeSplitter,
4244
rangeUpdater: RangeUpdater,
4345
private debug: Debug,
4446
hats: Hats,
4547
private commandServerApi: CommandServerApi,
4648
) {
4749
ide.disposeOnExit(this);
48-
this.activeMap = new IndividualHatMap(ide, rangeUpdater);
50+
this.activeMap = new IndividualHatMap(
51+
ide,
52+
tokenGraphemeSplitter,
53+
rangeUpdater,
54+
);
4955

5056
this.getActiveMap = this.getActiveMap.bind(this);
5157
this.allocateHats = this.allocateHats.bind(this);
5258

53-
this.hatAllocator = new HatAllocator(ide, hats, {
54-
getActiveMap: this.getActiveMap,
55-
});
59+
this.hatAllocator = new HatAllocator(
60+
ide,
61+
tokenGraphemeSplitter,
62+
hats,
63+
{
64+
getActiveMap: this.getActiveMap,
65+
},
66+
);
5667
}
5768

5869
/**

packages/cursorless-engine/src/core/IndividualHatMap.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
TokenHat,
88
} from "@cursorless/common";
99
import { getKey } from "@cursorless/common";
10-
import tokenGraphemeSplitter from "../singletons/tokenGraphemeSplitter.singleton";
10+
import type { TokenGraphemeSplitter } from "../tokenGraphemeSplitter";
1111
import { getMatcher } from "../tokenizer";
1212
import type { FullRangeInfo } from "../typings/updateSelections";
1313
import type { RangeUpdater } from "./updateSelections/RangeUpdater";
@@ -35,6 +35,7 @@ export class IndividualHatMap implements ReadOnlyHatMap {
3535

3636
constructor(
3737
private ide: IDE,
38+
private tokenGraphemeSplitter: TokenGraphemeSplitter,
3839
private rangeUpdater: RangeUpdater,
3940
) {}
4041

@@ -54,7 +55,11 @@ export class IndividualHatMap implements ReadOnlyHatMap {
5455
}
5556

5657
clone() {
57-
const ret = new IndividualHatMap(this.ide, this.rangeUpdater);
58+
const ret = new IndividualHatMap(
59+
this.ide,
60+
this.tokenGraphemeSplitter,
61+
this.rangeUpdater,
62+
);
5863

5964
ret.setTokenHats(this._tokenHats);
6065

@@ -119,10 +124,7 @@ export class IndividualHatMap implements ReadOnlyHatMap {
119124
getToken(hatStyle: HatStyleName, character: string): Token | undefined {
120125
this.checkExpired();
121126
return this.map[
122-
getKey(
123-
hatStyle,
124-
tokenGraphemeSplitter(this.ide).normalizeGrapheme(character),
125-
)
127+
getKey(hatStyle, this.tokenGraphemeSplitter.normalizeGrapheme(character))
126128
];
127129
}
128130

packages/cursorless-engine/src/cursorlessEngine.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { ScopeRangeProvider } from "./scopeProviders/ScopeRangeProvider";
3838
import { ScopeRangeWatcher } from "./scopeProviders/ScopeRangeWatcher";
3939
import { ScopeSupportChecker } from "./scopeProviders/ScopeSupportChecker";
4040
import { ScopeSupportWatcher } from "./scopeProviders/ScopeSupportWatcher";
41+
import { TokenGraphemeSplitter } from "./tokenGraphemeSplitter/tokenGraphemeSplitter";
4142

4243
export interface EngineProps {
4344
ide: IDE;
@@ -62,6 +63,7 @@ export async function createCursorlessEngine({
6263

6364
const debug = new Debug(injectedIde);
6465
const rangeUpdater = new RangeUpdater(injectedIde);
66+
const tokenGraphemeSplitter = new TokenGraphemeSplitter(injectedIde);
6567

6668
const storedTargets = new StoredTargetMap();
6769
const keyboardTargetUpdater = new KeyboardTargetUpdater(
@@ -77,6 +79,7 @@ export async function createCursorlessEngine({
7779
hats != null
7880
? new HatTokenMapImpl(
7981
injectedIde,
82+
tokenGraphemeSplitter,
8083
rangeUpdater,
8184
debug,
8285
hats,
@@ -95,6 +98,7 @@ export async function createCursorlessEngine({
9598

9699
injectedIde.disposeOnExit(
97100
rangeUpdater,
101+
tokenGraphemeSplitter,
98102
languageDefinitions,
99103
hatTokenMap,
100104
debug,

packages/cursorless-engine/src/singletons/tokenGraphemeSplitter.singleton.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)