@@ -7,6 +7,7 @@ import type {
7
7
TokenHat ,
8
8
} from "@cursorless/common" ;
9
9
import { CompositeKeyMap , DefaultMap , Range } from "@cursorless/common" ;
10
+ import { WordTokenizer } from "../../processTargets/modifiers/scopeHandlers/WordScopeHandler/WordTokenizer" ;
10
11
import type {
11
12
Grapheme ,
12
13
TokenGraphemeSplitter ,
@@ -19,6 +20,7 @@ export interface HatCandidate {
19
20
grapheme : Grapheme ;
20
21
style : HatStyleName ;
21
22
penalty : number ;
23
+ isFirstLetter : boolean ;
22
24
}
23
25
24
26
interface AllocateHatsOptions {
@@ -169,12 +171,21 @@ function getTokenRemainingHatCandidates(
169
171
graphemeRemainingHatCandidates : DefaultMap < string , HatStyleName [ ] > ,
170
172
enabledHatStyles : HatStyleMap ,
171
173
) : HatCandidate [ ] {
174
+ const candidates : HatCandidate [ ] = [ ] ;
175
+ const graphemes = tokenGraphemeSplitter . getTokenGraphemes ( token . text ) ;
176
+ const firstLetterOffsets = new Set (
177
+ new WordTokenizer ( token . editor . document . languageId )
178
+ . splitIdentifier ( token . text )
179
+ . map ( ( word ) => word . index ) ,
180
+ ) ;
181
+
172
182
// Use iteration here instead of functional constructs,
173
183
// because this is a hot path and we want to avoid allocating arrays
174
184
// and calling tiny functions lots of times.
175
- const candidates : HatCandidate [ ] = [ ] ;
176
- const graphemes = tokenGraphemeSplitter . getTokenGraphemes ( token . text ) ;
185
+
177
186
for ( const grapheme of graphemes ) {
187
+ const isFirstLetter = firstLetterOffsets . has ( grapheme . tokenStartOffset ) ;
188
+
178
189
for ( const style of graphemeRemainingHatCandidates . get ( grapheme . text ) ) {
179
190
// Allocating and pushing all of these objects is
180
191
// the single most expensive thing in hat allocation.
@@ -183,9 +194,11 @@ function getTokenRemainingHatCandidates(
183
194
grapheme,
184
195
style,
185
196
penalty : enabledHatStyles [ style ] . penalty ,
197
+ isFirstLetter,
186
198
} ) ;
187
199
}
188
200
}
201
+
189
202
return candidates ;
190
203
}
191
204
0 commit comments