33 * SPDX-License-Identifier: Apache-2.0
44 */
55
6- import { diffWordsWithSpace } from 'diff'
6+ import { diffWordsWithSpace , diffLines } from 'diff'
77import * as vscode from 'vscode'
88import { ToolkitError , getLogger } from 'aws-core-vscode/shared'
99import { diffUtilities } from 'aws-core-vscode/shared'
@@ -42,10 +42,6 @@ export class SvgGenerationService {
4242 throw new ToolkitError ( 'udiff format error' )
4343 }
4444 const newCode = await diffUtilities . getPatchedCode ( filePath , udiff )
45- const modifiedLines = diffUtilities . getModifiedLinesFromUnifiedDiff ( udiff )
46- // TODO remove
47- // eslint-disable-next-line aws-toolkits/no-json-stringify-in-log
48- logger . info ( `Line mapping: ${ JSON . stringify ( modifiedLines ) } ` )
4945
5046 const { createSVGWindow } = await import ( 'svgdom' )
5147
@@ -57,7 +53,12 @@ export class SvgGenerationService {
5753 const currentTheme = this . getEditorTheme ( )
5854
5955 // Get edit diffs with highlight
60- const { addedLines, removedLines } = this . getEditedLinesFromDiff ( udiff )
56+ const { addedLines, removedLines } = this . getEditedLinesFromCode ( originalCode , newCode )
57+
58+ const modifiedLines = diffUtilities . getModifiedLinesFromCode ( addedLines , removedLines )
59+ // TODO remove
60+ // eslint-disable-next-line aws-toolkits/no-json-stringify-in-log
61+ logger . info ( `Line mapping: ${ JSON . stringify ( modifiedLines ) } ` )
6162
6263 // Calculate dimensions based on code content
6364 const { offset, editStartLine, isPositionValid } = this . calculatePosition (
@@ -175,43 +176,25 @@ export class SvgGenerationService {
175176 }
176177
177178 /**
178- * Extract added and removed lines from the unified diff
179- * @param unifiedDiff The unified diff string
179+ * Extract added and removed lines by comparing original and new code
180+ * @param originalCode The original code string
181+ * @param newCode The new code string
180182 * @returns Object containing arrays of added and removed lines
181183 */
182- private getEditedLinesFromDiff ( unifiedDiff : string ) : { addedLines : string [ ] ; removedLines : string [ ] } {
184+ private getEditedLinesFromCode (
185+ originalCode : string ,
186+ newCode : string
187+ ) : { addedLines : string [ ] ; removedLines : string [ ] } {
183188 const addedLines : string [ ] = [ ]
184189 const removedLines : string [ ] = [ ]
185- const diffLines = unifiedDiff . split ( '\n' )
186-
187- // Find all hunks in the diff
188- const hunkStarts = diffLines
189- . map ( ( line , index ) => ( line . startsWith ( '@@ ' ) ? index : - 1 ) )
190- . filter ( ( index ) => index !== - 1 )
191190
192- // Process each hunk to find added and removed lines
193- for ( const hunkStart of hunkStarts ) {
194- const hunkHeader = diffLines [ hunkStart ]
195- const match = hunkHeader . match ( / @ @ - ( \d + ) , ( \d + ) \+ ( \d + ) , ( \d + ) @ @ / )
191+ const changes = diffLines ( originalCode , newCode )
196192
197- if ( ! match ) {
198- continue
199- }
200-
201- // Extract the content lines for this hunk
202- let i = hunkStart + 1
203- while ( i < diffLines . length && ! diffLines [ i ] . startsWith ( '@@' ) ) {
204- // Include lines that were added (start with '+')
205- if ( diffLines [ i ] . startsWith ( '+' ) && ! diffLines [ i ] . startsWith ( '+++' ) ) {
206- const lineContent = diffLines [ i ] . substring ( 1 )
207- addedLines . push ( lineContent )
208- }
209- // Include lines that were removed (start with '-')
210- else if ( diffLines [ i ] . startsWith ( '-' ) && ! diffLines [ i ] . startsWith ( '---' ) ) {
211- const lineContent = diffLines [ i ] . substring ( 1 )
212- removedLines . push ( lineContent )
213- }
214- i ++
193+ for ( const change of changes ) {
194+ if ( change . added ) {
195+ addedLines . push ( ...change . value . split ( '\n' ) . filter ( ( line ) => line . length > 0 ) )
196+ } else if ( change . removed ) {
197+ removedLines . push ( ...change . value . split ( '\n' ) . filter ( ( line ) => line . length > 0 ) )
215198 }
216199 }
217200
0 commit comments