From e0c50f1bff58bc6cd9c32848642818601e43b94f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=9F=E9=85=8C=20=E9=B5=AC=E5=85=84?= Date: Fri, 18 Mar 2016 05:32:51 +0800 Subject: [PATCH] Rewrite the way to init VimControls --- botanjs/src/Components/Vim/Controls.js | 35 ++++++++++++++++++------ botanjs/src/Components/Vim/Cursor.js | 6 ++-- botanjs/src/Components/Vim/LineFeeder.js | 12 +++++++- botanjs/src/Components/Vim/VimArea.js | 2 +- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/botanjs/src/Components/Vim/Controls.js b/botanjs/src/Components/Vim/Controls.js index dd23229..dcaa9dc 100644 --- a/botanjs/src/Components/Vim/Controls.js +++ b/botanjs/src/Components/Vim/Controls.js @@ -19,24 +19,33 @@ var U = 85; var V = 86; var W = 87; var X = 88; var Y = 89; var Z = 90; - var Controls = function() + var Controls = function( vimArea ) { + /** @type {Components.Vim.VimArea} */ + this.__vimArea = vimArea this.__keyChains = []; }; - Controls.prototype.__comboG = function( e ) + Controls.prototype.__comboG = function( keyCode ) { var keyON = this.__keyChains[ 0 ] == G; if( keyON ) { - switch( e.keyCode ) + var cursor = this.__vimArea.contentFeeder.cursor; + switch( keyCode ) { + case G: + cursor.moveY( -Number.MAX_VALUE ); + cursor.moveX( -Number.MAX_VALUE, true ); + this.__keyChains = []; + return true; default: this.__keyChains = []; + beep(); return true; } } - else if( e.keyCode == G ) + else if( keyCode == G ) { this.__keyChains[ 0 ] = G; return true; @@ -69,14 +78,20 @@ || ( 112 < e.keyCode && e.keyCode < 124 ) ) return; + var vArea = this.__vimArea; // Action Mode handled by the actions themselves - var cfeeder = sender.contentFeeder; + var cfeeder = vArea.contentFeeder; // Esc OR Ctrl + c var Escape = e.keyCode == 27 || ( e.ctrlKey && e.keyCode == 67 ); // Clear the keychains in combo commands - if( Escape ) this.__keyChains = []; + if( Escape && this.__keyChains.length ) + { + this.__keyChains = []; + beep(); + return; + } if( cfeeder.cursor.action ) { @@ -101,8 +116,8 @@ if( this.__comboKey( kCode ) ) return; - var cfeeder = sender.contentFeeder; - var sfeeder = sender.statusFeeder; + var cfeeder = vArea.contentFeeder; + var sfeeder = vArea.statusFeeder; var ccur = cfeeder.cursor; @@ -167,6 +182,10 @@ case SHIFT + I: // Append before the line start, after spaces break; + case SHIFT + G: // Goto last line + ccur.moveY( Number.MAX_VALUE ); + ccur.moveX( Number.MAX_VALUE, true ); + break; // remove characters case X: // Remove in cursor break; diff --git a/botanjs/src/Components/Vim/Cursor.js b/botanjs/src/Components/Vim/Cursor.js index c11d9a3..fc3d4e1 100644 --- a/botanjs/src/Components/Vim/Cursor.js +++ b/botanjs/src/Components/Vim/Cursor.js @@ -149,7 +149,7 @@ this.feeder.dispatcher.dispatchEvent( new BotanEvent( "VisualUpdate" ) ); }; - Cursor.prototype.moveY = function( d ) + Cursor.prototype.moveY = function( d, penentrate ) { var Y = this.Y + d; var line; @@ -172,7 +172,7 @@ var lineShift = Y - feeder.moreAt; var i = 0; - while( true ) + while( !feeder.EOF ) { feeder.pan( undefined, lineShift + i ); @@ -196,7 +196,7 @@ line = line.next ) { if( line.br ) i ++; - if( line.lineNum == Y ) break; + if( line.lineNum == Y || line.next.placeholder ) break; } this.Y = i; diff --git a/botanjs/src/Components/Vim/LineFeeder.js b/botanjs/src/Components/Vim/LineFeeder.js index 58258b4..3d0ca71 100644 --- a/botanjs/src/Components/Vim/LineFeeder.js +++ b/botanjs/src/Components/Vim/LineFeeder.js @@ -144,7 +144,13 @@ f = this.content.indexOf( "\n" ); for( i = 1; f != -1 && i < Y; i ++ ) { - f = this.content.indexOf( "\n", f + 1 ); + var a = this.content.indexOf( "\n", f + 1 ); + if( a == -1 ) + { + Y = i; + break; + } + f = a; } } @@ -163,6 +169,10 @@ this.__softRender(); }; + __readOnly( Feeder.prototype, "linesTotal", function() { + return this.content.match( "\n" ); + } ); + __readOnly( Feeder.prototype, "firstBuffer", function() { return this.lineBuffers[ 0 ]; } ); diff --git a/botanjs/src/Components/Vim/VimArea.js b/botanjs/src/Components/Vim/VimArea.js index 3f3eae5..b9a76e4 100644 --- a/botanjs/src/Components/Vim/VimArea.js +++ b/botanjs/src/Components/Vim/VimArea.js @@ -53,7 +53,7 @@ var _self = this; - var controls = new VimControls(); + var controls = new VimControls( this ); stage.addEventListener( "KeyDown" , KeyHandler( this, controls.handler.bind( controls ) )