@@ -69,46 +69,49 @@ export class FsWrite {
6969 }
7070 }
7171
72- private generateSmartDiff ( oldStr : string , newStr : string ) : string {
72+ private showStrReplacePreview ( oldStr : string , newStr : string ) : string {
7373 // Split both strings into arrays of lines
74- const oldLines = oldStr . split ( '\n' )
75- const newLines = newStr . split ( '\n' )
74+ const oldStrLines = oldStr . split ( '\n' )
75+ const newStrLines = newStr . split ( '\n' )
7676 let result = ''
7777
7878 // If strings are identical, return empty string
7979 if ( oldStr === newStr ) {
8080 return result
8181 }
8282
83- let i = 0 // Index for oldLines
84- let j = 0 // Index for newLines
85-
83+ let oldLineIndex = 0
84+ let newLineIndex = 0
8685 // Loop through both arrays until we've processed all lines
87- while ( i < oldLines . length || j < newLines . length ) {
88- if ( i < oldLines . length && j < newLines . length && oldLines [ i ] === newLines [ j ] ) {
86+ while ( oldLineIndex < oldStrLines . length || newLineIndex < newStrLines . length ) {
87+ if (
88+ oldLineIndex < oldStrLines . length &&
89+ newLineIndex < newStrLines . length &&
90+ oldStrLines [ oldLineIndex ] === newStrLines [ newLineIndex ]
91+ ) {
8992 // Line is unchanged - prefix with space
90- result += ` ${ oldLines [ i ] } \n`
91- i ++
92- j ++
93+ result += ` ${ oldStrLines [ oldLineIndex ] } \n`
94+ oldLineIndex ++
95+ newLineIndex ++
9396 } else {
9497 // Line is different
95- if ( i < oldLines . length ) {
98+ if ( oldLineIndex < oldStrLines . length ) {
9699 // Remove line - prefix with minus
97- result += `-${ oldLines [ i ] } \n`
98- i ++
100+ result += `- ${ oldStrLines [ oldLineIndex ] } \n`
101+ oldLineIndex ++
99102 }
100- if ( j < newLines . length ) {
103+ if ( newLineIndex < newStrLines . length ) {
101104 // Add line - prefix with plus
102- result += `+${ newLines [ j ] } \n`
103- j ++
105+ result += `+ ${ newStrLines [ newLineIndex ] } \n`
106+ newLineIndex ++
104107 }
105108 }
106109 }
107110
108111 return result
109112 }
110113
111- private async getInsertContext ( path : string , insertLine : number , newStr : string ) : Promise < string > {
114+ private async showInsertPreview ( path : string , insertLine : number , newStr : string ) : Promise < string > {
112115 const fileContent = await fs . readFileText ( path )
113116 const lines = fileContent . split ( '\n' )
114117 const startLine = Math . max ( 0 , insertLine - 2 )
@@ -117,22 +120,22 @@ export class FsWrite {
117120 const contextLines : string [ ] = [ ]
118121
119122 // Add lines before insertion point
120- for ( let i = startLine ; i < insertLine ; i ++ ) {
121- contextLines . push ( ` ${ lines [ i ] } ` )
123+ for ( let index = startLine ; index < insertLine ; index ++ ) {
124+ contextLines . push ( ` ${ lines [ index ] } ` )
122125 }
123126
124127 // Add the new line with a '+' prefix
125- contextLines . push ( `+${ newStr } ` )
128+ contextLines . push ( `+ ${ newStr } ` )
126129
127130 // Add lines after insertion point
128- for ( let i = insertLine ; i < endLine ; i ++ ) {
129- contextLines . push ( ` ${ lines [ i ] } ` )
131+ for ( let index = insertLine ; index < endLine ; index ++ ) {
132+ contextLines . push ( ` ${ lines [ index ] } ` )
130133 }
131134
132135 return contextLines . join ( '\n' )
133136 }
134137
135- private async handleAppendContent ( sanitizedPath : string , newStr : string ) {
138+ private async showAppendPreview ( sanitizedPath : string , newStr : string ) {
136139 const fileContent = await fs . readFileText ( sanitizedPath )
137140 const needsNewline = fileContent . length !== 0 && ! fileContent . endsWith ( '\n' )
138141
@@ -141,21 +144,14 @@ export class FsWrite {
141144 contentToAppend = '\n' + contentToAppend
142145 }
143146
144- // Get the last 3 lines from existing content
147+ // Get the last 3 lines from existing content for better UX
145148 const lines = fileContent . split ( '\n' )
146- const last3Lines = lines . slice ( - 3 )
147-
148- // Format the output with the last 3 lines and new content
149- // const formattedOutput = [
150- // ...last3Lines,
151- // `+ ${contentToAppend.trim()}`, // Add '+' prefix to new content
152- // ].join('\n')
149+ const linesForContext = lines . slice ( - 3 )
153150
154- return `${ last3Lines . join ( '\n' ) } \n+ ${ contentToAppend . trim ( ) } ` // [last3Lines, contentToAppend.trim()] // `${last3Lines .join('\n')}\n+ ${contentToAppend.trim()}`
151+ return `${ linesForContext . join ( '\n' ) } \n+ ${ contentToAppend . trim ( ) } `
155152 }
156153
157154 public async queueDescription ( updates : Writable ) : Promise < void > {
158- // const fileName = path.basename(this.params.path)
159155 switch ( this . params . command ) {
160156 case 'create' :
161157 updates . write ( `\`\`\`diff-typescript
@@ -164,18 +160,18 @@ ${'+' + this.params.fileText?.replace(/\n/g, '\n+')}
164160 break
165161 case 'strReplace' :
166162 updates . write ( `\`\`\`diff-typescript
167- ${ this . generateSmartDiff ( this . params . oldStr , this . params . newStr ) }
163+ ${ this . showStrReplacePreview ( this . params . oldStr , this . params . newStr ) }
168164\`\`\`
169165` )
170166 break
171167 case 'insert' :
172168 updates . write ( `\`\`\`diff-typescript
173- ${ await this . getInsertContext ( this . params . path , this . params . insertLine , this . params . newStr ) }
169+ ${ await this . showInsertPreview ( this . params . path , this . params . insertLine , this . params . newStr ) }
174170\`\`\`` )
175171 break
176172 case 'append' :
177173 updates . write ( `\`\`\`diff-typescript
178- ${ await this . handleAppendContent ( this . params . path , this . params . newStr ) }
174+ ${ await this . showAppendPreview ( this . params . path , this . params . newStr ) }
179175\`\`\`` )
180176 break
181177 }
@@ -242,29 +238,20 @@ ${await this.handleAppendContent(this.params.path, this.params.newStr)}
242238 await fs . writeFile ( sanitizedPath , newContent )
243239 }
244240
245- private async getLineToInsert (
246- sanitizedPath : string ,
247- insertLine : number ,
248- newStr : string
249- ) : Promise < [ number , string ] > {
241+ private async handleInsert ( params : InsertParams , sanitizedPath : string ) : Promise < void > {
250242 const fileContent = await fs . readFileText ( sanitizedPath )
251243 const lines = fileContent . split ( '\n' )
252244
253245 const numLines = lines . length
254- const insertLineInFile = Math . max ( 0 , Math . min ( insertLine , numLines ) )
246+ const insertLine = Math . max ( 0 , Math . min ( params . insertLine , numLines ) )
255247
256248 let newContent : string
257- if ( insertLineInFile === 0 ) {
258- newContent = newStr + '\n' + fileContent
249+ if ( insertLine === 0 ) {
250+ newContent = params . newStr + '\n' + fileContent
259251 } else {
260- newContent = [ ...lines . slice ( 0 , insertLineInFile ) , newStr , ...lines . slice ( insertLineInFile ) ] . join ( '\n' )
252+ newContent = [ ...lines . slice ( 0 , insertLine ) , params . newStr , ...lines . slice ( insertLine ) ] . join ( '\n' )
261253 }
262254
263- return [ insertLineInFile , newContent ]
264- }
265-
266- private async handleInsert ( params : InsertParams , sanitizedPath : string ) : Promise < void > {
267- const [ , newContent ] = await this . getLineToInsert ( sanitizedPath , params . insertLine , params . newStr )
268255 await fs . writeFile ( sanitizedPath , newContent )
269256 }
270257
0 commit comments