99 Token ,
1010 TokenHat ,
1111} from "@cursorless/common" ;
12- import { clone } from "lodash" ;
1312import { Grapheme , TokenGraphemeSplitter } from "../../tokenGraphemeSplitter" ;
1413import { chooseTokenHat } from "./chooseTokenHat" ;
1514import { getHatRankingContext } from "./getHatRankingContext" ;
@@ -74,8 +73,9 @@ export function allocateHats(
7473 * used for that grapheme. As we assign hats to tokens, we remove them from
7574 * these lists so that they don't get used again in this pass.
7675 */
77- const graphemeRemainingHatCandidates = new DefaultMap < string , HatStyleMap > (
78- ( ) => clone ( enabledHatStyles ) ,
76+ const allEnabledHatStyles = Object . keys ( enabledHatStyles ) ;
77+ const graphemeRemainingHatCandidates = new DefaultMap < string , HatStyleName [ ] > (
78+ ( ) => [ ...allEnabledHatStyles ] ,
7979 ) ;
8080
8181 // Iterate through tokens in order of decreasing rank, assigning each one a
@@ -90,6 +90,7 @@ export function allocateHats(
9090 tokenGraphemeSplitter ,
9191 token ,
9292 graphemeRemainingHatCandidates ,
93+ enabledHatStyles ,
9394 ) ;
9495
9596 const chosenHat = chooseTokenHat (
@@ -107,10 +108,12 @@ export function allocateHats(
107108 }
108109
109110 // Remove the hat we chose from consideration for lower ranked tokens
110- delete graphemeRemainingHatCandidates . get ( chosenHat . grapheme . text ) [
111- chosenHat . style
112- ] ;
113-
111+ graphemeRemainingHatCandidates . set (
112+ chosenHat . grapheme . text ,
113+ graphemeRemainingHatCandidates
114+ . get ( chosenHat . grapheme . text )
115+ . filter ( ( style ) => style !== chosenHat . style ) ,
116+ ) ;
114117 return constructHatRangeDescriptor ( token , chosenHat ) ;
115118 } )
116119 . filter ( ( value ) : value is TokenHat => value != null ) ;
@@ -135,19 +138,21 @@ function getTokenOldHatMap(oldTokenHats: readonly TokenHat[]) {
135138function getTokenRemainingHatCandidates (
136139 tokenGraphemeSplitter : TokenGraphemeSplitter ,
137140 token : Token ,
138- availableGraphemeStyles : DefaultMap < string , HatStyleMap > ,
141+ availableGraphemeStyles : DefaultMap < string , HatStyleName [ ] > ,
142+ allHatStyles : HatStyleMap ,
139143) : HatCandidate [ ] {
140- return tokenGraphemeSplitter
141- . getTokenGraphemes ( token . text )
142- . flatMap ( ( grapheme ) =>
143- Object . entries ( availableGraphemeStyles . get ( grapheme . text ) ) . map (
144- ( [ style , { penalty } ] ) => ( {
145- grapheme,
146- style,
147- penalty,
148- } ) ,
149- ) ,
150- ) ;
144+ const candidates : HatCandidate [ ] = [ ] ;
145+ const graphemes = tokenGraphemeSplitter . getTokenGraphemes ( token . text ) ;
146+ for ( const grapheme of graphemes ) {
147+ for ( const style of availableGraphemeStyles . get ( grapheme . text ) ! ) {
148+ candidates . push ( {
149+ grapheme,
150+ style,
151+ penalty : allHatStyles [ style ] . penalty ,
152+ } ) ;
153+ }
154+ }
155+ return candidates ;
151156}
152157
153158/**
0 commit comments