Fix bug where single line inserts were deleting all text after the in… (#1114)
This commit is contained in:
parent
11f524c125
commit
a7e45d47cd
|
@ -173,6 +173,21 @@ describe('useTextBuffer', () => {
|
||||||
expect(state.visualCursor).toEqual([0, 2]);
|
expect(state.visualCursor).toEqual([0, 2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('insert: should insert text in the middle of a line', () => {
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
useTextBuffer({
|
||||||
|
initialText: 'abc',
|
||||||
|
viewport,
|
||||||
|
isValidPath: () => false,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
act(() => result.current.move('right'));
|
||||||
|
act(() => result.current.insert('-NEW-'));
|
||||||
|
const state = getBufferState(result);
|
||||||
|
expect(state.text).toBe('a-NEW-bc');
|
||||||
|
expect(state.cursor).toEqual([0, 6]);
|
||||||
|
});
|
||||||
|
|
||||||
it('newline: should create a new line and move cursor', () => {
|
it('newline: should create a new line and move cursor', () => {
|
||||||
const { result } = renderHook(() =>
|
const { result } = renderHook(() =>
|
||||||
useTextBuffer({
|
useTextBuffer({
|
||||||
|
|
|
@ -635,9 +635,9 @@ export function useTextBuffer({
|
||||||
const lineContent = currentLine(newCursorRow);
|
const lineContent = currentLine(newCursorRow);
|
||||||
const before = cpSlice(lineContent, 0, newCursorCol);
|
const before = cpSlice(lineContent, 0, newCursorCol);
|
||||||
const after = cpSlice(lineContent, newCursorCol);
|
const after = cpSlice(lineContent, newCursorCol);
|
||||||
newLines[newCursorRow] = before + parts[0];
|
|
||||||
|
|
||||||
if (parts.length > 1) {
|
if (parts.length > 1) {
|
||||||
|
newLines[newCursorRow] = before + parts[0];
|
||||||
const remainingParts = parts.slice(1);
|
const remainingParts = parts.slice(1);
|
||||||
const lastPartOriginal = remainingParts.pop() ?? '';
|
const lastPartOriginal = remainingParts.pop() ?? '';
|
||||||
newLines.splice(newCursorRow + 1, 0, ...remainingParts);
|
newLines.splice(newCursorRow + 1, 0, ...remainingParts);
|
||||||
|
@ -649,6 +649,8 @@ export function useTextBuffer({
|
||||||
newCursorRow = newCursorRow + parts.length - 1;
|
newCursorRow = newCursorRow + parts.length - 1;
|
||||||
newCursorCol = cpLen(lastPartOriginal);
|
newCursorCol = cpLen(lastPartOriginal);
|
||||||
} else {
|
} else {
|
||||||
|
newLines[newCursorRow] = before + parts[0] + after;
|
||||||
|
|
||||||
newCursorCol = cpLen(before) + cpLen(parts[0]);
|
newCursorCol = cpLen(before) + cpLen(parts[0]);
|
||||||
}
|
}
|
||||||
} else if (op.type === 'backspace') {
|
} else if (op.type === 'backspace') {
|
||||||
|
|
Loading…
Reference in New Issue