Skip to content

Commit

Permalink
Fix replace in visual block mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfn committed Sep 29, 2016
1 parent 9775671 commit cb5719e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 23 deletions.
2 changes: 1 addition & 1 deletion extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ async function handleKeyEvent(key: string): Promise<void> {
const mh = await getAndUpdateModeHandler();

taskQueue.enqueueTask({
promise : async () => { await mh.handleKeyEvent(key); },
promise : async () => { console.log(key); await mh.handleKeyEvent(key); },

This comment has been minimized.

Copy link
@xconverge

xconverge Oct 1, 2016

Member

Do we want this there forever?

This comment has been minimized.

Copy link
@johnfn

johnfn Oct 2, 2016

Author Member

Oops, no. Thanks!!

isRunning : false
});
}
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
"command": "extension.vim_escape",
"when": "editorTextFocus && !inDebugRepl"
},
{
"key": "cmd+d",
"command": "extension.vim_cmd+d",
"when": "editorTextFocus && !inDebugRepl"
},
{
"key": "Backspace",
"command": "extension.vim_backspace",
Expand Down
48 changes: 36 additions & 12 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ export abstract class BaseCommand extends BaseAction {
*/
isCompleteAction = true;

supportsMultiCursor = true;

multicursorIndex: number | undefined = undefined;

/**
Expand Down Expand Up @@ -1083,22 +1081,22 @@ class CommandInsertInInsertMode extends BaseCommand {
} else {
if (vimState.isMultiCursor) {
vimState.recordedState.transformations.push({
type : "insertText",
text : char,
position: vimState.cursorPosition,
type : "insertText",
text : char,
position : vimState.cursorPosition,
diff : new PositionDiff(0, 1),
});
} else {
vimState.recordedState.transformations.push({
type : "insertTextVSCode",
text : char,
type : "insertTextVSCode",
text : char,
});
}
}

return vimState;
}


public toString(): string {
return this.keysPressed[this.keysPressed.length - 1];
}
Expand Down Expand Up @@ -3237,14 +3235,22 @@ class ActionReplaceCharacterVisualBlock extends BaseCommand {
canBeRepeatedWithDot = true;

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const toReplace = this.keysPressed[1];
const toInsert = this.keysPressed[1];

for (const { pos } of Position.IterateBlock(vimState.topLeft, vimState.bottomRight)) {
vimState = await new DeleteOperator().run(vimState, pos, pos);
await TextEditor.insertAt(toReplace, pos);
vimState.recordedState.transformations.push({
type : "replaceText",
text : toInsert,
start : pos.getLeft(),
end : pos,
});
}

vimState.cursorPosition = position;
const topLeft = VisualBlockMode.getTopLeftPosition(vimState.cursorPosition, vimState.cursorStartPosition);

vimState.allCursors = [ new Range(topLeft, topLeft) ];
vimState.currentMode = ModeName.Normal;

return vimState;
}
}
Expand Down Expand Up @@ -4397,3 +4403,21 @@ class MoveAroundTag extends MoveTagMatch {
keys = ["a", "t"];
includeTag = true;
}

@RegisterAction
class ActionOverrideCmdD extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual];
keys = ["<D-d>"];
runsOnceForEveryCursor() { return false; }

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vscode.commands.executeCommand('editor.action.addSelectionToNextFindMatch');
await waitForCursorUpdatesToHappen();

vimState.allCursors = vscode.window.activeTextEditor.selections.map(x =>
new Range(Position.FromVSCodePosition(x.start), Position.FromVSCodePosition(x.end)));
vimState.currentMode = ModeName.Visual;

return vimState;
}
}
35 changes: 25 additions & 10 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,17 @@ export class VimState {
/**
* In Multi Cursor Mode, the position of every cursor.
*/
public allCursors: Range[] = [ new Range(new Position(0, 0), new Position(0, 0)) ];
private _allCursors: Range[] = [ new Range(new Position(0, 0), new Position(0, 0)) ];

public get allCursors(): Range[] {
return this._allCursors;
}

public set allCursors(value: Range[]) {
this._allCursors = value;

this.isMultiCursor = this._allCursors.length > 1;
}

public cursorPositionJustBeforeAnythingHappened = [ new Position(0, 0) ];

Expand Down Expand Up @@ -594,26 +604,28 @@ export class ModeHandler implements vscode.Disposable {
return;
}


if (this.currentModeName === ModeName.VisualBlock ||
this.currentModeName === ModeName.VisualBlockInsertMode) {
// AArrgghhhh - johnfn

return;
}

if (this.vimState.isMultiCursor) {
// AAAAAARGGHHHHH - johnfn

return;
}

/*
if (this._vimState.currentMode !== ModeName.VisualBlock &&
this._vimState.currentMode !== ModeName.VisualBlockInsertMode &&
e.selections.length > this._vimState.allCursors.length) {
// Hey, we just added a selection. Either trigger or update Multi Cursor Mode.
if (e.selections.length >= 2) {
this._vimState.currentMode = ModeName.Visual;
this._vimState.isMultiCursor = true;

this.setCurrentModeByName(this._vimState);
} else {
if (e.selections.length === 2) {
// The selections ran together - go back to visual mode.
this._vimState.currentMode = ModeName.Visual;
this.setCurrentModeByName(this._vimState);
this._vimState.isMultiCursor = false;
Expand All @@ -629,6 +641,7 @@ export class ModeHandler implements vscode.Disposable {
return;
}
*/

if (!e.kind || e.kind === vscode.TextEditorSelectionChangeKind.Command) {
return;
Expand Down Expand Up @@ -1207,8 +1220,10 @@ export class ModeHandler implements vscode.Disposable {
if (transformations.length > 0) {
const firstTransformation = transformations[0];

if (firstTransformation.type === 'deleteRange' && firstTransformation.collapseRange) {
vimState.cursorPosition = new Position(vimState.cursorPosition.line, vimState.cursorStartPosition.character);
if (firstTransformation.type === 'deleteRange') {
if (firstTransformation.collapseRange) {
vimState.cursorPosition = new Position(vimState.cursorPosition.line, vimState.cursorStartPosition.character);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/notation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export class AngleBracketNotation {
private static _notationMap : { [key: string] : string[]; } = {
'C-': ['ctrl\\+', 'c\\-'],
'D-': ['cmd\\+', 'd\\-'],
'Esc': ['escape', 'esc'],
'BS': ['backspace', 'bs'],
'Del': ['delete', 'del'],
Expand Down

0 comments on commit cb5719e

Please sign in to comment.