Skip to content

Commit 7fd951f

Browse files
committed
feat: patch extension activation events (or remote)
feat: extension id completions in settings.json
1 parent 8e9a776 commit 7fd951f

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@types/vscode": "1.73.0",
4747
"@zardoy/vscode-utils": "^0.0.48",
4848
"got": "^12.6.0",
49+
"jsonc-parser": "^3.2.0",
4950
"lodash": "^4.17.21",
5051
"openai": "^3.2.1",
5152
"source-map": "^0.7.4",

pnpm-lock.yaml

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/configurationType.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ export type Configuration = {
6161
/**
6262
* @default {}
6363
*/
64-
// overrideActivationEvents: {
65-
// [id: string]: string[]
66-
// }
64+
overrideActivationEvents: {
65+
[id: string]: string[]
66+
}
6767
/**
6868
* @default []
6969
*/

src/extension.ts

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import * as fs from 'fs'
2+
13
import * as vscode from 'vscode'
2-
import { extensionCtx, getExtensionSetting, registerExtensionCommand } from 'vscode-framework'
4+
import { extensionCtx, getExtensionSetting, getExtensionSettingId, registerExtensionCommand } from 'vscode-framework'
35
import { watchExtensionSettings } from '@zardoy/vscode-utils/build/settings'
46
import { doPatch, removeAllPatches } from './patch'
7+
import { parseTree, findNodeAtOffset, getNodePath, getLocation } from 'jsonc-parser'
8+
import { join } from 'path'
59

610
export const activate = async () => {
711
const getNewConfig = () => {
@@ -31,11 +35,75 @@ export const activate = async () => {
3135
})
3236
// #endregion
3337

38+
vscode.languages.registerCompletionItemProvider(
39+
{
40+
pattern: '**/settings.json',
41+
},
42+
{
43+
provideCompletionItems(document, position, token, context) {
44+
const root = parseTree(document.getText(), [])
45+
if (!root) {
46+
return
47+
}
48+
const node = findNodeAtOffset(root, document.offsetAt(position))
49+
if (node?.type !== 'string') {
50+
return
51+
}
52+
53+
let path = getNodePath(node)
54+
const pathMatches = (compare: string[], useStartsWith = false) => {
55+
if (!useStartsWith && compare.length !== path.length) {
56+
return undefined
57+
}
58+
return compare.every((item, i) => item === '*' || item === path[i])
59+
}
60+
if (
61+
(pathMatches([getExtensionSettingId('overrideActivationEvents'), '*']) && node.parent?.type === 'property') ||
62+
pathMatches([getExtensionSettingId('disableProviders'), '*', '*'])
63+
) {
64+
const start = document.positionAt(node.offset + 1)
65+
const end = document.positionAt(node.offset + 1 + node.length - 2)
66+
const range = new vscode.Range(start, end)
67+
return vscode.extensions.all.map(ext => ({
68+
label: ext.id,
69+
detail: ext.packageJSON.displayName,
70+
filterText: `${ext.id}${ext.packageJSON.displayName}`,
71+
range,
72+
}))
73+
}
74+
return []
75+
},
76+
},
77+
)
78+
3479
// Main activation actions
3580

36-
// todo continue impl
37-
// for (const [id, expected] of Object.entries(getExtensionSetting('overrideActivationEvents'))) {
38-
// }
81+
let reloadExtHostExtensions = false
82+
for (const [id, expected] of Object.entries(getExtensionSetting('overrideActivationEvents'))) {
83+
const ext = vscode.extensions.getExtension(id)
84+
if (!ext) continue
85+
const { activationEvents = [] } = ext.packageJSON
86+
if (JSON.stringify(expected.sort()) !== JSON.stringify(activationEvents.sort())) {
87+
const packageJson = join(ext.extensionPath, 'package.json')
88+
fs.writeFileSync(
89+
packageJson,
90+
JSON.stringify(
91+
{
92+
...ext.packageJSON,
93+
activationEvents: expected,
94+
},
95+
undefined,
96+
4,
97+
),
98+
)
99+
reloadExtHostExtensions = true
100+
}
101+
}
102+
if (reloadExtHostExtensions) {
103+
vscode.window.showInformationMessage('Restarting extension host as activation events were patched...')
104+
await vscode.commands.executeCommand('workbench.action.restartExtensionHost')
105+
return
106+
}
39107

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

0 commit comments

Comments
 (0)