@@ -131,88 +131,6 @@ export function generateDiffContexts(
131131 }
132132}
133133
134- /** src: https://github.com/aws/aws-toolkit-vscode/blob/3921457b0a2094b831beea0d66cc2cbd2a833890/packages/amazonq/src/app/inline/EditRendering/diffUtils.ts#L18
135- * Apply a unified diff to original code to generate modified code
136- * @param originalCode The original code as a string
137- * @param unifiedDiff The unified diff content
138- * @returns The modified code after applying the diff
139- */
140- export function applyUnifiedDiff ( docText : string , unifiedDiff : string ) : string {
141- try {
142- // First try the standard diff package
143- try {
144- const result = diff . applyPatch ( docText , unifiedDiff )
145- if ( result !== false ) {
146- return result
147- }
148- } catch ( error ) { }
149-
150- // Parse the unified diff to extract the changes
151- const diffLines = unifiedDiff . split ( '\n' )
152- let result = docText
153-
154- // Find all hunks in the diff
155- const hunkStarts = diffLines
156- . map ( ( line , index ) => ( line . startsWith ( '@@ ' ) ? index : - 1 ) )
157- . filter ( index => index !== - 1 )
158-
159- // Process each hunk
160- for ( const hunkStart of hunkStarts ) {
161- // Parse the hunk header
162- const hunkHeader = diffLines [ hunkStart ]
163- const match = hunkHeader . match ( / @ @ - ( \d + ) , ( \d + ) \+ ( \d + ) , ( \d + ) @ @ / )
164-
165- if ( ! match ) {
166- continue
167- }
168-
169- const oldStart = parseInt ( match [ 1 ] )
170- const oldLines = parseInt ( match [ 2 ] )
171-
172- // Extract the content lines for this hunk
173- let i = hunkStart + 1
174- const contentLines = [ ]
175- while ( i < diffLines . length && ! diffLines [ i ] . startsWith ( '@@' ) ) {
176- contentLines . push ( diffLines [ i ] )
177- i ++
178- }
179-
180- // Build the old and new text
181- let oldText = ''
182- let newText = ''
183-
184- for ( const line of contentLines ) {
185- if ( line . startsWith ( '-' ) ) {
186- oldText += line . substring ( 1 ) + '\n'
187- } else if ( line . startsWith ( '+' ) ) {
188- newText += line . substring ( 1 ) + '\n'
189- } else if ( line . startsWith ( ' ' ) ) {
190- oldText += line . substring ( 1 ) + '\n'
191- newText += line . substring ( 1 ) + '\n'
192- }
193- }
194-
195- // Remove trailing newline if it was added
196- oldText = oldText . replace ( / \n $ / , '' )
197- newText = newText . replace ( / \n $ / , '' )
198-
199- // Find the text to replace in the document
200- const docLines = docText . split ( '\n' )
201- const startLine = oldStart - 1 // Convert to 0-based
202- const endLine = startLine + oldLines
203-
204- // Extract the text that should be replaced
205- const textToReplace = docLines . slice ( startLine , endLine ) . join ( '\n' )
206-
207- // Replace the text
208- result = result . replace ( textToReplace , newText )
209- }
210- return result
211- } catch ( error ) {
212- return docText // Return original text if all methods fail
213- }
214- }
215-
216134export function getAddedAndDeletedLines ( unifiedDiff : string ) : { addedLines : string [ ] ; deletedLines : string [ ] } {
217135 const lines = unifiedDiff . split ( '\n' )
218136 const addedLines = lines . filter ( line => line . startsWith ( '+' ) && ! line . startsWith ( '+++' ) ) . map ( line => line . slice ( 1 ) )
@@ -225,48 +143,6 @@ export function getAddedAndDeletedLines(unifiedDiff: string): { addedLines: stri
225143 }
226144}
227145
228- // src https://github.com/aws/aws-toolkit-vscode/blob/3921457b0a2094b831beea0d66cc2cbd2a833890/packages/amazonq/src/app/inline/EditRendering/diffUtils.ts#L147
229- export function getAddedAndDeletedChars ( unifiedDiff : string ) : {
230- addedCharacters : string
231- deletedCharacters : string
232- } {
233- let addedCharacters = ''
234- let deletedCharacters = ''
235- const lines = unifiedDiff . split ( '\n' )
236- for ( let i = 0 ; i < lines . length ; i ++ ) {
237- const line = lines [ i ]
238- if ( line . startsWith ( '+' ) && ! line . startsWith ( '+++' ) ) {
239- addedCharacters += line . slice ( 1 )
240- } else if ( line . startsWith ( '-' ) && ! line . startsWith ( '---' ) ) {
241- const removedLine = line . slice ( 1 )
242-
243- // Check if this is a modified line rather than a pure deletion
244- const nextLine = lines [ i + 1 ]
245- if ( nextLine && nextLine . startsWith ( '+' ) && ! nextLine . startsWith ( '+++' ) ) {
246- // This is a modified line, not a pure deletion
247- // We've already counted the deletion, so we'll just increment i to skip the next line
248- // since we'll process the addition on the next iteration
249- const addedLine = nextLine . slice ( 1 )
250- const changes = diff . diffChars ( removedLine , addedLine )
251- for ( const part of changes ) {
252- if ( part . removed ) {
253- deletedCharacters += part . value
254- } else if ( part . added ) {
255- addedCharacters += part . value
256- }
257- }
258- i += 1
259- } else {
260- deletedCharacters += removedLine
261- }
262- }
263- }
264- return {
265- addedCharacters,
266- deletedCharacters,
267- }
268- }
269-
270146/**
271147 * Calculate character differences between added and deleted text blocks using LCS
272148 */
0 commit comments