Set scroll option with <C-u> and <C-d> (#8985)

Fixes #8984
This commit is contained in:
Anton Jeppsson 2024-05-02 03:10:33 +02:00 committed by GitHub
parent 1bd4642423
commit 13830e49b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 21 deletions

View File

@ -74,6 +74,8 @@ abstract class CommandScrollAndMoveCursor extends BaseCommand {
modes = [Mode.Normal, Mode.Visual, Mode.VisualLine, Mode.VisualBlock];
override runsOnceForEachCountPrefix = false;
abstract to: EditorScrollDirection;
/** if true, set scroll option instead of repeating command */
setScroll = false;
/**
* @returns the number of lines this command should move the cursor
@ -90,7 +92,9 @@ abstract class CommandScrollAndMoveCursor extends BaseCommand {
.getConfiguration('editor')
.get<boolean>('smoothScrolling', false);
const timesToRepeat = vimState.recordedState.count || 1;
if (this.setScroll && vimState.recordedState.count)
configuration.scroll = vimState.recordedState.count;
const timesToRepeat = (!this.setScroll && vimState.recordedState.count) || 1;
const moveLines = timesToRepeat * this.getNumLines(visibleRanges);
let scrollLines = moveLines;
@ -153,9 +157,10 @@ class CommandMoveFullPageDown extends CommandScrollAndMoveCursor {
}
@RegisterAction
class CommandMoveHalfPageDown extends CommandScrollAndMoveCursor {
class CommandCtrlD extends CommandScrollAndMoveCursor {
keys = ['<C-d>'];
to: EditorScrollDirection = 'down';
override setScroll = true;
protected getNumLines(visibleRanges: vscode.Range[]) {
return configuration.getScrollLines(visibleRanges);
@ -163,9 +168,10 @@ class CommandMoveHalfPageDown extends CommandScrollAndMoveCursor {
}
@RegisterAction
class CommandMoveHalfPageUp extends CommandScrollAndMoveCursor {
class CommandCtrlU extends CommandScrollAndMoveCursor {
keys = ['<C-u>'];
to: EditorScrollDirection = 'up';
override setScroll = true;
protected getNumLines(visibleRanges: vscode.Range[]) {
return configuration.getScrollLines(visibleRanges);

View File

@ -2737,25 +2737,41 @@ suite('Mode Normal', () => {
endMode: Mode.Normal,
});
newTest({
title: 'can handle <C-u> when first line is visible and starting column is at the beginning',
start: ['\t hello world', 'hello', 'hi hello', '|foo'],
keysPressed: '<C-u>',
end: ['\t |hello world', 'hello', 'hi hello', 'foo'],
});
suite('<C-u> / <C-d>', () => {
newTest({
title: 'can handle <C-u> when first line is visible and starting column is at the beginning',
start: ['\t hello world', 'hello', 'hi hello', '|foo'],
keysPressed: '<C-u>',
end: ['\t |hello world', 'hello', 'hi hello', 'foo'],
});
newTest({
title: 'can handle <C-u> when first line is visible and starting column is at the end',
start: ['\t hello world', 'hello', 'hi hello', 'very long line at the bottom|'],
keysPressed: '<C-u>',
end: ['\t |hello world', 'hello', 'hi hello', 'very long line at the bottom'],
});
newTest({
title: 'can handle <C-u> when first line is visible and starting column is at the end',
start: ['\t hello world', 'hello', 'hi hello', 'very long line at the bottom|'],
keysPressed: '<C-u>',
end: ['\t |hello world', 'hello', 'hi hello', 'very long line at the bottom'],
});
newTest({
title: 'can handle <C-u> when first line is visible and starting column is in the middle',
start: ['\t hello world', 'hello', 'hi hello', 'very long line |at the bottom'],
keysPressed: '<C-u>',
end: ['\t |hello world', 'hello', 'hi hello', 'very long line at the bottom'],
newTest({
title: 'can handle <C-u> when first line is visible and starting column is in the middle',
start: ['\t hello world', 'hello', 'hi hello', 'very long line |at the bottom'],
keysPressed: '<C-u>',
end: ['\t |hello world', 'hello', 'hi hello', 'very long line at the bottom'],
});
newTest({
title: '[count]<C-u> sets and adheres to scroll option',
start: ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'st|u'],
keysPressed: '2<C-u><C-u>',
end: ['abc', 'def', '|ghi', 'jkl', 'mno', 'pqr', 'stu'],
});
newTest({
title: '[count]<C-d> sets and adheres to scroll option',
start: ['ab|c', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu'],
keysPressed: '2<C-d><C-d>',
end: ['abc', 'def', 'ghi', 'jkl', '|mno', 'pqr', 'stu'],
});
});
suite('<C-g>', () => {

View File

@ -149,7 +149,7 @@ class DocState {
}
/**
* Tokenize a string like "abc<Esc>d<C-c>" into ["a", "b", "c", "<Esc>", "d", "<C-c>"]
* Tokenize a string like `"abc<Esc>d<C-c>"` into `["a", "b", "c", "<Esc>", "d", "<C-c>"]`
*/
function tokenizeKeySequence(sequence: string): string[] {
let isBracketedKey = false;