( after: boolean, count: number, ctx: OperatorContext, )
| 292 | * Execute paste (p/P command). |
| 293 | */ |
| 294 | export function executePaste( |
| 295 | after: boolean, |
| 296 | count: number, |
| 297 | ctx: OperatorContext, |
| 298 | ): void { |
| 299 | const register = ctx.getRegister() |
| 300 | if (!register) return |
| 301 | |
| 302 | const isLinewise = register.endsWith('\n') |
| 303 | const content = isLinewise ? register.slice(0, -1) : register |
| 304 | |
| 305 | if (isLinewise) { |
| 306 | const text = ctx.text |
| 307 | const lines = text.split('\n') |
| 308 | const { line: currentLine } = ctx.cursor.getPosition() |
| 309 | |
| 310 | const insertLine = after ? currentLine + 1 : currentLine |
| 311 | const contentLines = content.split('\n') |
| 312 | const repeatedLines: string[] = [] |
| 313 | for (let i = 0; i < count; i++) { |
| 314 | repeatedLines.push(...contentLines) |
| 315 | } |
| 316 | |
| 317 | const newLines = [ |
| 318 | ...lines.slice(0, insertLine), |
| 319 | ...repeatedLines, |
| 320 | ...lines.slice(insertLine), |
| 321 | ] |
| 322 | |
| 323 | const newText = newLines.join('\n') |
| 324 | ctx.setText(newText) |
| 325 | ctx.setOffset(getLineStartOffset(newLines, insertLine)) |
| 326 | } else { |
| 327 | const textToInsert = content.repeat(count) |
| 328 | const insertPoint = |
| 329 | after && ctx.cursor.offset < ctx.text.length |
| 330 | ? ctx.cursor.measuredText.nextOffset(ctx.cursor.offset) |
| 331 | : ctx.cursor.offset |
| 332 | |
| 333 | const newText = |
| 334 | ctx.text.slice(0, insertPoint) + |
| 335 | textToInsert + |
| 336 | ctx.text.slice(insertPoint) |
| 337 | const lastGr = lastGrapheme(textToInsert) |
| 338 | const newOffset = insertPoint + textToInsert.length - (lastGr.length || 1) |
| 339 | |
| 340 | ctx.setText(newText) |
| 341 | ctx.setOffset(Math.max(insertPoint, newOffset)) |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Execute indent (>> command). |
no test coverage detected