Skip to content

Commit f2d4f09

Browse files
authored
Merge pull request #657 from CodinGame/lmn/fix-standalone-editor-pane-open-editor
fix: do not ignore call to openEditor from StandaloneEditorPane on a different model
2 parents 756a503 + 8f3182a commit f2d4f09

File tree

3 files changed

+63
-19
lines changed

3 files changed

+63
-19
lines changed

src/service-override/editor.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,16 @@ class EmptyEditorGroupsService implements IEditorGroupsService {
155155
}
156156

157157
class MonacoEditorGroupsService extends MonacoDelegateEditorGroupsService<EmptyEditorGroupsService> {
158-
constructor(@IInstantiationService instantiationService: IInstantiationService) {
159-
super(instantiationService.createInstance(EmptyEditorGroupsService), true, instantiationService)
158+
constructor(
159+
openEditor: OpenEditor,
160+
@IInstantiationService instantiationService: IInstantiationService
161+
) {
162+
super(
163+
instantiationService.createInstance(EmptyEditorGroupsService),
164+
true,
165+
openEditor,
166+
instantiationService
167+
)
160168
}
161169
}
162170

@@ -169,7 +177,7 @@ export default function getServiceOverride(openEditor: OpenEditor): IEditorOverr
169177
true
170178
),
171179
[ITextEditorService.toString()]: new SyncDescriptor(TextEditorService, [], false),
172-
[IEditorGroupsService.toString()]: new SyncDescriptor(MonacoEditorGroupsService)
180+
[IEditorGroupsService.toString()]: new SyncDescriptor(MonacoEditorGroupsService, [openEditor])
173181
}
174182
}
175183

src/service-override/tools/editor.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,15 @@ class EmptyEditorGroup implements IEditorGroup, IEditorGroupView {
176176
export const fakeActiveGroup = new EmptyEditorGroup()
177177

178178
class SimpleEditorPane implements IEditorPane {
179-
constructor(private editor?: ICodeEditor) {}
179+
constructor(
180+
public input: EditorInput | undefined,
181+
private editor?: ICodeEditor
182+
) {}
180183

181184
onDidChangeControl = Event.None
182185
onDidChangeSizeConstraints = Event.None
183186
onDidFocus = Event.None
184187
onDidBlur = Event.None
185-
input = undefined
186188
options = undefined
187189
group = fakeActiveGroup
188190
scopedContextKeyService = undefined
@@ -311,7 +313,7 @@ export function wrapOpenEditor(
311313
}
312314

313315
// Return a very simple editor pane, only the `getControl` method is used
314-
return new SimpleEditorPane(modelEditor)
316+
return new SimpleEditorPane(isEditorInput(editor) ? editor : undefined, modelEditor)
315317
}
316318

317319
return openEditor
@@ -414,7 +416,7 @@ export class MonacoEditorService extends EditorService {
414416

415417
class StandaloneEditorPane implements IVisibleEditorPane {
416418
constructor(
417-
public readonly editor: IStandaloneCodeEditor,
419+
public readonly editor: ICodeEditor,
418420
public input: TextResourceEditorInput,
419421
public group: IEditorGroup
420422
) {}
@@ -466,8 +468,11 @@ class StandaloneEditorGroup extends Disposable implements IEditorGroup, IEditorG
466468
public active: boolean = false
467469
constructor(
468470
public editor: IStandaloneCodeEditor,
471+
private openEditorFallback: OpenEditor | undefined,
469472
@IInstantiationService instantiationService: IInstantiationService,
470-
@IContextKeyService public scopedContextKeyService: IContextKeyService
473+
@IContextKeyService public scopedContextKeyService: IContextKeyService,
474+
@IEditorService public editorService: IEditorService,
475+
@ITextModelService private textModelService: ITextModelService
471476
) {
472477
super()
473478
const onNewModel = (uri: URI) => {
@@ -656,17 +661,31 @@ class StandaloneEditorGroup extends Disposable implements IEditorGroup, IEditorG
656661
getIndexOfEditor = (editorInput: EditorInput) =>
657662
this.pane != null && this.pane.input === editorInput ? 0 : -1
658663

659-
openEditor = async (editor: EditorInput): Promise<IEditorPane | undefined> => {
664+
openEditor = async (
665+
editor: EditorInput,
666+
options?: IEditorOptions
667+
): Promise<IEditorPane | undefined> => {
660668
if (editor.isDisposed()) {
661669
return undefined
662670
}
663671

664-
if (
665-
editor instanceof TextResourceEditorInput &&
666-
editor.resource.toString() === this.pane?.input.resource.toString()
667-
) {
668-
this.focus()
669-
return this.pane
672+
if (editor instanceof TextResourceEditorInput) {
673+
if (editor.resource.toString() === this.pane?.input.resource.toString()) {
674+
this.focus()
675+
return this.pane
676+
}
677+
678+
if (this.openEditorFallback != null) {
679+
const modelRef = await this.textModelService.createModelReference(editor.resource)
680+
const modelEditor = await this.openEditorFallback(modelRef, options, false)
681+
if (modelEditor == null) {
682+
// Dispose the newly created model if `openEditor` wasn't able to open it
683+
modelRef.dispose()
684+
return undefined
685+
}
686+
687+
return new SimpleEditorPane(editor, modelEditor)
688+
}
670689
}
671690
return undefined
672691
}
@@ -718,6 +737,7 @@ export class MonacoDelegateEditorGroupsService<D extends IEditorGroupsService>
718737
constructor(
719738
protected delegate: D,
720739
emptyDelegate: boolean,
740+
private openEditorFallback: OpenEditor | undefined,
721741
@IInstantiationService private instantiationService: IInstantiationService
722742
) {
723743
super()
@@ -771,7 +791,11 @@ export class MonacoDelegateEditorGroupsService<D extends IEditorGroupsService>
771791
onEditorFocused()
772792
}
773793

774-
const newGroup = instantiationService.createInstance(StandaloneEditorGroup, editor)
794+
const newGroup = instantiationService.createInstance(
795+
StandaloneEditorGroup,
796+
editor,
797+
this.openEditorFallback
798+
)
775799
this.additionalGroups.push(newGroup)
776800
this._onDidAddGroup.fire(newGroup)
777801
}

src/service-override/views.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,16 @@ class MonacoEditorParts
197197
extends MonacoDelegateEditorGroupsService<EditorParts>
198198
implements Omit<PublicInterface<EditorParts>, keyof IEditorGroupsService>
199199
{
200-
constructor(@IInstantiationService instantiationService: IInstantiationService) {
201-
super(instantiationService.createInstance(EditorParts), false, instantiationService)
200+
constructor(
201+
openEditorFallback: OpenEditor | undefined,
202+
@IInstantiationService instantiationService: IInstantiationService
203+
) {
204+
super(
205+
instantiationService.createInstance(EditorParts),
206+
false,
207+
openEditorFallback,
208+
instantiationService
209+
)
202210
}
203211

204212
getId(): string {
@@ -839,7 +847,11 @@ function getServiceOverride(
839847
...getKeybindingsOverride({
840848
shouldUseGlobalKeybindings: isEditorPartVisible
841849
}),
842-
[IEditorGroupsService.toString()]: new SyncDescriptor(MonacoEditorParts, [], false),
850+
[IEditorGroupsService.toString()]: new SyncDescriptor(
851+
MonacoEditorParts,
852+
[openEditorFallback],
853+
false
854+
),
843855
[IEditorService.toString()]: new SyncDescriptor(
844856
MonacoEditorService,
845857
[openEditorFallback, isEditorPartVisible],

0 commit comments

Comments
 (0)