@@ -17,13 +17,13 @@ Parameters:
1717Format:
18181. First line must be the file path
19192. Followed by search/replace blocks:
20- \`\`\`
21- <<<<<<< SEARCH
22- [exact content to find including whitespace]
23- =======
24- [new content to replace with]
25- >>>>>>> REPLACE
26- \`\`\`
20+ \`\`\`
21+ <<<<<<< SEARCH
22+ [exact content to find including whitespace]
23+ =======
24+ [new content to replace with]
25+ >>>>>>> REPLACE
26+ \`\`\`
2727
2828Example:
2929
@@ -105,18 +105,38 @@ Your search/replace content here
105105 // Get the matched lines from the original content
106106 const matchedLines = originalLines . slice ( matchIndex , matchIndex + searchLines . length ) ;
107107
108- // For each line in the match, get its indentation
109- const indentations = matchedLines . map ( line => {
110- const match = line . match ( / ^ ( \s * ) / ) ;
111- return match ? match [ 1 ] : '' ;
108+ // Get the exact indentation (preserving tabs/spaces) of each line
109+ const originalIndents = matchedLines . map ( line => {
110+ const match = line . match ( / ^ [ \t ] * / ) ;
111+ return match ? match [ 0 ] : '' ;
112112 } ) ;
113113
114- // Apply the replacement while preserving indentation
114+ // Get the exact indentation of each line in the search block
115+ const searchIndents = searchLines . map ( line => {
116+ const match = line . match ( / ^ [ \t ] * / ) ;
117+ return match ? match [ 0 ] : '' ;
118+ } ) ;
119+
120+ // Apply the replacement while preserving exact indentation
115121 const indentedReplace = replaceLines . map ( ( line , i ) => {
116- // Use the indentation from the corresponding line in the matched block
117- // If we have more lines than the original, use the last indentation
118- const indent = indentations [ Math . min ( i , indentations . length - 1 ) ] ;
119- return indent + line . trim ( ) ;
122+ // Get the corresponding original and search indentations
123+ const originalIndent = originalIndents [ Math . min ( i , originalIndents . length - 1 ) ] ;
124+ const searchIndent = searchIndents [ Math . min ( i , searchIndents . length - 1 ) ] ;
125+
126+ // Get the current line's indentation
127+ const currentIndentMatch = line . match ( / ^ [ \t ] * / ) ;
128+ const currentIndent = currentIndentMatch ? currentIndentMatch [ 0 ] : '' ;
129+
130+ // If this line has the same indentation level as the search block,
131+ // use the original indentation. Otherwise, calculate the difference
132+ // and preserve the exact type of whitespace characters
133+ if ( currentIndent . length === searchIndent . length ) {
134+ return originalIndent + line . trim ( ) ;
135+ } else {
136+ // Calculate additional indentation needed
137+ const additionalIndent = currentIndent . slice ( searchIndent . length ) ;
138+ return originalIndent + additionalIndent + line . trim ( ) ;
139+ }
120140 } ) ;
121141
122142 // Construct the final content
@@ -125,4 +145,4 @@ Your search/replace content here
125145
126146 return [ ...beforeMatch , ...indentedReplace , ...afterMatch ] . join ( '\n' ) ;
127147 }
128- }
148+ }
0 commit comments