-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathextension.ts
More file actions
156 lines (135 loc) · 5.67 KB
/
extension.ts
File metadata and controls
156 lines (135 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* eslint-disable @typescript-eslint/no-require-imports */
import * as vscode from 'vscode'
import { defaultJsSupersetLangs } from '@zardoy/vscode-utils/build/langs'
import { extensionCtx, getExtensionSetting, getExtensionSettingId } from 'vscode-framework'
import { pickObj } from '@zardoy/utils'
import { watchExtensionSettings } from '@zardoy/vscode-utils/build/settings'
import webImports from './webImports'
import { sendCommand } from './sendCommand'
import { registerEmmet } from './emmet'
import migrateSettings from './migrateSettings'
import figIntegration from './figIntegration'
import apiCommands from './apiCommands'
import onCompletionAccepted from './onCompletionAccepted'
import specialCommands from './specialCommands'
import vueVolarSupport from './vueVolarSupport'
import moreCompletions from './moreCompletions'
import { mergeSettingsFromScopes } from './mergeSettings'
import codeActionProvider from './codeActionProvider'
import nonTsCommands from './nonTsCommands'
import onEnterActions from './onEnterActions'
let isActivated = false
// let erroredStatusBarItem: vscode.StatusBarItem | undefined
export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted } | undefined) => {
if (isActivated) return
isActivated = true
let webWaitingForConfigSync = false
const getResolvedConfig = () => {
const configuration = vscode.workspace.getConfiguration()
const config: any = {
...configuration.get(process.env.IDS_PREFIX!),
editorSuggestInsertModeReplace: configuration.get('editor.suggest.insertMode') === 'replace',
}
mergeSettingsFromScopes(config, 'typescript', extensionCtx.extension.packageJSON)
return config
}
const syncConfig = () => {
if (!tsApi) return
console.log('sending configure request for typescript-essential-plugins')
// todo implement language-specific settings
const config = getResolvedConfig()
tsApi.configurePlugin('typescript-essential-plugins', config)
if (process.env.PLATFORM === 'node') {
// see comment in plugin
require('fs').writeFileSync(
require('path').join(extensionCtx.extensionPath, './plugin-config.json'),
JSON.stringify(pickObj(config, 'patchOutline', 'outline')),
)
}
if (process.env.PLATFORM === 'web') {
webWaitingForConfigSync = true
}
}
vscode.workspace.onDidChangeConfiguration(async ({ affectsConfiguration }) => {
if (affectsConfiguration(process.env.IDS_PREFIX!) || affectsConfiguration('editor.suggest.insertMode')) {
syncConfig()
if (
process.env.PLATFORM === 'node' &&
(affectsConfiguration(getExtensionSettingId('patchOutline')) ||
affectsConfiguration(getExtensionSettingId('outline.arraysTuplesNumberedItems')))
) {
await vscode.commands.executeCommand('typescript.restartTsServer')
void vscode.window.showWarningMessage('Outline will be updated after text changes or window reload')
}
}
})
syncConfig()
if (tsApi) onCompletionAccepted(tsApi)
if (process.env.PLATFORM === 'web') {
const possiblySyncConfig = async () => {
const { activeTextEditor } = vscode.window
if (!activeTextEditor || !vscode.languages.match(defaultJsSupersetLangs, activeTextEditor.document)) return
if (!webWaitingForConfigSync) return
// webWaitingForConfigSync = false
const config = getResolvedConfig()
void sendCommand(`updateConfig${JSON.stringify(config)}` as any, { inputOptions: {} })
}
vscode.window.onDidChangeActiveTextEditor(possiblySyncConfig)
void possiblySyncConfig()
}
moreCompletions()
void registerEmmet()
webImports()
apiCommands()
specialCommands()
codeActionProvider()
figIntegration()
vueVolarSupport()
onEnterActions()
if (process.env.PLATFORM === 'node' && process.env.NODE_ENV === 'development') {
require('./autoPluginReload').default()
}
}
export const activate = async () => {
nonTsCommands()
migrateSettings()
const possiblyActivateTsPlugin = async () => {
const tsExtension = vscode.extensions.getExtension('vscode.typescript-language-features')
if (tsExtension) {
await tsExtension.activate()
if (!tsExtension.exports?.getAPI) {
throw new Error("TS extension doesn't export API")
}
// Get the API from the TS extension
const api = tsExtension.exports.getAPI(0)
if (!api) {
throw new Error("TS extension doesn't have API")
}
activateTsPlugin(api)
return true
}
if (vscode.extensions.getExtension('Vue.volar') && getExtensionSetting('enableVueSupport')) {
activateTsPlugin(undefined)
return true
}
return false
}
const isActivated = (await possiblyActivateTsPlugin()) ?? false
if (!isActivated) {
// can be also used in future, for now only when activating TS or Volar extension manually
const disposables = []
const { dispose } = vscode.extensions.onDidChange(
async () => {
if (await possiblyActivateTsPlugin()) dispose()
},
undefined,
disposables,
)
watchExtensionSettings(['enableVueSupport'], async () => {
if (await possiblyActivateTsPlugin()) {
// todo
// disposables.forEach(d => d.dispose())
}
})
}
}