@@ -14,6 +14,8 @@ import CodeEditSourceEditor
1414/// A single instance of an editor in a group with a published ``EditorInstance/cursorPositions`` variable to publish
1515/// the user's current location in a file.
1616class EditorInstance : ObservableObject , Hashable {
17+ private static let defaultCursorPositions = [ CursorPosition ( line: 1 , column: 1 ) ]
18+
1719 /// The file presented in this editor instance.
1820 let file : CEWorkspaceFile
1921
@@ -43,9 +45,10 @@ class EditorInstance: ObservableObject, Hashable {
4345 replaceText = workspace? . searchState? . replaceText
4446 replaceTextSubject = PassthroughSubject ( )
4547
46- self . cursorPositions = (
47- cursorPositions ?? editorState? . editorCursorPositions ?? [ CursorPosition ( line: 1 , column: 1 ) ]
48- )
48+ let restoredCursorPositions = editorState? . editorCursorPositions
49+ self . cursorPositions = cursorPositions
50+ ?? ( restoredCursorPositions? . isEmpty == false ? restoredCursorPositions : nil )
51+ ?? Self . defaultCursorPositions
4952 self . scrollPosition = editorState? . scrollPosition
5053
5154 // Setup listeners
@@ -124,6 +127,8 @@ class EditorInstance: ObservableObject, Hashable {
124127
125128 /// Translates ranges (eg: from a cursor position) to other information like the number of lines in a range.
126129 class RangeTranslator : TextViewCoordinator {
130+ let controllerDidAppearSubject = PassthroughSubject < Void , Never > ( )
131+
127132 private weak var textViewController : TextViewController ?
128133
129134 init ( ) { }
@@ -136,6 +141,7 @@ class EditorInstance: ObservableObject, Hashable {
136141 if controller. isEditable && controller. isSelectable {
137142 controller. view. window? . makeFirstResponder ( controller. textView)
138143 }
144+ controllerDidAppearSubject. send ( )
139145 }
140146
141147 func destroy( ) {
@@ -158,6 +164,10 @@ class EditorInstance: ObservableObject, Hashable {
158164 return ( endTextLine. index - startTextLine. index) + 1
159165 }
160166
167+ func resolveCursorPosition( _ cursorPosition: CursorPosition ) -> CursorPosition {
168+ textViewController? . resolveCursorPosition ( cursorPosition) ?? cursorPosition
169+ }
170+
161171 func moveLinesUp( ) {
162172 guard let controller = textViewController else { return }
163173 controller. moveLinesUp ( )
0 commit comments