( dir: '>' | '<', count: number, ctx: OperatorContext, )
| 346 | * Execute indent (>> command). |
| 347 | */ |
| 348 | export function executeIndent( |
| 349 | dir: '>' | '<', |
| 350 | count: number, |
| 351 | ctx: OperatorContext, |
| 352 | ): void { |
| 353 | const text = ctx.text |
| 354 | const lines = text.split('\n') |
| 355 | const { line: currentLine } = ctx.cursor.getPosition() |
| 356 | const linesToAffect = Math.min(count, lines.length - currentLine) |
| 357 | const indent = ' ' // Two spaces |
| 358 | |
| 359 | for (let i = 0; i < linesToAffect; i++) { |
| 360 | const lineIdx = currentLine + i |
| 361 | const line = lines[lineIdx] ?? '' |
| 362 | |
| 363 | if (dir === '>') { |
| 364 | lines[lineIdx] = indent + line |
| 365 | } else if (line.startsWith(indent)) { |
| 366 | lines[lineIdx] = line.slice(indent.length) |
| 367 | } else if (line.startsWith('\t')) { |
| 368 | lines[lineIdx] = line.slice(1) |
| 369 | } else { |
| 370 | // Remove as much leading whitespace as possible up to indent length |
| 371 | let removed = 0 |
| 372 | let idx = 0 |
| 373 | while ( |
| 374 | idx < line.length && |
| 375 | removed < indent.length && |
| 376 | /\s/.test(line[idx]!) |
| 377 | ) { |
| 378 | removed++ |
| 379 | idx++ |
| 380 | } |
| 381 | lines[lineIdx] = line.slice(idx) |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | const newText = lines.join('\n') |
| 386 | const currentLineText = lines[currentLine] ?? '' |
| 387 | const firstNonBlank = (currentLineText.match(/^\s*/)?.[0] ?? '').length |
| 388 | |
| 389 | ctx.setText(newText) |
| 390 | ctx.setOffset(getLineStartOffset(lines, currentLine) + firstNonBlank) |
| 391 | ctx.recordChange({ type: 'indent', dir, count }) |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Execute open line (o/O command). |
no test coverage detected