@@ -11,6 +11,8 @@ import {
11
11
decrement ,
12
12
duplicateAndIncrement ,
13
13
duplicateAndDecrement ,
14
+ handleDuplicateAndIncrementDecrement ,
15
+ updateSelectionsAfterDuplicate ,
14
16
} from "./increment-decrement" ;
15
17
import { sequence } from "./sequence" ;
16
18
import { randomCase } from "./random-case" ;
@@ -75,15 +77,15 @@ export const stringFunction = async (
75
77
return ;
76
78
}
77
79
78
- const selectionMap : {
80
+ let selectionMap : {
79
81
[ key : number ] : { selection : vscode . Selection ; replaced : string } ;
80
82
} = { } ;
81
83
82
84
let multiselectData = { } ;
83
85
84
86
let stringFunc : ( str : string ) => string ;
85
87
86
- let replacedSelections = [ ] ;
88
+ let replacedSelections : string [ ] = [ ] ;
87
89
88
90
if ( functionNamesWithArgument . includes ( commandName ) ) {
89
91
const valueStr = await vscode . window . showInputBox ( ) ;
@@ -107,16 +109,16 @@ export const stringFunction = async (
107
109
commandName === "duplicateAndIncrement" ||
108
110
commandName === "duplicateAndDecrement"
109
111
) {
110
- for ( const [ index , selection ] of editor . selections . entries ( ) ) {
111
- const text = editor . document . getText ( selection ) ;
112
-
113
- const operation =
114
- commandName === "duplicateAndIncrement" ? increment : decrement ;
115
- const replaced = text + operation ( text ) ;
112
+ const operation =
113
+ commandName === "duplicateAndIncrement" ? increment : decrement ;
116
114
117
- replacedSelections . push ( replaced ) ;
118
- selectionMap [ index ] = { selection, replaced } ;
119
- }
115
+ const result = handleDuplicateAndIncrementDecrement (
116
+ editor ,
117
+ editor . selections ,
118
+ operation as ( str : string ) => string
119
+ ) ;
120
+ selectionMap = result . selectionMap ;
121
+ replacedSelections = result . replacedSelections ;
120
122
} else {
121
123
for ( const [ index , selection ] of editor . selections . entries ( ) ) {
122
124
const text = editor . document . getText ( selection ) ;
@@ -139,30 +141,7 @@ export const stringFunction = async (
139
141
commandName === "duplicateAndIncrement" ||
140
142
commandName === "duplicateAndDecrement"
141
143
) {
142
- const newSelections = editor . selections . map ( ( selection , index ) => {
143
- const originalSelection = selectionMap [ index ] . selection ;
144
- const originalText = editor . document . getText ( originalSelection ) ;
145
-
146
- // Calculate the start position of the duplicated text
147
- const startPos = originalSelection . end ;
148
-
149
- // Calculate the end position based on the original text length
150
- let endLine = startPos . line ;
151
- let endChar = startPos . character + originalText . length ;
152
-
153
- // Handle multi-line selections
154
- const lines = originalText . split ( "\n" ) ;
155
- if ( lines . length > 1 ) {
156
- endLine = startPos . line + lines . length - 1 ;
157
- // If multi-line, the end character should be the length of the last line
158
- endChar = lines [ lines . length - 1 ] . length ;
159
- }
160
-
161
- const endPos = new vscode . Position ( endLine , endChar ) ;
162
- return new vscode . Selection ( startPos , endPos ) ;
163
- } ) ;
164
-
165
- editor . selections = newSelections ;
144
+ editor . selections = updateSelectionsAfterDuplicate ( editor , selectionMap ) ;
166
145
}
167
146
168
147
context . globalState . update ( "lastAction" , commandName ) ;
0 commit comments