penguin/AstroJS

Javascript framework for my blog

commit 9da78e2711cad304da547fb09cb128088a13ffe7

author斟酌 鵬兄 <tgckpg@gmail.com>
date2017-11-16T04:25:50Z
subjectImprove the guessing of tabstop when file is in space
commit 9da78e2711cad304da547fb09cb128088a13ffe7
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2017-11-16T04:25:50Z

    Improve the guessing of tabstop when file is in space
---
 botanjs/src/Components/Vim/Actions/SHIFT_LINES.js | 74 +++++++++++++----------
 1 file changed, 41 insertions(+), 33 deletions(-)

diff --git a/botanjs/src/Components/Vim/Actions/SHIFT_LINES.js b/botanjs/src/Components/Vim/Actions/SHIFT_LINES.js
index 606e17d..a9c980b 100644
--- a/botanjs/src/Components/Vim/Actions/SHIFT_LINES.js
+++ b/botanjs/src/Components/Vim/Actions/SHIFT_LINES.js
@@ -91,7 +91,6 @@
 					{
 						end += ( nline - 1 );
 					}
-					console.log( start, end );
 				}
 
 				if( currAp < sp )
@@ -188,53 +187,62 @@
 			if( 1 < l )
 			{
 				debug.Info( "Guessing the tabstop:" );
-				var tabOccr = 0;
-				var spOccr = 0;
+				var lineTabs = 0;
+				var lineSpaces = 0;
 
 				// Guess indent
-				var tabStat = [];
+				var spStat = {};
+				var spIndents = [];
 
 				for( var i = 0; i < l; i ++ )
 				{
 					var ind = indents[ i ];
-					var indNext = indents[ i + 1 ];
-					tabOccr += occurence( ind, "\t" );
-					spOccr += occurence( ind, " " );
-					var d = indNext.length - ind.length;
-					if( d == 0 ) continue;
-
-					d = d < 0 ? -d : d;
-
-					if( !tabStat[ d ] ) tabStat[ d ] = 0;
+					var k = 0;
+					if( ind[0] == " " )
+					{
+						lineSpaces ++;
+						k = occurence( ind, " " );
 
-					tabStat[ d ] ++;
+						if( spIndents.indexOf( k ) == -1 )
+						{
+							spIndents.push( k );
+							spStat[ k ] = ind;
+						}
+					}
+					else
+					{
+						lineTabs ++;
+					}
 				}
 
-				var upperDiff = 0;
-				var indentCLen = 0;
-				for( var i in tabStat )
+				if( lineTabs < lineSpaces )
 				{
-					i = Number( i );
-					var p = tabStat[ i ];
-					if( upperDiff < p )
+					spIndents.sort(function( a, b ) { return a - b; });
+					var rHCF = 0;
+					for( var i = 0, l = spIndents.length; i < l; i ++ )
 					{
-						upperDiff = p;
-						indentCLen = i;
-					}
-				}
+						var spIdx = Number( spIndents[ i ] );
+						var nHCF = 1;
 
-				spOccr /= indentCLen;
+						// Forward count number of factors
+						for( var j = i + 1; j < l; j ++ )
+						{
+							if( spIndents[ j ].length % spIdx == 0 )
+							{
+								nHCF ++;
+							}
+						}
 
-				if( tabOccr < spOccr )
-				{
-					indentChar = "";
-					for( var i = 0; i < indentCLen; i ++ ) indentChar += " ";
+						if( rHCF < nHCF )
+						{
+							rHCF = nHCF;
+							indentChar = spStat[ spIdx ];
+						}
+					}
 				}
 
-				tabwidth = indentCLen;
-
-				debug.Info( "\tTab count: " + tabOccr );
-				debug.Info( "\tSpace count: " + spOccr );
+				debug.Info( "\tTab lines: " + lineTabs );
+				debug.Info( "\tSpace lines: " + lineSpaces );
 				debug.Info( "\ti.e. indent using " + JSON.stringify( indentChar ) );
 			}
 			else