DELETE command in VISUAL, generalized Stator Obj

This commit is contained in:
2016-03-23 03:15:02 +08:00
parent 5f5e0604fa
commit 78f8a20a5d
10 changed files with 241 additions and 134 deletions

View File

@@ -1,13 +1,24 @@
(function(){
var ns = __namespace( "Components.Vim.Actions" );
/** @type {System.Debug} */
var debug = __import( "System.Debug" );
/** @type {Components.Vim.State.Stator} */
var Stator = __import( "Components.Vim.State.Stator" );
/** @type {Components.Vim.State.Stack} */
var Stack = __import( "Components.Vim.State.Stack" );
var Mesg = __import( "Components.Vim.Message" );
var occurence = __import( "System.utils.Perf.CountSubstr" );
/** @type {Components.Vim.Cursor.IAction} */
var DELETE = function( Cursor )
{
/** @type {Components.Vim.Cursor} */
this.__cursor = Cursor;
this.__nline = 0;
};
DELETE.prototype.allowMovement = true;
@@ -17,15 +28,65 @@
};
DELETE.prototype.handler = function( e )
DELETE.prototype.handler = function( e, sp )
{
e.preventDefault();
/** @type {Components.Vim.State.Registers} */
var reg = e.target.registers;
var cur = this.__cursor;
var feeder = cur.feeder;
var c = feeder.content;
var s = sp;
var e = cur.aPos;
if( e < s )
{
s = cur.aPos;
e = sp;
}
var removed = c.substring( s, e + 1 );
reg.change( removed );
this.__nline = occurence( removed, "\n" );
feeder.content = c.substring( 0, s ) + c.substring( e + 1 );
var stator = new Stator( cur, s );
var stack = new Stack();
c = c[ e + 1 ];
if( c == "\n" || c == undefined )
{
cur.suppressEvent();
cur.moveX( -1 );
cur.unsuppressEvent();
}
var f = stator.save( 0, removed );
stack.store( function() {
f();
// Offset correction after REDO / UNDO
cur.moveX( 1 );
} );
cur.rec.record( stack );
feeder.pan();
};
DELETE.prototype.getMessage = function()
{
return "<TODO> DELETE COMMAND";
if( this.__nline )
{
return Mesg( "LINE_FEWER", this.__nline );
}
return "";
};
ns[ NS_EXPORT ]( EX_CLASS, "DELETE", DELETE );

View File

@@ -2,9 +2,11 @@
var ns = __namespace( "Components.Vim.Actions" );
/** @type {Components.Vim.State.Stack} */
var Stack = __import( "Components.Vim.State.Stack" );
var Stack = __import( "Components.Vim.State.Stack" );
/** @type {Components.Vim.State.Stator} */
var Stator = __import( "Components.Vim.State.Stator" );
/** @type {System.Debug} */
var debug = __import( "System.Debug" );
var debug = __import( "System.Debug" );
var Mesg = __import( "Components.Vim.Message" );
@@ -27,7 +29,7 @@
/** @type {Components.Vim.Cursor} */
this.__cursor = Cursor;
this.__startState = this.__saveCur();
this.__Stator = new Stator( Cursor );
// Initialize this stack
this.__rec( "", true );
@@ -35,66 +37,12 @@
INSERT.prototype.allowMovement = false;
INSERT.prototype.__saveCur = function()
{
var c = this.__cursor;
var obj = {
p: c.P
, x: c.X
, y: c.Y
, px: c.feeder.panX
, py: c.feeder.panY
};
if( 0 < obj.x )
{
obj.p -= 1;
obj.x -= 1;
}
return obj;
}
INSERT.prototype.dispose = function()
{
this.__cursor.moveX( -1 );
this.__rec( "", true );
};
INSERT.prototype.__storeState = function()
{
var cur = this.__cursor;
var feeder = cur.feeder;
var insertLength = this.__insertLength;
var contentUndo = this.__contentUndo;
var startPos = this.__startPosition;
var sSt = this.__startState;
var eSt = this.__saveCur();
var st = sSt;
// Calling this repeatedly will swap between UNDO / REDO state
return function() {
var contentRedo = feeder.content.substr( startPos, insertLength );
feeder.content =
feeder.content.substring( 0, startPos )
+ contentUndo
+ feeder.content.substring( startPos + insertLength );
insertLength = contentUndo.length;
contentUndo = contentRedo;
cur.PStart = st.p;
cur.PEnd = st.p + 1;
cur.X = st.x;
cur.Y = st.y;
feeder.panX = st.px;
feeder.panY = st.py;
feeder.pan();
st = ( st == sSt ) ? eSt : sSt;
};
};
INSERT.prototype.__rec = function( c, newRec )
{
if( newRec || !this.__stack )
@@ -107,7 +55,7 @@
) return;
this.__stack.store(
this.__storeState()
this.__Stator.save( this.__insertLength, this.__contentUndo )
);
this.__cursor.rec.record( this.__stack );
@@ -116,7 +64,6 @@
this.__insertLength = 0;
this.__contentUndo = "";
this.__stack = new Stack();
this.__startPosition = this.__cursor.aPos;
}
if( c == "\n" )
@@ -132,45 +79,37 @@
var cur = this.__cursor;
var feeder = cur.feeder;
switch( e.keyCode )
// Backspace
if( e.kMap( "BS" ) )
{
case 8: // Backspace
var oY = feeder.panY + cur.Y;
if( cur.X == 0 && feeder.panY == 0 && cur.Y == 0 ) return;
var oY = feeder.panY + cur.Y;
if( cur.X == 0 && feeder.panY == 0 && cur.Y == 0 ) return;
cur.moveX( -1, true, true );
cur.moveX( -1, true, true );
var f = cur.aPos;
var f = cur.aPos;
if( this.__insertLength <= 0 )
{
this.__contentUndo = feeder.content.substr( f, 1 ) + this.__contentUndo;
this.__startPosition --;
}
else
{
this.__insertLength --;
}
if( this.__insertLength <= 0 )
{
this.__contentUndo = feeder.content.substr( f, 1 ) + this.__contentUndo;
this.__insertLength --;
}
feeder.content =
feeder.content.substring( 0, f )
+ feeder.content.substring( f + 1 );
break;
case 46: // Delete
var f = cur.aPos;
this.__contentUndo += feeder.content.substr( f, 1 );
feeder.content =
feeder.content.substring( 0, f )
+ feeder.content.substring( f + 1 );
break;
default:
// Do nothing
return;
feeder.content =
feeder.content.substring( 0, f )
+ feeder.content.substring( f + 1 );
}
else if( e.kMap( "Del" ) )
{
var f = cur.aPos;
this.__contentUndo += feeder.content.substr( f, 1 );
feeder.content =
feeder.content.substring( 0, f )
+ feeder.content.substring( f + 1 );
}
else return;
feeder.pan();
feeder.dispatcher.dispatchEvent( new BotanEvent( "VisualUpdate" ) );

View File

@@ -23,6 +23,7 @@
this.__leaveMesg = "";
Cursor.blink = false;
Cursor.pSpace = true;
};
VISUAL.prototype.allowMovement = true;
@@ -31,6 +32,7 @@
{
this.__msg = this.__leaveMesg;
this.__cursor.blink = true;
this.__cursor.pSpace = false;
this.__cursor.PStart = this.__selStart;
this.__cursor.PEnd = this.__selStart + 1;
};
@@ -41,27 +43,18 @@
if( e.ModKeys ) return;
var cur = this.__cursor;
var Action = null;
switch( true )
{
case e.kMap( "y" ):
Action = new YANK( this.__cursor );
Action = new YANK( cur );
break;
case e.kMap( "d" ):
Action = new DELETE( this.__cursor );
Action = new DELETE( cur );
break;
}
if( Action )
{
Action.handler( e );
this.__leaveMesg = Action.getMessage();
Action.dispose();
return true;
}
var cur = this.__cursor;
var prevPos = this.__start;
var newPos = cur.PStart;
@@ -78,6 +71,18 @@
this.__selStart = prevPos;
}
if( Action )
{
cur.suppressEvent();
Action.handler( e, this.__startaP );
this.__leaveMesg = Action.getMessage();
Action.dispose();
cur.unsuppressEvent();
this.__selStart = cur.PStart;
return true;
}
cur.PStart = prevPos;
cur.PEnd = newPos;
};