Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit d6664ac

Browse files
committed
feat(hoverifier): support diff views without diff indicators
1 parent 9ad0fa1 commit d6664ac

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/positions.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ export const findPositionsFromEvents = (options: DOMFunctions) => (
6767
).pipe(
6868
// Find out the position that was hovered over
6969
map(({ target, codeView, ...rest }) => {
70-
const hoveredToken = locateTarget(target, {
71-
ignoreFirstChar: !!options.getDiffCodePart,
72-
...options,
73-
})
70+
const hoveredToken = locateTarget(target, options)
7471
const position = Position.is(hoveredToken) ? hoveredToken : undefined
7572
return { position, codeView, ...rest }
7673
})

src/token_position.ts

+20-10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ export interface DOMFunctions {
4141
* @returns The part of the diff `codeElement` belongs to
4242
*/
4343
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
4452
}
4553

4654
/**
@@ -229,10 +237,6 @@ export interface HoveredToken {
229237
part?: DiffPart
230238
}
231239

232-
export interface LocateTargetOptions extends DOMFunctions {
233-
ignoreFirstChar?: boolean
234-
}
235-
236240
/**
237241
* Determines the line and character offset for some source code, identified by its HTMLElement wrapper.
238242
* 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 {
243247
*/
244248
export function locateTarget(
245249
target: HTMLElement,
246-
{ ignoreFirstChar, getCodeElementFromTarget, getDiffCodePart, getLineNumberFromCodeElement }: LocateTargetOptions
250+
{
251+
getCodeElementFromTarget,
252+
getLineNumberFromCodeElement,
253+
getDiffCodePart,
254+
isFirstCharacterDiffIndicator,
255+
}: DOMFunctions
247256
): Line | HoveredToken | undefined {
248257
const codeElement = getCodeElementFromTarget(target)
249258

@@ -261,6 +270,7 @@ export function locateTarget(
261270
}
262271

263272
const part = getDiffCodePart && getDiffCodePart(codeElement)
273+
let ignoreFirstCharacter = !!isFirstCharacterDiffIndicator && isFirstCharacterDiffIndicator(codeElement)
264274

265275
let character = 1
266276
// Iterate recursively over the current target's children until we find the original target;
@@ -286,9 +296,9 @@ export function locateTarget(
286296
if (child.children.length === 0) {
287297
// Child is not the original target, but has no chidren to recurse on. Add to character offset.
288298
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
292302
}
293303
}
294304
}
@@ -344,15 +354,15 @@ export const getCodeElementsInRange = ({
344354
export const getTokenAtPosition = (
345355
codeView: HTMLElement,
346356
{ line, character }: Position,
347-
{ getCodeElementFromLineNumber, getDiffCodePart }: DOMFunctions,
357+
{ getCodeElementFromLineNumber, isFirstCharacterDiffIndicator }: DOMFunctions,
348358
part?: DiffPart
349359
): HTMLElement | undefined => {
350360
const codeElement = getCodeElementFromLineNumber(codeView, line, part)
351361
if (!codeElement) {
352362
return undefined
353363
}
354364
// On diff pages, account for the +/- indicator
355-
if (getDiffCodePart) {
365+
if (isFirstCharacterDiffIndicator && isFirstCharacterDiffIndicator(codeElement)) {
356366
character++
357367
}
358368
return findElementWithOffset(codeElement, character)

0 commit comments

Comments
 (0)