|
| 1 | +import * as fs from 'fs' |
| 2 | + |
1 | 3 | import * as vscode from 'vscode'
|
2 |
| -import { extensionCtx, getExtensionSetting, registerExtensionCommand } from 'vscode-framework' |
| 4 | +import { extensionCtx, getExtensionSetting, getExtensionSettingId, registerExtensionCommand } from 'vscode-framework' |
3 | 5 | import { watchExtensionSettings } from '@zardoy/vscode-utils/build/settings'
|
4 | 6 | import { doPatch, removeAllPatches } from './patch'
|
| 7 | +import { parseTree, findNodeAtOffset, getNodePath, getLocation } from 'jsonc-parser' |
| 8 | +import { join } from 'path' |
5 | 9 |
|
6 | 10 | export const activate = async () => {
|
7 | 11 | const getNewConfig = () => {
|
@@ -31,11 +35,75 @@ export const activate = async () => {
|
31 | 35 | })
|
32 | 36 | // #endregion
|
33 | 37 |
|
| 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 | + |
34 | 79 | // Main activation actions
|
35 | 80 |
|
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 | + } |
39 | 107 |
|
40 | 108 | const extVersion = extensionCtx.extension.packageJSON.version
|
41 | 109 | const currentLoadedConfig = process.env.VSC_CONTROL_EXT_CONFIG && JSON.parse(process.env.VSC_CONTROL_EXT_CONFIG)
|
|
0 commit comments