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 increment and decrement operators #515

Merged
merged 13 commits into from
Jul 26, 2016

Conversation

ascandella
Copy link
Contributor

@ascandella ascandella commented Jul 25, 2016

This adds support for single or numeric-prefix control+x and control+a.

Numeric modes supported:

  • Decimal
  • Hex
  • Octal

Fixes #513

}

// Advance to the end of the word in case it's longer
return Position.FromVSCodePosition(endPos.translate(0, newNum.length - oldWidth));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure about this, feels weird to create an intermediate object just to move a character. Thoughts?

@ascandella ascandella force-pushed the control-a branch 2 times, most recently from 46adb9d to faa43d3 Compare July 26, 2016 01:52
This adds support for single or numeric-prefix control+x and control+a.
const text = TextEditor.getLineAt(position).text;

// They may start on a number, so check the current word boundaries first.
let wordEnd = position.getCurrentWordEnd(true);
Copy link
Member

Choose a reason for hiding this comment

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

I'm worried about relying on word boundaries as users are allowed to customize word separators, see #487 . Will this be a problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting, yes that could be a problem. Is there going to be a different API to get "builtin" word boundary matching? Should I just use a custom regex for this movement?

Copy link
Member

@rebornix rebornix Jul 26, 2016

Choose a reason for hiding this comment

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

We can't use getWord as it always reads from user settings or default, I suppose want to want to get is something starts with numbers or + and -, a custom regex may do the trick.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like all of the current Position methods that take regexp's are private -- do you think I should change the visibility, or subclass Position to something like NonCustomizablePosition that does not respect user settings for word boundaries?

Copy link
Member

Choose a reason for hiding this comment

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

If a user is insane enough to define numbers as word separators, these motions make no semantic sense (is 1234 4 separate words or one? If we select the whole thing with visual mode and do ctrl a, what should happen?)

I'm completely fine with this being undefined behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good to me.

@ascandella
Copy link
Contributor Author

Updated with a NumericString class to handle hex -- I have no idea if that's the right place to put it or if I should just make some private inner class (do those even exist in TS?)

@ascandella
Copy link
Contributor Author

The only thing left that this PR does not implement (trying to understand how useful it is):

given

0x0

hitting ctrl+x

gives you

0xffffffffffffffff

@johnfn
Copy link
Member

johnfn commented Jul 26, 2016

@sectioneight that seems of very little use. It doesn't make a lot of sense to guess how many bits are in the user's variable. 😛

@@ -179,7 +179,7 @@ export async function activate(context: vscode.ExtensionContext) {
showCmdLine("", modeHandlerToEditorIdentity[new EditorIdentity(vscode.window.activeTextEditor).toString()]);
});

'rfbducw['.split('').forEach(key => {
'rfbducw[ax'.split('').forEach(key => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there any specific ordering to the strings here? should we alphabetize them? reverse-alphabetize? put our favorite ones first?

Copy link
Member

Choose a reason for hiding this comment

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

hah! there's no order, but yeah, this thing is pretty dumb. honestly i think we should parse them directly out of package.json and DRY this up, but that's a job for another day.

@@ -0,0 +1,36 @@
export class NumericString {
Copy link
Member

@johnfn johnfn Jul 26, 2016

Choose a reason for hiding this comment

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

I think you could actually write this whole thing very concisely (less than 10 lines) with 3 separate regexes. Just do grouped matches to grab the values and then send them through parseInt. Something like this:


8: /^0([0-7]+),

10: /^(\d+)/,

16: /0x([\da-fA-F]+)/


}```

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, updated with what I think you meant

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, LGTM, though maybe I slightly exaggerated how short it was going to be.

@@ -503,8 +503,6 @@ export class ModeHandler implements vscode.Disposable {
}

async handleKeyEvent(key: string): Promise<Boolean> {
if (key === "<c-r>") { key = "ctrl+r"; } // TODO - temporary hack for tests only!
Copy link
Member

Choose a reason for hiding this comment

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

Did someone fix this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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


for (let { start, end, word } of Position.IterateWords(position.getWordLeft(true))) {
// '-' doesn't count as a word, but is important to include in parsing the number
if (text[start.character - 1] === '-') {
Copy link
Member

Choose a reason for hiding this comment

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

Use word here rather than text, start and end.

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 can't, because I'm testing outside of the returned word (since - isn't included in the word boundaries)

Copy link
Member

Choose a reason for hiding this comment

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

Oh geez, that actually seems like it could have some bad interactions with people who DONT use - as a word delimiter. It's also really annoying. Oh well, it's fine for now.

@johnfn
Copy link
Member

johnfn commented Jul 26, 2016

Ok, sweet. Just remove the hack fix and we should be good to go.

@ascandella
Copy link
Contributor Author

Remove which hack fix? I think I already undid the changes to tokenization.

@johnfn
Copy link
Member

johnfn commented Jul 26, 2016

Yup, you're right, I must have been looking at an old version of the code.

Sweet. LGTM!

@johnfn johnfn merged commit 00f094a into VSCodeVim:master Jul 26, 2016
@ascandella ascandella deleted the control-a branch July 26, 2016 06:30
@johnfn
Copy link
Member

johnfn commented Jul 26, 2016

(Thanks for putting up with all my crazy requests! 😁 )

@ascandella
Copy link
Contributor Author

haha all good, I'm the same way on code reviews at work
On Mon, Jul 25, 2016 at 11:31 PM Grant Mathews [email protected]
wrote:

(Thanks for putting up with all my crazy requests! 😁 )


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#515 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAF7PwOcRzfyUespOaVu9jsA-RFYvqLbks5qZanNgaJpZM4JUoIG
.

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.

Support ctrl+a and ctrl+x to increment/decrement numbers
3 participants