Skip to content

Commit

Permalink
TextBuffer refacto with \n at the end of line done
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Ronvel committed Aug 17, 2018
1 parent 30cefe6 commit e188ac0
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 48 deletions.
13 changes: 10 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
'node': true
} ,
'parserOptions': {
'ecmaVersion': 8
'ecmaVersion': 2018
} ,
'extends': [ 'eslint:recommended' ] ,
'rules': {
Expand Down Expand Up @@ -58,7 +58,10 @@ module.exports = {
// Indent & spaces (general)
'indent': [ 'error' , 'tab' , {
'SwitchCase': 1 ,
'MemberExpression': 0 ,
'MemberExpression': 1 ,
} ] ,
'newline-per-chained-call': [ 'error', {
'ignoreChainWithDepth': 2
} ] ,
'no-multi-spaces': 'off' ,
'block-spacing': 'error' ,
Expand All @@ -68,7 +71,11 @@ module.exports = {
} ] ,
'no-whitespace-before-property': 'error' ,
'space-before-blocks': 'error' ,
'space-before-function-paren': [ 'error' , 'never' ] ,
'space-before-function-paren': [ 'error' , {
'anonymous': 'never',
'named': 'never',
'asyncArrow': 'always'
} ] ,
'space-infix-ops': 'error' ,
'space-unary-ops': [ 'error' , {
'words': true ,
Expand Down
143 changes: 98 additions & 45 deletions lib/TextBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ TextBuffer.prototype.moveForward = function moveForward( justSkipFiller ) {
var oldCx = this.cx ,
currentLine = this.buffer[ this.cy ] ;

if ( justSkipFiller && ( ! currentLine || ! currentLine[ this.cx ] || ! currentLine[ this.cx ].filler || currentLine[ this.cx ].char !== '\n' ) ) { return ; }
//if ( justSkipFiller && ( ! currentLine || ! currentLine[ this.cx ] || ! currentLine[ this.cx ].filler || currentLine[ this.cx ].char !== '\n' ) ) { return ; }
if ( justSkipFiller && ( ! currentLine || ! currentLine[ this.cx ] || ! currentLine[ this.cx ].filler ) ) { return ; }

for ( ;; ) {
if ( ! currentLine || this.cx + 1 > currentLine.length || ( this.cx < currentLine.length && currentLine[ this.cx ].char === '\n' ) ) {
Expand All @@ -352,7 +353,7 @@ TextBuffer.prototype.moveForward = function moveForward( justSkipFiller ) {
}

this.cx ++ ;

if ( ! currentLine[ this.cx ] || ! currentLine[ this.cx ].filler ) { break ; }
}

Expand All @@ -365,7 +366,8 @@ TextBuffer.prototype.moveBackward = function moveBackward( justSkipFiller ) {
var lineLength ,
currentLine = this.buffer[ this.cy ] ;

if ( justSkipFiller && ( ! currentLine || ! currentLine[ this.cx ] || ! currentLine[ this.cx ].filler || currentLine[ this.cx ].char !== '\n' ) ) { return ; }
//if ( justSkipFiller && ( ! currentLine || ! currentLine[ this.cx ] || ! currentLine[ this.cx ].filler || currentLine[ this.cx ].char !== '\n' ) ) { return ; }
if ( justSkipFiller && ( ! currentLine || ! currentLine[ this.cx ] || ! currentLine[ this.cx ].filler ) ) { return ; }

for ( ;; ) {
lineLength = currentLine ? currentLine.length : 0 ;
Expand All @@ -392,7 +394,7 @@ TextBuffer.prototype.moveBackward = function moveBackward( justSkipFiller ) {

TextBuffer.prototype.moveToEndOfLine = function moveToEndOfLine() {
var currentLine = this.buffer[ this.cy ] ;

if ( ! currentLine ) {
this.cx = 0 ;
}
Expand All @@ -408,13 +410,20 @@ TextBuffer.prototype.moveToEndOfLine = function moveToEndOfLine() {

TextBuffer.prototype.moveInBound = function moveInBound( ignoreCx ) {
var currentLine = this.buffer[ this.cy ] ;

if ( this.cy > this.buffer.length ) { this.cy = this.buffer.length ; }

if ( ignoreCx ) { return ; }

if ( ! currentLine ) { this.cx = 0 ; }
else if ( this.cx > currentLine.length ) { this.cx = currentLine.length ; }
if ( ! currentLine ) {
this.cx = 0 ;
}
else if ( currentLine.length && currentLine[ currentLine.length - 1 ].char === '\n' ) {
if ( this.cx > currentLine.length - 1 ) { this.cx = currentLine.length - 1 ; }
}
else if ( this.cx > currentLine.length ) {
this.cx = currentLine.length ;
}
} ;


Expand Down Expand Up @@ -445,33 +454,64 @@ TextBuffer.prototype.insert = function insert( text , attr ) {
// Internal API:
// Insert inline chars (no control chars)
TextBuffer.prototype.inlineInsert = function inlineInsert( text , attr ) {
var currentLineLength , tabIndex , fillSize ;
var currentLine , currentLineLength , hasNL , nlCell , tabIndex , fillSize ;

this.moveForward( true ) ; // just skip filler char

// Should come after moving forward
// Should come after moving forward (rely on this.cx)
var cells = string.unicode.toCells( Cell , text , this.tabWidth , this.cx , attr ) ;

if ( this.buffer[ this.cy ] === undefined ) { this.buffer[ this.cy ] = [] ; }
// Is this a new line?
if ( this.cy >= this.buffer.length ) {
// Create all missing lines, if any
while ( this.buffer.length < this.cy ) {
this.buffer.push( [ new Cell( '\n' , this.emptyCellAttr ) ] ) ;
}

// Add a '\n' to the last line, if it is missing
if (
this.cy && (
! this.buffer[ this.cy - 1 ].length ||
this.buffer[ this.cy - 1 ][ this.buffer[ this.cy - 1 ].length - 1 ].char !== '\n'
)
) {
this.buffer[ this.cy - 1 ].push( new Cell( '\n' , this.emptyCellAttr ) ) ;
}

currentLineLength = this.buffer[ this.cy ].length ;
this.buffer[ this.cy ] = [] ;
}

currentLine = this.buffer[ this.cy ] ;
currentLineLength = currentLine.length ;
hasNL = currentLineLength && currentLine[ currentLineLength - 1 ].char === '\n' ;

// Apply
if ( this.cx === currentLineLength ) {
this.buffer[ this.cy ].push( ... cells ) ;
if ( hasNL ) {
currentLine.splice( currentLineLength - 1 , 0 , new Cell( ' ' , this.emptyCellAttr ) , ... cells ) ;
}
else {
currentLine.push( ... cells ) ;
}
}
else if ( this.cx < currentLineLength ) {
this.buffer[ this.cy ].splice( this.cx , 0 , ... cells ) ;
currentLine.splice( this.cx , 0 , ... cells ) ;
}
// this.cx > currentLineLength
else if ( hasNL ) {
fillSize = this.cx - currentLineLength + 1 ;
nlCell = currentLine.pop() ;
while ( fillSize -- ) { currentLine.push( new Cell( ' ' , this.emptyCellAttr ) ) ; }
currentLine.push( ... cells , nlCell ) ;
}
else {
// if ( this.cx > currentLineLength )
fillSize = this.cx - currentLineLength ;
while ( fillSize -- ) { this.buffer[ this.cy ].push( new Cell( ' ' , this.emptyCellAttr ) ) ; }
this.buffer[ this.cy ].push( ... cells ) ;
while ( fillSize -- ) { currentLine.push( new Cell( ' ' , this.emptyCellAttr ) ) ; }
currentLine.push( ... cells ) ;
}

// Patch tab if needed
tabIndex = this.indexOfCharInLine( this.buffer[ this.cy ] , '\t' , this.cx ) ;
tabIndex = this.indexOfCharInLine( currentLine , '\t' , this.cx ) ;
this.cx += cells.length ;
if ( tabIndex !== -1 ) { this.reTabLine( tabIndex ) ; }
} ;
Expand All @@ -496,7 +536,7 @@ TextBuffer.prototype.indexOfCharInLine = function indexOfCharInLine( line , char

// Delete chars
TextBuffer.prototype.delete = function delete_( count ) {
var inlineCount ;
var currentLine , inlineCount ;

if ( count === undefined ) { count = 1 ; }

Expand All @@ -507,27 +547,32 @@ TextBuffer.prototype.delete = function delete_( count ) {
count -- ;
}


while ( count > 0 ) {
currentLine = this.buffer[ this.cy ] ;

// If we are already at the end of the buffer...
if ( this.cy >= this.buffer.length ||
( this.cy === this.buffer.length - 1 && this.cx >= this.buffer[ this.cy ].length ) ) {
( this.cy === this.buffer.length - 1 && this.cx >= currentLine.length ) ) {
return ;
}

if ( this.buffer[ this.cy ] ) {
if ( currentLine ) {
// If the cursor is too far away, move it at the end of the line
if ( this.cx > this.buffer[ this.cy ].length ) { this.cx = this.buffer[ this.cy ].length ; }

// Compute inline delete
//inlineCount = Math.min( count , this.buffer[ this.cy ].length - this.cx ) ;
inlineCount = this.countInlineForward( count ) ;

// Apply inline delete
if ( inlineCount > 0 ) {
this.buffer[ this.cy ].splice( this.cx , inlineCount ) ;
if ( this.cx > currentLine.length ) { this.cx = currentLine.length ; }

if ( currentLine[ this.cx ] && currentLine[ this.cx ].char !== '\n' ) {
// Compute inline delete
//inlineCount = Math.min( count , currentLine.length - this.cx ) ;
inlineCount = this.countInlineForward( count ) ;

// Apply inline delete
if ( inlineCount > 0 ) {
currentLine.splice( this.cx , inlineCount ) ;
}

count -= inlineCount ;
}

count -= inlineCount ;
}

if ( count > 0 ) {
Expand All @@ -537,7 +582,7 @@ TextBuffer.prototype.delete = function delete_( count ) {
}

// Patch tab if needed
//tabIndex = this.buffer[ this.cy ].indexOf( '\t' , this.cx ) ;
//tabIndex = currentLine.indexOf( '\t' , this.cx ) ;
//if ( tabIndex !== -1 ) { this.reTabLine( tabIndex ) ; }
this.reTabLine() ; // Do it every time, before finding a better way to do it
} ;
Expand All @@ -548,7 +593,7 @@ TextBuffer.prototype.delete = function delete_( count ) {

// Delete backward chars
TextBuffer.prototype.backDelete = function backDelete( count ) {
var inlineCount , tabIndex ;
var currentLine , inlineCount , tabIndex ;

if ( count === undefined ) { count = 1 ; }

Expand All @@ -559,24 +604,32 @@ TextBuffer.prototype.backDelete = function backDelete( count ) {
//count -- ; // do not downcount: the cursor is always on a \x00 before deleting a \t
}


while ( count > 0 ) {
currentLine = this.buffer[ this.cy ] ;

// If we are already at the begining of the buffer...
if ( this.cy === 0 && this.cx === 0 ) { return ; }

if ( this.buffer[ this.cy ] ) {
if ( currentLine ) {

// If the cursor is to far away, move it at the end of the line, it will cost one 'count'
if ( this.cx > this.buffer[ this.cy ].length ) {
this.cx = this.buffer[ this.cy ].length ;
if ( this.cx > currentLine.length ) {
if ( currentLine.length && currentLine[ currentLine.length - 1 ].char === '\n' ) { this.cx = currentLine.length - 1 ; }
else { this.cx = currentLine.length ; }

count -- ;
}
else if ( this.cx && this.cx === currentLine.length && currentLine[ currentLine.length - 1 ].char === '\n' ) {
this.cx = currentLine.length - 1 ;
}

// Compute inline delete
inlineCount = this.countInlineBackward( count ) ;

// Apply inline delete
if ( inlineCount > 0 ) {
this.buffer[ this.cy ].splice( this.cx - inlineCount , inlineCount ) ;
currentLine.splice( this.cx - inlineCount , inlineCount ) ;
this.cx -= inlineCount ;
}

Expand All @@ -585,14 +638,14 @@ TextBuffer.prototype.backDelete = function backDelete( count ) {

if ( count > 0 ) {
this.cy -- ;
this.cx = this.buffer[ this.cy ] ? this.buffer[ this.cy ].length : 0 ;
this.cx = currentLine ? currentLine.length : 0 ;
this.joinLine( true ) ;
count -- ;
}
}

// Patch tab if needed
//tabIndex = this.buffer[ this.cy ].indexOf( '\t' , this.cx ) ;
//tabIndex = currentLine.indexOf( '\t' , this.cx ) ;
//if ( tabIndex !== -1 ) { this.reTabLine( tabIndex ) ; }
this.reTabLine( tabIndex ) ; // Do it every time, before finding a better way to do it
} ;
Expand Down Expand Up @@ -642,7 +695,7 @@ TextBuffer.prototype.newLine = function newLine( internalCall ) {
}

currentLine.push( new Cell( '\n' , this.emptyCellAttr ) ) ;

this.buffer.splice( this.cy + 1 , 0 , nextLine ) ;

this.cx = 0 ;
Expand All @@ -664,9 +717,9 @@ TextBuffer.prototype.joinLine = function joinLine( internalCall ) {

if ( this.buffer[ this.cy ] === undefined ) { this.buffer[ this.cy ] = [] ; }
if ( this.buffer[ this.cy + 1 ] === undefined ) { this.buffer[ this.cy + 1 ] = [] ; }

currentLine = this.buffer[ this.cy ] ;

if ( currentLine.length && currentLine[ currentLine.length - 1 ].char === '\n' ) {
// Remove the last '\n' if any
currentLine.length -- ;
Expand Down Expand Up @@ -735,18 +788,18 @@ TextBuffer.prototype.drawCursor = function drawCursor( options ) {

TextBuffer.prototype.lineWrapper = function lineWrapper( width ) {
var output = [] ;

this.buffer.forEach( line => {
if ( line.length <= width ) {
output.push( line ) ;
return ;
}

for ( let offset = 0 ; offset < line.length ; offset += width ) {
output.push( line.slice( offset , offset + width ) ) ;
}
} ) ;

return output ;
} ;

Expand Down

0 comments on commit e188ac0

Please sign in to comment.