Skip to content

Commit

Permalink
Refactor ReplaceState into its own class.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfn committed Oct 3, 2016
1 parent 61b1c53 commit 21071b7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/actions/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { VimState, ReplaceState } from './../mode/modeHandler';
import { VimState } from './../mode/modeHandler';
import { SearchState, SearchDirection } from './../state/searchState';
import { ReplaceState } from './../state/replaceState';
import { VisualBlockMode } from './../mode/modeVisualBlock';
import { ModeName } from './../mode/mode';
import { VisualBlockInsertionType } from './../mode/modeVisualBlock';
Expand Down
31 changes: 1 addition & 30 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { PairMatcher } from './../matching/matcher';
import { Globals } from '../../src/globals';
import { allowVSCodeToPropagateCursorUpdatesAndReturnThem } from './../util';
import { SearchState } from './../state/searchState';
import { ReplaceState } from './../state/replaceState';

export class ViewChange {
public command: string;
Expand Down Expand Up @@ -200,36 +201,6 @@ export class VimState {
public whatILastSetTheSelectionTo: vscode.Selection;
}

export class ReplaceState {
/**
* The location of the cursor where you begun to replace characters.
*/
public replaceCursorStartPosition: Position;

public originalChars: string[] = [];

/**
* The characters the user inserted in replace mode. Useful for when
* we repeat a replace action with .
*/
public newChars: string[] = [];

/**
* Number of times we're going to repeat this replace action.
*/
public timesToRepeat: number;

constructor(startPosition: Position, timesToRepeat: number = 1) {
this.replaceCursorStartPosition = startPosition;
this.timesToRepeat = timesToRepeat;

let text = TextEditor.getLineAt(startPosition).text.substring(startPosition.character);
for (let [key, value] of text.split("").entries()) {
this.originalChars[key + startPosition.character] = value;
}
}
}

/**
* The RecordedState class holds the current action that the user is
* doing. Example: Imagine that the user types:
Expand Down
35 changes: 35 additions & 0 deletions src/state/replaceState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Position } from './../motion/position';
import { TextEditor } from './../textEditor';

/**
* State involved with entering Replace mode (R).
*/
export class ReplaceState {
/**
* The location of the cursor where you begun to replace characters.
*/
public replaceCursorStartPosition: Position;

public originalChars: string[] = [];

/**
* The characters the user inserted in replace mode. Useful for when
* we repeat a replace action with .
*/
public newChars: string[] = [];

/**
* Number of times we're going to repeat this replace action.
*/
public timesToRepeat: number;

constructor(startPosition: Position, timesToRepeat: number = 1) {
this.replaceCursorStartPosition = startPosition;
this.timesToRepeat = timesToRepeat;

let text = TextEditor.getLineAt(startPosition).text.substring(startPosition.character);
for (let [key, value] of text.split("").entries()) {
this.originalChars[key + startPosition.character] = value;
}
}
}
9 changes: 7 additions & 2 deletions src/state/searchState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ export enum SearchDirection {
Backward = -1
}

/**
* State involved with beginning a search (/).
*/
export class SearchState {
private static readonly MAX_SEARCH_RANGES = 1000;
private static specialCharactersRegex: RegExp = /[\-\[\]{}()*+?.,\\\^$|#\s]/g;

private _matchRanges: vscode.Range[] = [];

/**
* Every range in the document that matches the search string.
*/
private _matchRanges: vscode.Range[] = [];
public get matchRanges(): vscode.Range[] {
return this._matchRanges;
}
Expand Down Expand Up @@ -51,7 +55,8 @@ export class SearchState {
this._matchesDocVersion = TextEditor.getDocumentVersion();
this._matchRanges = [];

/* Decide whether the search is case sensitive.
/*
* Decide whether the search is case sensitive.
* If ignorecase is false, the search is case sensitive.
* If ignorecase is true, the search should be case insensitive.
* If both ignorecase and smartcase are true, the search is case sensitive only when the search string contains UpperCase character.
Expand Down

0 comments on commit 21071b7

Please sign in to comment.