@@ -50,23 +50,20 @@ export async function POST(request: NextRequest) {
5050 fs . readFile ( targetAbsolutePath , 'utf-8' )
5151 ] ) ;
5252
53- const diff = generateUnifiedDiff (
54- sourceContent ,
55- targetContent ,
56- sourceFilePath ,
57- targetFilePath ,
58- options
59- ) ;
60-
61- const stats = calculateDiffStats ( sourceContent , targetContent ) ;
53+ const diffLines = generateDiffLines ( sourceContent , targetContent ) ;
54+ const stats = calculateDiffStats ( diffLines ) ;
6255
6356 return NextResponse . json ( {
64- diff,
65- stats,
66- sourceSize : sourceStats . size ,
67- targetSize : targetStats . size ,
68- sourceModified : sourceStats . mtime . toISOString ( ) ,
69- targetModified : targetStats . mtime . toISOString ( )
57+ sourcePath : sourceFilePath ,
58+ targetPath : targetFilePath ,
59+ sourceContent,
60+ targetContent,
61+ lines : diffLines ,
62+ stats : {
63+ additions : stats . linesAdded ,
64+ deletions : stats . linesDeleted ,
65+ modifications : 0
66+ }
7067 } ) ;
7168
7269 } catch ( error : any ) {
@@ -126,11 +123,101 @@ function preprocessLines(lines: string[], options: DiffOptions): string[] {
126123 return processed ;
127124}
128125
126+ function generateDiffLines ( sourceContent : string , targetContent : string ) : DiffLine [ ] {
127+ const sourceLines = sourceContent . split ( '\n' ) ;
128+ const targetLines = targetContent . split ( '\n' ) ;
129+
130+ const diffLines : DiffLine [ ] = [ ] ;
131+ let sourceIndex = 0 ;
132+ let targetIndex = 0 ;
133+ let lineNumber = 1 ;
134+
135+ // Simple diff algorithm - can be enhanced with proper LCS
136+ while ( sourceIndex < sourceLines . length || targetIndex < targetLines . length ) {
137+ const sourceLine = sourceLines [ sourceIndex ] ;
138+ const targetLine = targetLines [ targetIndex ] ;
139+
140+ if ( sourceIndex >= sourceLines . length ) {
141+ // Only target lines left (additions)
142+ diffLines . push ( {
143+ lineNumber : lineNumber ++ ,
144+ content : targetLine ,
145+ type : 'added' ,
146+ newLineNumber : targetIndex + 1 ,
147+ } ) ;
148+ targetIndex ++ ;
149+ } else if ( targetIndex >= targetLines . length ) {
150+ // Only source lines left (deletions)
151+ diffLines . push ( {
152+ lineNumber : lineNumber ++ ,
153+ content : sourceLine ,
154+ type : 'removed' ,
155+ oldLineNumber : sourceIndex + 1 ,
156+ } ) ;
157+ sourceIndex ++ ;
158+ } else if ( sourceLine === targetLine ) {
159+ // Lines are identical
160+ diffLines . push ( {
161+ lineNumber : lineNumber ++ ,
162+ content : sourceLine ,
163+ type : 'unchanged' ,
164+ oldLineNumber : sourceIndex + 1 ,
165+ newLineNumber : targetIndex + 1 ,
166+ } ) ;
167+ sourceIndex ++ ;
168+ targetIndex ++ ;
169+ } else {
170+ // Lines are different - check if it's a modification or add/remove
171+ const nextSourceMatch = targetLines . slice ( targetIndex + 1 ) . findIndex ( line => line === sourceLine ) ;
172+ const nextTargetMatch = sourceLines . slice ( sourceIndex + 1 ) . findIndex ( line => line === targetLine ) ;
173+
174+ if ( nextSourceMatch === - 1 && nextTargetMatch === - 1 ) {
175+ // Likely a modification
176+ diffLines . push ( {
177+ lineNumber : lineNumber ++ ,
178+ content : sourceLine ,
179+ type : 'removed' ,
180+ oldLineNumber : sourceIndex + 1 ,
181+ } ) ;
182+ diffLines . push ( {
183+ lineNumber : lineNumber ++ ,
184+ content : targetLine ,
185+ type : 'added' ,
186+ newLineNumber : targetIndex + 1 ,
187+ } ) ;
188+ sourceIndex ++ ;
189+ targetIndex ++ ;
190+ } else if ( nextSourceMatch !== - 1 && ( nextTargetMatch === - 1 || nextSourceMatch < nextTargetMatch ) ) {
191+ // Source line appears later in target, so target lines are additions
192+ diffLines . push ( {
193+ lineNumber : lineNumber ++ ,
194+ content : targetLine ,
195+ type : 'added' ,
196+ newLineNumber : targetIndex + 1 ,
197+ } ) ;
198+ targetIndex ++ ;
199+ } else {
200+ // Target line appears later in source, so source line is deletion
201+ diffLines . push ( {
202+ lineNumber : lineNumber ++ ,
203+ content : sourceLine ,
204+ type : 'removed' ,
205+ oldLineNumber : sourceIndex + 1 ,
206+ } ) ;
207+ sourceIndex ++ ;
208+ }
209+ }
210+ }
211+
212+ return diffLines ;
213+ }
214+
129215interface DiffLine {
130- type : 'context' | 'added' | 'deleted' ;
216+ lineNumber : number ;
131217 content : string ;
132- sourceLineNumber ?: number ;
133- targetLineNumber ?: number ;
218+ type : 'added' | 'removed' | 'unchanged' | 'modified' ;
219+ oldLineNumber ?: number ;
220+ newLineNumber ?: number ;
134221}
135222
136223function computeLCS ( sourceLines : string [ ] , targetLines : string [ ] ) : DiffLine [ ] {
@@ -343,21 +430,10 @@ function createHunk(lines: DiffLine[]): Hunk {
343430 } ;
344431}
345432
346- function calculateDiffStats ( sourceContent : string , targetContent : string ) {
347- const sourceLines = sourceContent . split ( '\n' ) ;
348- const targetLines = targetContent . split ( '\n' ) ;
349-
350- const diff = computeLCS ( sourceLines , targetLines ) ;
351-
352- const stats = {
353- linesAdded : diff . filter ( line => line . type === 'added' ) . length ,
354- linesDeleted : diff . filter ( line => line . type === 'deleted' ) . length ,
355- linesChanged : 0 ,
356- totalSourceLines : sourceLines . length ,
357- totalTargetLines : targetLines . length
433+ function calculateDiffStats ( diffLines : DiffLine [ ] ) {
434+ return {
435+ linesAdded : diffLines . filter ( line => line . type === 'added' ) . length ,
436+ linesDeleted : diffLines . filter ( line => line . type === 'removed' ) . length ,
437+ linesChanged : diffLines . filter ( line => line . type === 'modified' ) . length ,
358438 } ;
359-
360- stats . linesChanged = stats . linesAdded + stats . linesDeleted ;
361-
362- return stats ;
363439}
0 commit comments