diff --git a/cptr/frontend/src/lib/components/FileEditor.svelte b/cptr/frontend/src/lib/components/FileEditor.svelte index 505ff57..af82583 100644 --- a/cptr/frontend/src/lib/components/FileEditor.svelte +++ b/cptr/frontend/src/lib/components/FileEditor.svelte @@ -20,6 +20,8 @@ import SvgPreview from './preview/SvgPreview.svelte'; import OfficePreview from './preview/OfficePreview.svelte'; import Spinner from './common/Spinner.svelte'; + import { groupDiffLines, languageForPath, withInlineDiffSegments } from '$lib/utils/diff'; + import SyntaxDiffLine from './SyntaxDiffLine.svelte'; import { EditorState, StateEffect, @@ -158,7 +160,6 @@ type DiffHunk = { header: string; lines: DiffLine[] }; type DiffFileEntry = { path: string; hunks: DiffHunk[] }; type NumberedLine = DiffLine & { oldNumber: number | null; newNumber: number | null }; - type LineGroup = { type: DiffLine['type']; lines: NumberedLine[] }; type GitLineChange = { line: number; type: 'added' | 'modified' }; let diffMode = $state(false); let diffFiles = $state([]); @@ -265,16 +266,6 @@ }); } - function diffGroupLines(lines: NumberedLine[]): LineGroup[] { - const groups: LineGroup[] = []; - for (const line of lines) { - const last = groups[groups.length - 1]; - if (last && last.type === line.type) last.lines.push(line); - else groups.push({ type: line.type, lines: [line] }); - } - return groups; - } - function splitLines(text: string): string[] { return text.length === 0 ? [] : text.split('\n'); } @@ -1235,7 +1226,7 @@ {hunk.header} - {#each diffGroupLines(diffNumberedLines(hunk)) as group} + {#each groupDiffLines(withInlineDiffSegments(diffNumberedLines(hunk))) as group}
{#each group.lines as line}
{diffPrefix(line.type)} - {line.content || ' '} +
{/each}
diff --git a/cptr/frontend/src/lib/components/GitBar.svelte b/cptr/frontend/src/lib/components/GitBar.svelte index b3b3607..b9cd9e8 100644 --- a/cptr/frontend/src/lib/components/GitBar.svelte +++ b/cptr/frontend/src/lib/components/GitBar.svelte @@ -32,6 +32,15 @@ import { tooltip } from '$lib/tooltip'; import { t } from '$lib/i18n'; import Spinner from '$lib/components/common/Spinner.svelte'; + import { + groupDiffLines, + languageForPath, + numberDiffLines, + withInlineDiffSegments, + type DiffFile, + type DiffLine + } from '$lib/utils/diff'; + import SyntaxDiffLine from './SyntaxDiffLine.svelte'; type GitFile = { path: string; @@ -41,8 +50,6 @@ staged_status?: string; unstaged_status?: string; }; - type DiffHunk = { header: string; lines: { type: string; content: string }[] }; - type DiffFile = { path: string; hunks: DiffHunk[] }; type Commit = { hash: string; short_hash: string; author: string; date: string; message: string }; type BranchItem = { name: string; @@ -710,22 +717,7 @@ } } - type LineGroup = { type: string; lines: { type: string; content: string }[] }; - - function groupLines(lines: { type: string; content: string }[]): LineGroup[] { - const groups: LineGroup[] = []; - for (const line of lines) { - const last = groups[groups.length - 1]; - if (last && last.type === line.type) { - last.lines.push(line); - } else { - groups.push({ type: line.type, lines: [line] }); - } - } - return groups; - } - - function blockClass(type: string): string { + function blockClass(type: DiffLine['type']): string { switch (type) { case 'added': return 'bg-green-100 border-l-[0.1875rem] border-l-green-500 dark:bg-green-500/15 dark:border-l-green-400'; @@ -736,6 +728,24 @@ } } + function textClass(type: DiffLine['type']): string { + if (type === 'added') return 'text-green-900 dark:text-green-300'; + if (type === 'removed') return 'text-red-900 dark:text-red-300'; + return 'text-gray-600 dark:text-gray-400'; + } + + function prefixClass(type: DiffLine['type']): string { + if (type === 'added') return 'text-green-600 dark:text-green-400'; + if (type === 'removed') return 'text-red-500 dark:text-red-400'; + return 'text-gray-400 dark:text-gray-600'; + } + + function linePrefix(type: DiffLine['type']): string { + if (type === 'added') return '+'; + if (type === 'removed') return '-'; + return ' '; + } + $effect(() => { if (!expanded) return; function onKeydown(e: KeyboardEvent) { @@ -1412,28 +1422,20 @@ > {hunk.header} - {#each groupLines(hunk.lines) as group} + {#each groupDiffLines(withInlineDiffSegments(numberDiffLines(hunk))) as group}
{#each group.lines as line} -
- {line.type === 'added' - ? '+' - : line.type === 'removed' - ? '-' - : ' '}{line.content} + {linePrefix(line.type)} +
{/each}
diff --git a/cptr/frontend/src/lib/components/GitView.svelte b/cptr/frontend/src/lib/components/GitView.svelte index 78246c3..41ce9bc 100644 --- a/cptr/frontend/src/lib/components/GitView.svelte +++ b/cptr/frontend/src/lib/components/GitView.svelte @@ -5,12 +5,18 @@ import { tooltip } from '$lib/tooltip'; import { t } from '$lib/i18n'; + import { + groupDiffLines, + languageForPath, + numberDiffLines, + withInlineDiffSegments, + type DiffFile, + type DiffLine + } from '$lib/utils/diff'; import Icon from './Icon.svelte'; import Spinner from '$lib/components/common/Spinner.svelte'; + import SyntaxDiffLine from './SyntaxDiffLine.svelte'; - type DiffLine = { type: 'added' | 'removed' | 'context'; content: string }; - type DiffHunk = { header: string; lines: DiffLine[] }; - type DiffFile = { path: string; hunks: DiffHunk[] }; type ReviewFile = { key: string; path: string; @@ -21,7 +27,6 @@ deletions: number; expanded: boolean; }; - type NumberedLine = DiffLine & { oldNumber: number | null; newNumber: number | null }; let gitStatus = $derived(gitStatusStore.status); let reviewFiles = $state([]); @@ -178,28 +183,6 @@ } } - function hunkStart(header: string): { oldStart: number; newStart: number } { - const match = header.match(/^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/); - return { - oldStart: match ? Number(match[1]) : 0, - newStart: match ? Number(match[2]) : 0 - }; - } - - function numberedLines(hunk: DiffHunk): NumberedLine[] { - let { oldStart, newStart } = hunkStart(hunk.header); - - return hunk.lines.map((line) => { - if (line.type === 'added') { - return { ...line, oldNumber: null, newNumber: newStart++ }; - } - if (line.type === 'removed') { - return { ...line, oldNumber: oldStart++, newNumber: null }; - } - return { ...line, oldNumber: oldStart++, newNumber: newStart++ }; - }); - } - function blockClass(type: DiffLine['type']): string { switch (type) { case 'added': @@ -238,21 +221,6 @@ if (type === 'removed') return '-'; return ' '; } - - type LineGroup = { type: DiffLine['type']; lines: NumberedLine[] }; - - function groupLines(lines: NumberedLine[]): LineGroup[] { - const groups: LineGroup[] = []; - for (const line of lines) { - const last = groups[groups.length - 1]; - if (last && last.type === line.type) { - last.lines.push(line); - } else { - groups.push({ type: line.type, lines: [line] }); - } - } - return groups; - }
{hunk.header}
- {#each groupLines(numberedLines(hunk)) as group} + {#each groupDiffLines(withInlineDiffSegments(numberDiffLines(hunk))) as group}
{#each group.lines as line}
@@ -453,9 +421,13 @@ class="select-none px-1 text-center {prefixClass(line.type)}" >{linePrefix(line.type)} - {line.content || ' '} +
{/each}
diff --git a/cptr/frontend/src/lib/components/SyntaxDiffLine.svelte b/cptr/frontend/src/lib/components/SyntaxDiffLine.svelte new file mode 100644 index 0000000..139fe16 --- /dev/null +++ b/cptr/frontend/src/lib/components/SyntaxDiffLine.svelte @@ -0,0 +1,228 @@ + + + + +{@html html} + + diff --git a/cptr/frontend/src/lib/utils/diff.ts b/cptr/frontend/src/lib/utils/diff.ts new file mode 100644 index 0000000..66c653c --- /dev/null +++ b/cptr/frontend/src/lib/utils/diff.ts @@ -0,0 +1,172 @@ +export type DiffLineType = 'added' | 'removed' | 'context'; +export type DiffLine = { type: DiffLineType; content: string }; +export type DiffHunk = { header: string; lines: DiffLine[] }; +export type DiffFile = { path: string; hunks: DiffHunk[] }; +export type NumberedDiffLine = DiffLine & { oldNumber: number | null; newNumber: number | null }; +export type InlineDiffSegment = { text: string; changed: boolean }; +export type InlineDiffLine = NumberedDiffLine & { segments: InlineDiffSegment[] }; +export type DiffLineGroup = { type: T['type']; lines: T[] }; + +export function languageForPath(path: string): string { + const ext = path.slice(path.lastIndexOf('.')).toLowerCase(); + const byExt: Record = { + '.py': 'python', + '.js': 'javascript', + '.ts': 'typescript', + '.jsx': 'jsx', + '.tsx': 'tsx', + '.svelte': 'svelte', + '.css': 'css', + '.scss': 'scss', + '.html': 'html', + '.json': 'json', + '.xml': 'xml', + '.yaml': 'yaml', + '.yml': 'yaml', + '.toml': 'toml', + '.md': 'markdown', + '.sh': 'bash', + '.bash': 'bash', + '.zsh': 'bash', + '.rs': 'rust', + '.go': 'go', + '.java': 'java', + '.c': 'c', + '.h': 'c', + '.cpp': 'cpp', + '.hpp': 'cpp', + '.rb': 'ruby', + '.php': 'php', + '.swift': 'swift', + '.kt': 'kotlin', + '.sql': 'sql', + '.lua': 'lua', + '.dockerfile': 'dockerfile', + '.makefile': 'makefile' + }; + return byExt[ext] ?? 'text'; +} + +export function numberDiffLines(hunk: DiffHunk): NumberedDiffLine[] { + const match = hunk.header.match(/^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/); + let oldNumber = match ? Number(match[1]) : 0; + let newNumber = match ? Number(match[2]) : 0; + + return hunk.lines.map((line) => { + if (line.type === 'added') return { ...line, oldNumber: null, newNumber: newNumber++ }; + if (line.type === 'removed') return { ...line, oldNumber: oldNumber++, newNumber: null }; + return { ...line, oldNumber: oldNumber++, newNumber: newNumber++ }; + }); +} + +export function groupDiffLines(lines: T[]): DiffLineGroup[] { + const groups: DiffLineGroup[] = []; + for (const line of lines) { + const last = groups[groups.length - 1]; + if (last && last.type === line.type) last.lines.push(line); + else groups.push({ type: line.type, lines: [line] }); + } + return groups; +} + +export function withInlineDiffSegments(lines: NumberedDiffLine[]): InlineDiffLine[] { + const result: InlineDiffLine[] = []; + let i = 0; + + while (i < lines.length) { + const line = lines[i]; + if (line.type === 'context') { + result.push({ ...line, segments: [{ text: line.content || ' ', changed: false }] }); + i += 1; + continue; + } + + const block: NumberedDiffLine[] = []; + while (i < lines.length && lines[i].type !== 'context') { + block.push(lines[i]); + i += 1; + } + + const removed = block.filter((item) => item.type === 'removed'); + const added = block.filter((item) => item.type === 'added'); + const removedSegments = removed.map((item, index) => + segmentsForPair(item.content, added[index]?.content, 'removed') + ); + const addedSegments = added.map((item, index) => + segmentsForPair(removed[index]?.content, item.content, 'added') + ); + + for (const item of block) { + const index = + item.type === 'removed' + ? removed.indexOf(item) + : item.type === 'added' + ? added.indexOf(item) + : -1; + const segments = + item.type === 'removed' + ? removedSegments[index] + : item.type === 'added' + ? addedSegments[index] + : [{ text: item.content || ' ', changed: false }]; + result.push({ ...item, segments }); + } + } + + return result; +} + +function segmentsForPair( + oldText: string | undefined, + newText: string | undefined, + side: 'removed' | 'added' +): InlineDiffSegment[] { + const text = side === 'removed' ? oldText : newText; + if (text === undefined) return []; + if (oldText === undefined || newText === undefined) + return [{ text: text || ' ', changed: false }]; + if (oldText === newText) return [{ text: text || ' ', changed: false }]; + + let prefix = 0; + const maxPrefix = Math.min(oldText.length, newText.length); + while (prefix < maxPrefix && oldText[prefix] === newText[prefix]) prefix += 1; + + let oldSuffix = oldText.length; + let newSuffix = newText.length; + while ( + oldSuffix > prefix && + newSuffix > prefix && + oldText[oldSuffix - 1] === newText[newSuffix - 1] + ) { + oldSuffix -= 1; + newSuffix -= 1; + } + + const oldChangedLength = oldSuffix - prefix; + const newChangedLength = newSuffix - prefix; + const sharedLength = Math.max( + oldText.length - oldChangedLength, + newText.length - newChangedLength + ); + const similarity = sharedLength / Math.max(oldText.length, newText.length, 1); + const changedLength = side === 'removed' ? oldChangedLength : newChangedLength; + if (similarity < 0.55 || changedLength / Math.max(text.length, 1) > 0.45) { + return [{ text: text || ' ', changed: false }]; + } + + const end = side === 'removed' ? oldSuffix : newSuffix; + const changed = text.slice(prefix, end); + const tokenStart = changed.search(/[A-Za-z0-9_$]/); + if (tokenStart < 0) return [{ text: text || ' ', changed: false }]; + + const tokenEnd = changed.search(/[A-Za-z0-9_$][^A-Za-z0-9_$]*$/); + const highlightStart = tokenStart >= 0 ? prefix + tokenStart : prefix; + const highlightEnd = tokenEnd >= 0 ? prefix + tokenEnd + 1 : end; + const segments = [ + { text: text.slice(0, highlightStart), changed: false }, + { text: text.slice(highlightStart, highlightEnd), changed: true }, + { text: text.slice(highlightEnd), changed: false } + ].filter((segment) => segment.text.length > 0); + + return segments.length ? segments : [{ text: ' ', changed: false }]; +}