@google/gemini-cli 0.1.10-dev.13 → 0.1.10-dev.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/package.json +2 -2
- package/dist/src/generated/git-commit.d.ts +1 -1
- package/dist/src/generated/git-commit.js +1 -1
- package/dist/src/ui/App.js +1 -1
- package/dist/src/ui/App.js.map +1 -1
- package/dist/src/ui/components/shared/text-buffer.d.ts +70 -15
- package/dist/src/ui/components/shared/text-buffer.js +562 -674
- package/dist/src/ui/components/shared/text-buffer.js.map +1 -1
- package/dist/src/ui/themes/ansi.js +1 -1
- package/dist/src/ui/themes/ansi.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
|
@@ -8,7 +8,7 @@ import { spawnSync } from 'child_process';
|
|
|
8
8
|
import fs from 'fs';
|
|
9
9
|
import os from 'os';
|
|
10
10
|
import pathMod from 'path';
|
|
11
|
-
import { useState, useCallback, useEffect, useMemo } from 'react';
|
|
11
|
+
import { useState, useCallback, useEffect, useMemo, useReducer } from 'react';
|
|
12
12
|
import stringWidth from 'string-width';
|
|
13
13
|
import { unescapePath } from '@google/gemini-cli-core';
|
|
14
14
|
import { toCodePoints, cpLen, cpSlice } from '../../utils/textUtils.js';
|
|
@@ -43,17 +43,6 @@ function stripUnsafeCharacters(str) {
|
|
|
43
43
|
function clamp(v, min, max) {
|
|
44
44
|
return v < min ? min : v > max ? max : v;
|
|
45
45
|
}
|
|
46
|
-
/* -------------------------------------------------------------------------
|
|
47
|
-
* Debug helper – enable verbose logging by setting env var TEXTBUFFER_DEBUG=1
|
|
48
|
-
* ---------------------------------------------------------------------- */
|
|
49
|
-
// Enable verbose logging only when requested via env var.
|
|
50
|
-
const DEBUG = process.env['TEXTBUFFER_DEBUG'] === '1' ||
|
|
51
|
-
process.env['TEXTBUFFER_DEBUG'] === 'true';
|
|
52
|
-
function dbg(...args) {
|
|
53
|
-
if (DEBUG) {
|
|
54
|
-
console.log('[TextBuffer]', ...args);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
46
|
function calculateInitialCursorPosition(initialLines, offset) {
|
|
58
47
|
let remainingChars = offset;
|
|
59
48
|
let row = 0;
|
|
@@ -289,37 +278,512 @@ function calculateVisualLayout(logicalLines, logicalCursor, viewportWidth) {
|
|
|
289
278
|
visualToLogicalMap,
|
|
290
279
|
};
|
|
291
280
|
}
|
|
281
|
+
const historyLimit = 100;
|
|
282
|
+
export function textBufferReducer(state, action) {
|
|
283
|
+
const pushUndo = (currentState) => {
|
|
284
|
+
const snapshot = {
|
|
285
|
+
lines: [...currentState.lines],
|
|
286
|
+
cursorRow: currentState.cursorRow,
|
|
287
|
+
cursorCol: currentState.cursorCol,
|
|
288
|
+
};
|
|
289
|
+
const newStack = [...currentState.undoStack, snapshot];
|
|
290
|
+
if (newStack.length > historyLimit) {
|
|
291
|
+
newStack.shift();
|
|
292
|
+
}
|
|
293
|
+
return { ...currentState, undoStack: newStack, redoStack: [] };
|
|
294
|
+
};
|
|
295
|
+
const currentLine = (r) => state.lines[r] ?? '';
|
|
296
|
+
const currentLineLen = (r) => cpLen(currentLine(r));
|
|
297
|
+
switch (action.type) {
|
|
298
|
+
case 'set_text': {
|
|
299
|
+
let nextState = state;
|
|
300
|
+
if (action.pushToUndo !== false) {
|
|
301
|
+
nextState = pushUndo(state);
|
|
302
|
+
}
|
|
303
|
+
const newContentLines = action.payload
|
|
304
|
+
.replace(/\r\n?/g, '\n')
|
|
305
|
+
.split('\n');
|
|
306
|
+
const lines = newContentLines.length === 0 ? [''] : newContentLines;
|
|
307
|
+
const lastNewLineIndex = lines.length - 1;
|
|
308
|
+
return {
|
|
309
|
+
...nextState,
|
|
310
|
+
lines,
|
|
311
|
+
cursorRow: lastNewLineIndex,
|
|
312
|
+
cursorCol: cpLen(lines[lastNewLineIndex] ?? ''),
|
|
313
|
+
preferredCol: null,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
case 'insert': {
|
|
317
|
+
const nextState = pushUndo(state);
|
|
318
|
+
const newLines = [...nextState.lines];
|
|
319
|
+
let newCursorRow = nextState.cursorRow;
|
|
320
|
+
let newCursorCol = nextState.cursorCol;
|
|
321
|
+
const currentLine = (r) => newLines[r] ?? '';
|
|
322
|
+
const str = stripUnsafeCharacters(action.payload.replace(/\r\n/g, '\n').replace(/\r/g, '\n'));
|
|
323
|
+
const parts = str.split('\n');
|
|
324
|
+
const lineContent = currentLine(newCursorRow);
|
|
325
|
+
const before = cpSlice(lineContent, 0, newCursorCol);
|
|
326
|
+
const after = cpSlice(lineContent, newCursorCol);
|
|
327
|
+
if (parts.length > 1) {
|
|
328
|
+
newLines[newCursorRow] = before + parts[0];
|
|
329
|
+
const remainingParts = parts.slice(1);
|
|
330
|
+
const lastPartOriginal = remainingParts.pop() ?? '';
|
|
331
|
+
newLines.splice(newCursorRow + 1, 0, ...remainingParts);
|
|
332
|
+
newLines.splice(newCursorRow + parts.length - 1, 0, lastPartOriginal + after);
|
|
333
|
+
newCursorRow = newCursorRow + parts.length - 1;
|
|
334
|
+
newCursorCol = cpLen(lastPartOriginal);
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
newLines[newCursorRow] = before + parts[0] + after;
|
|
338
|
+
newCursorCol = cpLen(before) + cpLen(parts[0]);
|
|
339
|
+
}
|
|
340
|
+
return {
|
|
341
|
+
...nextState,
|
|
342
|
+
lines: newLines,
|
|
343
|
+
cursorRow: newCursorRow,
|
|
344
|
+
cursorCol: newCursorCol,
|
|
345
|
+
preferredCol: null,
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
case 'backspace': {
|
|
349
|
+
const nextState = pushUndo(state);
|
|
350
|
+
const newLines = [...nextState.lines];
|
|
351
|
+
let newCursorRow = nextState.cursorRow;
|
|
352
|
+
let newCursorCol = nextState.cursorCol;
|
|
353
|
+
const currentLine = (r) => newLines[r] ?? '';
|
|
354
|
+
if (newCursorCol === 0 && newCursorRow === 0)
|
|
355
|
+
return state;
|
|
356
|
+
if (newCursorCol > 0) {
|
|
357
|
+
const lineContent = currentLine(newCursorRow);
|
|
358
|
+
newLines[newCursorRow] =
|
|
359
|
+
cpSlice(lineContent, 0, newCursorCol - 1) +
|
|
360
|
+
cpSlice(lineContent, newCursorCol);
|
|
361
|
+
newCursorCol--;
|
|
362
|
+
}
|
|
363
|
+
else if (newCursorRow > 0) {
|
|
364
|
+
const prevLineContent = currentLine(newCursorRow - 1);
|
|
365
|
+
const currentLineContentVal = currentLine(newCursorRow);
|
|
366
|
+
const newCol = cpLen(prevLineContent);
|
|
367
|
+
newLines[newCursorRow - 1] = prevLineContent + currentLineContentVal;
|
|
368
|
+
newLines.splice(newCursorRow, 1);
|
|
369
|
+
newCursorRow--;
|
|
370
|
+
newCursorCol = newCol;
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
...nextState,
|
|
374
|
+
lines: newLines,
|
|
375
|
+
cursorRow: newCursorRow,
|
|
376
|
+
cursorCol: newCursorCol,
|
|
377
|
+
preferredCol: null,
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
case 'set_viewport_width': {
|
|
381
|
+
if (action.payload === state.viewportWidth) {
|
|
382
|
+
return state;
|
|
383
|
+
}
|
|
384
|
+
return { ...state, viewportWidth: action.payload };
|
|
385
|
+
}
|
|
386
|
+
case 'move': {
|
|
387
|
+
const { dir } = action.payload;
|
|
388
|
+
const { lines, cursorRow, cursorCol, viewportWidth } = state;
|
|
389
|
+
const visualLayout = calculateVisualLayout(lines, [cursorRow, cursorCol], viewportWidth);
|
|
390
|
+
const { visualLines, visualCursor, visualToLogicalMap } = visualLayout;
|
|
391
|
+
let newVisualRow = visualCursor[0];
|
|
392
|
+
let newVisualCol = visualCursor[1];
|
|
393
|
+
let newPreferredCol = state.preferredCol;
|
|
394
|
+
const currentVisLineLen = cpLen(visualLines[newVisualRow] ?? '');
|
|
395
|
+
switch (dir) {
|
|
396
|
+
case 'left':
|
|
397
|
+
newPreferredCol = null;
|
|
398
|
+
if (newVisualCol > 0) {
|
|
399
|
+
newVisualCol--;
|
|
400
|
+
}
|
|
401
|
+
else if (newVisualRow > 0) {
|
|
402
|
+
newVisualRow--;
|
|
403
|
+
newVisualCol = cpLen(visualLines[newVisualRow] ?? '');
|
|
404
|
+
}
|
|
405
|
+
break;
|
|
406
|
+
case 'right':
|
|
407
|
+
newPreferredCol = null;
|
|
408
|
+
if (newVisualCol < currentVisLineLen) {
|
|
409
|
+
newVisualCol++;
|
|
410
|
+
}
|
|
411
|
+
else if (newVisualRow < visualLines.length - 1) {
|
|
412
|
+
newVisualRow++;
|
|
413
|
+
newVisualCol = 0;
|
|
414
|
+
}
|
|
415
|
+
break;
|
|
416
|
+
case 'up':
|
|
417
|
+
if (newVisualRow > 0) {
|
|
418
|
+
if (newPreferredCol === null)
|
|
419
|
+
newPreferredCol = newVisualCol;
|
|
420
|
+
newVisualRow--;
|
|
421
|
+
newVisualCol = clamp(newPreferredCol, 0, cpLen(visualLines[newVisualRow] ?? ''));
|
|
422
|
+
}
|
|
423
|
+
break;
|
|
424
|
+
case 'down':
|
|
425
|
+
if (newVisualRow < visualLines.length - 1) {
|
|
426
|
+
if (newPreferredCol === null)
|
|
427
|
+
newPreferredCol = newVisualCol;
|
|
428
|
+
newVisualRow++;
|
|
429
|
+
newVisualCol = clamp(newPreferredCol, 0, cpLen(visualLines[newVisualRow] ?? ''));
|
|
430
|
+
}
|
|
431
|
+
break;
|
|
432
|
+
case 'home':
|
|
433
|
+
newPreferredCol = null;
|
|
434
|
+
newVisualCol = 0;
|
|
435
|
+
break;
|
|
436
|
+
case 'end':
|
|
437
|
+
newPreferredCol = null;
|
|
438
|
+
newVisualCol = currentVisLineLen;
|
|
439
|
+
break;
|
|
440
|
+
case 'wordLeft': {
|
|
441
|
+
const { cursorRow, cursorCol, lines } = state;
|
|
442
|
+
if (cursorCol === 0 && cursorRow === 0)
|
|
443
|
+
return state;
|
|
444
|
+
let newCursorRow = cursorRow;
|
|
445
|
+
let newCursorCol = cursorCol;
|
|
446
|
+
if (cursorCol === 0) {
|
|
447
|
+
newCursorRow--;
|
|
448
|
+
newCursorCol = cpLen(lines[newCursorRow] ?? '');
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
const lineContent = lines[cursorRow];
|
|
452
|
+
const arr = toCodePoints(lineContent);
|
|
453
|
+
let start = cursorCol;
|
|
454
|
+
let onlySpaces = true;
|
|
455
|
+
for (let i = 0; i < start; i++) {
|
|
456
|
+
if (isWordChar(arr[i])) {
|
|
457
|
+
onlySpaces = false;
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
if (onlySpaces && start > 0) {
|
|
462
|
+
start--;
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
while (start > 0 && !isWordChar(arr[start - 1]))
|
|
466
|
+
start--;
|
|
467
|
+
while (start > 0 && isWordChar(arr[start - 1]))
|
|
468
|
+
start--;
|
|
469
|
+
}
|
|
470
|
+
newCursorCol = start;
|
|
471
|
+
}
|
|
472
|
+
return {
|
|
473
|
+
...state,
|
|
474
|
+
cursorRow: newCursorRow,
|
|
475
|
+
cursorCol: newCursorCol,
|
|
476
|
+
preferredCol: null,
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
case 'wordRight': {
|
|
480
|
+
const { cursorRow, cursorCol, lines } = state;
|
|
481
|
+
if (cursorRow === lines.length - 1 &&
|
|
482
|
+
cursorCol === cpLen(lines[cursorRow] ?? '')) {
|
|
483
|
+
return state;
|
|
484
|
+
}
|
|
485
|
+
let newCursorRow = cursorRow;
|
|
486
|
+
let newCursorCol = cursorCol;
|
|
487
|
+
const lineContent = lines[cursorRow] ?? '';
|
|
488
|
+
const arr = toCodePoints(lineContent);
|
|
489
|
+
if (cursorCol >= arr.length) {
|
|
490
|
+
newCursorRow++;
|
|
491
|
+
newCursorCol = 0;
|
|
492
|
+
}
|
|
493
|
+
else {
|
|
494
|
+
let end = cursorCol;
|
|
495
|
+
while (end < arr.length && !isWordChar(arr[end]))
|
|
496
|
+
end++;
|
|
497
|
+
while (end < arr.length && isWordChar(arr[end]))
|
|
498
|
+
end++;
|
|
499
|
+
newCursorCol = end;
|
|
500
|
+
}
|
|
501
|
+
return {
|
|
502
|
+
...state,
|
|
503
|
+
cursorRow: newCursorRow,
|
|
504
|
+
cursorCol: newCursorCol,
|
|
505
|
+
preferredCol: null,
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
default:
|
|
509
|
+
break;
|
|
510
|
+
}
|
|
511
|
+
if (visualToLogicalMap[newVisualRow]) {
|
|
512
|
+
const [logRow, logStartCol] = visualToLogicalMap[newVisualRow];
|
|
513
|
+
return {
|
|
514
|
+
...state,
|
|
515
|
+
cursorRow: logRow,
|
|
516
|
+
cursorCol: clamp(logStartCol + newVisualCol, 0, cpLen(state.lines[logRow] ?? '')),
|
|
517
|
+
preferredCol: newPreferredCol,
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
return state;
|
|
521
|
+
}
|
|
522
|
+
case 'delete': {
|
|
523
|
+
const { cursorRow, cursorCol, lines } = state;
|
|
524
|
+
const lineContent = currentLine(cursorRow);
|
|
525
|
+
if (cursorCol < currentLineLen(cursorRow)) {
|
|
526
|
+
const nextState = pushUndo(state);
|
|
527
|
+
const newLines = [...nextState.lines];
|
|
528
|
+
newLines[cursorRow] =
|
|
529
|
+
cpSlice(lineContent, 0, cursorCol) +
|
|
530
|
+
cpSlice(lineContent, cursorCol + 1);
|
|
531
|
+
return { ...nextState, lines: newLines, preferredCol: null };
|
|
532
|
+
}
|
|
533
|
+
else if (cursorRow < lines.length - 1) {
|
|
534
|
+
const nextState = pushUndo(state);
|
|
535
|
+
const nextLineContent = currentLine(cursorRow + 1);
|
|
536
|
+
const newLines = [...nextState.lines];
|
|
537
|
+
newLines[cursorRow] = lineContent + nextLineContent;
|
|
538
|
+
newLines.splice(cursorRow + 1, 1);
|
|
539
|
+
return { ...nextState, lines: newLines, preferredCol: null };
|
|
540
|
+
}
|
|
541
|
+
return state;
|
|
542
|
+
}
|
|
543
|
+
case 'delete_word_left': {
|
|
544
|
+
const { cursorRow, cursorCol } = state;
|
|
545
|
+
if (cursorCol === 0 && cursorRow === 0)
|
|
546
|
+
return state;
|
|
547
|
+
if (cursorCol === 0) {
|
|
548
|
+
// Act as a backspace
|
|
549
|
+
const nextState = pushUndo(state);
|
|
550
|
+
const prevLineContent = currentLine(cursorRow - 1);
|
|
551
|
+
const currentLineContentVal = currentLine(cursorRow);
|
|
552
|
+
const newCol = cpLen(prevLineContent);
|
|
553
|
+
const newLines = [...nextState.lines];
|
|
554
|
+
newLines[cursorRow - 1] = prevLineContent + currentLineContentVal;
|
|
555
|
+
newLines.splice(cursorRow, 1);
|
|
556
|
+
return {
|
|
557
|
+
...nextState,
|
|
558
|
+
lines: newLines,
|
|
559
|
+
cursorRow: cursorRow - 1,
|
|
560
|
+
cursorCol: newCol,
|
|
561
|
+
preferredCol: null,
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
const nextState = pushUndo(state);
|
|
565
|
+
const lineContent = currentLine(cursorRow);
|
|
566
|
+
const arr = toCodePoints(lineContent);
|
|
567
|
+
let start = cursorCol;
|
|
568
|
+
let onlySpaces = true;
|
|
569
|
+
for (let i = 0; i < start; i++) {
|
|
570
|
+
if (isWordChar(arr[i])) {
|
|
571
|
+
onlySpaces = false;
|
|
572
|
+
break;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
if (onlySpaces && start > 0) {
|
|
576
|
+
start--;
|
|
577
|
+
}
|
|
578
|
+
else {
|
|
579
|
+
while (start > 0 && !isWordChar(arr[start - 1]))
|
|
580
|
+
start--;
|
|
581
|
+
while (start > 0 && isWordChar(arr[start - 1]))
|
|
582
|
+
start--;
|
|
583
|
+
}
|
|
584
|
+
const newLines = [...nextState.lines];
|
|
585
|
+
newLines[cursorRow] =
|
|
586
|
+
cpSlice(lineContent, 0, start) + cpSlice(lineContent, cursorCol);
|
|
587
|
+
return {
|
|
588
|
+
...nextState,
|
|
589
|
+
lines: newLines,
|
|
590
|
+
cursorCol: start,
|
|
591
|
+
preferredCol: null,
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
case 'delete_word_right': {
|
|
595
|
+
const { cursorRow, cursorCol, lines } = state;
|
|
596
|
+
const lineContent = currentLine(cursorRow);
|
|
597
|
+
const arr = toCodePoints(lineContent);
|
|
598
|
+
if (cursorCol >= arr.length && cursorRow === lines.length - 1)
|
|
599
|
+
return state;
|
|
600
|
+
if (cursorCol >= arr.length) {
|
|
601
|
+
// Act as a delete
|
|
602
|
+
const nextState = pushUndo(state);
|
|
603
|
+
const nextLineContent = currentLine(cursorRow + 1);
|
|
604
|
+
const newLines = [...nextState.lines];
|
|
605
|
+
newLines[cursorRow] = lineContent + nextLineContent;
|
|
606
|
+
newLines.splice(cursorRow + 1, 1);
|
|
607
|
+
return { ...nextState, lines: newLines, preferredCol: null };
|
|
608
|
+
}
|
|
609
|
+
const nextState = pushUndo(state);
|
|
610
|
+
let end = cursorCol;
|
|
611
|
+
while (end < arr.length && !isWordChar(arr[end]))
|
|
612
|
+
end++;
|
|
613
|
+
while (end < arr.length && isWordChar(arr[end]))
|
|
614
|
+
end++;
|
|
615
|
+
const newLines = [...nextState.lines];
|
|
616
|
+
newLines[cursorRow] =
|
|
617
|
+
cpSlice(lineContent, 0, cursorCol) + cpSlice(lineContent, end);
|
|
618
|
+
return { ...nextState, lines: newLines, preferredCol: null };
|
|
619
|
+
}
|
|
620
|
+
case 'kill_line_right': {
|
|
621
|
+
const { cursorRow, cursorCol, lines } = state;
|
|
622
|
+
const lineContent = currentLine(cursorRow);
|
|
623
|
+
if (cursorCol < currentLineLen(cursorRow)) {
|
|
624
|
+
const nextState = pushUndo(state);
|
|
625
|
+
const newLines = [...nextState.lines];
|
|
626
|
+
newLines[cursorRow] = cpSlice(lineContent, 0, cursorCol);
|
|
627
|
+
return { ...nextState, lines: newLines };
|
|
628
|
+
}
|
|
629
|
+
else if (cursorRow < lines.length - 1) {
|
|
630
|
+
// Act as a delete
|
|
631
|
+
const nextState = pushUndo(state);
|
|
632
|
+
const nextLineContent = currentLine(cursorRow + 1);
|
|
633
|
+
const newLines = [...nextState.lines];
|
|
634
|
+
newLines[cursorRow] = lineContent + nextLineContent;
|
|
635
|
+
newLines.splice(cursorRow + 1, 1);
|
|
636
|
+
return { ...nextState, lines: newLines, preferredCol: null };
|
|
637
|
+
}
|
|
638
|
+
return state;
|
|
639
|
+
}
|
|
640
|
+
case 'kill_line_left': {
|
|
641
|
+
const { cursorRow, cursorCol } = state;
|
|
642
|
+
if (cursorCol > 0) {
|
|
643
|
+
const nextState = pushUndo(state);
|
|
644
|
+
const lineContent = currentLine(cursorRow);
|
|
645
|
+
const newLines = [...nextState.lines];
|
|
646
|
+
newLines[cursorRow] = cpSlice(lineContent, cursorCol);
|
|
647
|
+
return {
|
|
648
|
+
...nextState,
|
|
649
|
+
lines: newLines,
|
|
650
|
+
cursorCol: 0,
|
|
651
|
+
preferredCol: null,
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
return state;
|
|
655
|
+
}
|
|
656
|
+
case 'undo': {
|
|
657
|
+
const stateToRestore = state.undoStack[state.undoStack.length - 1];
|
|
658
|
+
if (!stateToRestore)
|
|
659
|
+
return state;
|
|
660
|
+
const currentSnapshot = {
|
|
661
|
+
lines: [...state.lines],
|
|
662
|
+
cursorRow: state.cursorRow,
|
|
663
|
+
cursorCol: state.cursorCol,
|
|
664
|
+
};
|
|
665
|
+
return {
|
|
666
|
+
...state,
|
|
667
|
+
...stateToRestore,
|
|
668
|
+
undoStack: state.undoStack.slice(0, -1),
|
|
669
|
+
redoStack: [...state.redoStack, currentSnapshot],
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
case 'redo': {
|
|
673
|
+
const stateToRestore = state.redoStack[state.redoStack.length - 1];
|
|
674
|
+
if (!stateToRestore)
|
|
675
|
+
return state;
|
|
676
|
+
const currentSnapshot = {
|
|
677
|
+
lines: [...state.lines],
|
|
678
|
+
cursorRow: state.cursorRow,
|
|
679
|
+
cursorCol: state.cursorCol,
|
|
680
|
+
};
|
|
681
|
+
return {
|
|
682
|
+
...state,
|
|
683
|
+
...stateToRestore,
|
|
684
|
+
redoStack: state.redoStack.slice(0, -1),
|
|
685
|
+
undoStack: [...state.undoStack, currentSnapshot],
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
case 'replace_range': {
|
|
689
|
+
const { startRow, startCol, endRow, endCol, text } = action.payload;
|
|
690
|
+
if (startRow > endRow ||
|
|
691
|
+
(startRow === endRow && startCol > endCol) ||
|
|
692
|
+
startRow < 0 ||
|
|
693
|
+
startCol < 0 ||
|
|
694
|
+
endRow >= state.lines.length ||
|
|
695
|
+
(endRow < state.lines.length && endCol > currentLineLen(endRow))) {
|
|
696
|
+
return state; // Invalid range
|
|
697
|
+
}
|
|
698
|
+
const nextState = pushUndo(state);
|
|
699
|
+
const newLines = [...nextState.lines];
|
|
700
|
+
const sCol = clamp(startCol, 0, currentLineLen(startRow));
|
|
701
|
+
const eCol = clamp(endCol, 0, currentLineLen(endRow));
|
|
702
|
+
const prefix = cpSlice(currentLine(startRow), 0, sCol);
|
|
703
|
+
const suffix = cpSlice(currentLine(endRow), eCol);
|
|
704
|
+
const normalisedReplacement = text
|
|
705
|
+
.replace(/\r\n/g, '\n')
|
|
706
|
+
.replace(/\r/g, '\n');
|
|
707
|
+
const replacementParts = normalisedReplacement.split('\n');
|
|
708
|
+
// Replace the content
|
|
709
|
+
if (startRow === endRow) {
|
|
710
|
+
newLines[startRow] = prefix + normalisedReplacement + suffix;
|
|
711
|
+
}
|
|
712
|
+
else {
|
|
713
|
+
const firstLine = prefix + replacementParts[0];
|
|
714
|
+
if (replacementParts.length === 1) {
|
|
715
|
+
// Single line of replacement text, but spanning multiple original lines
|
|
716
|
+
newLines.splice(startRow, endRow - startRow + 1, firstLine + suffix);
|
|
717
|
+
}
|
|
718
|
+
else {
|
|
719
|
+
// Multi-line replacement text
|
|
720
|
+
const lastLine = replacementParts[replacementParts.length - 1] + suffix;
|
|
721
|
+
const middleLines = replacementParts.slice(1, -1);
|
|
722
|
+
newLines.splice(startRow, endRow - startRow + 1, firstLine, ...middleLines, lastLine);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
const finalCursorRow = startRow + replacementParts.length - 1;
|
|
726
|
+
const finalCursorCol = (replacementParts.length > 1 ? 0 : sCol) +
|
|
727
|
+
cpLen(replacementParts[replacementParts.length - 1]);
|
|
728
|
+
return {
|
|
729
|
+
...nextState,
|
|
730
|
+
lines: newLines,
|
|
731
|
+
cursorRow: finalCursorRow,
|
|
732
|
+
cursorCol: finalCursorCol,
|
|
733
|
+
preferredCol: null,
|
|
734
|
+
};
|
|
735
|
+
}
|
|
736
|
+
case 'move_to_offset': {
|
|
737
|
+
const { offset } = action.payload;
|
|
738
|
+
const [newRow, newCol] = offsetToLogicalPos(state.lines.join('\n'), offset);
|
|
739
|
+
return {
|
|
740
|
+
...state,
|
|
741
|
+
cursorRow: newRow,
|
|
742
|
+
cursorCol: newCol,
|
|
743
|
+
preferredCol: null,
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
case 'create_undo_snapshot': {
|
|
747
|
+
return pushUndo(state);
|
|
748
|
+
}
|
|
749
|
+
default: {
|
|
750
|
+
const exhaustiveCheck = action;
|
|
751
|
+
console.error(`Unknown action encountered: ${exhaustiveCheck}`);
|
|
752
|
+
return state;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
// --- End of reducer logic ---
|
|
292
757
|
export function useTextBuffer({ initialText = '', initialCursorOffset = 0, viewport, stdin, setRawMode, onChange, isValidPath, }) {
|
|
293
|
-
const
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
const [
|
|
309
|
-
const
|
|
758
|
+
const initialState = useMemo(() => {
|
|
759
|
+
const lines = initialText.split('\n');
|
|
760
|
+
const [initialCursorRow, initialCursorCol] = calculateInitialCursorPosition(lines.length === 0 ? [''] : lines, initialCursorOffset);
|
|
761
|
+
return {
|
|
762
|
+
lines: lines.length === 0 ? [''] : lines,
|
|
763
|
+
cursorRow: initialCursorRow,
|
|
764
|
+
cursorCol: initialCursorCol,
|
|
765
|
+
preferredCol: null,
|
|
766
|
+
undoStack: [],
|
|
767
|
+
redoStack: [],
|
|
768
|
+
clipboard: null,
|
|
769
|
+
selectionAnchor: null,
|
|
770
|
+
viewportWidth: viewport.width,
|
|
771
|
+
};
|
|
772
|
+
}, [initialText, initialCursorOffset, viewport.width]);
|
|
773
|
+
const [state, dispatch] = useReducer(textBufferReducer, initialState);
|
|
774
|
+
const { lines, cursorRow, cursorCol, preferredCol, selectionAnchor } = state;
|
|
775
|
+
const text = useMemo(() => lines.join('\n'), [lines]);
|
|
776
|
+
const visualLayout = useMemo(() => calculateVisualLayout(lines, [cursorRow, cursorCol], state.viewportWidth), [lines, cursorRow, cursorCol, state.viewportWidth]);
|
|
777
|
+
const { visualLines, visualCursor } = visualLayout;
|
|
310
778
|
const [visualScrollRow, setVisualScrollRow] = useState(0);
|
|
311
|
-
const [logicalToVisualMap, setLogicalToVisualMap] = useState([]);
|
|
312
|
-
const [visualToLogicalMap, setVisualToLogicalMap] = useState([]);
|
|
313
|
-
const currentLine = useCallback((r) => lines[r] ?? '', [lines]);
|
|
314
|
-
const currentLineLen = useCallback((r) => cpLen(currentLine(r)), [currentLine]);
|
|
315
|
-
// Recalculate visual layout whenever logical lines or viewport width changes
|
|
316
779
|
useEffect(() => {
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
780
|
+
if (onChange) {
|
|
781
|
+
onChange(text);
|
|
782
|
+
}
|
|
783
|
+
}, [text, onChange]);
|
|
784
|
+
useEffect(() => {
|
|
785
|
+
dispatch({ type: 'set_viewport_width', payload: viewport.width });
|
|
786
|
+
}, [viewport.width]);
|
|
323
787
|
// Update visual scroll (vertical)
|
|
324
788
|
useEffect(() => {
|
|
325
789
|
const { height } = viewport;
|
|
@@ -334,149 +798,13 @@ export function useTextBuffer({ initialText = '', initialCursorOffset = 0, viewp
|
|
|
334
798
|
setVisualScrollRow(newVisualScrollRow);
|
|
335
799
|
}
|
|
336
800
|
}, [visualCursor, visualScrollRow, viewport]);
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
setUndoStack((prev) => {
|
|
341
|
-
const newStack = [...prev, snapshot];
|
|
342
|
-
if (newStack.length > historyLimit) {
|
|
343
|
-
newStack.shift();
|
|
344
|
-
}
|
|
345
|
-
return newStack;
|
|
346
|
-
});
|
|
347
|
-
setRedoStack([]);
|
|
348
|
-
}, [lines, cursorRow, cursorCol, historyLimit]);
|
|
349
|
-
const _restoreState = useCallback((state) => {
|
|
350
|
-
if (!state)
|
|
351
|
-
return false;
|
|
352
|
-
setLines(state.lines);
|
|
353
|
-
setCursorRow(state.cursorRow);
|
|
354
|
-
setCursorCol(state.cursorCol);
|
|
355
|
-
return true;
|
|
356
|
-
}, []);
|
|
357
|
-
const text = lines.join('\n');
|
|
358
|
-
useEffect(() => {
|
|
359
|
-
if (onChange) {
|
|
360
|
-
onChange(text);
|
|
361
|
-
}
|
|
362
|
-
}, [text, onChange]);
|
|
363
|
-
const undo = useCallback(() => {
|
|
364
|
-
const state = undoStack[undoStack.length - 1];
|
|
365
|
-
if (!state)
|
|
366
|
-
return false;
|
|
367
|
-
setUndoStack((prev) => prev.slice(0, -1));
|
|
368
|
-
const currentSnapshot = { lines: [...lines], cursorRow, cursorCol };
|
|
369
|
-
setRedoStack((prev) => [...prev, currentSnapshot]);
|
|
370
|
-
return _restoreState(state);
|
|
371
|
-
}, [undoStack, lines, cursorRow, cursorCol, _restoreState]);
|
|
372
|
-
const redo = useCallback(() => {
|
|
373
|
-
const state = redoStack[redoStack.length - 1];
|
|
374
|
-
if (!state)
|
|
375
|
-
return false;
|
|
376
|
-
setRedoStack((prev) => prev.slice(0, -1));
|
|
377
|
-
const currentSnapshot = { lines: [...lines], cursorRow, cursorCol };
|
|
378
|
-
setUndoStack((prev) => [...prev, currentSnapshot]);
|
|
379
|
-
return _restoreState(state);
|
|
380
|
-
}, [redoStack, lines, cursorRow, cursorCol, _restoreState]);
|
|
381
|
-
const applyOperations = useCallback((ops) => {
|
|
382
|
-
if (ops.length === 0)
|
|
383
|
-
return;
|
|
384
|
-
setOpQueue((prev) => [...prev, ...ops]);
|
|
385
|
-
}, []);
|
|
386
|
-
useEffect(() => {
|
|
387
|
-
if (opQueue.length === 0)
|
|
388
|
-
return;
|
|
389
|
-
const expandedOps = [];
|
|
390
|
-
for (const op of opQueue) {
|
|
391
|
-
if (op.type === 'insert') {
|
|
392
|
-
let currentText = '';
|
|
393
|
-
for (const char of toCodePoints(op.payload)) {
|
|
394
|
-
if (char.codePointAt(0) === 127) {
|
|
395
|
-
// \x7f
|
|
396
|
-
if (currentText.length > 0) {
|
|
397
|
-
expandedOps.push({ type: 'insert', payload: currentText });
|
|
398
|
-
currentText = '';
|
|
399
|
-
}
|
|
400
|
-
expandedOps.push({ type: 'backspace' });
|
|
401
|
-
}
|
|
402
|
-
else {
|
|
403
|
-
currentText += char;
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
if (currentText.length > 0) {
|
|
407
|
-
expandedOps.push({ type: 'insert', payload: currentText });
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
else {
|
|
411
|
-
expandedOps.push(op);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
if (expandedOps.length === 0) {
|
|
415
|
-
setOpQueue([]); // Clear queue even if ops were no-ops
|
|
801
|
+
const insert = useCallback((ch) => {
|
|
802
|
+
if (/[\n\r]/.test(ch)) {
|
|
803
|
+
dispatch({ type: 'insert', payload: ch });
|
|
416
804
|
return;
|
|
417
805
|
}
|
|
418
|
-
pushUndo(); // Snapshot before applying batch of updates
|
|
419
|
-
const newLines = [...lines];
|
|
420
|
-
let newCursorRow = cursorRow;
|
|
421
|
-
let newCursorCol = cursorCol;
|
|
422
|
-
const currentLine = (r) => newLines[r] ?? '';
|
|
423
|
-
for (const op of expandedOps) {
|
|
424
|
-
if (op.type === 'insert') {
|
|
425
|
-
const str = stripUnsafeCharacters(op.payload.replace(/\r\n/g, '\n').replace(/\r/g, '\n'));
|
|
426
|
-
const parts = str.split('\n');
|
|
427
|
-
const lineContent = currentLine(newCursorRow);
|
|
428
|
-
const before = cpSlice(lineContent, 0, newCursorCol);
|
|
429
|
-
const after = cpSlice(lineContent, newCursorCol);
|
|
430
|
-
if (parts.length > 1) {
|
|
431
|
-
newLines[newCursorRow] = before + parts[0];
|
|
432
|
-
const remainingParts = parts.slice(1);
|
|
433
|
-
const lastPartOriginal = remainingParts.pop() ?? '';
|
|
434
|
-
newLines.splice(newCursorRow + 1, 0, ...remainingParts);
|
|
435
|
-
newLines.splice(newCursorRow + parts.length - 1, 0, lastPartOriginal + after);
|
|
436
|
-
newCursorRow = newCursorRow + parts.length - 1;
|
|
437
|
-
newCursorCol = cpLen(lastPartOriginal);
|
|
438
|
-
}
|
|
439
|
-
else {
|
|
440
|
-
newLines[newCursorRow] = before + parts[0] + after;
|
|
441
|
-
newCursorCol = cpLen(before) + cpLen(parts[0]);
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
else if (op.type === 'backspace') {
|
|
445
|
-
if (newCursorCol === 0 && newCursorRow === 0)
|
|
446
|
-
continue;
|
|
447
|
-
if (newCursorCol > 0) {
|
|
448
|
-
const lineContent = currentLine(newCursorRow);
|
|
449
|
-
newLines[newCursorRow] =
|
|
450
|
-
cpSlice(lineContent, 0, newCursorCol - 1) +
|
|
451
|
-
cpSlice(lineContent, newCursorCol);
|
|
452
|
-
newCursorCol--;
|
|
453
|
-
}
|
|
454
|
-
else if (newCursorRow > 0) {
|
|
455
|
-
const prevLineContent = currentLine(newCursorRow - 1);
|
|
456
|
-
const currentLineContentVal = currentLine(newCursorRow);
|
|
457
|
-
const newCol = cpLen(prevLineContent);
|
|
458
|
-
newLines[newCursorRow - 1] = prevLineContent + currentLineContentVal;
|
|
459
|
-
newLines.splice(newCursorRow, 1);
|
|
460
|
-
newCursorRow--;
|
|
461
|
-
newCursorCol = newCol;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
setLines(newLines);
|
|
466
|
-
setCursorRow(newCursorRow);
|
|
467
|
-
setCursorCol(newCursorCol);
|
|
468
|
-
setPreferredCol(null);
|
|
469
|
-
// Clear the queue after processing
|
|
470
|
-
setOpQueue((prev) => prev.slice(opQueue.length));
|
|
471
|
-
}, [opQueue, lines, cursorRow, cursorCol, pushUndo, setPreferredCol]);
|
|
472
|
-
const insert = useCallback((ch) => {
|
|
473
|
-
dbg('insert', { ch, beforeCursor: [cursorRow, cursorCol] });
|
|
474
|
-
ch = stripUnsafeCharacters(ch);
|
|
475
|
-
// Arbitrary threshold to avoid false positives on normal key presses
|
|
476
|
-
// while still detecting virtually all reasonable length file paths.
|
|
477
806
|
const minLengthToInferAsDragDrop = 3;
|
|
478
807
|
if (ch.length >= minLengthToInferAsDragDrop) {
|
|
479
|
-
// Possible drag and drop of a file path.
|
|
480
808
|
let potentialPath = ch;
|
|
481
809
|
if (potentialPath.length > 2 &&
|
|
482
810
|
potentialPath.startsWith("'") &&
|
|
@@ -484,433 +812,60 @@ export function useTextBuffer({ initialText = '', initialCursorOffset = 0, viewp
|
|
|
484
812
|
potentialPath = ch.slice(1, -1);
|
|
485
813
|
}
|
|
486
814
|
potentialPath = potentialPath.trim();
|
|
487
|
-
// Be conservative and only add an @ if the path is valid.
|
|
488
815
|
if (isValidPath(unescapePath(potentialPath))) {
|
|
489
816
|
ch = `@${potentialPath}`;
|
|
490
817
|
}
|
|
491
818
|
}
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
const backspace = useCallback(() => {
|
|
499
|
-
dbg('backspace', { beforeCursor: [cursorRow, cursorCol] });
|
|
500
|
-
if (cursorCol === 0 && cursorRow === 0)
|
|
501
|
-
return;
|
|
502
|
-
applyOperations([{ type: 'backspace' }]);
|
|
503
|
-
}, [applyOperations, cursorRow, cursorCol]);
|
|
504
|
-
const del = useCallback(() => {
|
|
505
|
-
dbg('delete', { beforeCursor: [cursorRow, cursorCol] });
|
|
506
|
-
const lineContent = currentLine(cursorRow);
|
|
507
|
-
if (cursorCol < currentLineLen(cursorRow)) {
|
|
508
|
-
pushUndo();
|
|
509
|
-
setLines((prevLines) => {
|
|
510
|
-
const newLines = [...prevLines];
|
|
511
|
-
newLines[cursorRow] =
|
|
512
|
-
cpSlice(lineContent, 0, cursorCol) +
|
|
513
|
-
cpSlice(lineContent, cursorCol + 1);
|
|
514
|
-
return newLines;
|
|
515
|
-
});
|
|
516
|
-
}
|
|
517
|
-
else if (cursorRow < lines.length - 1) {
|
|
518
|
-
pushUndo();
|
|
519
|
-
const nextLineContent = currentLine(cursorRow + 1);
|
|
520
|
-
setLines((prevLines) => {
|
|
521
|
-
const newLines = [...prevLines];
|
|
522
|
-
newLines[cursorRow] = lineContent + nextLineContent;
|
|
523
|
-
newLines.splice(cursorRow + 1, 1);
|
|
524
|
-
return newLines;
|
|
525
|
-
});
|
|
526
|
-
}
|
|
527
|
-
// cursor position does not change for del
|
|
528
|
-
setPreferredCol(null);
|
|
529
|
-
}, [
|
|
530
|
-
pushUndo,
|
|
531
|
-
cursorRow,
|
|
532
|
-
cursorCol,
|
|
533
|
-
currentLine,
|
|
534
|
-
currentLineLen,
|
|
535
|
-
lines.length,
|
|
536
|
-
setPreferredCol,
|
|
537
|
-
]);
|
|
538
|
-
const setText = useCallback((newText) => {
|
|
539
|
-
dbg('setText', { text: newText });
|
|
540
|
-
pushUndo();
|
|
541
|
-
const newContentLines = newText.replace(/\r\n?/g, '\n').split('\n');
|
|
542
|
-
setLines(newContentLines.length === 0 ? [''] : newContentLines);
|
|
543
|
-
// Set logical cursor to the end of the new text
|
|
544
|
-
const lastNewLineIndex = newContentLines.length - 1;
|
|
545
|
-
setCursorRow(lastNewLineIndex);
|
|
546
|
-
setCursorCol(cpLen(newContentLines[lastNewLineIndex] ?? ''));
|
|
547
|
-
setPreferredCol(null);
|
|
548
|
-
}, [pushUndo, setPreferredCol]);
|
|
549
|
-
const replaceRange = useCallback((startRow, startCol, endRow, endCol, replacementText) => {
|
|
550
|
-
if (startRow > endRow ||
|
|
551
|
-
(startRow === endRow && startCol > endCol) ||
|
|
552
|
-
startRow < 0 ||
|
|
553
|
-
startCol < 0 ||
|
|
554
|
-
endRow >= lines.length ||
|
|
555
|
-
(endRow < lines.length && endCol > currentLineLen(endRow))) {
|
|
556
|
-
console.error('Invalid range provided to replaceRange', {
|
|
557
|
-
startRow,
|
|
558
|
-
startCol,
|
|
559
|
-
endRow,
|
|
560
|
-
endCol,
|
|
561
|
-
linesLength: lines.length,
|
|
562
|
-
endRowLineLength: currentLineLen(endRow),
|
|
563
|
-
});
|
|
564
|
-
return false;
|
|
565
|
-
}
|
|
566
|
-
dbg('replaceRange', {
|
|
567
|
-
start: [startRow, startCol],
|
|
568
|
-
end: [endRow, endCol],
|
|
569
|
-
text: replacementText,
|
|
570
|
-
});
|
|
571
|
-
pushUndo();
|
|
572
|
-
const sCol = clamp(startCol, 0, currentLineLen(startRow));
|
|
573
|
-
const eCol = clamp(endCol, 0, currentLineLen(endRow));
|
|
574
|
-
const prefix = cpSlice(currentLine(startRow), 0, sCol);
|
|
575
|
-
const suffix = cpSlice(currentLine(endRow), eCol);
|
|
576
|
-
const normalisedReplacement = replacementText
|
|
577
|
-
.replace(/\r\n/g, '\n')
|
|
578
|
-
.replace(/\r/g, '\n');
|
|
579
|
-
const replacementParts = normalisedReplacement.split('\n');
|
|
580
|
-
setLines((prevLines) => {
|
|
581
|
-
const newLines = [...prevLines];
|
|
582
|
-
// Remove lines between startRow and endRow (exclusive of startRow, inclusive of endRow if different)
|
|
583
|
-
if (startRow < endRow) {
|
|
584
|
-
newLines.splice(startRow + 1, endRow - startRow);
|
|
585
|
-
}
|
|
586
|
-
// Construct the new content for the startRow
|
|
587
|
-
newLines[startRow] = prefix + replacementParts[0];
|
|
588
|
-
// If replacementText has multiple lines, insert them
|
|
589
|
-
if (replacementParts.length > 1) {
|
|
590
|
-
const lastReplacementPart = replacementParts.pop() ?? ''; // parts are already split by \n
|
|
591
|
-
// Insert middle parts (if any)
|
|
592
|
-
if (replacementParts.length > 1) {
|
|
593
|
-
// parts[0] is already used
|
|
594
|
-
newLines.splice(startRow + 1, 0, ...replacementParts.slice(1));
|
|
595
|
-
}
|
|
596
|
-
// The line where the last part of the replacement will go
|
|
597
|
-
const targetRowForLastPart = startRow + (replacementParts.length - 1); // -1 because parts[0] is on startRow
|
|
598
|
-
// If the last part is not the first part (multi-line replacement)
|
|
599
|
-
if (targetRowForLastPart > startRow ||
|
|
600
|
-
(replacementParts.length === 1 && lastReplacementPart !== '')) {
|
|
601
|
-
// If the target row for the last part doesn't exist (because it's a new line created by replacement)
|
|
602
|
-
// ensure it's created before trying to append suffix.
|
|
603
|
-
// This case should be handled by splice if replacementParts.length > 1
|
|
604
|
-
// For single line replacement that becomes multi-line due to parts.length > 1 logic, this is tricky.
|
|
605
|
-
// Let's assume newLines[targetRowForLastPart] exists due to previous splice or it's newLines[startRow]
|
|
606
|
-
if (newLines[targetRowForLastPart] === undefined &&
|
|
607
|
-
targetRowForLastPart === startRow + 1 &&
|
|
608
|
-
replacementParts.length === 1) {
|
|
609
|
-
// This implies a single line replacement that became two lines.
|
|
610
|
-
// e.g. "abc" replace "b" with "B\nC" -> "aB", "C", "c"
|
|
611
|
-
// Here, lastReplacementPart is "C", targetRowForLastPart is startRow + 1
|
|
612
|
-
newLines.splice(targetRowForLastPart, 0, lastReplacementPart + suffix);
|
|
613
|
-
}
|
|
614
|
-
else {
|
|
615
|
-
newLines[targetRowForLastPart] =
|
|
616
|
-
(newLines[targetRowForLastPart] || '') +
|
|
617
|
-
lastReplacementPart +
|
|
618
|
-
suffix;
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
else {
|
|
622
|
-
// Single line in replacementParts, but it was the only part
|
|
623
|
-
newLines[startRow] += suffix;
|
|
819
|
+
let currentText = '';
|
|
820
|
+
for (const char of toCodePoints(ch)) {
|
|
821
|
+
if (char.codePointAt(0) === 127) {
|
|
822
|
+
if (currentText.length > 0) {
|
|
823
|
+
dispatch({ type: 'insert', payload: currentText });
|
|
824
|
+
currentText = '';
|
|
624
825
|
}
|
|
625
|
-
|
|
626
|
-
setCursorCol(cpLen(newLines[targetRowForLastPart]) - cpLen(suffix));
|
|
826
|
+
dispatch({ type: 'backspace' });
|
|
627
827
|
}
|
|
628
828
|
else {
|
|
629
|
-
|
|
630
|
-
newLines[startRow] += suffix;
|
|
631
|
-
setCursorRow(startRow);
|
|
632
|
-
setCursorCol(cpLen(prefix) + cpLen(replacementParts[0]));
|
|
829
|
+
currentText += char;
|
|
633
830
|
}
|
|
634
|
-
return newLines;
|
|
635
|
-
});
|
|
636
|
-
setPreferredCol(null);
|
|
637
|
-
return true;
|
|
638
|
-
}, [pushUndo, lines, currentLine, currentLineLen, setPreferredCol]);
|
|
639
|
-
const deleteWordLeft = useCallback(() => {
|
|
640
|
-
dbg('deleteWordLeft', { beforeCursor: [cursorRow, cursorCol] });
|
|
641
|
-
if (cursorCol === 0 && cursorRow === 0)
|
|
642
|
-
return;
|
|
643
|
-
if (cursorCol === 0) {
|
|
644
|
-
backspace();
|
|
645
|
-
return;
|
|
646
831
|
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
const arr = toCodePoints(lineContent);
|
|
650
|
-
let start = cursorCol;
|
|
651
|
-
let onlySpaces = true;
|
|
652
|
-
for (let i = 0; i < start; i++) {
|
|
653
|
-
if (isWordChar(arr[i])) {
|
|
654
|
-
onlySpaces = false;
|
|
655
|
-
break;
|
|
656
|
-
}
|
|
832
|
+
if (currentText.length > 0) {
|
|
833
|
+
dispatch({ type: 'insert', payload: currentText });
|
|
657
834
|
}
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
});
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
835
|
+
}, [isValidPath]);
|
|
836
|
+
const newline = useCallback(() => {
|
|
837
|
+
dispatch({ type: 'insert', payload: '\n' });
|
|
838
|
+
}, []);
|
|
839
|
+
const backspace = useCallback(() => {
|
|
840
|
+
dispatch({ type: 'backspace' });
|
|
841
|
+
}, []);
|
|
842
|
+
const del = useCallback(() => {
|
|
843
|
+
dispatch({ type: 'delete' });
|
|
844
|
+
}, []);
|
|
845
|
+
const move = useCallback((dir) => {
|
|
846
|
+
dispatch({ type: 'move', payload: { dir } });
|
|
847
|
+
}, []);
|
|
848
|
+
const undo = useCallback(() => {
|
|
849
|
+
dispatch({ type: 'undo' });
|
|
850
|
+
}, []);
|
|
851
|
+
const redo = useCallback(() => {
|
|
852
|
+
dispatch({ type: 'redo' });
|
|
853
|
+
}, []);
|
|
854
|
+
const setText = useCallback((newText) => {
|
|
855
|
+
dispatch({ type: 'set_text', payload: newText });
|
|
856
|
+
}, []);
|
|
857
|
+
const deleteWordLeft = useCallback(() => {
|
|
858
|
+
dispatch({ type: 'delete_word_left' });
|
|
859
|
+
}, []);
|
|
676
860
|
const deleteWordRight = useCallback(() => {
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
const arr = toCodePoints(lineContent);
|
|
680
|
-
if (cursorCol >= arr.length && cursorRow === lines.length - 1)
|
|
681
|
-
return;
|
|
682
|
-
if (cursorCol >= arr.length) {
|
|
683
|
-
del();
|
|
684
|
-
return;
|
|
685
|
-
}
|
|
686
|
-
pushUndo();
|
|
687
|
-
let end = cursorCol;
|
|
688
|
-
while (end < arr.length && !isWordChar(arr[end]))
|
|
689
|
-
end++;
|
|
690
|
-
while (end < arr.length && isWordChar(arr[end]))
|
|
691
|
-
end++;
|
|
692
|
-
setLines((prevLines) => {
|
|
693
|
-
const newLines = [...prevLines];
|
|
694
|
-
newLines[cursorRow] =
|
|
695
|
-
cpSlice(lineContent, 0, cursorCol) + cpSlice(lineContent, end);
|
|
696
|
-
return newLines;
|
|
697
|
-
});
|
|
698
|
-
setPreferredCol(null);
|
|
699
|
-
}, [
|
|
700
|
-
pushUndo,
|
|
701
|
-
cursorRow,
|
|
702
|
-
cursorCol,
|
|
703
|
-
currentLine,
|
|
704
|
-
del,
|
|
705
|
-
lines.length,
|
|
706
|
-
setPreferredCol,
|
|
707
|
-
]);
|
|
861
|
+
dispatch({ type: 'delete_word_right' });
|
|
862
|
+
}, []);
|
|
708
863
|
const killLineRight = useCallback(() => {
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
// Cursor is before the end of the line's content, delete text to the right
|
|
712
|
-
pushUndo();
|
|
713
|
-
setLines((prevLines) => {
|
|
714
|
-
const newLines = [...prevLines];
|
|
715
|
-
newLines[cursorRow] = cpSlice(lineContent, 0, cursorCol);
|
|
716
|
-
return newLines;
|
|
717
|
-
});
|
|
718
|
-
// Cursor position and preferredCol do not change in this case
|
|
719
|
-
}
|
|
720
|
-
else if (cursorCol === currentLineLen(cursorRow) &&
|
|
721
|
-
cursorRow < lines.length - 1) {
|
|
722
|
-
// Cursor is at the end of the line's content (or line is empty),
|
|
723
|
-
// and it's not the last line. Delete the newline.
|
|
724
|
-
// `del()` handles pushUndo and setPreferredCol.
|
|
725
|
-
del();
|
|
726
|
-
}
|
|
727
|
-
// If cursor is at the end of the line and it's the last line, do nothing.
|
|
728
|
-
}, [
|
|
729
|
-
pushUndo,
|
|
730
|
-
cursorRow,
|
|
731
|
-
cursorCol,
|
|
732
|
-
currentLine,
|
|
733
|
-
currentLineLen,
|
|
734
|
-
lines.length,
|
|
735
|
-
del,
|
|
736
|
-
]);
|
|
864
|
+
dispatch({ type: 'kill_line_right' });
|
|
865
|
+
}, []);
|
|
737
866
|
const killLineLeft = useCallback(() => {
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
if (cursorCol > 0) {
|
|
741
|
-
pushUndo();
|
|
742
|
-
setLines((prevLines) => {
|
|
743
|
-
const newLines = [...prevLines];
|
|
744
|
-
newLines[cursorRow] = cpSlice(lineContent, cursorCol);
|
|
745
|
-
return newLines;
|
|
746
|
-
});
|
|
747
|
-
setCursorCol(0);
|
|
748
|
-
setPreferredCol(null);
|
|
749
|
-
}
|
|
750
|
-
}, [pushUndo, cursorRow, cursorCol, currentLine, setPreferredCol]);
|
|
751
|
-
const move = useCallback((dir) => {
|
|
752
|
-
let newVisualRow = visualCursor[0];
|
|
753
|
-
let newVisualCol = visualCursor[1];
|
|
754
|
-
let newPreferredCol = preferredCol;
|
|
755
|
-
const currentVisLineLen = cpLen(visualLines[newVisualRow] ?? '');
|
|
756
|
-
switch (dir) {
|
|
757
|
-
case 'left':
|
|
758
|
-
newPreferredCol = null;
|
|
759
|
-
if (newVisualCol > 0) {
|
|
760
|
-
newVisualCol--;
|
|
761
|
-
}
|
|
762
|
-
else if (newVisualRow > 0) {
|
|
763
|
-
newVisualRow--;
|
|
764
|
-
newVisualCol = cpLen(visualLines[newVisualRow] ?? '');
|
|
765
|
-
}
|
|
766
|
-
break;
|
|
767
|
-
case 'right':
|
|
768
|
-
newPreferredCol = null;
|
|
769
|
-
if (newVisualCol < currentVisLineLen) {
|
|
770
|
-
newVisualCol++;
|
|
771
|
-
}
|
|
772
|
-
else if (newVisualRow < visualLines.length - 1) {
|
|
773
|
-
newVisualRow++;
|
|
774
|
-
newVisualCol = 0;
|
|
775
|
-
}
|
|
776
|
-
break;
|
|
777
|
-
case 'up':
|
|
778
|
-
if (newVisualRow > 0) {
|
|
779
|
-
if (newPreferredCol === null)
|
|
780
|
-
newPreferredCol = newVisualCol;
|
|
781
|
-
newVisualRow--;
|
|
782
|
-
newVisualCol = clamp(newPreferredCol, 0, cpLen(visualLines[newVisualRow] ?? ''));
|
|
783
|
-
}
|
|
784
|
-
break;
|
|
785
|
-
case 'down':
|
|
786
|
-
if (newVisualRow < visualLines.length - 1) {
|
|
787
|
-
if (newPreferredCol === null)
|
|
788
|
-
newPreferredCol = newVisualCol;
|
|
789
|
-
newVisualRow++;
|
|
790
|
-
newVisualCol = clamp(newPreferredCol, 0, cpLen(visualLines[newVisualRow] ?? ''));
|
|
791
|
-
}
|
|
792
|
-
break;
|
|
793
|
-
case 'home':
|
|
794
|
-
newPreferredCol = null;
|
|
795
|
-
newVisualCol = 0;
|
|
796
|
-
break;
|
|
797
|
-
case 'end':
|
|
798
|
-
newPreferredCol = null;
|
|
799
|
-
newVisualCol = currentVisLineLen;
|
|
800
|
-
break;
|
|
801
|
-
// wordLeft and wordRight might need more sophisticated visual handling
|
|
802
|
-
// For now, they operate on the logical line derived from the visual cursor
|
|
803
|
-
case 'wordLeft': {
|
|
804
|
-
newPreferredCol = null;
|
|
805
|
-
if (visualToLogicalMap.length === 0 ||
|
|
806
|
-
logicalToVisualMap.length === 0)
|
|
807
|
-
break;
|
|
808
|
-
const [logRow, logColInitial] = visualToLogicalMap[newVisualRow] ?? [
|
|
809
|
-
0, 0,
|
|
810
|
-
];
|
|
811
|
-
const currentLogCol = logColInitial + newVisualCol;
|
|
812
|
-
const lineText = lines[logRow];
|
|
813
|
-
const sliceToCursor = cpSlice(lineText, 0, currentLogCol).replace(/[\s,.;!?]+$/, '');
|
|
814
|
-
let lastIdx = 0;
|
|
815
|
-
const regex = /[\s,.;!?]+/g;
|
|
816
|
-
let m;
|
|
817
|
-
while ((m = regex.exec(sliceToCursor)) != null)
|
|
818
|
-
lastIdx = m.index;
|
|
819
|
-
const newLogicalCol = lastIdx === 0 ? 0 : cpLen(sliceToCursor.slice(0, lastIdx)) + 1;
|
|
820
|
-
// Map newLogicalCol back to visual
|
|
821
|
-
const targetLogicalMapEntries = logicalToVisualMap[logRow];
|
|
822
|
-
if (!targetLogicalMapEntries)
|
|
823
|
-
break;
|
|
824
|
-
for (let i = targetLogicalMapEntries.length - 1; i >= 0; i--) {
|
|
825
|
-
const [visRow, logStartCol] = targetLogicalMapEntries[i];
|
|
826
|
-
if (newLogicalCol >= logStartCol) {
|
|
827
|
-
newVisualRow = visRow;
|
|
828
|
-
newVisualCol = newLogicalCol - logStartCol;
|
|
829
|
-
break;
|
|
830
|
-
}
|
|
831
|
-
}
|
|
832
|
-
break;
|
|
833
|
-
}
|
|
834
|
-
case 'wordRight': {
|
|
835
|
-
newPreferredCol = null;
|
|
836
|
-
if (visualToLogicalMap.length === 0 ||
|
|
837
|
-
logicalToVisualMap.length === 0)
|
|
838
|
-
break;
|
|
839
|
-
const [logRow, logColInitial] = visualToLogicalMap[newVisualRow] ?? [
|
|
840
|
-
0, 0,
|
|
841
|
-
];
|
|
842
|
-
const currentLogCol = logColInitial + newVisualCol;
|
|
843
|
-
const lineText = lines[logRow];
|
|
844
|
-
const regex = /[\s,.;!?]+/g;
|
|
845
|
-
let moved = false;
|
|
846
|
-
let m;
|
|
847
|
-
let newLogicalCol = currentLineLen(logRow); // Default to end of logical line
|
|
848
|
-
while ((m = regex.exec(lineText)) != null) {
|
|
849
|
-
const cpIdx = cpLen(lineText.slice(0, m.index));
|
|
850
|
-
if (cpIdx > currentLogCol) {
|
|
851
|
-
newLogicalCol = cpIdx;
|
|
852
|
-
moved = true;
|
|
853
|
-
break;
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
if (!moved && currentLogCol < currentLineLen(logRow)) {
|
|
857
|
-
// If no word break found after cursor, move to end
|
|
858
|
-
newLogicalCol = currentLineLen(logRow);
|
|
859
|
-
}
|
|
860
|
-
// Map newLogicalCol back to visual
|
|
861
|
-
const targetLogicalMapEntries = logicalToVisualMap[logRow];
|
|
862
|
-
if (!targetLogicalMapEntries)
|
|
863
|
-
break;
|
|
864
|
-
for (let i = 0; i < targetLogicalMapEntries.length; i++) {
|
|
865
|
-
const [visRow, logStartCol] = targetLogicalMapEntries[i];
|
|
866
|
-
const nextLogStartCol = i + 1 < targetLogicalMapEntries.length
|
|
867
|
-
? targetLogicalMapEntries[i + 1][1]
|
|
868
|
-
: Infinity;
|
|
869
|
-
if (newLogicalCol >= logStartCol &&
|
|
870
|
-
newLogicalCol < nextLogStartCol) {
|
|
871
|
-
newVisualRow = visRow;
|
|
872
|
-
newVisualCol = newLogicalCol - logStartCol;
|
|
873
|
-
break;
|
|
874
|
-
}
|
|
875
|
-
if (newLogicalCol === logStartCol &&
|
|
876
|
-
i === targetLogicalMapEntries.length - 1 &&
|
|
877
|
-
cpLen(visualLines[visRow] ?? '') === 0) {
|
|
878
|
-
// Special case: moving to an empty visual line at the end of a logical line
|
|
879
|
-
newVisualRow = visRow;
|
|
880
|
-
newVisualCol = 0;
|
|
881
|
-
break;
|
|
882
|
-
}
|
|
883
|
-
}
|
|
884
|
-
break;
|
|
885
|
-
}
|
|
886
|
-
default:
|
|
887
|
-
break;
|
|
888
|
-
}
|
|
889
|
-
setVisualCursor([newVisualRow, newVisualCol]);
|
|
890
|
-
setPreferredCol(newPreferredCol);
|
|
891
|
-
// Update logical cursor based on new visual cursor
|
|
892
|
-
if (visualToLogicalMap[newVisualRow]) {
|
|
893
|
-
const [logRow, logStartCol] = visualToLogicalMap[newVisualRow];
|
|
894
|
-
setCursorRow(logRow);
|
|
895
|
-
setCursorCol(clamp(logStartCol + newVisualCol, 0, currentLineLen(logRow)));
|
|
896
|
-
}
|
|
897
|
-
dbg('move', {
|
|
898
|
-
dir,
|
|
899
|
-
visualBefore: visualCursor,
|
|
900
|
-
visualAfter: [newVisualRow, newVisualCol],
|
|
901
|
-
logicalAfter: [cursorRow, cursorCol],
|
|
902
|
-
});
|
|
903
|
-
}, [
|
|
904
|
-
visualCursor,
|
|
905
|
-
visualLines,
|
|
906
|
-
preferredCol,
|
|
907
|
-
lines,
|
|
908
|
-
currentLineLen,
|
|
909
|
-
visualToLogicalMap,
|
|
910
|
-
logicalToVisualMap,
|
|
911
|
-
cursorCol,
|
|
912
|
-
cursorRow,
|
|
913
|
-
]);
|
|
867
|
+
dispatch({ type: 'kill_line_left' });
|
|
868
|
+
}, []);
|
|
914
869
|
const openInExternalEditor = useCallback(async (opts = {}) => {
|
|
915
870
|
const editor = opts.editor ??
|
|
916
871
|
process.env['VISUAL'] ??
|
|
@@ -919,7 +874,7 @@ export function useTextBuffer({ initialText = '', initialCursorOffset = 0, viewp
|
|
|
919
874
|
const tmpDir = fs.mkdtempSync(pathMod.join(os.tmpdir(), 'gemini-edit-'));
|
|
920
875
|
const filePath = pathMod.join(tmpDir, 'buffer.txt');
|
|
921
876
|
fs.writeFileSync(filePath, text, 'utf8');
|
|
922
|
-
|
|
877
|
+
dispatch({ type: 'create_undo_snapshot' });
|
|
923
878
|
const wasRaw = stdin?.isRaw ?? false;
|
|
924
879
|
try {
|
|
925
880
|
setRawMode?.(false);
|
|
@@ -932,11 +887,10 @@ export function useTextBuffer({ initialText = '', initialCursorOffset = 0, viewp
|
|
|
932
887
|
throw new Error(`External editor exited with status ${status}`);
|
|
933
888
|
let newText = fs.readFileSync(filePath, 'utf8');
|
|
934
889
|
newText = newText.replace(/\r\n?/g, '\n');
|
|
935
|
-
|
|
890
|
+
dispatch({ type: 'set_text', payload: newText, pushToUndo: false });
|
|
936
891
|
}
|
|
937
892
|
catch (err) {
|
|
938
893
|
console.error('[useTextBuffer] external editor error', err);
|
|
939
|
-
// TODO(jacobr): potentially revert or handle error state.
|
|
940
894
|
}
|
|
941
895
|
finally {
|
|
942
896
|
if (wasRaw)
|
|
@@ -954,19 +908,9 @@ export function useTextBuffer({ initialText = '', initialCursorOffset = 0, viewp
|
|
|
954
908
|
/* ignore */
|
|
955
909
|
}
|
|
956
910
|
}
|
|
957
|
-
}, [text,
|
|
911
|
+
}, [text, stdin, setRawMode]);
|
|
958
912
|
const handleInput = useCallback((key) => {
|
|
959
913
|
const { sequence: input } = key;
|
|
960
|
-
dbg('handleInput', {
|
|
961
|
-
key,
|
|
962
|
-
cursor: [cursorRow, cursorCol],
|
|
963
|
-
visualCursor,
|
|
964
|
-
});
|
|
965
|
-
const beforeText = text;
|
|
966
|
-
const beforeLogicalCursor = [cursorRow, cursorCol];
|
|
967
|
-
const beforeVisualCursor = [...visualCursor];
|
|
968
|
-
if (key.name === 'escape')
|
|
969
|
-
return false;
|
|
970
914
|
if (key.name === 'return' ||
|
|
971
915
|
input === '\r' ||
|
|
972
916
|
input === '\n' ||
|
|
@@ -1017,47 +961,22 @@ export function useTextBuffer({ initialText = '', initialCursorOffset = 0, viewp
|
|
|
1017
961
|
else if (input && !key.ctrl && !key.meta) {
|
|
1018
962
|
insert(input);
|
|
1019
963
|
}
|
|
1020
|
-
|
|
1021
|
-
// After operations, visualCursor might not be immediately updated if the change
|
|
1022
|
-
// was to `lines`, `cursorRow`, or `cursorCol` which then triggers the useEffect.
|
|
1023
|
-
// So, for return value, we check logical cursor change.
|
|
1024
|
-
const cursorChanged = cursorRow !== beforeLogicalCursor[0] ||
|
|
1025
|
-
cursorCol !== beforeLogicalCursor[1] ||
|
|
1026
|
-
visualCursor[0] !== beforeVisualCursor[0] ||
|
|
1027
|
-
visualCursor[1] !== beforeVisualCursor[1];
|
|
1028
|
-
dbg('handleInput:after', {
|
|
1029
|
-
cursor: [cursorRow, cursorCol],
|
|
1030
|
-
visualCursor,
|
|
1031
|
-
text,
|
|
1032
|
-
});
|
|
1033
|
-
return textChanged || cursorChanged;
|
|
1034
|
-
}, [
|
|
1035
|
-
text,
|
|
1036
|
-
cursorRow,
|
|
1037
|
-
cursorCol,
|
|
1038
|
-
visualCursor,
|
|
1039
|
-
newline,
|
|
1040
|
-
move,
|
|
1041
|
-
deleteWordLeft,
|
|
1042
|
-
deleteWordRight,
|
|
1043
|
-
backspace,
|
|
1044
|
-
del,
|
|
1045
|
-
insert,
|
|
1046
|
-
]);
|
|
964
|
+
}, [newline, move, deleteWordLeft, deleteWordRight, backspace, del, insert]);
|
|
1047
965
|
const renderedVisualLines = useMemo(() => visualLines.slice(visualScrollRow, visualScrollRow + viewport.height), [visualLines, visualScrollRow, viewport.height]);
|
|
966
|
+
const replaceRange = useCallback((startRow, startCol, endRow, endCol, text) => {
|
|
967
|
+
dispatch({
|
|
968
|
+
type: 'replace_range',
|
|
969
|
+
payload: { startRow, startCol, endRow, endCol, text },
|
|
970
|
+
});
|
|
971
|
+
}, []);
|
|
1048
972
|
const replaceRangeByOffset = useCallback((startOffset, endOffset, replacementText) => {
|
|
1049
|
-
dbg('replaceRangeByOffset', { startOffset, endOffset, replacementText });
|
|
1050
973
|
const [startRow, startCol] = offsetToLogicalPos(text, startOffset);
|
|
1051
974
|
const [endRow, endCol] = offsetToLogicalPos(text, endOffset);
|
|
1052
|
-
|
|
975
|
+
replaceRange(startRow, startCol, endRow, endCol, replacementText);
|
|
1053
976
|
}, [text, replaceRange]);
|
|
1054
977
|
const moveToOffset = useCallback((offset) => {
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
setCursorCol(newCol);
|
|
1058
|
-
setPreferredCol(null);
|
|
1059
|
-
dbg('moveToOffset', { offset, newCursor: [newRow, newCol] });
|
|
1060
|
-
}, [text, setPreferredCol]);
|
|
978
|
+
dispatch({ type: 'move_to_offset', payload: { offset } });
|
|
979
|
+
}, []);
|
|
1061
980
|
const returnValue = {
|
|
1062
981
|
lines,
|
|
1063
982
|
text,
|
|
@@ -1078,44 +997,13 @@ export function useTextBuffer({ initialText = '', initialCursorOffset = 0, viewp
|
|
|
1078
997
|
redo,
|
|
1079
998
|
replaceRange,
|
|
1080
999
|
replaceRangeByOffset,
|
|
1081
|
-
moveToOffset,
|
|
1000
|
+
moveToOffset,
|
|
1082
1001
|
deleteWordLeft,
|
|
1083
1002
|
deleteWordRight,
|
|
1084
1003
|
killLineRight,
|
|
1085
1004
|
killLineLeft,
|
|
1086
1005
|
handleInput,
|
|
1087
1006
|
openInExternalEditor,
|
|
1088
|
-
applyOperations,
|
|
1089
|
-
copy: useCallback(() => {
|
|
1090
|
-
if (!selectionAnchor)
|
|
1091
|
-
return null;
|
|
1092
|
-
const [ar, ac] = selectionAnchor;
|
|
1093
|
-
const [br, bc] = [cursorRow, cursorCol];
|
|
1094
|
-
if (ar === br && ac === bc)
|
|
1095
|
-
return null;
|
|
1096
|
-
const topBefore = ar < br || (ar === br && ac < bc);
|
|
1097
|
-
const [sr, sc, er, ec] = topBefore ? [ar, ac, br, bc] : [br, bc, ar, ac];
|
|
1098
|
-
let selectedTextVal;
|
|
1099
|
-
if (sr === er) {
|
|
1100
|
-
selectedTextVal = cpSlice(currentLine(sr), sc, ec);
|
|
1101
|
-
}
|
|
1102
|
-
else {
|
|
1103
|
-
const parts = [cpSlice(currentLine(sr), sc)];
|
|
1104
|
-
for (let r = sr + 1; r < er; r++)
|
|
1105
|
-
parts.push(currentLine(r));
|
|
1106
|
-
parts.push(cpSlice(currentLine(er), 0, ec));
|
|
1107
|
-
selectedTextVal = parts.join('\n');
|
|
1108
|
-
}
|
|
1109
|
-
setClipboard(selectedTextVal);
|
|
1110
|
-
return selectedTextVal;
|
|
1111
|
-
}, [selectionAnchor, cursorRow, cursorCol, currentLine, setClipboard]),
|
|
1112
|
-
paste: useCallback(() => {
|
|
1113
|
-
if (clipboard === null)
|
|
1114
|
-
return false;
|
|
1115
|
-
applyOperations([{ type: 'insert', payload: clipboard }]);
|
|
1116
|
-
return true;
|
|
1117
|
-
}, [clipboard, applyOperations]),
|
|
1118
|
-
startSelection: useCallback(() => setSelectionAnchor([cursorRow, cursorCol]), [cursorRow, cursorCol, setSelectionAnchor]),
|
|
1119
1007
|
};
|
|
1120
1008
|
return returnValue;
|
|
1121
1009
|
}
|