Skip to content

Commit 4c7745b

Browse files
committed
Merge branch 'develop' of https://github.com/zardoy/typescript-vscode-plugins into develop
2 parents b2aba64 + aaa5757 commit 4c7745b

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

typescript/src/index.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,23 @@ import decorateDefinitions from './definitions'
1717
import decorateDocumentHighlights from './documentHighlights'
1818
import completionEntryDetails from './completionEntryDetails'
1919

20-
const thisPluginMarker = Symbol('__essentialPluginsMarker__')
20+
const thisPluginMarker = '__essentialPluginsMarker__'
2121

2222
let _configuration: Configuration
2323
const c: GetConfig = key => get(_configuration, key)
2424

25-
const decorateLanguageService = (info: ts.server.PluginCreateInfo, existingProxy?: ts.LanguageService) => {
26-
// Set up decorator object
27-
const proxy: ts.LanguageService = existingProxy ?? Object.create(null)
28-
29-
for (const k of Object.keys(info.languageService)) {
30-
const x = info.languageService[k]!
25+
const getInitialProxy = (languageService: ts.LanguageService, proxy = Object.create(null)): ts.LanguageService => {
26+
for (const k of Object.keys(languageService)) {
27+
const x = languageService[k]!
3128
// @ts-expect-error - JS runtime trickery which is tricky to type tersely
32-
proxy[k] = (...args: Array<Record<string, unknown>>) => x.apply(info.languageService, args)
29+
proxy[k] = (...args: Array<Record<string, unknown>>) => x.apply(languageService, args)
3330
}
31+
return proxy
32+
}
33+
34+
const decorateLanguageService = (info: ts.server.PluginCreateInfo, existingProxy?: ts.LanguageService) => {
35+
// Set up decorator object
36+
const proxy = getInitialProxy(info.languageService, existingProxy)
3437

3538
const { languageService } = info
3639

@@ -58,10 +61,6 @@ const decorateLanguageService = (info: ts.server.PluginCreateInfo, existingProxy
5861
}
5962

6063
proxy.getCompletionEntryDetails = (fileName, position, entryName, formatOptions, source, preferences, data) => {
61-
if (fileName === 'disposeLanguageService') {
62-
process.exit(1)
63-
return
64-
}
6564
const program = languageService.getProgram()
6665
const sourceFile = program?.getSourceFile(fileName)
6766
if (!program || !sourceFile) return
@@ -118,27 +117,24 @@ const plugin: ts.server.PluginModuleFactory = ({ typescript }) => {
118117
_configuration = info.config
119118
console.log('receive config', JSON.stringify(_configuration))
120119
if (info.languageService[thisPluginMarker]) return info.languageService
121-
try {
122-
info.languageService.getCompletionEntryDetails('disposeLanguageService', 0, '', undefined, undefined, undefined, undefined)
123-
} catch {}
124120

125-
const proxy = decorateLanguageService(info, undefined)
121+
const proxy = _configuration.enablePlugin === false ? getInitialProxy(info.languageService) : decorateLanguageService(info, undefined)
126122

123+
// #region watch enablePlugin setting
127124
let prevPluginEnabledSetting = _configuration.enablePlugin
128125
updateConfigListeners.push(() => {
129-
if (prevPluginEnabledSetting && !_configuration.enablePlugin) {
126+
if ((prevPluginEnabledSetting === true || prevPluginEnabledSetting === undefined) && !_configuration.enablePlugin) {
130127
// plugin got disabled, restore original languageService methods
131-
for (const key of Object.keys(proxy)) {
132-
//@ts-expect-error
133-
proxy[key] = (...args: Array<Record<string, unknown>>) => info.languageService[key].apply(info.languageService, args)
134-
}
135-
} else if (!prevPluginEnabledSetting && _configuration.enablePlugin) {
128+
// todo resetting doesn't work after tsconfig changes
129+
getInitialProxy(info.languageService, proxy)
130+
} else if (prevPluginEnabledSetting === false && _configuration.enablePlugin) {
136131
// plugin got enabled
137132
decorateLanguageService(info, proxy)
138133
}
139134

140135
prevPluginEnabledSetting = _configuration.enablePlugin
141136
})
137+
// #endregion
142138

143139
return proxy
144140
},

0 commit comments

Comments
 (0)