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

Add CamelCaseMotion plugin #3483

Merged
merged 8 commits into from
Feb 18, 2019
Merged

Add CamelCaseMotion plugin #3483

merged 8 commits into from
Feb 18, 2019

Conversation

jkillian
Copy link
Contributor

What this PR does / why we need it:

Adds a CamelCamelCase motion plugin inspired by https://github.com/bkad/CamelCaseMotion. Adds support for the following motions when enabled:

  • Move to next camelCase word segment: <leader>w
  • Move to end of camelCase word segment: <leader>e
  • Move back to beginning of camelCase word segment: <leader>b
  • Change/delete/select whole camelCase word segment: <operator>i<leader>w commands.

In addition to camelCase, the plugin should also work correctly for snake_case.

Which issue(s) this PR fixes:

Fixes #1220
Fixes #2808 (likely)

Special notes for your reviewer:

  • There's a gnarly regex in there that's admittedly hard to review, but it's well-documented and can be played around with in Regex101 if using the latest Chrome. I chose to use a regex instead of trying to use VSCode's built in subword navigation or some other mechanism because it made the rest of the implementation quite clean and similar to existing motions.

  • The aforementioned regex uses some lookbehinds, and thus requires Node 9.0+. This shouldn't be a problem since the latest VSCode bundles Node 10.2. I did bump the docker image to use Node 10 as well for consistency, though I'm not sure this was strictly necessary.

Let me know if any changes to this PR are needed! Hope you don't mind getting a PR for this feature out of the blue 😄

Copy link
Member

@jpoon jpoon left a comment

Choose a reason for hiding this comment

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

Dope!

build/Dockerfile Show resolved Hide resolved
@@ -101,6 +101,7 @@ export class Position extends vscode.Position {

private _nonWordCharRegex: RegExp;
private _nonBigWordCharRegex: RegExp;
private _nonCamelCaseWordCharRegex: RegExp;
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 wondering if we should move away from having position.ts the dumping ground for all positioning related logic as it's a bit of a mess.

As the changes you introduced to this file are specific to camel case, I wonder if we should create a new file under /plugins camelcase.position.ts?

What do you think?

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'm not opposed to the idea in principle, but if I were to move I'd either have to duplicate or make public all the get*Word*WithRegex functions and maybe the getAllPositions family of functions since they're shared among all the word-based motions. Your call, I'm happy either way!


// prettier-ignore
const firstSegment =
'(' + // OPEN: group for matching camel case words
Copy link
Member

Choose a reason for hiding this comment

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

Ermagod. Thank you for writing comments.

test/plugins/camelCaseMotion.test.ts Outdated Show resolved Hide resolved
@jkillian
Copy link
Contributor Author

Thanks for the quick review @jpoon! I'll make these changes a bit later today

@jkillian
Copy link
Contributor Author

Made CR changes! Though I didn't adjust Position yet until we decide if that's something we definitely want to do

Copy link
Member

@jpoon jpoon left a comment

Choose a reason for hiding this comment

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

We can skip the position refactoring. Left one tiny comment if you could address. LGTM otherwise.

package.json Outdated
@@ -449,6 +449,11 @@
"description": "Override the 'ignorecase' option if the search pattern contains upper case characters.",
"default": true
},
"vim.camelCaseMotion": {
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 trying to make this more hierarchic. Can you make this vim.camelCaseMotion.enable instead? Such that if we do end up creating more configs for camel case, they can live under vim.camelCaseMotion

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 idea! I've switched the setting over as you suggested and pushed the new changes.

@TravisBuddy
Copy link

Hey @jkillian,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

View build log

TravisBuddy Request Identifier: 4d2d4a10-32f9-11e9-909a-37b9af0e35d8

@jpoon jpoon merged commit d8564f9 into VSCodeVim:master Feb 18, 2019
@jkillian jkillian deleted the camelcase branch February 18, 2019 14:50
@ELLIOTTCABLE
Copy link

ELLIOTTCABLE commented Mar 7, 2019

Is there any way to configure the keybinds of this PR?

(Also, freakin' AWESOME.)

@jkillian
Copy link
Contributor Author

jkillian commented Mar 7, 2019

Glad you like it @ELLIOTTCABLE 😄

You should be able to change the keybindings following the docs here.

For example, to make \ x move by a camel case word, I could add something like this to my VsCode settings.json:

    "vim.normalModeKeyBindingsNonRecursive": [
        {
            "before": ["<leader>", "x"],
            "after": ["<leader>", "w"]
        }
    ]

You can also modify the vim.leader setting if you simply want to change what key <leader> maps to.

@ELLIOTTCABLE
Copy link

Ah! Any issues you forsee with using this to remap the default motion to being by-camelcase?

    "vim.normalModeKeyBindingsNonRecursive": [
        {
            "before": ["w"],
            "after": ["<leader>", "w"]
        },
        {
            "before": ["<leader>", "w"],
            "after": ["w"]
        },
    ]

@jkillian
Copy link
Contributor Author

jkillian commented Mar 8, 2019

Not entirely sure @ELLIOTTCABLE, I'd say give it a go and see how it works for you 😄 One thing that might be tricky is remapping ciw so it works as if you said ci<leader>w. Had trouble getting that to work when I tried it out

@ELLIOTTCABLE
Copy link

Ugh, that's a good point. And unfortunate. Might need integrated support to pull off operator-motions. v.v

@gapop
Copy link

gapop commented Jul 16, 2020

@ELLIOTTCABLE did you eventually find a way to bind camel case motion to the default keys and have composed commands working?

@berknam
Copy link
Contributor

berknam commented Jul 16, 2020

@ELLIOTTCABLE did you eventually find a way to bind camel case motion to the default keys and have composed commands working?

Once PR #4735 gets merged it will allow you to do operator-pending mode remaps so you will be able to do this:

"vim.operatorPendingModeKeyBindingsNonRecursive": [
  {
    "before": ["w"],
    "after": ["<leader>", "w"]
  },
  {
    "before": ["<leader>", "w"],
    "after": ["w"]
  },
]

It needs to be non recursive otherwise it would loop between the two until 'maxmapdepth'. Also bear in mind that this will work well with PR #4735 but after PR #4995 gets merged this won't work because the <leader>w won't be an action itself anymore but instead a remap to <Plug>CamelCaseMotion_w. So after that PR you will have to do this:

"vim.operatorPendingModeKeyBindingsNonRecursive": [
  {
    "before": ["w"],
    "after": ["<Plug>CamelCaseMotion_w"]
  },
  {
    "before": ["<leader>", "w"],
    "after": ["w"]
  },
]

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.

FEATURE REQUEST: Add operator support for VSCodes new subword navigation Support camelCaseMotion Script
6 participants