Skip to content

Commit

Permalink
Add hlsearchF option
Browse files Browse the repository at this point in the history
* If this option is enabled, the f command will highlight the characters in a search
* This Also, the highlighted characters will be the characters between the beginning and the end of the line, ignoring the current curser position. This means that the result will be the same as a search from the beginning of the line.
  • Loading branch information
Yuuki77 committed Jul 31, 2020
1 parent 50b9a93 commit 61f3d72
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,11 @@
"description": "Show all matches of the most recent search pattern.",
"default": false
},
"vim.hlsearchF": {
"type": "boolean",
"description": "Show all matches in the line by f search.",
"default": true
},
"vim.incsearch": {
"type": "boolean",
"markdownDescription": "Show where a `/` or `?` search matches as you type it.",
Expand Down
26 changes: 25 additions & 1 deletion src/actions/motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { globalState } from '../state/globalState';
import { reportSearch } from '../util/statusBarTextUtils';
import { SneakForward, SneakBackward } from './plugins/sneak';
import { Notation } from '../configuration/notation';
import { SearchDirection } from '../state/searchState';
import { SearchDirection, SearchState } from '../state/searchState';
import { StatusBar } from '../statusBar';
import { clamp } from '../util/util';

Expand Down Expand Up @@ -684,6 +684,18 @@ class MoveFindForward extends BaseMovement {
return pos;
}

if (configuration.hlsearchF) {
globalState.hl = true;
globalState.searchState = new SearchState(
SearchDirection.Forward,
vimState.cursorStopPosition,
this.keysPressed[1],
{ isRegex: true },
vimState.currentMode,
true
);
}

count = count || 1;
const toFind = Notation.ToControlCharacter(this.keysPressed[1]);
let result = findHelper(position, toFind, count, 'forward');
Expand Down Expand Up @@ -720,6 +732,18 @@ class MoveFindBackward extends BaseMovement {
);
}

if (configuration.hlsearchF) {
globalState.hl = true;
globalState.searchState = new SearchState(
SearchDirection.Backward,
vimState.cursorStopPosition,
this.keysPressed[1],
{ isRegex: true },
vimState.currentMode,
true
);
}

count = count || 1;
const toFind = Notation.ToControlCharacter(this.keysPressed[1]);
let result = findHelper(position, toFind, count, 'backward');
Expand Down
1 change: 1 addition & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class Configuration implements IConfiguration {
sneak = false;
sneakUseIgnorecaseAndSmartcase = false;
sneakReplacesF = false;
hlsearchF = false;

surround = true;

Expand Down
5 changes: 5 additions & 0 deletions src/configuration/iconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ export interface IConfiguration {
*/
sneakReplacesF: boolean;

/**
* Show all matches in the line by f search.
*/
hlsearchF: boolean;

/**
* Use surround plugin?
*/
Expand Down
27 changes: 19 additions & 8 deletions src/state/searchState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class SearchState {
public readonly previousMode: Mode;
public readonly searchDirection: SearchDirection;
public readonly cursorStartPosition: Position;
oneLineSearch: boolean;

/**
* Every range in the document that matches the search string.
Expand Down Expand Up @@ -175,7 +176,10 @@ export class SearchState {

// We store the entire text file as a string inside text, and run the
// regex against it many times to find all of our matches.
const text = document.getText();
const text = this.oneLineSearch
? document.lineAt(this.cursorStartPosition).text
: document.getText();

const selection = vscode.window.activeTextEditor!.selection;
const startOffset = document.offsetAt(selection.active);
const regex = this.needleRegex;
Expand All @@ -192,13 +196,18 @@ export class SearchState {
// We've found our first match again
break;
}
const start = this.oneLineSearch
? new Position(document.lineAt(this.cursorStartPosition).lineNumber, result.index)
: document.positionAt(result.index);

const end = this.oneLineSearch
? new Position(
document.lineAt(this.cursorStartPosition).lineNumber,
result.index + result[0].length
)
: document.positionAt(result.index + result[0].length);

matchRanges.push(
new vscode.Range(
document.positionAt(result.index),
document.positionAt(result.index + result[0].length)
)
);
matchRanges.push(new vscode.Range(start, end));

if (matchRanges.length >= SearchState.MAX_SEARCH_RANGES) {
break;
Expand Down Expand Up @@ -365,13 +374,15 @@ export class SearchState {
startPosition: Position,
searchString = '',
{ isRegex = false, ignoreSmartcase = false } = {},
currentMode: Mode
currentMode: Mode,
oneLineSearch = false
) {
this.searchDirection = direction;
this.cursorStartPosition = startPosition;
this.isRegex = isRegex;
this.ignoreSmartcase = ignoreSmartcase;
this.searchString = searchString;
this.previousMode = currentMode;
this.oneLineSearch = oneLineSearch;
}
}

0 comments on commit 61f3d72

Please sign in to comment.