-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert test to async/await style.
- Loading branch information
Showing
15 changed files
with
277 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
out | ||
node_modules | ||
typings | ||
*.swp | ||
*.sw? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
"use strict"; | ||
|
||
import * as vscode from 'vscode'; | ||
import * as _ from 'lodash' | ||
|
||
import {ModeName, Mode} from './mode'; | ||
import {TextEditor} from './../textEditor'; | ||
import {Motion} from './../motion/motion'; | ||
import {Position, PositionOptions} from './../motion/position'; | ||
import { Operator } from './../operator/operator' | ||
import { DeleteOperator } from './../operator/delete' | ||
import { ModeHandler } from './modeHandler.ts' | ||
import { ChangeOperator } from './../operator/change' | ||
|
||
export class VisualMode extends Mode { | ||
private _selectionStart: Position; | ||
private _selectionStop : Position; | ||
private _modeHandler : ModeHandler; | ||
|
||
private _keysToOperators: { [key: string]: Operator }; | ||
|
||
constructor(motion: Motion, modeHandler: ModeHandler) { | ||
super(ModeName.Visual, motion); | ||
|
||
this._keysToOperators = { | ||
// TODO: use DeleteOperator.key() | ||
|
||
// TODO: Don't pass in mode handler to DeleteOperators, | ||
// simply allow the operators to say what mode they transition into. | ||
'd': new DeleteOperator(modeHandler), | ||
'x': new DeleteOperator(modeHandler), | ||
'c': new ChangeOperator(modeHandler), | ||
} | ||
} | ||
|
||
shouldBeActivated(key: string, currentMode: ModeName): boolean { | ||
return key === "v"; | ||
} | ||
|
||
async handleActivation(key: string): Promise<void> { | ||
this._selectionStart = this.motion.position; | ||
this._selectionStop = this._selectionStart.getRight(); | ||
|
||
this.motion.selectTo(this._selectionStop); | ||
} | ||
|
||
handleDeactivation(): void { | ||
super.handleDeactivation(); | ||
|
||
this.motion.moveTo(this._selectionStop.line, this._selectionStop.character); | ||
} | ||
|
||
/** | ||
* TODO: | ||
* | ||
* Eventually, the following functions should be moved into a unified | ||
* key handler and dispatcher thing. | ||
*/ | ||
|
||
private async _handleMotion(): Promise<boolean> { | ||
let keyHandled = false; | ||
let keysPressed: string; | ||
|
||
for (let window = this.keyHistory.length; window > 0; window--) { | ||
keysPressed = _.takeRight(this.keyHistory, window).join(''); | ||
if (this.keyToNewPosition[keysPressed] !== undefined) { | ||
keyHandled = true; | ||
break; | ||
} | ||
} | ||
|
||
if (keyHandled) { | ||
this._selectionStop = await this.keyToNewPosition[keysPressed](this._selectionStop); | ||
|
||
this.motion.moveTo(this._selectionStart.line, this._selectionStart.character); | ||
this.motion.selectTo(this._selectionStop.getRight()); | ||
|
||
this.keyHistory = []; | ||
} | ||
|
||
return keyHandled; | ||
} | ||
|
||
private async _handleOperator(): Promise<boolean> { | ||
let keyHandled = false; | ||
let keysPressed: string; | ||
let operator: Operator; | ||
|
||
for (let window = this.keyHistory.length; window > 0; window--) { | ||
keysPressed = _.takeRight(this.keyHistory, window).join(''); | ||
if (this._keysToOperators[keysPressed] !== undefined) { | ||
operator = this._keysToOperators[keysPressed]; | ||
break; | ||
} | ||
} | ||
|
||
if (operator) { | ||
if (this._selectionStart.compareTo(this._selectionStop)) { | ||
operator.run(this._selectionStart, this._selectionStop.getRight()); | ||
} else { | ||
operator.run(this._selectionStop, this._selectionStart.getRight()); | ||
} | ||
} | ||
|
||
return !!operator; | ||
} | ||
|
||
async handleKeyEvent(key: string): Promise<void> { | ||
this.keyHistory.push(key); | ||
|
||
const wasMotion = await this._handleMotion(); | ||
|
||
if (!wasMotion) { | ||
await this._handleOperator(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"use strict"; | ||
|
||
import {Position, PositionOptions} from './../motion/position'; | ||
import { TextEditor } from './../textEditor' | ||
import { Operator } from './Operator' | ||
import { ModeHandler } from './../mode/modeHandler.ts' | ||
import { ModeName } from './../mode/mode' | ||
|
||
import * as vscode from 'vscode' | ||
|
||
export class ChangeOperator { | ||
private _modeHandler: ModeHandler; | ||
|
||
constructor(modeHandler: ModeHandler) { | ||
this._modeHandler = modeHandler; | ||
} | ||
|
||
public key(): string { return "d"; } | ||
|
||
/** | ||
* Run this operator on a range. | ||
*/ | ||
public async run(start: Position, end: Position): Promise<void> { | ||
await TextEditor.delete(new vscode.Range(start, end)); | ||
|
||
this._modeHandler.setCurrentModeByName(ModeName.Insert); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"use strict"; | ||
|
||
import {Position, PositionOptions} from './../motion/position'; | ||
import { TextEditor } from './../textEditor' | ||
import { Operator } from './Operator' | ||
import { ModeHandler } from './../mode/modeHandler.ts' | ||
import { ModeName } from './../mode/mode' | ||
|
||
import * as vscode from 'vscode' | ||
|
||
export class DeleteOperator { | ||
private _modeHandler: ModeHandler; | ||
|
||
constructor(modeHandler: ModeHandler) { | ||
this._modeHandler = modeHandler; | ||
} | ||
|
||
public key(): string { return "d"; } | ||
|
||
/** | ||
* Run this operator on a range. | ||
*/ | ||
public async run(start: Position, end: Position): Promise<void> { | ||
await TextEditor.delete(new vscode.Range(start, end)); | ||
|
||
this._modeHandler.setCurrentModeByName(ModeName.Normal); | ||
} | ||
} |
Oops, something went wrong.