Skip to content

Commit

Permalink
Merge pull request vrapper#72 from richq/master
Browse files Browse the repository at this point in the history
Fix visual cursor paren match proposal
  • Loading branch information
keforbes committed Jun 9, 2012
2 parents 62d4ab5 + 82d1f55 commit d7897d3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
package net.sourceforge.vrapper.vim.commands;

import net.sourceforge.vrapper.platform.CursorService;
import net.sourceforge.vrapper.utils.Position;
import net.sourceforge.vrapper.vim.EditorAdaptor;
import net.sourceforge.vrapper.vim.modes.NormalMode;

Expand All @@ -18,7 +20,14 @@ public void execute(EditorAdaptor editorAdaptor) throws CommandExecutionExceptio
}

public static void doIt(EditorAdaptor editorAdaptor) throws CommandExecutionException {
Selection sel = editorAdaptor.getSelection();
editorAdaptor.setSelection(null);
editorAdaptor.changeMode(NormalMode.NAME);
// Fix off-by-one position if selection was left to right
if (sel.getStart().getModelOffset() < sel.getEnd().getModelOffset()) {
CursorService service = editorAdaptor.getCursorService();
Position position = service.getPosition().addModelOffset(-1);
service.setPosition(position, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@
import net.sourceforge.vrapper.utils.Position;
import net.sourceforge.vrapper.vim.EditorAdaptor;
import net.sourceforge.vrapper.vim.commands.CommandExecutionException;
import net.sourceforge.vrapper.vim.commands.Selection;
import net.sourceforge.vrapper.vim.modes.VisualMode;

public abstract class AbstractModelSideMotion extends CountAwareMotion {

protected abstract int destination(int offset, TextContent content, int count) throws CommandExecutionException;
protected boolean isLeftRight() {return false;}
protected void setCurrentState(String mode, Selection sel) {}

@Override
public Position destination(EditorAdaptor editorAdaptor, int count) throws CommandExecutionException {
if (count == NO_COUNT_GIVEN) {
count = 1;
}
int modelOffset = editorAdaptor.getCursorService().getPosition().getModelOffset();
if (VisualMode.NAME.equals(editorAdaptor.getCurrentModeName())) {
Selection selection = editorAdaptor.getSelection();
if (selection != null && !isLeftRight() && selection.getStart().getModelOffset() < selection.getEnd().getModelOffset()) {
modelOffset--;
}
}
setCurrentState(editorAdaptor.getCurrentModeName(), editorAdaptor.getSelection());
TextContent modelContent = editorAdaptor.getModelContent();
int destination = destination(modelOffset, modelContent, count);
return editorAdaptor.getCursorService().newPositionForModelOffset(destination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public boolean updateStickyColumn() {
return true;
}

@Override
protected boolean isLeftRight() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.sourceforge.vrapper.utils.LineInformation;
import net.sourceforge.vrapper.vim.commands.BorderPolicy;
import net.sourceforge.vrapper.vim.commands.CommandExecutionException;
import net.sourceforge.vrapper.vim.commands.Selection;
import net.sourceforge.vrapper.vim.modes.VisualMode;

// TODO: can we use underlying eclipse to do extra-smart stuff
// like matching XML tags, LaTeX \begin{paragraph}\end{paragraph},
Expand All @@ -32,6 +34,14 @@ public class ParenthesesMove extends AbstractModelSideMotion {
}

public static final ParenthesesMove INSTANCE = new ParenthesesMove();
private String mode;
private Selection sel;

@Override
protected void setCurrentState(String mode, Selection sel) {
this.mode = mode;
this.sel = sel;
}

//default match algorithm, find match for character under cursor
@Override
Expand All @@ -49,8 +59,13 @@ protected int destination(int offset, TextContent content, int count) throws Com
if (pair == null) {
throw new CommandExecutionException("no parentheses to jump found");
}

return findMatch(index, pair, content, count);

int match = findMatch(index, pair, content, count);
// adjust for caret offset if the match is before the selection start
if (VisualMode.NAME.equals(mode) && match < sel.getStart().getModelOffset()) {
match--;
}
return match;
}

public static final ParenthesesMove MATCH_OPEN_PAREN = new ParenthesesMove() {
Expand Down

0 comments on commit d7897d3

Please sign in to comment.