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

Support exact and inexact current word search #1277

Merged
merged 3 commits into from
Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 52 additions & 28 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1570,58 +1570,82 @@ class CommandCmdA extends BaseCommand {
}
}

@RegisterAction
class CommandStar extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["*"];
isMotion = true;
runsOnceForEachCountPrefix = true;

public async exec(position: Position, vimState: VimState): Promise<VimState> {
function searchCurrentWord(position: Position, vimState: VimState, direction: SearchDirection, isExact: boolean) {
const currentWord = TextEditor.getWord(position);
if (currentWord === undefined) {
return vimState;
}

vimState.globalState.searchState = new SearchState(SearchDirection.Forward, vimState.cursorPosition, currentWord);
// For an exact search we need to use a regex with word bounds.
const searchString = isExact
? `\\b${currentWord}\\b`
: currentWord;

// Start a search for the given term.
vimState.globalState.searchState = new SearchState(
direction, vimState.cursorPosition, searchString, { isRegex: isExact }
);

do {
vimState.cursorPosition = vimState.globalState.searchState.getNextSearchMatchPosition(vimState.cursorPosition).pos;
} while (TextEditor.getWord(vimState.cursorPosition) !== currentWord);
// If the search is going left then use `getWordLeft()` on position to start
// at the beginning of the word. This ensures that any matches happen
// outside of the currently selected word.
const searchStartCursorPosition = direction === SearchDirection.Backward
? vimState.cursorPosition.getWordLeft(true)
: vimState.cursorPosition;

vimState.cursorPosition = vimState.globalState.searchState.getNextSearchMatchPosition(searchStartCursorPosition).pos;

// Turn one of the highlighting flags back on (turned off with :nohl)
Configuration.hl = true;

return vimState;
}

@RegisterAction
class CommandSearchCurrentWordExactForward extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["*"];
isMotion = true;
runsOnceForEachCountPrefix = true;

public async exec(position: Position, vimState: VimState): Promise<VimState> {
return searchCurrentWord(position, vimState, SearchDirection.Forward, true);
}
}

@RegisterAction
class CommandHash extends BaseCommand {
class CommandSearchCurrentWordForward extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["#"];
keys = ["g", "*"];
isMotion = true;
runsOnceForEachCountPrefix = true;

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const currentWord = TextEditor.getWord(position);
if (currentWord === undefined) {
return vimState;
}
return searchCurrentWord(position, vimState, SearchDirection.Forward, false);
}
}

vimState.globalState.searchState = new SearchState(SearchDirection.Backward, vimState.cursorPosition, currentWord);
@RegisterAction
class CommandSearchCurrentWordExactBackward extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["#"];
isMotion = true;
runsOnceForEachCountPrefix = true;

do {
// use getWordLeft() on position to start at the beginning of the word.
// this ensures that any matches happen ounside of the word currently selected,
// which are the desired semantics for this motion.
vimState.cursorPosition = vimState.globalState.searchState.getNextSearchMatchPosition(vimState.cursorPosition.getWordLeft(true)).pos;
} while (TextEditor.getWord(vimState.cursorPosition) !== currentWord);
public async exec(position: Position, vimState: VimState): Promise<VimState> {
return searchCurrentWord(position, vimState, SearchDirection.Backward, true);
}
}

// Turn one of the highlighting flags back on (turned off with :nohl)
Configuration.hl = true;
@RegisterAction
class CommandSearchCurrentWordBackward extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["g", "#"];
isMotion = true;
runsOnceForEachCountPrefix = true;

return vimState;
public async exec(position: Position, vimState: VimState): Promise<VimState> {
return searchCurrentWord(position, vimState, SearchDirection.Backward, false);
}
}

Expand Down
42 changes: 32 additions & 10 deletions test/mode/normalModeTests/motions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,17 +463,25 @@ suite("Motions in Normal Mode", () => {
});

newTest({
title: "Can handle *",
start: ['|blah duh blah duh blah'],
keysPressed: '*',
end: ['blah duh |blah duh blah']
title: "Can handle g*",
start: ['|blah duh blahblah duh blah'],
keysPressed: 'g*',
end: ['blah duh |blahblah duh blah']
});

newTest({
title: "Can handle g*n",
start: ['|blah duh blahblah duh blah'],
keysPressed: 'g*n',
end: ['blah duh blah|blah duh blah']
});


newTest({
title: "Can handle tricky *",
start: ['|blah blahblah duh blah'],
title: "Can handle *",
start: ['|blah blahblah duh blah blah'],
keysPressed: '*',
end: ['blah blahblah duh |blah']
end: ['blah blahblah duh |blah blah']
});

newTest({
Expand All @@ -497,11 +505,25 @@ suite("Motions in Normal Mode", () => {
end: ['abc abcdef abc| '],
});

newTest({
title: "Can handle g#",
start: ['blah duh blahblah duh |blah'],
keysPressed: 'g#',
end: ['blah duh blah|blah duh blah']
});

newTest({
title: "Can handle g#n",
start: ['blah duh blahblah duh |blah'],
keysPressed: 'g#n',
end: ['blah duh |blahblah duh blah']
});

newTest({
title: "Can handle #",
start: ['blah duh |blah duh blah'],
start: ['blah blah blahblah duh |blah'],
keysPressed: '#',
end: ['|blah duh blah duh blah']
end: ['blah |blah blahblah duh blah']
});

newTest({
Expand Down Expand Up @@ -630,4 +652,4 @@ suite("Motions in Normal Mode", () => {
keysPressed: '<right>',
end: ['blah', 'duh', 'd|ur', 'hur']
});
});
});