9
9
Token ,
10
10
TokenHat ,
11
11
} from "@cursorless/common" ;
12
- import { clone } from "lodash" ;
13
12
import { Grapheme , TokenGraphemeSplitter } from "../../tokenGraphemeSplitter" ;
14
13
import { chooseTokenHat } from "./chooseTokenHat" ;
15
14
import { getHatRankingContext } from "./getHatRankingContext" ;
@@ -74,8 +73,9 @@ export function allocateHats(
74
73
* used for that grapheme. As we assign hats to tokens, we remove them from
75
74
* these lists so that they don't get used again in this pass.
76
75
*/
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 ] ,
79
79
) ;
80
80
81
81
// Iterate through tokens in order of decreasing rank, assigning each one a
@@ -90,6 +90,7 @@ export function allocateHats(
90
90
tokenGraphemeSplitter ,
91
91
token ,
92
92
graphemeRemainingHatCandidates ,
93
+ enabledHatStyles ,
93
94
) ;
94
95
95
96
const chosenHat = chooseTokenHat (
@@ -107,10 +108,12 @@ export function allocateHats(
107
108
}
108
109
109
110
// 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
+ ) ;
114
117
return constructHatRangeDescriptor ( token , chosenHat ) ;
115
118
} )
116
119
. filter ( ( value ) : value is TokenHat => value != null ) ;
@@ -135,19 +138,21 @@ function getTokenOldHatMap(oldTokenHats: readonly TokenHat[]) {
135
138
function getTokenRemainingHatCandidates (
136
139
tokenGraphemeSplitter : TokenGraphemeSplitter ,
137
140
token : Token ,
138
- availableGraphemeStyles : DefaultMap < string , HatStyleMap > ,
141
+ availableGraphemeStyles : DefaultMap < string , HatStyleName [ ] > ,
142
+ allHatStyles : HatStyleMap ,
139
143
) : 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 ;
151
156
}
152
157
153
158
/**
0 commit comments