@@ -41,6 +41,14 @@ export interface DOMFunctions {
41
41
* @returns The part of the diff `codeElement` belongs to
42
42
*/
43
43
getDiffCodePart ?: ( codeElement : HTMLElement ) => DiffPart
44
+
45
+ /**
46
+ * Must return `true` if the first character in the code element is always
47
+ * the diff indicator (`+`, `-` or space), `false` otherwise.
48
+ *
49
+ * @param codeElement is the element containing a line of code.
50
+ */
51
+ isFirstCharacterDiffIndicator ?( codeElement : HTMLElement ) : boolean
44
52
}
45
53
46
54
/**
@@ -229,10 +237,6 @@ export interface HoveredToken {
229
237
part ?: DiffPart
230
238
}
231
239
232
- export interface LocateTargetOptions extends DOMFunctions {
233
- ignoreFirstChar ?: boolean
234
- }
235
-
236
240
/**
237
241
* Determines the line and character offset for some source code, identified by its HTMLElement wrapper.
238
242
* It works by traversing the DOM until the HTMLElement's TD ancestor. Once the ancestor is found, we traverse the DOM again
@@ -243,7 +247,12 @@ export interface LocateTargetOptions extends DOMFunctions {
243
247
*/
244
248
export function locateTarget (
245
249
target : HTMLElement ,
246
- { ignoreFirstChar, getCodeElementFromTarget, getDiffCodePart, getLineNumberFromCodeElement } : LocateTargetOptions
250
+ {
251
+ getCodeElementFromTarget,
252
+ getLineNumberFromCodeElement,
253
+ getDiffCodePart,
254
+ isFirstCharacterDiffIndicator,
255
+ } : DOMFunctions
247
256
) : Line | HoveredToken | undefined {
248
257
const codeElement = getCodeElementFromTarget ( target )
249
258
@@ -261,6 +270,7 @@ export function locateTarget(
261
270
}
262
271
263
272
const part = getDiffCodePart && getDiffCodePart ( codeElement )
273
+ let ignoreFirstCharacter = ! ! isFirstCharacterDiffIndicator && isFirstCharacterDiffIndicator ( codeElement )
264
274
265
275
let character = 1
266
276
// Iterate recursively over the current target's children until we find the original target;
@@ -286,9 +296,9 @@ export function locateTarget(
286
296
if ( child . children . length === 0 ) {
287
297
// Child is not the original target, but has no chidren to recurse on. Add to character offset.
288
298
character += ( child . textContent as string ) . length // TODO(john): I think this needs to be escaped before we add its length...
289
- if ( ignoreFirstChar ) {
290
- character -= 1 // make sure not to count weird diff prefix
291
- ignoreFirstChar = false
299
+ if ( ignoreFirstCharacter ) {
300
+ character -= 1
301
+ ignoreFirstCharacter = false
292
302
}
293
303
}
294
304
}
@@ -344,15 +354,15 @@ export const getCodeElementsInRange = ({
344
354
export const getTokenAtPosition = (
345
355
codeView : HTMLElement ,
346
356
{ line, character } : Position ,
347
- { getCodeElementFromLineNumber, getDiffCodePart } : DOMFunctions ,
357
+ { getCodeElementFromLineNumber, isFirstCharacterDiffIndicator } : DOMFunctions ,
348
358
part ?: DiffPart
349
359
) : HTMLElement | undefined => {
350
360
const codeElement = getCodeElementFromLineNumber ( codeView , line , part )
351
361
if ( ! codeElement ) {
352
362
return undefined
353
363
}
354
364
// On diff pages, account for the +/- indicator
355
- if ( getDiffCodePart ) {
365
+ if ( isFirstCharacterDiffIndicator && isFirstCharacterDiffIndicator ( codeElement ) ) {
356
366
character ++
357
367
}
358
368
return findElementWithOffset ( codeElement , character )
0 commit comments