Skip to content
This repository was archived by the owner on Apr 6, 2018. It is now read-only.

Bracket matching motion with % - #254

Merged
bhuga merged 7 commits into
atom:masterfrom
carlosdcastillo:bracket-matching-motion-pr
May 28, 2014
Merged

bhuga merged 7 commits into
atom:masterfrom
carlosdcastillo:bracket-matching-motion-pr

Conversation

@carlosdcastillo

Copy link
Copy Markdown
Contributor

% in command mode will move to the matching bracket, right now as proof of concept ( [ { with ) ] }.

works correctly with d% or y%, as expected.

Now just press % to go to the matching bracket.

Conflicts:
	lib/motions/search-motion.coffee
instead of just the start locations, to be able to compare the ranges for
bracker matching
@bhuga

bhuga commented May 18, 2014

Copy link
Copy Markdown
Contributor

Can you rebase this or merge master into your branch?

@carlosdcastillo

Copy link
Copy Markdown
Contributor Author

@bhuga I've merged master into my branch. Also added specs for the motion.

Comment thread lib/motions/search-motion.coffee Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This block is pretty wordy. What about something like this pseudocode:

switch @character
  when ']' then '['
  when '[' then ']'
  ...
  else ''

@reverse = true if _.contains(['(', '[', '{')], @character)

@bhuga

bhuga commented May 25, 2014

Copy link
Copy Markdown
Contributor

I've really been wanting this! Just one nitpick in the code.

I am seeing an off-by-one error, though. n the text string abc123 [text] abc123, putting the cursor on the x in text, % goes to the [, correctly, then to the space after the ], then back to [ and then reliably repeats. If I start on the ], % takes me to the first t in text, then back to]`, all correctly.

The tests pass and look fine to me, so I'm not sure why it's different.

@carlosdcastillo

Copy link
Copy Markdown
Contributor Author

Yes. I just realized that in vim if you have ( abc ) and stand on the b and hit % it goes back to the (. If you do it this pr it won't do anything. I'm just learning that in vim if you hit % and it looks forward for a symbol is on the current line and is the next matching symbol. I have never used this behavior.

My use case is stand on the opening and hit % or d% to cut or something.

I'll try to add this to the PR.

Now you can press % inside (abc) and it will do the same as in vim.
Now there are tests of  when it finds the nearest symbol to match. For examples
(abcd) and you hit % while on b.
@carlosdcastillo

Copy link
Copy Markdown
Contributor Author

@bhuga I've commited some changes. Should work now. Hitting % with ( abc ) with the cursor on the b will take you to the (. More complex things like vim handles them are also supported: hitting % with abc ( def ), with cursor on the b will take you to the )

@bhuga bhuga mentioned this pull request May 27, 2014
Comment thread lib/motions/search-motion.coffee Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

easy one liner: cur = if @startUp then @startUpPos else @editor.getCursorBufferPosition()

@bhuga

bhuga commented May 28, 2014

Copy link
Copy Markdown
Contributor

Unfortunately I'm still getting some errors 😦:

For x(abc)y:

  • % on x brings me to the first bracket character after looping past the end of the file.
  • % on ( brings me to y instead of )
  • % on a brings me to ), correctly, but
  • % on b or c brings me to (
  • % on y brings me to (

I think there is some state getting leaked between multiple presses. While trying to make this list, I think some cases did not repeat every time.

@carlosdcastillo

Copy link
Copy Markdown
Contributor Author

@bhuga I can't seem to reproduce any of those. In the example x(abc)y at all locations it does the same as vim:
% at x goes to )
% at ( goest to )
% at a goes to (
% at b goes to (
% at c goes to (
% at ) goes to (
except:
% at y which should be a a no-op is going over to the next character.

I want to fix the last one, but I want to make sure we're seeing the same behavior first.

@bhuga

bhuga commented May 28, 2014

Copy link
Copy Markdown
Contributor

How embarrassing, I had a user keymap for % for the bracket matcher module.

This code is kind of weird, but then again, so is vim's behavior. Great work.

bhuga pushed a commit that referenced this pull request May 28, 2014
@bhuga
bhuga merged commit 4491e68 into atom:master May 28, 2014
MarkusSN pushed a commit to MarkusSN/vim-mode that referenced this pull request May 28, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants