Previous approach had performance issue, fixing it

This commit is contained in:
斟酌 鵬兄 2016-03-30 06:17:37 +08:00
parent 799a911e06
commit 564aef86aa
3 changed files with 51 additions and 51 deletions

View File

@ -292,18 +292,11 @@
break; break;
} }
var d = 1; ccur.moveTo(
var at = bracketMatch.close; bracketMatch.selected == bracketMatch.close
if( bracketMatch.selected == at ) ? bracketMatch.open
{ : bracketMatch.close
d = -1; );
at = bracketMatch.open;
}
while( ccur.aPos != at )
{
ccur.moveX( d, true );
}
break; break;
case T: // To case T: // To

View File

@ -88,6 +88,37 @@
// Set by VimArea // Set by VimArea
Cursor.prototype.Vim; Cursor.prototype.Vim;
// Move to an absolute position
Cursor.prototype.moveTo = function( aPos )
{
var content = this.feeder.content;
var lastLineNum = this.getLine().lineNum;
var expLineNum = 0;
var lineStart = 0;
for( var i = content.indexOf( "\n" ); 0 <= i ; i = content.indexOf( "\n", i ) )
{
if( aPos <= i )
{
break;
}
lineStart = i;
i ++;
expLineNum ++;
}
var jumpY = expLineNum - lastLineNum;
var jumpX = aPos < lineStart ? lineStart - aPos : aPos - lineStart;
if( jumpY ) this.moveY( jumpY );
if( 0 < this.getLine().lineNum && lineStart <= aPos ) jumpX --;
this.moveX( - Number.MAX_VALUE );
this.moveX( jumpX );
};
// 0 will be treated as default ( 1 ) // 0 will be treated as default ( 1 )
Cursor.prototype.moveX = function( d, penetrate, phantomSpace ) Cursor.prototype.moveX = function( d, penetrate, phantomSpace )
{ {
@ -120,38 +151,12 @@
// Motion includes empty lines before cursor end // Motion includes empty lines before cursor end
if( ( phantomSpace && cLen - 1 <= x ) || ( cLen == 1 && c == undefined ) ) if( ( phantomSpace && cLen - 1 <= x ) || ( cLen == 1 && c == undefined ) )
{ {
if( 0 < d ) x = 0 < d ? cLen - 1 : 0;
{
x = cLen - 1;
if( penetrate )
{
this.X = 0;
this.moveY( 1 );
return;
}
}
else
{
x = 0;
}
} }
// ( 2 < cLen ) motion excludes empty lines at cursor end // ( 2 < cLen ) motion excludes empty lines at cursor end
else if( ( 2 <= cLen && x == cLen - 1 && c == " " ) || c == undefined ) else if( ( 2 <= cLen && x == cLen - 1 && c == " " ) || c == undefined )
{ {
if( 0 < d ) x = 0 < d ? cLen - 2 : 0;
{
x = cLen - 2;
if( penetrate )
{
this.X = 0;
this.moveY( 1 );
return;
}
}
else
{
x = 0;
}
} }
else if( c == "\n" ) else if( c == "\n" )
{ {
@ -199,7 +204,7 @@
this.feeder.dispatcher.dispatchEvent( new BotanEvent( "VisualUpdate" ) ); this.feeder.dispatcher.dispatchEvent( new BotanEvent( "VisualUpdate" ) );
}; };
Cursor.prototype.moveY = function( d, penetrate ) Cursor.prototype.moveY = function( d )
{ {
var i; var i;
var Y = this.Y + d; var Y = this.Y + d;
@ -208,7 +213,7 @@
if( Y < 0 ) if( Y < 0 )
{ {
feeder.pan( undefined, d ); feeder.pan( undefined, Y );
this.Y = 0; this.Y = 0;
this.moveX(); this.moveX();
@ -222,11 +227,7 @@
{ {
var feeder = this.feeder; var feeder = this.feeder;
if( penetrate ) if( feeder.linesTotal < Y )
{
feeder.pan( undefined, Y - moreAt );
}
else if( feeder.linesTotal < Y )
{ {
while( !feeder.EOF ) while( !feeder.EOF )
{ {
@ -244,18 +245,22 @@
if( !feeder.EOF ) if( !feeder.EOF )
feeder.pan( undefined, lineShift ); feeder.pan( undefined, lineShift );
// The line number cursor need to be in
Y = thisLine + d;
// if it turns out to be the same line // if it turns out to be the same line
// OR the cursor can not reside on the needed line
// before after panning // before after panning
// we keep scrolling it ( panning ) // we keep scrolling it ( panning )
// until the entire line cosumes the screen // until the entire line cosumes the screen
while( !feeder.EOF && feeder.lastBuffer.lineNum == lastLine ) while( !feeder.EOF && (
feeder.lastBuffer.lineNum == lastLine
|| feeder.lastBuffer.lineNum < Y
) )
{ {
feeder.pan( undefined, 1 ); feeder.pan( undefined, 1 );
} }
// The line number cursor need to be in
Y = thisLine + d;
i = this.Y; i = this.Y;
this.Y = 0; this.Y = 0;
// Calculate the visual line position "i" // Calculate the visual line position "i"

View File

@ -10,6 +10,8 @@ Components.Vim.Cursor.action;
/** @type {Components.Vim.State.Recorder} */ /** @type {Components.Vim.State.Recorder} */
Components.Vim.Cursor.rec; Components.Vim.Cursor.rec;
/** @type Function */
Components.Vim.Cursor.moveTo;
/** @type Function */ /** @type Function */
Components.Vim.Cursor.moveX; Components.Vim.Cursor.moveX;
/** @type Function */ /** @type Function */