Repository navigation
feat(desktop): add VSCode-style scroll indicators to diff viewer #2378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
250 changes: 250 additions & 0 deletions
250
...ns/main/components/WorkspaceView/components/DiffScrollIndicators/DiffScrollIndicators.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| import { parseDiffFromFile } from "@pierre/diffs"; | ||
| import { | ||
| type RefObject, | ||
| useCallback, | ||
| useEffect, | ||
| useMemo, | ||
| useRef, | ||
| useState, | ||
| } from "react"; | ||
| import { MIDNIGHT_CODE_COLORS } from "renderer/screens/main/components/WorkspaceView/components/CodeEditor/constants"; | ||
|
|
||
| type ChangeType = "addition" | "deletion" | "modified"; | ||
|
|
||
| interface ChangeRegion { | ||
| startLine: number; | ||
| lineCount: number; | ||
| type: ChangeType; | ||
| } | ||
|
|
||
| interface DiffScrollIndicatorsProps { | ||
| scrollRef: RefObject<HTMLElement | null>; | ||
| original: string; | ||
| modified: string; | ||
| filePath: string; | ||
| } | ||
|
|
||
| const STRIP_WIDTH = 14; | ||
| const MIN_BAND_PX = 3; | ||
| const COLUMN_GAP = 3; | ||
| const TRACK_BG = "#1e2127"; | ||
| const TRACK_BG_HOVER = "#22262e"; | ||
| const VIEWPORT_THRESHOLD = 0.95; | ||
| const MIN_THUMB_PX = 20; | ||
|
|
||
| const DELETION_COLOR = `${MIDNIGHT_CODE_COLORS.deletion}cc`; | ||
| const ADDITION_COLOR = `${MIDNIGHT_CODE_COLORS.addition}cc`; | ||
|
|
||
| function computeRegions( | ||
| original: string, | ||
| modified: string, | ||
| filePath: string, | ||
| ): { regions: Array<ChangeRegion>; totalLines: number } { | ||
| const diff = parseDiffFromFile( | ||
| { name: filePath, contents: original }, | ||
| { name: filePath, contents: modified }, | ||
| ); | ||
|
|
||
| const modifiedLineCount = modified.split("\n").length; | ||
| const totalLines = Math.max(modifiedLineCount, 1); | ||
| const regions: Array<ChangeRegion> = []; | ||
|
|
||
| for (const hunk of diff.hunks) { | ||
| let currentLine = hunk.additionStart; | ||
|
|
||
| for (const chunk of hunk.hunkContent) { | ||
| if (chunk.type === "context") { | ||
| currentLine += chunk.lines.length; | ||
| continue; | ||
| } | ||
|
|
||
| const hasDeletions = chunk.deletions.length > 0; | ||
| const hasAdditions = chunk.additions.length > 0; | ||
|
|
||
| if (hasDeletions && hasAdditions) { | ||
| regions.push({ | ||
| startLine: currentLine, | ||
| lineCount: chunk.additions.length, | ||
| type: "modified", | ||
| }); | ||
| currentLine += chunk.additions.length; | ||
| } else if (hasDeletions) { | ||
| regions.push({ | ||
| startLine: currentLine, | ||
| lineCount: Math.max(chunk.deletions.length, 1), | ||
| type: "deletion", | ||
| }); | ||
| } else if (hasAdditions) { | ||
| regions.push({ | ||
| startLine: currentLine, | ||
| lineCount: chunk.additions.length, | ||
| type: "addition", | ||
| }); | ||
| currentLine += chunk.additions.length; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { regions, totalLines }; | ||
| } | ||
|
|
||
| function renderBand( | ||
| region: ChangeRegion, | ||
| topPercent: number, | ||
| heightPercent: number, | ||
| ) { | ||
| const bandStyle = { | ||
| top: `${topPercent}%`, | ||
| height: `max(${MIN_BAND_PX}px, ${heightPercent}%)`, | ||
| borderRadius: 1, | ||
| }; | ||
|
|
||
| if (region.type === "modified") { | ||
| return ( | ||
| <div | ||
| key={`mod-${region.startLine}`} | ||
| className="absolute" | ||
| style={{ ...bandStyle, left: 0, right: 0, display: "flex" }} | ||
| > | ||
| <div | ||
| className="h-full" | ||
| style={{ flex: 1, backgroundColor: DELETION_COLOR, borderRadius: 1 }} | ||
| /> | ||
| <div style={{ width: COLUMN_GAP }} /> | ||
| <div | ||
| className="h-full" | ||
| style={{ flex: 1, backgroundColor: ADDITION_COLOR, borderRadius: 1 }} | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| const isDeletion = region.type === "deletion"; | ||
|
|
||
| return ( | ||
| <div | ||
| key={`${region.type}-${region.startLine}`} | ||
| className="absolute" | ||
| style={{ | ||
| ...bandStyle, | ||
| left: isDeletion ? 0 : `calc(50% + ${COLUMN_GAP / 2}px)`, | ||
| right: isDeletion ? `calc(50% + ${COLUMN_GAP / 2}px)` : 0, | ||
| backgroundColor: isDeletion ? DELETION_COLOR : ADDITION_COLOR, | ||
| }} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export function DiffScrollIndicators({ | ||
| scrollRef, | ||
| original, | ||
| modified, | ||
| filePath, | ||
| }: DiffScrollIndicatorsProps) { | ||
| const [viewportRatio, setViewportRatio] = useState({ top: 0, height: 1 }); | ||
| const trackRef = useRef<HTMLDivElement | null>(null); | ||
|
|
||
| const { regions, totalLines } = useMemo( | ||
| () => computeRegions(original, modified, filePath), | ||
| [original, modified, filePath], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| const el = scrollRef.current; | ||
| if (!el) return; | ||
|
|
||
| const updateViewport = () => { | ||
| const sh = el.scrollHeight; | ||
| if (sh > 0) { | ||
| setViewportRatio({ | ||
| top: el.scrollTop / sh, | ||
| height: el.clientHeight / sh, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| updateViewport(); | ||
| el.addEventListener("scroll", updateViewport, { passive: true }); | ||
| const resizeObserver = new ResizeObserver(updateViewport); | ||
| resizeObserver.observe(el); | ||
|
|
||
| return () => { | ||
| el.removeEventListener("scroll", updateViewport); | ||
| resizeObserver.disconnect(); | ||
| }; | ||
| }, [scrollRef]); | ||
|
|
||
| const handleClick = useCallback( | ||
| (event: React.MouseEvent) => { | ||
| const el = scrollRef.current; | ||
| const track = trackRef.current; | ||
| if (!el || !track) return; | ||
|
|
||
| const rect = track.getBoundingClientRect(); | ||
| const ratio = (event.clientY - rect.top) / rect.height; | ||
| const targetScroll = ratio * el.scrollHeight - el.clientHeight / 2; | ||
| const prefersReduced = window.matchMedia( | ||
| "(prefers-reduced-motion: reduce)", | ||
| ).matches; | ||
| el.scrollTo({ | ||
| top: targetScroll, | ||
| behavior: prefersReduced ? "auto" : "smooth", | ||
| }); | ||
| }, | ||
| [scrollRef], | ||
| ); | ||
|
|
||
| function handleMouseEnter() { | ||
| if (trackRef.current) { | ||
| trackRef.current.style.backgroundColor = TRACK_BG_HOVER; | ||
| } | ||
| } | ||
|
|
||
| function handleMouseLeave() { | ||
| if (trackRef.current) { | ||
| trackRef.current.style.backgroundColor = TRACK_BG; | ||
| } | ||
| } | ||
|
|
||
| if (regions.length === 0) return null; | ||
|
|
||
| const showViewportThumb = viewportRatio.height < VIEWPORT_THRESHOLD; | ||
|
|
||
| return ( | ||
| <div | ||
| ref={trackRef} | ||
| aria-hidden="true" | ||
| className="shrink-0 cursor-pointer" | ||
| style={{ | ||
| width: STRIP_WIDTH, | ||
| backgroundColor: TRACK_BG, | ||
| borderLeft: `1px solid ${MIDNIGHT_CODE_COLORS.border}`, | ||
| position: "relative", | ||
| overflow: "hidden", | ||
| transition: "background-color 150ms", | ||
| }} | ||
| onClick={handleClick} | ||
| onMouseEnter={handleMouseEnter} | ||
| onMouseLeave={handleMouseLeave} | ||
| > | ||
| {regions.map((region) => { | ||
| const topPercent = ((region.startLine - 1) / totalLines) * 100; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Clamp region start/size before converting to percentages so deletion markers at EOF are still visible in the minimap. Prompt for AI agents |
||
| const heightPercent = (region.lineCount / totalLines) * 100; | ||
| return renderBand(region, topPercent, heightPercent); | ||
| })} | ||
|
|
||
| {showViewportThumb && ( | ||
| <div | ||
| className="absolute left-0 right-0 pointer-events-none" | ||
| style={{ | ||
| top: `${viewportRatio.top * 100}%`, | ||
| height: `max(${MIN_THUMB_PX}px, ${viewportRatio.height * 100}%)`, | ||
| backgroundColor: "rgba(255, 255, 255, 0.08)", | ||
| borderTop: "1px solid rgba(255, 255, 255, 0.15)", | ||
| borderBottom: "1px solid rgba(255, 255, 255, 0.15)", | ||
| }} | ||
| /> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
1 change: 1 addition & 0 deletions
1
...c/renderer/screens/main/components/WorkspaceView/components/DiffScrollIndicators/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { DiffScrollIndicators } from "./DiffScrollIndicators"; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: By wrapping both the diff container and
DiffScrollIndicatorsinsideDiffViewerContextMenu, the diff's context menu will unexpectedly trigger when right-clicking the scroll indicators. This breaks menu actions: 'Edit Here' will jump to a stale location (the last line right-clicked in the diff), and 'Select All' will select text in the adjacent diff viewer. Move the flex layout wrapper outside ofDiffViewerContextMenuso the menu is strictly bound to the diff container.Prompt for AI agents