@@ -16,6 +16,7 @@ import { globalState } from '../src/state/globalState';
1616import { StatusBar } from '../src/statusBar' ;
1717import { TextEditor } from '../src/textEditor' ;
1818import { assertEqualLines , reloadConfiguration , setupWorkspace } from './testUtils' ;
19+ import { VimState } from '../src/state/vimState' ;
1920
2021function newTestGeneric < T extends ITestObject | ITestWithRemapsObject > (
2122 testObj : T ,
@@ -193,6 +194,28 @@ function tokenizeKeySequence(sequence: string): string[] {
193194 return result ;
194195}
195196
197+ async function applyDocState ( editor : vscode . TextEditor , docState : DocState ) : Promise < void > {
198+ assert . ok (
199+ await editor . edit ( ( builder ) => {
200+ builder . replace ( TextEditor . getDocumentRange ( editor . document ) , docState . lines . join ( '\n' ) ) ;
201+ } ) ,
202+ 'Edit failed' ,
203+ ) ;
204+ editor . selections = [ new vscode . Selection ( docState . cursor , docState . cursor ) ] ;
205+ }
206+
207+ function assertDocState ( vimState : VimState , docState : DocState ) : void {
208+ assertEqualLines ( docState . lines ) ;
209+
210+ const actualPos = vimState . editor . selection . start ;
211+ const expectedPos = docState . cursor ;
212+ assert . deepEqual (
213+ { line : actualPos . line , character : actualPos . character } ,
214+ { line : expectedPos . line , character : expectedPos . character } ,
215+ 'Cursor position is wrong.' ,
216+ ) ;
217+ }
218+
196219async function testIt ( testObj : ITestObject ) : Promise < ModeHandler > {
197220 if ( vscode . window . activeTextEditor === undefined ) {
198221 await setupWorkspace ( {
@@ -210,22 +233,18 @@ async function testIt(testObj: ITestObject): Promise<ModeHandler> {
210233 editor . options = testObj . editorOptions ;
211234 }
212235
213- // Initialize the editor with the starting text and cursor selection
214- assert . ok (
215- await editor . edit ( ( builder ) => {
216- builder . replace ( TextEditor . getDocumentRange ( editor . document ) , start . lines . join ( '\n' ) ) ;
217- } ) ,
218- 'Edit failed' ,
219- ) ;
236+ await applyDocState ( editor , start ) ;
237+
220238 if ( testObj . saveDocBeforeTest ) {
221239 assert . ok ( await editor . document . save ( ) , 'Save failed' ) ;
222240 }
223- editor . selections = [ new vscode . Selection ( start . cursor , start . cursor ) ] ;
224241
225242 // Generate a brand new ModeHandler for this editor
226243 ModeHandlerMap . clear ( ) ;
227244 const [ modeHandler , _ ] = await ModeHandlerMap . getOrCreate ( editor ) ;
228245
246+ assertDocState ( modeHandler . vimState , start ) ;
247+
229248 globalState . lastInvokedMacro = undefined ;
230249 globalState . jumpTracker . clearJumps ( ) ;
231250
@@ -243,17 +262,7 @@ async function testIt(testObj: ITestObject): Promise<ModeHandler> {
243262 await modeHandler . handleMultipleKeyEvents ( tokenizeKeySequence ( testObj . keysPressed ) , false ) ;
244263 }
245264
246- // Check given end output is correct
247- assertEqualLines ( end . lines ) ;
248-
249- // Check final cursor position
250- const actualPosition = modeHandler . vimState . editor . selection . start ;
251- const expectedPosition = end . cursor ;
252- assert . deepEqual (
253- { line : actualPosition . line , character : actualPosition . character } ,
254- { line : expectedPosition . line , character : expectedPosition . character } ,
255- 'Cursor position is wrong.' ,
256- ) ;
265+ assertDocState ( modeHandler . vimState , end ) ;
257266
258267 if ( testObj . endMode !== undefined ) {
259268 assert . equal (
@@ -315,14 +324,7 @@ async function testItWithRemaps(testObj: ITestWithRemapsObject): Promise<ModeHan
315324 const editor = vscode . window . activeTextEditor ;
316325 assert ( editor , 'Expected an active editor' ) ;
317326
318- // Initialize the editor with the starting text and cursor selection
319- await editor . edit ( ( builder ) => {
320- builder . insert ( new Position ( 0 , 0 ) , testObj . start . join ( '\n' ) . replace ( '|' , '' ) ) ;
321- } ) ;
322- {
323- const start = DocState . parse ( testObj . start ) ;
324- editor . selections = [ new vscode . Selection ( start . cursor , start . cursor ) ] ;
325- }
327+ await applyDocState ( editor , DocState . parse ( testObj . start ) ) ;
326328
327329 // Generate a brand new ModeHandler for this editor
328330 ModeHandlerMap . clear ( ) ;
0 commit comments