Skip to content

Commit

Permalink
fix: wait service initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Mangeonjean committed Feb 16, 2024
1 parent f15c865 commit d5ab6a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
10 changes: 7 additions & 3 deletions src/monaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { ResolvedKeybindingItem } from 'vs/platform/keybinding/common/resolvedKe
import { Keybinding, ResolvedKeybinding } from 'vs/base/common/keybindings'
import { Emitter, Event } from 'vs/base/common/event'
import { createInjectedClass } from './tools/injection'
import { getService } from './services'

function computeConfiguration (configuration: IEditorConfiguration, overrides?: Readonly<IEditorOptions>): IEditorOptions {
const editorConfiguration: IEditorOptions = isObject(configuration.editor) ? deepClone(configuration.editor) : Object.create(null)
Expand Down Expand Up @@ -235,18 +236,21 @@ class ConfiguredStandaloneDiffEditor extends createInjectedClass(StandaloneDiffE
}

export async function writeFile (uri: URI, content: string): Promise<void> {
await StandaloneServices.get(IFileService).writeFile(uri, VSBuffer.fromString(content))
const fileService = await getService(IFileService)
await fileService.writeFile(uri, VSBuffer.fromString(content))
}

export async function deleteFile (uri: URI, options?: Partial<IFileDeleteOptions>): Promise<void> {
await StandaloneServices.get(IFileService).del(uri, options)
const fileService = await getService(IFileService)
await fileService.del(uri, options)
}

export async function createModelReference (resource: URI, content?: string): Promise<IReference<ITextFileEditorModel>> {
if (content != null) {
await writeFile(resource, content)
}
return (await StandaloneServices.get(ITextModelService).createModelReference(resource)) as IReference<ITextFileEditorModel>
const textModelService = await getService(ITextModelService)
return (await textModelService.createModelReference(resource)) as IReference<ITextFileEditorModel>
}

export interface KeybindingProvider {
Expand Down
26 changes: 15 additions & 11 deletions src/service-override/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IEditorOverrideServices, StandaloneServices } from 'vs/editor/standalone/browser/standaloneServices'
import { IEditorOverrideServices } from 'vs/editor/standalone/browser/standaloneServices'
import { WorkspaceService } from 'vs/workbench/services/configuration/browser/configurationService'
import { IConfigurationService } from 'vs/platform/configuration/common/configuration'
import { ITextResourceConfigurationService, ITextResourcePropertiesService } from 'vs/editor/common/services/textResourceConfiguration'
Expand Down Expand Up @@ -36,7 +36,7 @@ import { TextResourcePropertiesService } from 'vs/workbench/services/textresourc
import getFileServiceOverride, { initFile } from './files'
import { memoizedConstructor, unsupported } from '../tools'
import { registerServiceInitializePreParticipant } from '../lifecycle'
import { getService } from '../services'
import { getService, withReadyServices } from '../services'
import { getWorkspaceIdentifier } from '../workbench'

// This is the default value, but can be overriden by overriding the Environment or UserDataProfileService service
Expand All @@ -54,20 +54,24 @@ async function initUserConfiguration (configurationJson: string, options?: Parti
*/
async function updateUserConfiguration (configurationJson: string): Promise<void> {
const userDataProfilesService = await getService(IUserDataProfilesService)
await StandaloneServices.get(IFileService).writeFile(userDataProfilesService.defaultProfile.settingsResource, VSBuffer.fromString(configurationJson))
const fileService = await getService(IFileService)
await fileService.writeFile(userDataProfilesService.defaultProfile.settingsResource, VSBuffer.fromString(configurationJson))
}

async function getUserConfiguration (): Promise<string> {
const userDataProfilesService = StandaloneServices.get(IUserDataProfilesService)
return (await StandaloneServices.get(IFileService).readFile(userDataProfilesService.defaultProfile.settingsResource)).value.toString()
const userDataProfilesService = await getService(IUserDataProfilesService)
const fileService = await getService(IFileService)
return (await fileService.readFile(userDataProfilesService.defaultProfile.settingsResource)).value.toString()
}

function onUserConfigurationChange (callback: () => void): IDisposable {
const userDataProfilesService = StandaloneServices.get(IUserDataProfilesService)
return StandaloneServices.get(IFileService).onDidFilesChange(e => {
if (e.affects(userDataProfilesService.defaultProfile.settingsResource)) {
callback()
}
return withReadyServices(accessor => {
const userDataProfilesService = accessor.get(IUserDataProfilesService)
return accessor.get(IFileService).onDidFilesChange(e => {
if (e.affects(userDataProfilesService.defaultProfile.settingsResource)) {
callback()
}
})
})
}

Expand Down Expand Up @@ -131,7 +135,7 @@ registerServiceInitializePreParticipant(async (accessor) => {
const MemoizedInjectedConfigurationService = memoizedConstructor(InjectedConfigurationService)

export async function reinitializeWorkspace (workspace: IAnyWorkspaceIdentifier): Promise<void> {
const workspaceService = StandaloneServices.get(IWorkspaceContextService) as WorkspaceService
const workspaceService = await getService(IWorkspaceContextService) as WorkspaceService
await workspaceService.initialize(workspace)
}

Expand Down
8 changes: 5 additions & 3 deletions src/service-override/keybindings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IEditorOverrideServices, StandaloneServices } from 'vs/editor/standalone/browser/standaloneServices'
import { IEditorOverrideServices } from 'vs/editor/standalone/browser/standaloneServices'
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors'
import { WorkbenchKeybindingService } from 'vs/workbench/services/keybinding/browser/keybindingService'
import { IKeybindingService, IKeyboardEvent, IUserFriendlyKeybinding } from 'vs/platform/keybinding/common/keybinding'
Expand Down Expand Up @@ -30,6 +30,7 @@ import 'vs/workbench/browser/workbench.contribution'
import 'vs/workbench/contrib/keybindings/browser/keybindings.contribution'
import 'vs/workbench/contrib/preferences/browser/keybindingsEditorContribution'
import 'vs/workbench/contrib/commands/common/commands.contribution'
import { getService } from '../services'

// This is the default value, but can be overriden by overriding the Environment or UserDataProfileService service
const defaultUserKeybindindsFile = URI.from({ scheme: Schemas.vscodeUserData, path: '/User/keybindings.json' })
Expand All @@ -46,8 +47,9 @@ async function initUserKeybindings (configurationJson: string, options?: Partial
*/

async function updateUserKeybindings (keybindingsJson: string): Promise<void> {
const userDataProfilesService: IUserDataProfilesService = StandaloneServices.get(IUserDataProfilesService)
await StandaloneServices.get(IFileService).writeFile(userDataProfilesService.defaultProfile.keybindingsResource, VSBuffer.fromString(keybindingsJson))
const userDataProfilesService: IUserDataProfilesService = await getService(IUserDataProfilesService)
const fileService = await getService(IFileService)
await fileService.writeFile(userDataProfilesService.defaultProfile.keybindingsResource, VSBuffer.fromString(keybindingsJson))
}

class DynamicWorkbenchKeybindingService extends WorkbenchKeybindingService implements DynamicKeybindingService {
Expand Down

0 comments on commit d5ab6a0

Please sign in to comment.