Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split local ext host #276

Merged
merged 13 commits into from
Dec 5, 2023
4 changes: 3 additions & 1 deletion .ncurc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"vscode-oniguruma",
"yazl",
"@types/vscode",
"@types/node"
"@types/node",
"vscode-semver",
"@types/vscode-semver"
]
}
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,14 @@ The reference can then be disposed, the model will only be disposed if there is

## VSCode api usage

You can just import it as if you were in a vscode extension:
To be able to use the VSCode api directly from your code, you need to import `vscode/localExtensionHost` and the services to be initialized.

You will then be able to import it as if you were in a VSCode extension:

```typescript
import * as vscode from 'vscode'
import { initialize } from 'vscode/extensions'
import 'vscode/localExtensionHost'

await initialize()

Expand Down
40 changes: 20 additions & 20 deletions demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"@codingame/monaco-vscode-rollup-vsix-plugin": "file:../dist/rollup-vsix-plugin",
"@types/dockerode": "^3.3.23",
"@types/express": "^4.17.21",
"@types/wicg-file-system-access": "^2023.10.3",
"@types/ws": "^8.5.9",
"@types/wicg-file-system-access": "^2023.10.4",
"@types/ws": "^8.5.10",
"fast-glob": "^3.3.2",
"ts-node": "^10.9.1",
"typescript": "~5.2.2",
"vite": "~4.4.9"
"typescript": "~5.3.2",
"vite": "~4.4.12"
},
"dependencies": {
"@codingame/monaco-vscode-log-service-override": "file:../dist/service-override-log",
Expand Down
4 changes: 1 addition & 3 deletions demo/src/features/intellisense.ts
Original file line number Diff line number Diff line change
@@ -1,12 +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))
import '../setup' // import setup file to wait for services initialization

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

await new Promise<void>(resolve => onExtHostInitialized(resolve))
import '../setup' // import setup file to wait for services initialization

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
106 changes: 54 additions & 52 deletions demo/src/features/scm.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,63 @@
import { onExtHostInitialized, registerExtension } from 'vscode/extensions'
import type { Uri } from 'vscode'
import { registerExtension } from 'vscode/extensions'

onExtHostInitialized(async () => {
const { getApi } = registerExtension({
name: 'scm-demo',
publisher: 'codingame',
engines: {
vscode: '*'
},
version: '1.0.0',
enabledApiProposals: ['scmActionButton']
}, 1)
const { getApi } = registerExtension({
name: 'scm-demo',
publisher: 'codingame',
engines: {
vscode: '*'
},
version: '1.0.0',
enabledApiProposals: ['scmActionButton']
}, 1)

const vscode = await getApi()
const vscode = await getApi()

const workspaceFolder = vscode.workspace.workspaceFolders![0]
if (workspaceFolder == null) {
throw new Error('No workspace folder')
}
const workspaceFolder = vscode.workspace.workspaceFolders![0]
if (workspaceFolder == null) {
throw new Error('No workspace folder')
}

vscode.commands.registerCommand('scm-demo.click-file', async () => {
await vscode.window.showInformationMessage('You pressed a file!')
})
vscode.commands.registerCommand('scm-demo.commit', async () => {
await vscode.window.showInformationMessage("You've committed!")
})
vscode.commands.registerCommand('scm-demo.click-file', async (uri: Uri) => {
await vscode.commands.executeCommand('vscode.open', uri)
await vscode.window.showInformationMessage(`You pressed a file! (${uri.toString()})`)
})
vscode.commands.registerCommand('scm-demo.commit', async () => {
await vscode.window.showInformationMessage("You've committed!")
})

const scm = vscode.scm.createSourceControl('demo-source-control', 'Demo Source Control', workspaceFolder.uri)
scm.inputBox.placeholder = 'Hello, you can write anything here!'
scm.acceptInputCommand = {
const scm = vscode.scm.createSourceControl('demo-source-control', 'Demo Source Control', workspaceFolder.uri)
scm.inputBox.placeholder = 'Hello, you can write anything here!'
scm.acceptInputCommand = {
command: 'scm-demo.commit',
title: 'Commit'
}
scm.actionButton = {
command: {
command: 'scm-demo.commit',
title: 'Commit'
},
enabled: true
}
scm.count = 2

const group = scm.createResourceGroup('working-tree', 'Working Tree')
group.resourceStates = [{
resourceUri: vscode.Uri.file('/tmp/test.js'),
command: {
title: 'Commit',
command: 'scm-demo.click-file',
arguments: [vscode.Uri.file('/tmp/test.js')]
}
scm.actionButton = {
command: {
command: 'scm-demo.commit',
title: 'Commit'
},
enabled: true
}, {
resourceUri: vscode.Uri.file('/tmp/test_readonly.js'),
command: {
title: 'Commit',
command: 'scm-demo.click-file',
arguments: [vscode.Uri.file('/tmp/test_readonly.js')]
},
decorations: {
strikeThrough: true,
tooltip: 'File is read-only'
}
scm.count = 2

const group = scm.createResourceGroup('working-tree', 'Working Tree')
group.resourceStates = [{
resourceUri: vscode.Uri.file('/tmp/test.js'),
command: {
title: 'Commit',
command: 'scm-demo.click-file'
}
}, {
resourceUri: vscode.Uri.file('/tmp/test_readonly.js'),
command: {
title: 'Commit',
command: 'scm-demo.click-file'
},
decorations: {
strikeThrough: true,
tooltip: 'File is read-only'
}
}]
})
}]
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 @@ -48,6 +47,7 @@ import defaultConfiguration from './user/configuration.json?raw'
import defaultKeybindings from './user/keybindings.json?raw'
import { workerConfig } from './tools/extHostWorker'
import { Worker } from './tools/crossOriginWorker'
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
2 changes: 1 addition & 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 Down
Loading