@@ -2851,6 +2851,12 @@ bool App::loadFileFromPath(
28512851 openFileFromPath ( path );
28522852 } else {
28532853 UITab* tab = mSplitter ->isDocumentOpen ( path );
2854+ auto onLoadedRestoreState = [this , onLoaded]( UICodeEditor* codeEditor,
2855+ const std::string& path ) {
2856+ restoreClosedDocumentState ( codeEditor, path );
2857+ if ( onLoaded )
2858+ onLoaded ( codeEditor, path );
2859+ };
28542860
28552861 if ( tab && tab->getOwnedWidget ()->isType ( UI_TYPE_CODEEDITOR ) ) {
28562862 UICodeEditor* editor = tab->getOwnedWidget ()->asType <UICodeEditor>();
@@ -2863,9 +2869,9 @@ bool App::loadFileFromPath(
28632869 }
28642870 } else {
28652871 if ( inNewTab ) {
2866- mSplitter ->loadAsyncFileFromPathInNewTab ( path, onLoaded );
2872+ mSplitter ->loadAsyncFileFromPathInNewTab ( path, onLoadedRestoreState );
28672873 } else {
2868- mSplitter ->loadAsyncFileFromPath ( path, codeEditor, onLoaded );
2874+ mSplitter ->loadAsyncFileFromPath ( path, codeEditor, onLoadedRestoreState );
28692875 }
28702876 }
28712877 }
@@ -3180,13 +3186,14 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) {
31803186 updateNonUniqueTabTitles ();
31813187 } );
31823188
3183- editor->on ( Event::OnDocumentClosed, [this ]( const Event* event ) {
3189+ editor->on ( Event::OnDocumentClosed, [this , editor ]( const Event* event ) {
31843190 if ( !appInstance )
31853191 return ;
31863192 const DocEvent* docEvent = static_cast <const DocEvent*>( event );
31873193 std::string dir ( FileSystem::fileRemoveFileName ( docEvent->getDoc ()->getFilePath () ) );
31883194 if ( dir.empty () )
31893195 return ;
3196+ rememberClosedDocumentState ( editor );
31903197 mRecentClosedFiles .push ( docEvent->getDoc ()->getFilePath () );
31913198 mSettings ->updatedReopenClosedFileState ();
31923199 Lock l ( mWatchesLock );
@@ -3458,6 +3465,40 @@ void App::reopenClosedTab() {
34583465 loadFileFromPath ( prevTabPath );
34593466}
34603467
3468+ std::string App::closedDocumentStateKey ( const std::string& path ) const {
3469+ if ( path.empty () )
3470+ return {};
3471+ return FileSystem::fileExists ( path ) ? FileSystem::getRealPath ( path ) : path;
3472+ }
3473+
3474+ void App::rememberClosedDocumentState ( UICodeEditor* editor ) {
3475+ if ( !editor || !editor->getDocument ().hasFilepath () )
3476+ return ;
3477+
3478+ std::string key ( closedDocumentStateKey ( editor->getDocument ().getFilePath () ) );
3479+ if ( key.empty () )
3480+ return ;
3481+
3482+ mClosedDocumentState [key] = editor->getDocument ().getSelections ();
3483+ }
3484+
3485+ void App::restoreClosedDocumentState ( UICodeEditor* editor, const std::string& path ) {
3486+ if ( !editor )
3487+ return ;
3488+
3489+ std::string key ( closedDocumentStateKey ( path ) );
3490+ if ( key.empty () )
3491+ return ;
3492+
3493+ auto it = mClosedDocumentState .find ( key );
3494+ if ( it == mClosedDocumentState .end () )
3495+ return ;
3496+
3497+ editor->getDocument ().setSelection ( it->second );
3498+ editor->scrollToCursor ();
3499+ mClosedDocumentState .erase ( it );
3500+ }
3501+
34613502void App::updateEditorState () {
34623503 if ( mSplitter ->curEditorExistsAndFocused () ) {
34633504 updateEditorTitle ( mSplitter ->getCurEditor () );
@@ -4149,6 +4190,7 @@ void App::loadFolder( std::string path, bool forceNewWindow ) {
41494190 mSplitter ->removeTabWithOwnedWidgetId ( " welcome_ecode" );
41504191 mStatusBar ->setVisible ( mConfig .ui .showStatusBar );
41514192 }
4193+ mClosedDocumentState .clear ();
41524194
41534195 if ( !mProjectTreeView ) {
41544196 showSidePanel ( mConfig .ui .showSidePanel );
0 commit comments