Skip to content

Commit

Permalink
backup
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Mangeonjean committed Dec 1, 2023
1 parent 8642e6e commit 925ee4d
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 293 deletions.
16 changes: 8 additions & 8 deletions demo/src/features/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RegisteredFileSystemProvider, registerFileSystemOverlay, RegisteredReadOnlyFile, RegisteredMemoryFile, initFile } from '@codingame/monaco-vscode-files-service-override'
import * as vscode from 'vscode'
import * as monaco from 'monaco-editor'

void initFile(vscode.Uri.file('/tmp/test.js'), `// import anotherfile
void initFile(monaco.Uri.file('/tmp/test.js'), `// import anotherfile
let variable = 1
function inc () {
variable++
Expand All @@ -13,9 +13,9 @@ while (variable < 5000) {
}`)

const fileSystemProvider = new RegisteredFileSystemProvider(false)
fileSystemProvider.registerFile(new RegisteredReadOnlyFile(vscode.Uri.file('/tmp/test_readonly.js'), async () => 'This is a readonly static file'))
fileSystemProvider.registerFile(new RegisteredReadOnlyFile(monaco.Uri.file('/tmp/test_readonly.js'), async () => 'This is a readonly static file'))

fileSystemProvider.registerFile(new RegisteredMemoryFile(vscode.Uri.file('/tmp/jsconfig.json'), `{
fileSystemProvider.registerFile(new RegisteredMemoryFile(monaco.Uri.file('/tmp/jsconfig.json'), `{
"compilerOptions": {
"target": "es2020",
"module": "esnext",
Expand All @@ -27,7 +27,7 @@ fileSystemProvider.registerFile(new RegisteredMemoryFile(vscode.Uri.file('/tmp/j
}`
))

fileSystemProvider.registerFile(new RegisteredMemoryFile(vscode.Uri.file('/tmp/index.html'), `
fileSystemProvider.registerFile(new RegisteredMemoryFile(monaco.Uri.file('/tmp/index.html'), `
<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -47,7 +47,7 @@ fileSystemProvider.registerFile(new RegisteredMemoryFile(vscode.Uri.file('/tmp/i
</html>`
))

fileSystemProvider.registerFile(new RegisteredMemoryFile(vscode.Uri.file('/tmp/test.md'), `
fileSystemProvider.registerFile(new RegisteredMemoryFile(monaco.Uri.file('/tmp/test.md'), `
***Hello World***
Math block:
Expand All @@ -60,11 +60,11 @@ $$
$$`
))

fileSystemProvider.registerFile(new RegisteredMemoryFile(vscode.Uri.file('/tmp/test.customeditor'), `
fileSystemProvider.registerFile(new RegisteredMemoryFile(monaco.Uri.file('/tmp/test.customeditor'), `
Custom Editor!`
))

fileSystemProvider.registerFile(new RegisteredMemoryFile(vscode.Uri.file('/tmp/test.css'), `
fileSystemProvider.registerFile(new RegisteredMemoryFile(monaco.Uri.file('/tmp/test.css'), `
h1 {
color: DeepSkyBlue;
}`
Expand Down
3 changes: 0 additions & 3 deletions demo/src/features/intellisense.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import * as vscode from 'vscode'
import { onExtHostInitialized } from 'vscode/extensions'
import '@codingame/monaco-vscode-json-language-features-default-extension'
import '@codingame/monaco-vscode-typescript-language-features-default-extension'
import '@codingame/monaco-vscode-html-language-features-default-extension'
import '@codingame/monaco-vscode-css-language-features-default-extension'
import '@codingame/monaco-vscode-markdown-language-features-default-extension'

await new Promise<void>(resolve => onExtHostInitialized(resolve))

vscode.languages.registerCallHierarchyProvider('javascript', {
prepareCallHierarchy: function (): vscode.ProviderResult<vscode.CallHierarchyItem | vscode.CallHierarchyItem[]> {
return {
Expand Down
3 changes: 0 additions & 3 deletions demo/src/features/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import * as vscode from 'vscode'
import { onExtHostInitialized } from 'vscode/extensions'

await new Promise<void>(resolve => onExtHostInitialized(resolve))

void vscode.window.showInformationMessage('Hello', {
detail: 'Welcome to the monaco-vscode-api demo',
Expand Down
4 changes: 1 addition & 3 deletions demo/src/features/output.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { ExtensionHostKind, onExtHostInitialized, registerExtension } from 'vscode/extensions'

await new Promise<void>(resolve => onExtHostInitialized(resolve))
import { ExtensionHostKind, registerExtension } from 'vscode/extensions'

const { getApi } = registerExtension({
name: 'outputDemo',
Expand Down
3 changes: 2 additions & 1 deletion demo/src/features/terminal.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ITerminalChildProcess, SimpleTerminalBackend, SimpleTerminalProcess } from '@codingame/monaco-vscode-terminal-service-override'
import ansiColors from 'ansi-colors'
import * as vscode from 'vscode'
import type {} from 'vscode'

export class TerminalBackend extends SimpleTerminalBackend {
override getDefaultSystemShell = async (): Promise<string> => 'fake'
override createProcess = async (): Promise<ITerminalChildProcess> => {
const vscode = await import('vscode')
const dataEmitter = new vscode.EventEmitter<string>()
const propertyEmitter = new vscode.EventEmitter<{
type: string
Expand Down
4 changes: 1 addition & 3 deletions demo/src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { IStorageService, LogLevel, getService, initialize as initializeMonacoService } from 'vscode/services'
import { initialize as initializeVscodeExtensions } from 'vscode/extensions'
import getModelServiceOverride from '@codingame/monaco-vscode-model-service-override'
import getNotificationServiceOverride from '@codingame/monaco-vscode-notifications-service-override'
import getDialogsServiceOverride from '@codingame/monaco-vscode-dialogs-service-override'
Expand Down Expand Up @@ -50,6 +49,7 @@ import { openNewCodeEditor } from './features/editor'
import { toCrossOriginWorker, toWorkerConfig } from './tools/workers'
import defaultConfiguration from './user/configuration.json?raw'
import defaultKeybindings from './user/keybindings.json?raw'
import 'vscode/localExtensionHost'

const userDataProvider = await createIndexedDBProviders()

Expand Down Expand Up @@ -188,8 +188,6 @@ export async function clearStorage (): Promise<void> {
await (await getService(IStorageService) as BrowserStorageService).clear()
}

await initializeVscodeExtensions()

for (const { part, element } of [
{ part: Parts.TITLEBAR_PART, element: '#titleBar' },
{ part: Parts.BANNER_PART, element: '#banner' },
Expand Down
5 changes: 4 additions & 1 deletion demo/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default defineConfig({
// add all local dependencies...
...localDependencies,
// and their exports
'vscode/extensions', 'vscode/services', 'vscode/monaco',
'vscode/extensions', 'vscode/services', 'vscode/monaco', 'vscode/localExtensionHost',

// These 2 lines prevent vite from reloading the whole page when starting a worker (so 2 times in a row after cleaning the vite cache - for the editor then the textmate workers)
// it's mainly empirical and probably not the best way, fix me if you find a better way
Expand All @@ -64,6 +64,9 @@ export default defineConfig({
],
exclude: [],
esbuildOptions: {
supported: {
'top-level-await': true // browsers can handle top-level-await features
},
plugins: [{
name: 'import.meta.url',
setup ({ onLoad }) {
Expand Down
10 changes: 9 additions & 1 deletion rollup/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ function resolveVscode (importee: string, importer?: string) {

const input = {
api: './src/api.ts',
localExtensionHost: './src/localExtensionHost.ts',
extensions: './src/extensions.ts',
services: './src/services.ts',
l10n: './src/l10n.ts',
Expand Down Expand Up @@ -875,6 +876,10 @@ export default (args: Record<string, string>): rollup.RollupOptions[] => {
types: './services.d.ts',
default: './services.js'
},
'./localExtensionHost': {
types: './localExtensionHost.d.ts',
default: './localExtensionHost.js'
},
'./extensions': {
types: './extensions.d.ts',
default: './extensions.js'
Expand Down Expand Up @@ -914,6 +919,9 @@ export default (args: Record<string, string>): rollup.RollupOptions[] => {
},
typesVersions: {
'*': {
'./localExtensionHost': [
'./localExtensionHost.d.ts'
],
services: [
'./services.d.ts'
],
Expand Down Expand Up @@ -1032,7 +1040,7 @@ export default (args: Record<string, string>): rollup.RollupOptions[] => {
const isVscodeFile = resolved.startsWith(VSCODE_SRC_DIST_DIR)
const isServiceOverride = path.dirname(resolved) === DIST_SERVICE_OVERRIDE_DIR_MAIN
const isNotExclusive = (isVscodeFile || isServiceOverride) && !exclusiveModules.has(resolvedWithExtension)
const shouldBeShared = ['monaco.js', 'assets.js', 'lifecycle.js', 'workbench.js', 'missing-services.js'].includes(path.relative(DIST_DIR_MAIN, resolvedWithExtension))
const shouldBeShared = ['extensions.js', 'monaco.js', 'assets.js', 'lifecycle.js', 'workbench.js', 'missing-services.js'].includes(path.relative(DIST_DIR_MAIN, resolvedWithExtension))

if (isNotExclusive || shouldBeShared) {
// Those modules will be imported from external monaco-vscode-api
Expand Down
1 change: 1 addition & 0 deletions rollup/rollup.types.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function isExternal (id: string, main: boolean) {
export default rollup.defineConfig((<{input: Record<string, string>, output: string, preserveModulesRoot?: string, main?: boolean}[]>[{
input: {
'services.d': './dist/types/src/services.d.ts',
'localExtensionHost.d': './dist/types/src/localExtensionHost.d.ts',
'extensions.d': './dist/types/src/extensions.d.ts',
'monaco.d': './dist/types/src/monaco.d.ts',
'l10n.d': './dist/types/src/l10n.d.ts',
Expand Down
53 changes: 24 additions & 29 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,39 @@ import * as log from 'vs/platform/log/common/log'
import * as editSessions from 'vs/platform/workspace/common/editSessions'
import * as editorOptions from 'vs/editor/common/config/editorOptions'
import * as uri from 'vs/base/common/uri'
import { getLocalApi } from './localExtensionHost'
import { getOverrideDefaultApi } from './extensions'

let defaultApi: typeof vscode | undefined

let defaultApi: typeof vscode
export function setDefaultApi (api: typeof vscode): void {
defaultApi = api
}

function createProxy<T extends keyof typeof vscode> (key: T): typeof vscode[T] {
return new Proxy({}, {
get (target, p) {
if (defaultApi == null) {
throw new Error('Default api is not ready yet, do not forget to call `initialize` from \'vscode/extensions\'')
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (defaultApi[key] as any)[p]
}
}) as typeof vscode[T]
defaultApi = await getLocalApi()

function getApi () {
return getOverrideDefaultApi() ?? defaultApi
}

const api: typeof vscode = {
version: VSCODE_VERSION,
tasks: createProxy('tasks'),
notebooks: createProxy('notebooks'),
scm: createProxy('scm'),
comments: createProxy('comments'),
authentication: createProxy('authentication'),
tests: createProxy('tests'),
extensions: createProxy('extensions'),
debug: createProxy('debug'),
env: createProxy('env'),
commands: createProxy('commands'),
window: createProxy('window'),
workspace: createProxy('workspace'),
languages: createProxy('languages'),
l10n: createProxy('l10n'),
interactive: createProxy('interactive'),
ai: createProxy('ai'),
chat: createProxy('chat'),
get tasks () { return getApi().tasks },
get notebooks () { return getApi().notebooks },
get scm () { return getApi().scm },
get comments () { return getApi().comments },
get authentication () { return getApi().authentication },
get tests () { return getApi().tests },
get extensions () { return getApi().extensions },
get debug () { return getApi().debug },
get env () { return getApi().env },
get commands () { return getApi().commands },
get window () { return getApi().window },
get workspace () { return getApi().workspace },
get languages () { return getApi().languages },
get l10n () { return getApi().l10n },
get interactive () { return getApi().interactive },
get ai () { return getApi().ai },
get chat () { return getApi().chat },

// types
Breakpoint: extHostTypes.Breakpoint,
Expand Down
36 changes: 20 additions & 16 deletions src/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,34 @@ import { DisposableStore, IDisposable } from 'vs/base/common/lifecycle'
import { ITranslations } from 'vs/platform/extensionManagement/common/extensionNls'
import { joinPath } from 'vs/base/common/resources'
import { FileAccess, Schemas } from 'vs/base/common/network'
import { Barrier } from 'vs/base/common/async'
import { ExtensionHostKind } from 'vs/workbench/services/extensions/common/extensionHostKind'
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService'
import { parse } from 'vs/base/common/json'
import { IFileService } from 'vs/platform/files/common/files'
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'
import { IExtensionWithExtHostKind, ExtensionServiceOverride, getLocalExtHostExtensionService } from './service-override/extensions'
import { IExtensionWithExtHostKind, ExtensionServiceOverride } from './service-override/extensions'
import { CustomSchemas, registerExtensionFile } from './service-override/files'
import { setDefaultApi } from './api'
import { getService } from './services'
import { ExtensionManifestTranslator } from './tools/l10n'
import { throttle } from './tools'

const defaultApiInitializeBarrier = new Barrier()
export async function initialize (): Promise<void> {
await getLocalExtHostExtensionService().then(async (extHostExtensionService) => {
setDefaultApi(await extHostExtensionService.getApi())
defaultApiInitializeBarrier.open()
})
export type ApiFactory = (extensionId?: string) => Promise<typeof vscode>

let apiFactory: ApiFactory | undefined
export function registerLocalApiFactory (_apiFactory: ApiFactory): void {
apiFactory = _apiFactory
}

let overrideDefaultApi: typeof vscode | undefined
export function getOverrideDefaultApi (): typeof vscode | undefined {
return overrideDefaultApi
}

function getApiFactory (): ApiFactory {
if (apiFactory == null) {
throw new Error('The local api can\'t be used without registering the local extension host by importing `vscode/localExtensionHost`')
}
return apiFactory
}

interface RegisterExtensionParams {
Expand Down Expand Up @@ -155,29 +164,24 @@ export function registerExtension (manifest: IExtensionManifest, extHostKind?: E
if (extHostKind === ExtensionHostKind.LocalProcess) {
async function getApi () {
await addExtensionPromise
return (await getLocalExtHostExtensionService()).getApi(id)
return getApiFactory()(id)
}

api = <RegisterLocalProcessExtensionResult>{
...api,
getApi,
async setAsDefaultApi () {
setDefaultApi(await getApi())
overrideDefaultApi = await getApi()
}
}
}

return api
}

function onExtHostInitialized (fct: () => void): void {
void defaultApiInitializeBarrier.wait().then(fct)
}

export {
IExtensionManifest,
ITranslations,
IExtensionContributions,
onExtHostInitialized,
ExtensionHostKind
}
Loading

0 comments on commit 925ee4d

Please sign in to comment.