Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/actions/commands/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -153,19 +157,21 @@ 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);
}
}

@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);
Expand Down
50 changes: 33 additions & 17 deletions test/mode/modeNormal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/testSimplifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading