Skip to content
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

Implement normal command #8896

Open
wants to merge 38 commits into
base: master
Choose a base branch
from

Conversation

s-kai273
Copy link
Contributor

@s-kai273 s-kai273 commented Feb 21, 2024

What this PR does / why we need it:

  • implement normal command(both for single line and multi lines)
  • test codes of normal command

Which issue(s) this PR fixes

Special notes for your reviewer

  • hope to check implementation design is proper
    it is designed referring to macro implementation.

for (const line of resultLines) {
vimState.cursorStopPosition = vimState.cursorStartPosition =
TextEditor.getFirstNonWhitespaceCharOnLine(vimState.document, line.lineNumber);
await modeHandler.handleMultipleKeyEvents(keystroke.value);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to call rerunRecordedState from the second line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean that dot command is working properly or not?
actually :normal . is doing well..


override async execute(vimState: VimState): Promise<void> {
const keystroke = this.keystroke;
vimState.recordedState.transformer.addTransformation({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set start/end line number to vimState.cursorStopPosition, the logic in executeNormal will be clearer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that not setting start/end is better, because normal command behavior with visual and normal mode are different.
for example,

  • text: aaaaa
  • start position: end of line
  • command: :normal iX

executing above with visual mode, the result is Xaaaaa.
but with normal mode, it is aaaaaX.

to achive the difference, executeTransformations checks setting start/end or not.
withRange stores it as boolean.

if (withRange) {
vimState.cursorStopPosition = vimState.cursorStartPosition =
    TextEditor.getFirstNonWhitespaceCharOnLine(vimState.document, line.lineNumber);
}
await modeHandler.handleMultipleKeyEvents(keystroke.value);
await vimState.setCurrentMode(Mode.Normal);

@feeiyu
if you have better idea, please tell me..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@feeiyu
now I added withRange parameter to ExecuteNormalCommandTransformation.
implementing it realizes fulfilling start/end line and determining executing normal command with range or not.
I think this is better idea.
1261062

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we replace startLineNumber/endLineNumber/withRange with this?

range?: LineRange,

test/normalCommand.test.ts Outdated Show resolved Hide resolved
@s-kai273
Copy link
Contributor Author

I implemented test codes.
If it is necessary to add more test cases, please tell me it.

Copy link
Member

@J-Fields J-Fields left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some feedback but this is looking good overall. Thanks!

@s-kai273
Copy link
Contributor Author

@J-Fields
Thank you for your reviewing.
I finished to modify and comment the code.
Please check it.

@s-kai273 s-kai273 requested review from J-Fields and feeiyu April 4, 2024 15:10
@s-kai273
Copy link
Contributor Author

@J-Fields @feeiyu

Hi,
I have completed the revisions to my code as per your comments. If there are any additional adjustments needed, please let me know.

Thank you for your feedback.

@s-kai273
Copy link
Contributor Author

After merging #8979, some conflicts occurred.
I have fixed them in commit dbd0bd2.

Comment on lines +8 to +9
public static readonly argParser: Parser<NormalCommand> = seq(whitespace, all).map(
([_, keystroke]) => new NormalCommand(keystroke),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a bit nicer as:

whitespace.then(all).map(...);

const resultLineNumbers: number[] = [];
if (withRange) {
for (let i = startLineNumber; i <= endLineNumber; i++) {
resultLineNumbers.push(vimState.document.lineAt(i).lineNumber);
Copy link
Member

@J-Fields J-Fields May 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resultLineNumbers.push(vimState.document.lineAt(i).lineNumber);
resultLineNumbers.push(i);

const { start, end } = selection;

for (let i = start.line; i <= end.line; i++) {
resultLineNumbers.push(vimState.document.lineAt(i).lineNumber);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resultLineNumbers.push(vimState.document.lineAt(i).lineNumber);
resultLineNumbers.push(i);

}
await modeHandler.handleMultipleKeyEvents(stroke.value);
if (vimState.currentMode === Mode.Insert) {
await modeHandler.handleMultipleKeyEvents(['<Esc>']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await modeHandler.handleMultipleKeyEvents(['<Esc>']);
await modeHandler.handleKeyEvent('<Esc>');

@@ -186,6 +186,14 @@ export interface ContentChangeTransformation {
diff: PositionDiff;
}

export interface ExecuteNormalTransformation {
type: 'executeNormal';
keystroke: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be called keystrokes? (as well as NormalCommand.keystrokes?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants