@@ -275,6 +275,24 @@ export function readUdiff(unifiedDiff: string): UnifiedDiff {
275275 const firstMinusIndex = relevantLines . findIndex ( s => s . startsWith ( '-' ) )
276276 const firstPlusIndex = relevantLines . findIndex ( s => s . startsWith ( '+' ) )
277277
278+ // TODO: Comment these out as they are used for a different version of addonly type determination logic in case the current implementation doesn't work.
279+ // Could remove later if we are sure current imple works.
280+ /**
281+ * Concatenate all contiguous added lines (i.e., unbroken sequence of "+"s).
282+ * Exclude all newlines when concatenating, so we get a single line representing the new text
283+ */
284+ // let singleLine = ''
285+ // let prev: number | undefined
286+ // for (const idx of plusIndexes) {
287+ // if (!prev || idx === prev + 1) {
288+ // const removedPlus = relevantLines[idx].substring(1)
289+ // const removedStartNewline = trimStartNewline(removedPlus)
290+ // singleLine += removedStartNewline
291+ // } else {
292+ // break
293+ // }
294+ // }
295+
278296 return {
279297 linesWithoutHeaders : relevantLines ,
280298 firstMinusIndex : firstMinusIndex ,
@@ -291,14 +309,17 @@ export function categorizeUnifieddiff(unifiedDiff: string): 'addOnly' | 'deleteO
291309 const firstPlusIndex = d . firstPlusIndex
292310 const diffWithoutHeaders = d . linesWithoutHeaders
293311
312+ // Shouldn't be the case but if there is no - nor +, assume it's an edit
294313 if ( firstMinusIndex === - 1 && firstPlusIndex === - 1 ) {
295314 return 'edit'
296315 }
297316
317+ // Naive case, only +
298318 if ( firstMinusIndex === - 1 && firstPlusIndex !== - 1 ) {
299319 return 'addOnly'
300320 }
301321
322+ // Naive case, only -
302323 if ( firstMinusIndex !== - 1 && firstPlusIndex === - 1 ) {
303324 return 'deleteOnly'
304325 }
@@ -321,12 +342,47 @@ export function categorizeUnifieddiff(unifiedDiff: string): 'addOnly' | 'deleteO
321342
322343 // If last '-' line is followed by '+' block, it could be addonly
323344 if ( plusIndexes [ 0 ] === minusIndexes [ minusIndexes . length - 1 ] + 1 ) {
345+ /**
346+ -------------------------------
347+ - return
348+ + return a - b;
349+ -------------------------------
350+ commonPrefix = "return "
351+ minusLinesDelta = ""
352+
353+ --------------------------------
354+ -\t\t\t
355+ +\treturn a - b;
356+ --------------------------------
357+ commonPrefix = "\t"
358+ minusLinesDelta = "\t\t"
359+
360+ *
361+ *
362+ *
363+ */
324364 const minusLine = diffWithoutHeaders [ minusIndexes [ minusIndexes . length - 1 ] ] . substring ( 1 )
325365 const pluscode = extractAdditions ( unifiedDiff )
326366
327367 // If minusLine subtract the longest common substring of minusLine and plugcode and it's empty string, it's addonly
328368 const commonPrefix = longestCommonPrefix ( minusLine , pluscode )
329- if ( minusLine . substring ( commonPrefix . length ) . trim ( ) . length === 0 ) {
369+ const minusLinesDelta = minusLine . substring ( commonPrefix . length )
370+ if ( minusLinesDelta . trim ( ) . length === 0 ) {
371+ return 'addOnly'
372+ }
373+
374+ /**
375+ -------------------------------
376+ - return a * b;
377+ + return a * b * c;
378+ -------------------------------
379+ commonPrefix = "return a * b"
380+ minusLinesDelta = ";"
381+ pluscodeDelta = " * c;"
382+ *
383+ */
384+ const pluscodeDelta = pluscode . substring ( commonPrefix . length )
385+ if ( pluscodeDelta . endsWith ( minusLinesDelta ) ) {
330386 return 'addOnly'
331387 }
332388 }
@@ -400,3 +456,23 @@ export function longestCommonPrefix(str1: string, str2: string): string {
400456
401457 return prefix
402458}
459+
460+ // TODO: They are used for a different version of addonly type determination logic in case the current implementation doesn't work.
461+ // Could remove later if we are sure current impl works.
462+ // function trimStartNewline(str: string): string {
463+ // return str.replace(/^[\n\r]+/, '')
464+ // }
465+
466+ // function hasOneContiguousInsert(original: string, changed: string) {
467+ // const delta = changed.length - original.length
468+ // if (delta <= 0) {
469+ // // Changed string must be longer
470+ // return false
471+ // }
472+
473+ // let p, s
474+ // for (p = 0; original[p] === changed[p] && p < original.length; ++p);
475+ // for (s = original.length - 1; original[s] === changed[s + delta] && s >= 0; --s);
476+
477+ // return p === s + 1
478+ // }
0 commit comments