From 7a29206edb243aa0efbe034645b6d678e56e5722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=9F=E9=85=8C=20=E9=B5=AC=E5=85=84?= Date: Sun, 22 Jul 2018 01:06:22 +0800 Subject: [PATCH] Dispatch a "change" Event on quit to notify other handlers --- botanjs/src/Components/Vim/Actions/VISUAL.js | 50 ++++++++++++-------- botanjs/src/Components/Vim/LineFeeder.js | 2 +- botanjs/src/Components/Vim/State/Marks.js | 20 +++++++- botanjs/src/Components/Vim/VimArea.js | 1 + 4 files changed, 50 insertions(+), 23 deletions(-) diff --git a/botanjs/src/Components/Vim/Actions/VISUAL.js b/botanjs/src/Components/Vim/Actions/VISUAL.js index bb75ba9..49940cb 100644 --- a/botanjs/src/Components/Vim/Actions/VISUAL.js +++ b/botanjs/src/Components/Vim/Actions/VISUAL.js @@ -48,9 +48,19 @@ return i; }; - /** marker @type {Components.Vim.State.Marks} */ - var MarkSelected = function( marker, PStart, PEnd ) + var lineInfo = function( c ) { + return { + lineNum: c.getLine().lineNum + , aX: c.aX + }; + }; + + /** marker @type {Components.Vim.State.Marks} */ + var MarkSelected = function( marker, s, e ) + { + marker.set( "<", s.lineNum, s.aX ); + marker.set( ">", e.lineNum, e.aX ); }; /** @type {Components.Vim.IAction} */ @@ -68,14 +78,12 @@ /** @type {Components.Vim.Cursor} */ this.__cursor = Cursor; - var s = { - lineNum: Cursor.getLine().lineNum - , X: Cursor.X - , aPos: Cursor.aPos - , pstart: Cursor.PStart - }; + var s = lineInfo( Cursor ); - s.aStart = s.aPos - Cursor.aX; + s.aPos = Cursor.aPos; + s.X = Cursor.X; + s.pStart = Cursor.PStart; + s.aStart = s.aPos - s.aX; Cursor.suppressEvent(); Cursor.lineEnd( true ); @@ -220,7 +228,7 @@ Action.dispose(); cur.unsuppressEvent(); - startLine.pstart = cur.PStart; + startLine.pStart = cur.PStart; return true; } @@ -253,13 +261,13 @@ debug.Info( "Min aPos: " + minAp, "Max aPos: " + maxAp ); - var pstart = startLine.X; + var pStart = startLine.X; var nstart = cur.PStart; // highlight from the start if( startLine.aPos < minAp ) { - pstart = 0; + pStart = 0; if( this.__mode == MODE_LINE ) { @@ -276,14 +284,14 @@ // highlight from the end else if( maxAp < startLine.aPos ) { - pstart = -2; + pStart = -2; var i = 0; do { if( line.placeholder ) break; if( i <= feeder.moreAt ) { - pstart += line.toString().length + 1; + pStart += line.toString().length + 1; } i ++; } @@ -295,11 +303,11 @@ if( this.__mode == MODE_LINE ) { cur.suppressEvent(); - pstart = 0; + pStart = 0; if( currAp < startLine.aPos ) { - pstart = -1; + pStart = -1; l ++; cur.lineStart(); @@ -316,7 +324,7 @@ cur.lineStart(); nstart = cur.PStart; cur.lineEnd( true ); - pstart = cur.PStart; + pStart = cur.PStart; l = line.lineNum; } @@ -334,12 +342,12 @@ do { if( line.lineNum == l || line.placeholder ) break; - pstart += line.toString().length + 1; + pStart += line.toString().length + 1; } while( line = line.next ); } - var prevPos = pstart; + var prevPos = pStart; var newPos = nstart; var posDiff = newPos - prevPos; @@ -351,12 +359,14 @@ if( 0 <= posDiff ) { newPos = newPos + 1; + MarkSelected( e.target.marks, startLine, lineInfo( cur ) ); } // e<--s else if( posDiff < 0 ) { prevPos += posDiff; - newPos = pstart + 1; + newPos = pStart + 1; + MarkSelected( e.target.marks, lineInfo( cur ), startLine ); } cur.PStart = prevPos; diff --git a/botanjs/src/Components/Vim/LineFeeder.js b/botanjs/src/Components/Vim/LineFeeder.js index b939daf..e50e6b9 100644 --- a/botanjs/src/Components/Vim/LineFeeder.js +++ b/botanjs/src/Components/Vim/LineFeeder.js @@ -240,7 +240,7 @@ if( this.__content[ i + 1 ] == "\t" ) tabs ++; i ++; } - while( i < l ) + while( i < l ); if( tabs ) { diff --git a/botanjs/src/Components/Vim/State/Marks.js b/botanjs/src/Components/Vim/State/Marks.js index 9f5412b..b63545a 100644 --- a/botanjs/src/Components/Vim/State/Marks.js +++ b/botanjs/src/Components/Vim/State/Marks.js @@ -6,7 +6,7 @@ var beep = __import( "Components.Vim.Beep" ); - var Keys = "'ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxy\"[]^.<>"; + var Keys = "'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\"[]^.<>"; var Marks = function() { @@ -25,12 +25,28 @@ return true; }; + Marks.prototype.save = function() + { + var saved = {}; + // A-z + for( var i = 1; i < 53; i ++ ) + { + var k = Keys[ i ]; + if( this.__marks[ k ] != undefined ) + { + saved[ k ] = this.__marks[ k ]; + } + } + + return saved; + }; + Marks.prototype.get = function( t ) { return this.__marks[ t ]; }; - __readOnly( Marks, "Keys", function() { return Keys; } ); + __const( Marks, "Keys", Keys ); ns[ NS_EXPORT ]( EX_CLASS, "Marks", Marks ); diff --git a/botanjs/src/Components/Vim/VimArea.js b/botanjs/src/Components/Vim/VimArea.js index 6a08d95..08e79fb 100644 --- a/botanjs/src/Components/Vim/VimArea.js +++ b/botanjs/src/Components/Vim/VimArea.js @@ -381,6 +381,7 @@ stage.element.value = this.content; delete Insts[ this.__instIndex ]; + stage.dispatchEvent( new Event( "change" ) ); }; __readOnly( VimArea.prototype, "index", function()