Fix bug pasting multiline strings (#632)
This commit is contained in:
parent
8365c8f954
commit
51949f3121
|
@ -514,6 +514,34 @@ describe('useTextBuffer', () => {
|
||||||
act(() => result.current.handleInput('\r', {})); // Simulates Shift+Enter in VSCode terminal
|
act(() => result.current.handleInput('\r', {})); // Simulates Shift+Enter in VSCode terminal
|
||||||
expect(getBufferState(result).lines).toEqual(['', '']);
|
expect(getBufferState(result).lines).toEqual(['', '']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should correctly handle repeated pasting of long text', () => {
|
||||||
|
const { result } = renderHook(() => useTextBuffer({ viewport }));
|
||||||
|
const longText = `not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
|
||||||
|
|
||||||
|
Why do we use it?
|
||||||
|
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
|
||||||
|
|
||||||
|
Where does it come from?
|
||||||
|
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lore
|
||||||
|
`;
|
||||||
|
|
||||||
|
// Simulate pasting the long text multiple times
|
||||||
|
act(() => result.current.insertStr(longText));
|
||||||
|
act(() => result.current.insertStr(longText));
|
||||||
|
act(() => result.current.insertStr(longText));
|
||||||
|
|
||||||
|
const state = getBufferState(result);
|
||||||
|
// Check that the text is the result of three concatenations.
|
||||||
|
expect(state.lines).toStrictEqual(
|
||||||
|
(longText + longText + longText).split('\n'),
|
||||||
|
);
|
||||||
|
const expectedCursorPos = offsetToLogicalPos(
|
||||||
|
state.text,
|
||||||
|
state.text.length,
|
||||||
|
);
|
||||||
|
expect(state.cursor).toEqual(expectedCursorPos);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// More tests would be needed for:
|
// More tests would be needed for:
|
||||||
|
|
|
@ -524,8 +524,7 @@ export function useTextBuffer({
|
||||||
const normalised = str.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
const normalised = str.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
||||||
const parts = normalised.split('\n');
|
const parts = normalised.split('\n');
|
||||||
|
|
||||||
setLines((prevLines) => {
|
const newLines = [...lines];
|
||||||
const newLines = [...prevLines];
|
|
||||||
const lineContent = currentLine(cursorRow);
|
const lineContent = currentLine(cursorRow);
|
||||||
const before = cpSlice(lineContent, 0, cursorCol);
|
const before = cpSlice(lineContent, 0, cursorCol);
|
||||||
const after = cpSlice(lineContent, cursorCol);
|
const after = cpSlice(lineContent, cursorCol);
|
||||||
|
@ -542,17 +541,16 @@ export function useTextBuffer({
|
||||||
0,
|
0,
|
||||||
lastPartOriginal + after,
|
lastPartOriginal + after,
|
||||||
);
|
);
|
||||||
setCursorRow((prev) => prev + parts.length - 1);
|
setCursorRow(cursorRow + parts.length - 1);
|
||||||
setCursorCol(cpLen(lastPartOriginal));
|
setCursorCol(cpLen(lastPartOriginal));
|
||||||
} else {
|
} else {
|
||||||
setCursorCol(cpLen(before) + cpLen(parts[0]));
|
setCursorCol(cpLen(before) + cpLen(parts[0]));
|
||||||
}
|
}
|
||||||
return newLines;
|
setLines(newLines);
|
||||||
});
|
|
||||||
setPreferredCol(null);
|
setPreferredCol(null);
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
[pushUndo, cursorRow, cursorCol, currentLine, setPreferredCol],
|
[pushUndo, cursorRow, cursorCol, lines, currentLine, setPreferredCol],
|
||||||
);
|
);
|
||||||
|
|
||||||
const insert = useCallback(
|
const insert = useCallback(
|
||||||
|
@ -1275,6 +1273,7 @@ export function useTextBuffer({
|
||||||
|
|
||||||
setText,
|
setText,
|
||||||
insert,
|
insert,
|
||||||
|
insertStr,
|
||||||
newline,
|
newline,
|
||||||
backspace,
|
backspace,
|
||||||
del,
|
del,
|
||||||
|
@ -1354,6 +1353,7 @@ export interface TextBuffer {
|
||||||
* Insert a single character or string without newlines.
|
* Insert a single character or string without newlines.
|
||||||
*/
|
*/
|
||||||
insert: (ch: string) => void;
|
insert: (ch: string) => void;
|
||||||
|
insertStr: (str: string) => boolean;
|
||||||
newline: () => void;
|
newline: () => void;
|
||||||
backspace: () => void;
|
backspace: () => void;
|
||||||
del: () => void;
|
del: () => void;
|
||||||
|
|
Loading…
Reference in New Issue