commit 8bc4db72831bf35e59ca918638a5db50994b7a4e
| author | 斟酌 鵬兄 <tgckpg@gmail.com> |
| date | 2016-03-15T16:11:39Z |
| subject | partial insert support |
commit 8bc4db72831bf35e59ca918638a5db50994b7a4e
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date: 2016-03-15T16:11:39Z
partial insert support
---
botanjs/src/Components/Vim/Actions/INSERT.js | 47 +++++++++++++++++++++++-
botanjs/src/Components/Vim/Cursor.js | 22 ++++++++++-
botanjs/src/Components/Vim/LineBuffer.js | 5 ++-
botanjs/src/Components/Vim/LineFeeder.js | 19 +++++-----
botanjs/src/externs/Components.Vim.Cursor.js | 2 +
botanjs/src/externs/Components.Vim.LineFeeder.js | 2 +
6 files changed, 83 insertions(+), 14 deletions(-)
diff --git a/botanjs/src/Components/Vim/Actions/INSERT.js b/botanjs/src/Components/Vim/Actions/INSERT.js
index f9fe2f3..e131c88 100644
--- a/botanjs/src/Components/Vim/Actions/INSERT.js
+++ b/botanjs/src/Components/Vim/Actions/INSERT.js
@@ -5,6 +5,17 @@
var debug = __import( "System.Debug" );
var Mesg = __import( "Components.Vim.Message" );
+ var Translate = function( c )
+ {
+ switch( c )
+ {
+ case "Tab":
+ return "\t";
+ default:
+ return c;
+ }
+ };
+
/** @type {Components.Vim.Cursor.IAction} */
var INSERT = function( Cursor )
{
@@ -19,7 +30,41 @@
INSERT.prototype.handler = function( e )
{
e.preventDefault();
- var inputChar = e.key;
+ var inputChar = Translate( e.key );
+
+ if( inputChar.length != 1 ) return;
+
+ var cur = this.cursor;
+ var feeder = cur.feeder;
+
+ var line = cur.getLine();
+ var n = line.lineNum;
+
+ var cont = feeder.content;
+
+ var f = 0;
+ if( 0 < n )
+ {
+ f = cont.indexOf( "\n" );
+ for( i = 1; f != -1 && i < n; i ++ )
+ {
+ f = cont.indexOf( "\n", f + 1 );
+ }
+
+ if( this.cursor.feeder.wrap )
+ {
+ // wordwrap offset
+ f ++;
+ }
+ }
+
+ f += cur.aX;
+
+ feeder.content = cont.substring( 0, f ) + inputChar + cont.substring( f );
+ feeder.pan();
+ feeder.dispatcher.dispatchEvent( new BotanEvent( "VisualUpdate" ) );
+
+ cur.moveX( 1 );
};
INSERT.prototype.getMessage = function()
diff --git a/botanjs/src/Components/Vim/Cursor.js b/botanjs/src/Components/Vim/Cursor.js
index 6d42ad2..b73c4ed 100644
--- a/botanjs/src/Components/Vim/Cursor.js
+++ b/botanjs/src/Components/Vim/Cursor.js
@@ -238,12 +238,32 @@
return line;
};
+ // The absX for current Line
+ __readOnly( Cursor.prototype, "aX", function()
+ {
+ var X = this.X;
+ var f = this.feeder;
+
+ // Calculate wordwrap offset
+ if( f.wrap )
+ {
+ var cols = f.firstBuffer.cols + 1;
+ var w = X < cols ? 0 : Math.floor( X / cols );
+
+ if( 0 < w )
+ {
+ X -= w;
+ }
+ }
+
+ return X;
+ } );
+
__readOnly( Cursor.prototype, "message", function()
{
return this.action && this.action.getMessage();
} );
-
__readOnly( Cursor.prototype, "position", function()
{
return {
diff --git a/botanjs/src/Components/Vim/LineBuffer.js b/botanjs/src/Components/Vim/LineBuffer.js
index 41bbb03..2eccb0d 100644
--- a/botanjs/src/Components/Vim/LineBuffer.js
+++ b/botanjs/src/Components/Vim/LineBuffer.js
@@ -39,9 +39,10 @@
var i = 0;
var numTabs = 0;
+ var tabw = this.tabWidth - 1;
if( wrap )
{
- for( ; i < this.cols - numTabs * this.tabWidth; i ++ )
+ for( ; i < this.cols - numTabs * tabw; i ++ )
{
var c = content[i];
if( c === undefined ) break;
@@ -78,7 +79,7 @@
numTabs ++;
}
- if( i < this.cols - numTabs * this.tabWidth )
+ if( i < this.cols - numTabs * tabw )
{
line += c;
}
diff --git a/botanjs/src/Components/Vim/LineFeeder.js b/botanjs/src/Components/Vim/LineFeeder.js
index 04842af..059fd04 100644
--- a/botanjs/src/Components/Vim/LineFeeder.js
+++ b/botanjs/src/Components/Vim/LineFeeder.js
@@ -26,6 +26,8 @@
this.panX = 0;
this.panY = 0;
+ this.wrap = true;
+
this.setRender();
this.cursor = new Cursor( this );
@@ -34,8 +36,6 @@
this.__clseLine = null;
this.__moreAt = -1;
this.__rows = rows;
-
- this.__wrap = true;
};
Feeder.prototype.init = function( content, wrap )
@@ -43,13 +43,15 @@
this.content = content;
this.setWrap( wrap );
- this.firstBuffer.Push( content, this.__wrap, 0 );
+ this.firstBuffer.Push( content, this.wrap, 0 );
};
Feeder.prototype.setWrap = function( wrap )
{
if( wrap == undefined ) return;
- this.__wrap = wrap;
+ this.wrap = wrap;
+
+ // TODO: Update
};
Feeder.prototype.setRender = function( placeholder )
@@ -128,8 +130,6 @@
if( dX == undefined ) dX = 0;
if( dY == undefined ) dY = 0;
- if( dX == 0 && dY == 0 ) return;
-
var X = this.panX + dX;
var Y = this.panY + dY;
@@ -142,7 +142,7 @@
if( 0 < Y )
{
f = this.content.indexOf( "\n" );
- for( i = 1; f != - 1 && i < Y; i ++ )
+ for( i = 1; f != -1 && i < Y; i ++ )
{
f = this.content.indexOf( "\n", f + 1 );
}
@@ -150,7 +150,7 @@
this.firstBuffer.Push(
this.content.substr( f + 1 )
- , this.__wrap, i );
+ , this.wrap, i );
this.panX = X;
this.panY = Y;
@@ -195,8 +195,7 @@
} );
__readOnly( Feeder.prototype, "lineStat", function() {
- var X = this.cursor.X;
-
+ var X = this.cursor.aX;
var line = this.cursor.getLine();
var tabStat = "";
diff --git a/botanjs/src/externs/Components.Vim.Cursor.js b/botanjs/src/externs/Components.Vim.Cursor.js
index 050c5c6..4564825 100644
--- a/botanjs/src/externs/Components.Vim.Cursor.js
+++ b/botanjs/src/externs/Components.Vim.Cursor.js
@@ -28,6 +28,8 @@ Components.Vim.Cursor.pX;
/** @type Number */
Components.Vim.Cursor.P;
/** @type Number */
+Components.Vim.Cursor.aX;
+/** @type Number */
Components.Vim.Cursor.X;
/** @type Number */
Components.Vim.Cursor.Y;
diff --git a/botanjs/src/externs/Components.Vim.LineFeeder.js b/botanjs/src/externs/Components.Vim.LineFeeder.js
index 64fe726..1730d42 100644
--- a/botanjs/src/externs/Components.Vim.LineFeeder.js
+++ b/botanjs/src/externs/Components.Vim.LineFeeder.js
@@ -28,6 +28,8 @@ Components.Vim.LineFeeder.setWrap;
Components.Vim.LineFeeder.lineBuffers;
/** @type Boolean */
Components.Vim.LineFeeder.EOF;
+/** @type Boolean */
+Components.Vim.LineFeeder.wrap;
/** @type Number */
Components.Vim.LineFeeder.panX;
/** @type Number */