Some placeholder commands, working substitution

This commit is contained in:
2016-04-03 05:23:26 +08:00
parent 63575ce2e6
commit f4dc0e7d9c
19 changed files with 573 additions and 64 deletions

View File

@@ -14,7 +14,7 @@
var occurence = __import( "System.utils.Perf.CountSubstr" );
/** @type {Components.Vim.Cursor.IAction} */
/** @type {Components.Vim.IAction} */
var DELETE = function( Cursor )
{
/** @type {Components.Vim.Cursor} */

View File

@@ -0,0 +1,165 @@
(function(){
var ns = __namespace( "Components.Vim.Actions" );
/** @type {System.Debug} */
var debug = __import( "System.Debug" );
var VimError = __import( "Components.Vim.Error" );
var CMD_RANGE = 0;
var CMD_TYPE = 1;
var CMD_ARGS = 2;
var CMD_ERR = 3;
var ParseCommand = function( pattern )
{
var i = 1;
var range = "";
var out = [];
if( ".$%".indexOf( pattern[ i ] ) != -1 )
{
range = pattern[ i ++ ];
}
else
{
for( ; "0123456789".indexOf( pattern[ i ] ) != -1; i ++ )
{
range += pattern[ i ];
}
}
var command = "";
if( "/?".indexOf( pattern[ i ] ) != -1 )
{
command = pattern[ i ];
}
else
{
var cmdReg = /\w/g;
for( var j = pattern[ i ]; j = pattern[ i ]; i ++ )
{
if( j.match( cmdReg ) )
{
command += j;
}
else break;
}
}
var allowRange = false;
switch( command )
{
case "s":
case "su":
case "substitute":
allowRange = true;
out[ CMD_TYPE ] = "REPLACE";
break;
case "/":
allowRange = true;
out[ CMD_TYPE ] = "FIND";
break;
case "buffers":
case "ls":
out[ CMD_TYPE ] = "BUFFERS";
break;
case "w":
case "write":
out[ CMD_TYPE ] = "WRITE";
break;
case "q":
case "quit":
out[ CMD_TYPE ] = "QUIT";
break;
case "register":
case "registers":
out[ CMD_TYPE ] = "REGISTERS";
break;
case "ver":
case "version":
out[ CMD_TYPE ] = "VERSION";
break;
case "h":
case "help":
out[ CMD_TYPE ] = "HELP";
break;
}
if( range !== "" )
{
if( allowRange ) out[ CMD_RANGE ] = range;
else out[ CMD_ERR ] = VimError( "E481" );
}
out[ CMD_ARGS ] = pattern.slice( i );
return out;
};
/** @type {Components.Vim.IAction} */
var EDITOR_COMMAND = function( Cursor )
{
/** @type {Components.Vim.Cursor} */
this.__cursor = Cursor;
this.__msg = "";
Cursor.suppressEvent();
};
EDITOR_COMMAND.prototype.dispose = function()
{
this.__cursor.unsuppressEvent();
};
EDITOR_COMMAND.prototype.handler = function( e, p )
{
e.preventDefault();
var cmd = ParseCommand( p );
if( cmd[ CMD_ERR ] )
{
this.__msg = cmd[ CMD_ERR ];
return true;
}
else if( !cmd[ CMD_TYPE ] )
{
this.__msg = VimError( "E492", p.slice( 1 ).join( "" ) );
return true;
}
try
{
ns[ NS_INVOKE ]( cmd[ CMD_TYPE ] );
}
catch( ex )
{
this.__msg = VimError( "TODO", cmd[ CMD_TYPE ] );
return true;
}
try
{
this.__cursor.openRunAction(
cmd[ CMD_TYPE ], e, cmd[ CMD_ARGS ], cmd[ CMD_RANGE ]
);
this.__msg = this.__cursor.message;
}
catch( ex )
{
debug.Error( ex );
}
return true;
};
EDITOR_COMMAND.prototype.getMessage = function()
{
return this.__msg;
};
ns[ NS_EXPORT ]( EX_CLASS, "EDITOR_COMMAND", EDITOR_COMMAND );
})();

View File

@@ -4,10 +4,11 @@
/** @type {System.Debug} */
var debug = __import( "System.Debug" );
var VimError = __import( "Components.Vim.Error" );
var Mesg = __import( "Components.Vim.Message" );
// Private static
var PATTERN = "";
var PATTERN = [];
var ParsePattern = function( pattern )
{
@@ -23,28 +24,27 @@
break;
case "\\":
var tok = pattern[ ++ i ];
debug.Error( "Unknown escaped token: " + tok );
++ i;
if( "nrts.[]()^".indexOf( tok ) != -1 )
{
parsed += "\\" + tok;
}
else
{
throw new Error( "Missing token impl: \"" + tok + "\"" );
}
break;
default:
parsed += pattern[ i ];
}
}
var RegEx = null;
try
{
var RegEx = new RegExp( parsed, "gm" );
}
catch( ex )
{
debug.Error( ex );
}
// The root bracket as back ref 0
var RegEx = new RegExp( "(" + parsed + ")", "g" );
return RegEx;
};
/** @type {Components.Vim.Cursor.IAction} */
/** @type {Components.Vim.IAction} */
var FIND = function( Cursor )
{
/** @type {Components.Vim.Cursor} */
@@ -62,9 +62,37 @@
{
e.preventDefault();
if( p ) PATTERN = p;
if( p )
{
if( p.length < 2 )
{
if( PATTERN.length < 1 )
{
this.__msg = VimError( "E35" );
return true;
}
else p = PATTERN;
}
var search = ParsePattern( PATTERN );
PATTERN = p;
}
if( PATTERN.length < 1 )
{
this.__msg = VimError( "E35" );
return true;
}
var search;
try
{
search = ParsePattern( PATTERN );
}
catch( ex )
{
this.__msg = VimError( "EX1", ex.message );
return true;
}
var content = this.__cursor.feeder.content;
@@ -111,6 +139,7 @@
if( Hit == undefined )
{
this.__msg = VimError( "E486", PATTERN.slice( 1 ).join( "" ) );
}
else
{
@@ -123,5 +152,7 @@
return this.__msg;
};
__static_method( FIND, "Pattern", ParsePattern );
ns[ NS_EXPORT ]( EX_CLASS, "FIND", FIND );
})();

View File

@@ -23,7 +23,7 @@
}
};
/** @type {Components.Vim.Cursor.IAction} */
/** @type {Components.Vim.IAction} */
var INSERT = function( Cursor )
{
/** @type {Components.Vim.Cursor} */

View File

@@ -4,7 +4,7 @@
/** @type {System.Debug} */
var debug = __import( "System.Debug" );
/** @type {Components.Vim.Cursor.IAction} */
/** @type {Components.Vim.IAction} */
var PRINT_HEX = function( Cursor )
{
/** @type {Components.Vim.Cursor} */

View File

@@ -9,7 +9,7 @@
var Mesg = __import( "Components.Vim.Message" );
var occurence = __import( "System.utils.Perf.CountSubstr" );
/** @type {Components.Vim.Cursor.IAction} */
/** @type {Components.Vim.IAction} */
var PUT = function( Cursor )
{
/** @type {Components.Vim.Cursor} */

View File

@@ -3,7 +3,7 @@
var Mesg = __import( "Components.Vim.Message" );
/** @type {Components.Vim.Cursor.IAction} */
/** @type {Components.Vim.IAction} */
var REDO = function( Cursor )
{
/** @type {Components.Vim.Cursor} */

View File

@@ -0,0 +1,260 @@
(function(){
var ns = __namespace( "Components.Vim.Actions" );
/** @type {System.Debug} */
var debug = __import( "System.Debug" );
/** @type {Components.Vim.State.Stack} */
var Stack = __import( "Components.Vim.State.Stack" );
var VimError = __import( "Components.Vim.Error" );
var Mesg = __import( "Components.Vim.Message" );
var occurence = __import( "System.utils.Perf.CountSubstr" );
/** @type {Components.Vim.Actions.FIND} */
var FIND = ns[ NS_INVOKE ]( "FIND" );
var REPL_BEFORE = 0;
var REPL_OFFSET = 1;
var REPL_LENGTH = 2;
var ParseReplace = function( repl )
{
var parsed = "";
var l = repl.length;
var rStack = [ "" ]
for( var i = 1; i < l; i ++ )
{
switch( repl[ i ] )
{
case "^I":
parsed += "\t";
break;
case "\\":
i ++;
var j = repl[ i ];
if( "$nrt\\".indexOf( j ) != -1 )
{
parsed += JSON.parse( "\"\\" + j + "\"" );
break;
}
// 9 is shifted by 0
// which I think is more important
if( "012345678".indexOf( j ) != -1 )
{
rStack.push( parsed.length );
parsed += j;
}
else if( j == "9" )
{
throw new Error( "Back ref 9 is reserved for back ref 0" );
}
else
{
throw new Error( "Missing token impl: \"" + tok + "\"" );
}
break;
default:
parsed += repl[ i ];
}
}
rStack[0] = parsed;
return rStack;
};
/** @type {Components.Vim.IAction} */
var REPLACE = function( Cursor )
{
/** @type {Components.Vim.Cursor} */
this.__cursor = Cursor;
this.__msg = "";
this.__repl = "";
this.__replLen = 0;
this.__replCallback = this.__replCallback.bind( this );
this.__pOffset = 0;
this.__replacedGroups = [];
Cursor.suppressEvent();
};
REPLACE.prototype.dispose = function()
{
this.__cursor.unsuppressEvent();
};
REPLACE.prototype.handler = function( e, p, range )
{
e.preventDefault();
var search;
var spattern;
try
{
var slash = p.indexOf( "/", 0 );
var secSlash = p.indexOf( "/", slash + 1 );
if( slash == -1 )
{
this.__msg = VimError( "MISSING_FEATURE", "REPLACE %s" );
return true;
}
else if( secSlash == -1 )
{
search = FIND.Pattern( p );
spattern = p;
}
else
{
spattern = p.slice( slash, secSlash );
search = FIND.Pattern( spattern );
var thdSlash = p.indexOf( "/", secSlash + 1 );
if( thdSlash == -1 )
{
this.__repl = ParseReplace( p.slice( secSlash ) );
}
else
{
this.__repl = ParseReplace( p.slice( secSlash, thdSlash ) );
}
this.__replLen = this.__repl[0].length;
}
}
catch( ex )
{
this.__msg = VimError( "EX1", ex.message );
return true;
}
debug.Info( "Replace: " + search + ", [ " + this.__repl + " ]" );
var feeder = this.__cursor.feeder;
var content = feeder.content.slice( 0, -1 )
.replace( search, this.__replCallback ) + "\n";
if( !this.__replacedGroups.length )
{
this.__msg = VimError( "E486", spattern.join( "" ) );
}
feeder.content = content;
// Record this step for UNDO / REDO
this.__rec();
/* Move the cursor to last replaced line
var cur = this.__cursor;
var p = cur.aPos;
*/
feeder.pan();
feeder.softReset();
return true;
};
REPLACE.prototype.__replCallback = function()
{
var match = arguments[0];
var backRefs = Array.prototype.slice.call( arguments, 1, -2 );
var offset = this.__pOffset + arguments[ arguments.length - 2 ];
var replacedStr = "";
var replCand = this.__repl[0];
for( var i = 0; i < this.__replLen; i ++ )
{
var j = this.__repl.indexOf( i, 1 );
if( j == -1 )
{
replacedStr += replCand[ i ];
}
else
{
replacedStr += backRefs[ replCand[ this.__repl[ j ] ] ];
}
}
var rLen = replacedStr.length;
this.__pOffset += rLen - match.length;
var ReplObj = [];
ReplObj[ REPL_BEFORE ] = match;
ReplObj[ REPL_OFFSET ] = offset;
ReplObj[ REPL_LENGTH ] = rLen;
this.__replacedGroups.push( ReplObj );
return replacedStr;
};
REPLACE.prototype.__rec = function()
{
var stack = new Stack();
var reGroups = this.__replacedGroups;
var l = reGroups.length;
var cur = this.__cursor;
var feeder = cur.feeder;
stack.store( function()
{
var cont = feeder.content;
var newCont = "";
var st = 0;
var curStart = -1;
for( var i = 0; i < l; i ++ )
{
var grp = reGroups[ i ];
var RO = grp[ REPL_OFFSET ];
var RL = grp[ REPL_LENGTH ];
var RB = grp[ REPL_BEFORE ];
var NRL = RB.length;
newCont += cont.substring( st, RO ) + RB;
st = grp[ REPL_OFFSET ] + RL;
grp[ REPL_BEFORE ] = cont.substr( RO, RL );
grp[ REPL_OFFSET ] = newCont.length - NRL;
grp[ REPL_LENGTH ] = NRL;
if( curStart == -1 )
{
curStart = RO;
}
}
newCont += cont.substring( st );
feeder.content = newCont;
cur.moveTo( curStart );
feeder.pan();
} );
cur.rec.record( stack );
};
REPLACE.prototype.getMessage = function()
{
return this.__msg;
};
ns[ NS_EXPORT ]( EX_CLASS, "REPLACE", REPLACE );
})();

View File

@@ -3,7 +3,7 @@
var Mesg = __import( "Components.Vim.Message" );
/** @type {Components.Vim.Cursor.IAction} */
/** @type {Components.Vim.IAction} */
var UNDO = function( Cursor )
{
/** @type {Components.Vim.Cursor} */

View File

@@ -6,12 +6,12 @@
var Mesg = __import( "Components.Vim.Message" );
/** @type {Components.Vim.Cursor.IAction} */
/** @type {Components.Vim.IAction} */
var YANK = ns[ NS_INVOKE ]( "YANK" );
/** @type {Components.Vim.Cursor.IAction} */
/** @type {Components.Vim.IAction} */
var DELETE = ns[ NS_INVOKE ]( "DELETE" );
/** @type {Components.Vim.Cursor.IAction} */
/** @type {Components.Vim.IAction} */
var VISUAL = function( Cursor )
{
this.__reset( Cursor );

View File

@@ -9,7 +9,7 @@
var occurence = __import( "System.utils.Perf.CountSubstr" );
/** @type {Components.Vim.Cursor.IAction} */
/** @type {Components.Vim.IAction} */
var YANK = function( Cursor )
{
/** @type {Components.Vim.Cursor} */