Skip to content

Commit

Permalink
Merge pull request #181 from CodinGame/simulate-editor-group-for-stan…
Browse files Browse the repository at this point in the history
…dalone-editors

Simulate an editor group for each standalone editor
  • Loading branch information
CGNonofr authored Sep 5, 2023
2 parents 76ba937 + 5637570 commit 40ba45c
Show file tree
Hide file tree
Showing 4 changed files with 624 additions and 176 deletions.
76 changes: 54 additions & 22 deletions src/missing-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ import { ITestResultService } from 'vs/workbench/contrib/testing/common/testResu
import { IDiagnosticsService, NullDiagnosticsService } from 'vs/platform/diagnostics/common/diagnostics'
import { INotebookSearchService } from 'vs/workbench/contrib/search/browser/notebookSearch'
import { ResourceSet } from 'vs/base/common/map'
import { IEditorGroupView } from 'vs/workbench/browser/parts/editor/editor'
import { unsupported } from './tools'

class NullLoggerService extends AbstractLoggerService {
Expand Down Expand Up @@ -268,7 +269,32 @@ registerSingleton(IFileService, class FileService implements IFileService {
}
}, InstantiationType.Eager)

class EmptyEditorGroup implements IEditorGroup {
class EmptyEditorGroup implements IEditorGroup, IEditorGroupView {
onDidFocus = Event.None
onDidOpenEditorFail = Event.None
whenRestored = Promise.resolve()
get titleHeight () {
return unsupported()
}

disposed = false
setActive = unsupported
notifyIndexChanged = unsupported
relayout = unsupported
dispose = unsupported
toJSON = unsupported
preferredWidth?: number | undefined
preferredHeight?: number | undefined
get element () {
return unsupported()
}

minimumWidth = 0
maximumWidth = Number.POSITIVE_INFINITY
minimumHeight = 0
maximumHeight = Number.POSITIVE_INFINITY
onDidChange = Event.None
layout = unsupported
onDidModelChange = Event.None
onWillDispose = Event.None
onDidActiveEditorChange = Event.None
Expand All @@ -288,17 +314,17 @@ class EmptyEditorGroup implements IEditorGroup {
isLocked = false
stickyCount = 0
editors = []
get scopedContextKeyService () { return StandaloneServices.get(IContextKeyService) }
getEditors = unsupported
findEditors = unsupported
getEditorByIndex = unsupported
get scopedContextKeyService (): IContextKeyService { return StandaloneServices.get(IContextKeyService) }
getEditors = () => []
findEditors = () => []
getEditorByIndex = () => undefined
getIndexOfEditor = unsupported
openEditor = unsupported
openEditors = unsupported
isPinned = unsupported
isSticky = unsupported
isActive = unsupported
contains = unsupported
isPinned = () => false
isSticky = () => false
isActive = () => false
contains = () => false
moveEditor = unsupported
moveEditors = unsupported
copyEditor = unsupported
Expand All @@ -315,12 +341,12 @@ class EmptyEditorGroup implements IEditorGroup {
// ignore
}

isFirst = () => true
isLast = () => true
isFirst = unsupported
isLast = unsupported
}

const fakeActiveGroup = new EmptyEditorGroup()
registerSingleton(IEditorGroupsService, class EditorGroupsService implements IEditorGroupsService {
export class EmptyEditorGroupsService implements IEditorGroupsService {
readonly _serviceBrand = undefined
getLayout = unsupported
onDidChangeActiveGroup = Event.None
Expand All @@ -332,37 +358,39 @@ registerSingleton(IEditorGroupsService, class EditorGroupsService implements IEd
onDidScroll = Event.None
onDidChangeGroupIndex = Event.None
onDidChangeGroupLocked = Event.None
get contentDimension () { return unsupported() }
get contentDimension (): never { return unsupported() }
activeGroup = fakeActiveGroup
get sideGroup () { return unsupported() }
get sideGroup (): never { return unsupported() }
groups = [fakeActiveGroup]
count = 0
orientation = GroupOrientation.HORIZONTAL
isReady = false
whenReady = Promise.resolve()
whenRestored = Promise.resolve()
hasRestorableState = false
getGroups = () => []
getGroup = () => undefined
getGroups = (): never[] => []
getGroup = (): undefined => undefined
activateGroup = unsupported
getSize = unsupported
setSize = unsupported
arrangeGroups = unsupported
applyLayout = unsupported
centerLayout = unsupported
isLayoutCentered = () => false
isLayoutCentered = (): boolean => false
setGroupOrientation = unsupported
findGroup = () => undefined
findGroup = (): undefined => undefined
addGroup = unsupported
removeGroup = unsupported
moveGroup = unsupported
mergeGroup = unsupported
mergeAllGroups = unsupported
copyGroup = unsupported
get partOptions () { return unsupported() }
partOptions = {}
onDidChangeEditorPartOptions = Event.None
enforcePartOptions = unsupported
}, InstantiationType.Eager)
}

registerSingleton(IEditorGroupsService, EmptyEditorGroupsService, InstantiationType.Eager)

registerSingleton(IWorkingCopyFileService, WorkingCopyFileService, InstantiationType.Eager)
registerSingleton(IPathService, BrowserPathService, InstantiationType.Delayed)
Expand Down Expand Up @@ -1137,8 +1165,12 @@ registerSingleton(IUpdateService, class UpdateService implements IUpdateService
registerSingleton(IStatusbarService, class StatusbarService implements IStatusbarService {
_serviceBrand: undefined
onDidChangeEntryVisibility = Event.None
addEntry = unsupported
isEntryVisible = unsupported
addEntry = () => ({
dispose: () => {},
update: () => {}
})

isEntryVisible = () => false
updateEntryVisibility = unsupported
focus = unsupported
focusNextEntry = unsupported
Expand Down
106 changes: 20 additions & 86 deletions src/service-override/editor.ts
Original file line number Diff line number Diff line change
@@ -1,96 +1,29 @@
import '../missing-services'
import { IEditorOverrideServices, StandaloneServices } from 'vs/editor/standalone/browser/standaloneServices'
import { IResolvedTextEditorModel, ITextModelService } from 'vs/editor/common/services/resolverService'
import { IEditorOverrideServices } from 'vs/editor/standalone/browser/standaloneServices'
import { IResolvedTextEditorModel } from 'vs/editor/common/services/resolverService'
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService'
import { CodeEditorService } from 'vs/workbench/services/editor/browser/codeEditorService'
import { IEditorService, IEditorsChangeEvent, IOpenEditorsOptions, IRevertAllEditorsOptions, ISaveAllEditorsOptions, ISaveEditorsOptions, ISaveEditorsResult, IUntypedEditorReplacement, PreferredGroup } from 'vs/workbench/services/editor/common/editorService'
import { EditorExtensions, EditorInputWithOptions, EditorsOrder, GroupIdentifier, IEditorCloseEvent, IEditorIdentifier, IFindEditorOptions, IRevertOptions, IVisibleEditorPane } from 'vs/workbench/common/editor'
import type { IEditorFactoryRegistry, IEditorPane, IFileEditorInput, IResourceDiffEditorInput, ITextDiffEditorPane, IUntitledTextResourceEditorInput, IUntypedEditorInput } from 'vs/workbench/common/editor'
import { Emitter, Event } from 'vs/base/common/event'
import { EditorInput } from 'vs/workbench/common/editor/editorInput'
import { IEditorOptions, IResourceEditorInput, IResourceEditorInputIdentifier, ITextResourceEditorInput } from 'vs/platform/editor/common/editor'
import { IEditor } from 'vs/editor/common/editorCommon'
import { IEditorService } from 'vs/workbench/services/editor/common/editorService'
import { EditorExtensions, IEditorFactoryRegistry, IFileEditorInput } from 'vs/workbench/common/editor'
import { IEditorOptions } from 'vs/platform/editor/common/editor'
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors'
import { Disposable, IReference } from 'vs/base/common/lifecycle'
import { ICodeEditor, IDiffEditor } from 'vs/editor/browser/editorBrowser'
import { IReference } from 'vs/base/common/lifecycle'
import { ITextEditorService, TextEditorService } from 'vs/workbench/services/textfile/common/textEditorService'
import { ICloseEditorOptions, IEditorGroup, IEditorReplacement } from 'vs/workbench/services/editor/common/editorGroupsService'
import { URI } from 'vs/base/common/uri'
import { Registry } from 'vs/platform/registry/common/platform'
import { FILE_EDITOR_INPUT_ID } from 'vs/workbench/contrib/files/common/files'
import { OpenEditor, wrapOpenEditor } from './tools/editor'
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService'
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'
import { MonacoDelegateEditorGroupsService, MonacoEditorService, OpenEditor } from './tools/editor'
import { unsupported } from '../tools'
import { EmptyEditorGroupsService } from '../missing-services'
import 'vs/workbench/browser/parts/editor/editor.contribution'

class SimpleEditorService extends Disposable implements IEditorService {
public activeTextEditorControl: IEditor | undefined
private _onDidActiveEditorChange = this._register(new Emitter<void>())

constructor (
_openEditor: OpenEditor,
@ITextModelService textModelService: ITextModelService
) {
super()

setTimeout(() => {
const codeEditorService = StandaloneServices.get(ICodeEditorService)
this.activeTextEditorControl = codeEditorService.getFocusedCodeEditor() ?? undefined
const handleCodeEditor = (editor: ICodeEditor) => {
const onEditorFocused = () => {
const newFocusedEditor = codeEditorService.getFocusedCodeEditor()
if (newFocusedEditor !== this.activeTextEditorControl) {
this.activeTextEditorControl = newFocusedEditor ?? undefined
this._onDidActiveEditorChange.fire()
}
}
editor.onDidFocusEditorText(onEditorFocused)
editor.onDidFocusEditorWidget(onEditorFocused)
}
this._register(codeEditorService.onCodeEditorAdd(handleCodeEditor))
codeEditorService.listCodeEditors().forEach(handleCodeEditor)
})

this.openEditor = wrapOpenEditor(textModelService, this.openEditor.bind(this), _openEditor)
class MonacoEditorGroupsService extends MonacoDelegateEditorGroupsService<EmptyEditorGroupsService> {
constructor (@IInstantiationService instantiationService: IInstantiationService) {
super(
instantiationService.createInstance(EmptyEditorGroupsService),
instantiationService
)
}

readonly _serviceBrand: undefined
onDidActiveEditorChange: Event<void> = this._onDidActiveEditorChange.event
onDidVisibleEditorsChange: Event<void> = Event.None
onDidEditorsChange: Event<IEditorsChangeEvent> = Event.None
onDidCloseEditor: Event<IEditorCloseEvent> = Event.None
activeEditorPane: IVisibleEditorPane | undefined
activeEditor: EditorInput | undefined
activeTextEditorLanguageId: string | undefined
visibleEditorPanes: IVisibleEditorPane[] = []
visibleEditors: EditorInput[] = []
visibleTextEditorControls: Array<IEditor | IDiffEditor> = []
editors: EditorInput[] = []
count: number = 0
getEditors: (order: EditorsOrder, options?: { excludeSticky?: boolean }) => readonly IEditorIdentifier[] = () => []

openEditor(editor: EditorInput, options?: IEditorOptions, group?: PreferredGroup): Promise<IEditorPane | undefined>
openEditor(editor: IUntypedEditorInput, group?: PreferredGroup): Promise<IEditorPane | undefined>
openEditor(editor: IResourceEditorInput, group?: PreferredGroup): Promise<IEditorPane | undefined>
openEditor(editor: ITextResourceEditorInput | IUntitledTextResourceEditorInput, group?: PreferredGroup): Promise<IEditorPane | undefined>
openEditor(editor: IResourceDiffEditorInput, group?: PreferredGroup): Promise<ITextDiffEditorPane | undefined>
openEditor(editor: EditorInput | IUntypedEditorInput, optionsOrPreferredGroup?: IEditorOptions | PreferredGroup, preferredGroup?: PreferredGroup): Promise<IEditorPane | undefined>
async openEditor (_editor: EditorInput | IUntypedEditorInput, _optionsOrPreferredGroup?: IEditorOptions | PreferredGroup, _preferredGroup?: PreferredGroup): Promise<IEditorPane | undefined> {
return undefined
}

openEditors: (editors: Array<EditorInputWithOptions | IUntypedEditorInput>, preferredGroup?: PreferredGroup, options?: IOpenEditorsOptions) => Promise<IEditorPane[]> = unsupported
replaceEditors: (replacements: Array<IEditorReplacement | IUntypedEditorReplacement>, group: IEditorGroup | GroupIdentifier) => Promise<void> = unsupported
isOpened: (editor: IResourceEditorInputIdentifier) => boolean = () => false
isVisible: (editor: EditorInput) => boolean = () => false
findEditors(resource: URI, options?: IFindEditorOptions): readonly IEditorIdentifier[]
findEditors(editor: IResourceEditorInputIdentifier, options?: IFindEditorOptions): readonly IEditorIdentifier[]
findEditors (): readonly IEditorIdentifier[] { return [] }
save: (editors: IEditorIdentifier | IEditorIdentifier[], options?: ISaveEditorsOptions) => Promise<ISaveEditorsResult> = async () => ({ success: true, editors: [] })
saveAll: (options?: ISaveAllEditorsOptions) => Promise<ISaveEditorsResult> = async () => ({ success: true, editors: [] })
revert: (editors: IEditorIdentifier | IEditorIdentifier[], options?: IRevertOptions) => Promise<boolean> = unsupported
revertAll: (options?: IRevertAllEditorsOptions) => Promise<boolean> = unsupported
closeEditor: (editor: IEditorIdentifier, options?: ICloseEditorOptions) => Promise<void> = unsupported
closeEditors: (editors: readonly IEditorIdentifier[], options?: ICloseEditorOptions) => Promise<void> = unsupported
}

/**
Expand All @@ -106,8 +39,9 @@ Registry.as<IEditorFactoryRegistry>(EditorExtensions.EditorFactory).registerFile
export default function getServiceOverride (openEditor: OpenEditor): IEditorOverrideServices {
return {
[ICodeEditorService.toString()]: new SyncDescriptor(CodeEditorService, undefined, true),
[IEditorService.toString()]: new SyncDescriptor(SimpleEditorService, [openEditor], true),
[ITextEditorService.toString()]: new SyncDescriptor(TextEditorService, [], false)
[IEditorService.toString()]: new SyncDescriptor(MonacoEditorService, [openEditor, () => false], true),
[ITextEditorService.toString()]: new SyncDescriptor(TextEditorService, [], false),
[IEditorGroupsService.toString()]: new SyncDescriptor(MonacoEditorGroupsService)
}
}

Expand All @@ -116,5 +50,5 @@ export {
IEditorOptions,
IResolvedTextEditorModel,
IReference,
SimpleEditorService
MonacoEditorService
}
Loading

0 comments on commit 40ba45c

Please sign in to comment.