Skip to content

Commit

Permalink
feat: patch extension activation events (or remote)
Browse files Browse the repository at this point in the history
feat: extension id completions in settings.json
  • Loading branch information
zardoy committed Apr 26, 2023
1 parent 8e9a776 commit 7fd951f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@types/vscode": "1.73.0",
"@zardoy/vscode-utils": "^0.0.48",
"got": "^12.6.0",
"jsonc-parser": "^3.2.0",
"lodash": "^4.17.21",
"openai": "^3.2.1",
"source-map": "^0.7.4",
Expand Down
8 changes: 6 additions & 2 deletions pnpm-lock.yaml

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

6 changes: 3 additions & 3 deletions src/configurationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export type Configuration = {
/**
* @default {}
*/
// overrideActivationEvents: {
// [id: string]: string[]
// }
overrideActivationEvents: {
[id: string]: string[]
}
/**
* @default []
*/
Expand Down
76 changes: 72 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as fs from 'fs'

import * as vscode from 'vscode'
import { extensionCtx, getExtensionSetting, registerExtensionCommand } from 'vscode-framework'
import { extensionCtx, getExtensionSetting, getExtensionSettingId, registerExtensionCommand } from 'vscode-framework'
import { watchExtensionSettings } from '@zardoy/vscode-utils/build/settings'
import { doPatch, removeAllPatches } from './patch'
import { parseTree, findNodeAtOffset, getNodePath, getLocation } from 'jsonc-parser'
import { join } from 'path'

export const activate = async () => {
const getNewConfig = () => {
Expand Down Expand Up @@ -31,11 +35,75 @@ export const activate = async () => {
})
// #endregion

vscode.languages.registerCompletionItemProvider(
{
pattern: '**/settings.json',
},
{
provideCompletionItems(document, position, token, context) {
const root = parseTree(document.getText(), [])
if (!root) {
return
}
const node = findNodeAtOffset(root, document.offsetAt(position))
if (node?.type !== 'string') {
return
}

let path = getNodePath(node)
const pathMatches = (compare: string[], useStartsWith = false) => {
if (!useStartsWith && compare.length !== path.length) {
return undefined
}
return compare.every((item, i) => item === '*' || item === path[i])
}
if (
(pathMatches([getExtensionSettingId('overrideActivationEvents'), '*']) && node.parent?.type === 'property') ||
pathMatches([getExtensionSettingId('disableProviders'), '*', '*'])
) {
const start = document.positionAt(node.offset + 1)
const end = document.positionAt(node.offset + 1 + node.length - 2)
const range = new vscode.Range(start, end)
return vscode.extensions.all.map(ext => ({
label: ext.id,
detail: ext.packageJSON.displayName,
filterText: `${ext.id}${ext.packageJSON.displayName}`,
range,
}))
}
return []
},
},
)

// Main activation actions

// todo continue impl
// for (const [id, expected] of Object.entries(getExtensionSetting('overrideActivationEvents'))) {
// }
let reloadExtHostExtensions = false
for (const [id, expected] of Object.entries(getExtensionSetting('overrideActivationEvents'))) {
const ext = vscode.extensions.getExtension(id)
if (!ext) continue
const { activationEvents = [] } = ext.packageJSON
if (JSON.stringify(expected.sort()) !== JSON.stringify(activationEvents.sort())) {
const packageJson = join(ext.extensionPath, 'package.json')
fs.writeFileSync(
packageJson,
JSON.stringify(
{
...ext.packageJSON,
activationEvents: expected,
},
undefined,
4,
),
)
reloadExtHostExtensions = true
}
}
if (reloadExtHostExtensions) {
vscode.window.showInformationMessage('Restarting extension host as activation events were patched...')
await vscode.commands.executeCommand('workbench.action.restartExtensionHost')
return
}

const extVersion = extensionCtx.extension.packageJSON.version
const currentLoadedConfig = process.env.VSC_CONTROL_EXT_CONFIG && JSON.parse(process.env.VSC_CONTROL_EXT_CONFIG)
Expand Down

0 comments on commit 7fd951f

Please sign in to comment.