@@ -64,6 +64,7 @@ import { ILanguageFeaturesService } from '@theia/monaco-editor-core/esm/vs/edito
6464import * as objects from '@theia/monaco-editor-core/esm/vs/base/common/objects' ;
6565import { Selection } from '@theia/editor/lib/browser/editor' ;
6666import { IHoverService } from '@theia/monaco-editor-core/esm/vs/platform/hover/browser/hover' ;
67+ import { MonacoTextModelService } from './monaco-text-model-service' ;
6768
6869export type ServicePair < T > = [ ServiceIdentifier < T > , T ] ;
6970
@@ -81,13 +82,28 @@ export class MonacoEditorServices {
8182 @inject ( ContextKeyService )
8283 protected readonly contextKeyService : ContextKeyService ;
8384
85+ @inject ( MonacoTextModelService )
86+ protected readonly monacoModelService : MonacoTextModelService ;
87+
8488 constructor ( @unmanaged ( ) services : MonacoEditorServices ) {
8589 Object . assign ( this , services ) ;
8690 }
8791}
8892
8993export class MonacoEditor extends MonacoEditorServices implements TextEditor {
9094
95+ static async create ( uri : URI ,
96+ document : MonacoEditorModel ,
97+ node : HTMLElement ,
98+ services : MonacoEditorServices ,
99+ options ?: MonacoEditor . IOptions ,
100+ override ?: EditorServiceOverrides ,
101+ parentEditor ?: MonacoEditor ) : Promise < MonacoEditor > {
102+ const instance = new MonacoEditor ( uri , document , node , services , options , override , parentEditor ) ;
103+ await instance . init ( ) ;
104+ return instance ;
105+ }
106+
91107 protected readonly toDispose = new DisposableCollection ( ) ;
92108
93109 protected readonly autoSizing : boolean ;
@@ -109,8 +125,10 @@ export class MonacoEditor extends MonacoEditorServices implements TextEditor {
109125 readonly onDidResize = this . onResizeEmitter . event ;
110126
111127 readonly documents = new Set < MonacoEditorModel > ( ) ;
128+ protected model : monaco . editor . ITextModel | null ;
129+ savedViewState : monaco . editor . ICodeEditorViewState | null ;
112130
113- constructor (
131+ protected constructor (
114132 readonly uri : URI ,
115133 readonly document : MonacoEditorModel ,
116134 readonly node : HTMLElement ,
@@ -140,6 +158,10 @@ export class MonacoEditor extends MonacoEditorServices implements TextEditor {
140158 this . addHandlers ( this . editor ) ;
141159 }
142160
161+ protected async init ( ) : Promise < void > {
162+ this . toDispose . push ( await this . monacoModelService . createModelReference ( this . uri ) ) ;
163+ }
164+
143165 getEncoding ( ) : string {
144166 return this . document . getEncoding ( ) || UTF8 ;
145167 }
@@ -228,6 +250,22 @@ export class MonacoEditor extends MonacoEditorServices implements TextEditor {
228250 } ) ) ;
229251 }
230252
253+ handleVisibilityChanged ( nowVisible : boolean ) : void {
254+ if ( nowVisible ) {
255+ if ( this . model ) {
256+ this . editor . setModel ( this . model ) ;
257+ this . editor . restoreViewState ( this . savedViewState ) ;
258+ this . editor . focus ( ) ;
259+ }
260+ } else {
261+ this . model = this . editor . getModel ( ) ;
262+ this . savedViewState = this . editor . saveViewState ( ) ;
263+
264+ // eslint-disable-next-line no-null/no-null
265+ this . editor . setModel ( null ) ; // workaround for https://github.com/eclipse-theia/theia/issues/14880
266+ }
267+ }
268+
231269 getVisibleRanges ( ) : Range [ ] {
232270 return this . editor . getVisibleRanges ( ) . map ( range => this . m2p . asRange ( range ) ) ;
233271 }
0 commit comments