penguin/AstroJS

Javascript framework for my blog

commit 97480cd0c61e50b3e5e5aaeec65041b6f6b84b19

author斟酌 鵬兄 <tgckpg@gmail.com>
date2016-03-14T15:28:30Z
subjectwordwrap panning for j
commit 97480cd0c61e50b3e5e5aaeec65041b6f6b84b19
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2016-03-14T15:28:30Z

    wordwrap panning for j
---
 botanjs/src/Components/Vim/Cursor.js     | 39 +++++++++++++++++++++-----------
 botanjs/src/Components/Vim/LineBuffer.js | 11 +++++++++
 botanjs/src/Components/Vim/VimArea.js    |  7 ++++--
 3 files changed, 42 insertions(+), 15 deletions(-)

diff --git a/botanjs/src/Components/Vim/Cursor.js b/botanjs/src/Components/Vim/Cursor.js
index 542051c..b9caecf 100644
--- a/botanjs/src/Components/Vim/Cursor.js
+++ b/botanjs/src/Components/Vim/Cursor.js
@@ -46,18 +46,12 @@
 				if( line.next && line.next.placeholder )
 					break LineLoop;
 
-				if( line.next && line.next.br )
-				{
-					offset += line.toString().length + 1;
-					line = line.next;
-					break;
-				}
-				else
-				{
-					offset += line.toString().length + 1;
-				}
-
+				// Using toString because tab is 1 byte 
+				// but variable width
+				offset += line.toString().length + 1;
 				line = line.next;
+
+				if( line && line.br ) break;
 			}
 		}
 
@@ -142,6 +136,7 @@
 	Cursor.prototype.moveY = function( d )
 	{
 		var Y = this.Y;
+		var line;
 
 		Y += d;
 		if( Y < 0 )
@@ -152,13 +147,16 @@
 		{
 			var feeder = this.feeder;
 			var lastLine = feeder.lastBuffer.lineNum;
+			var lineShift = Y - feeder.moreAt;
 
 			var i = 0;
 			while( true )
 			{
-				feeder.pan( undefined, Y - feeder.moreAt + i );
+				feeder.pan( undefined, lineShift + i );
 
-				// If it is the same line we keep scrolling it
+				// if it turns out to be the same line
+				// before after panning
+				// we keep scrolling it ( panning )
 				// until the entire line cosumes the screen
 				if( feeder.lastBuffer.lineNum == lastLine )
 				{
@@ -167,6 +165,10 @@
 				else break;
 			}
 
+			// The line number cursor need to be in
+			Y = lastLine + lineShift;
+
+			// Calculate the visual line position
 			for( i = 0, line = feeder.firstBuffer;
 				line != feeder.lastBuffer;
 				line = line.next )
@@ -184,6 +186,17 @@
 
 			return;
 		}
+		else if ( this.Y < Y )
+		{
+			// If panning is forward
+			// and next line does not exists
+			line = this.getLine().nextLine;
+			if( !line || line.placeholder )
+			{
+				// do nothing
+				return;
+			}
+		}
 
 		this.Y = Y;
 
diff --git a/botanjs/src/Components/Vim/LineBuffer.js b/botanjs/src/Components/Vim/LineBuffer.js
index 05a7f5c..51c9861 100644
--- a/botanjs/src/Components/Vim/LineBuffer.js
+++ b/botanjs/src/Components/Vim/LineBuffer.js
@@ -33,6 +33,7 @@
 		this.lineNum = n;
 		if( content == undefined || content === "" )
 		{
+			this.lineNum = ++n;
 			this.content = "~";
 			this.br = true;
 			this.placeholder = true;
@@ -107,6 +108,16 @@
 		return this.content || " ";
 	};
 
+	__readOnly( LineBuffer.prototype, "nextLine", function()
+	{
+		var line = this;
+		var thisLine = this.lineNum;
+
+		while( ( line = line.next ) && line.lineNum == thisLine );
+
+		return line;
+	} );
+
 	__readOnly( LineBuffer.prototype, "visualLines", function()
 	{
 		var lines = [ this ];
diff --git a/botanjs/src/Components/Vim/VimArea.js b/botanjs/src/Components/Vim/VimArea.js
index bf3105b..a030595 100644
--- a/botanjs/src/Components/Vim/VimArea.js
+++ b/botanjs/src/Components/Vim/VimArea.js
@@ -149,12 +149,15 @@
 		var r = this.rows;
 		var c = this.cols;
 
+		// StatusFeeder always consumes at least 1 line
+		var cRange = r - 1;
+
 		// Content feeder
-		var cfeeder = new LineFeeder( r, c );
+		var cfeeder = new LineFeeder( cRange, c );
 
 		cfeeder.init( content );
 
-		// Status feeder
+		// Status can consumes up to full screen, I think
 		sfeeder = new LineFeeder( r, c );
 		sfeeder.setRender( false );