@@ -82,8 +82,8 @@ const commandNameFunctionMap: { [key: string]: CommandFunction } = {
82
82
repeat : ( n : number ) => defaultFunction ( "repeat" , n ) ,
83
83
increment,
84
84
decrement,
85
- duplicateAndIncrement : ( str : string ) => str + increment ( str ) ,
86
- duplicateAndDecrement : ( str : string ) => str + decrement ( str ) ,
85
+ duplicateAndIncrement : ( ) => "" ,
86
+ duplicateAndDecrement : ( ) => "" ,
87
87
sequence,
88
88
utf8ToChar : ( str : string ) =>
89
89
str
@@ -153,12 +153,28 @@ export const stringFunction = async (
153
153
stringFunc = commandNameFunctionMap [ commandName ] as StringFunction ;
154
154
}
155
155
156
- for ( const [ index , selection ] of editor . selections . entries ( ) ) {
157
- const text = editor . document . getText ( selection ) ;
158
- const textParts = text . split ( "\n" ) ;
159
- const replaced = textParts . map ( ( part ) => stringFunc ( part ) ) . join ( "\n" ) ;
160
- replacedSelections . push ( replaced ) ;
161
- selectionMap [ index ] = { selection, replaced } ;
156
+ if (
157
+ commandName === "duplicateAndIncrement" ||
158
+ commandName === "duplicateAndDecrement"
159
+ ) {
160
+ for ( const [ index , selection ] of editor . selections . entries ( ) ) {
161
+ const text = editor . document . getText ( selection ) ;
162
+
163
+ const operation =
164
+ commandName === "duplicateAndIncrement" ? increment : decrement ;
165
+ const replaced = text + operation ( text ) ;
166
+
167
+ replacedSelections . push ( replaced ) ;
168
+ selectionMap [ index ] = { selection, replaced } ;
169
+ }
170
+ } else {
171
+ for ( const [ index , selection ] of editor . selections . entries ( ) ) {
172
+ const text = editor . document . getText ( selection ) ;
173
+ const textParts = text . split ( "\n" ) ;
174
+ const replaced = textParts . map ( ( part ) => stringFunc ( part ) ) . join ( "\n" ) ;
175
+ replacedSelections . push ( replaced ) ;
176
+ selectionMap [ index ] = { selection, replaced } ;
177
+ }
162
178
}
163
179
164
180
if ( shouldApply ) {
@@ -168,6 +184,37 @@ export const stringFunction = async (
168
184
} ) ;
169
185
} ) ;
170
186
187
+ // Set the selection to the duplicated part for duplicateAndIncrement and duplicateAndDecrement
188
+ if (
189
+ commandName === "duplicateAndIncrement" ||
190
+ commandName === "duplicateAndDecrement"
191
+ ) {
192
+ const newSelections = editor . selections . map ( ( selection , index ) => {
193
+ const originalSelection = selectionMap [ index ] . selection ;
194
+ const originalText = editor . document . getText ( originalSelection ) ;
195
+
196
+ // Calculate the start position of the duplicated text
197
+ const startPos = originalSelection . end ;
198
+
199
+ // Calculate the end position based on the original text length
200
+ let endLine = startPos . line ;
201
+ let endChar = startPos . character + originalText . length ;
202
+
203
+ // Handle multi-line selections
204
+ const lines = originalText . split ( "\n" ) ;
205
+ if ( lines . length > 1 ) {
206
+ endLine = startPos . line + lines . length - 1 ;
207
+ // If multi-line, the end character should be the length of the last line
208
+ endChar = lines [ lines . length - 1 ] . length ;
209
+ }
210
+
211
+ const endPos = new vscode . Position ( endLine , endChar ) ;
212
+ return new vscode . Selection ( startPos , endPos ) ;
213
+ } ) ;
214
+
215
+ editor . selections = newSelections ;
216
+ }
217
+
171
218
context . globalState . update ( "lastAction" , commandName ) ;
172
219
}
173
220
0 commit comments