@@ -17,20 +17,23 @@ import decorateDefinitions from './definitions'
17
17
import decorateDocumentHighlights from './documentHighlights'
18
18
import completionEntryDetails from './completionEntryDetails'
19
19
20
- const thisPluginMarker = Symbol ( '__essentialPluginsMarker__' )
20
+ const thisPluginMarker = '__essentialPluginsMarker__'
21
21
22
22
let _configuration : Configuration
23
23
const c : GetConfig = key => get ( _configuration , key )
24
24
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 ] !
31
28
// @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 )
33
30
}
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 )
34
37
35
38
const { languageService } = info
36
39
@@ -58,10 +61,6 @@ const decorateLanguageService = (info: ts.server.PluginCreateInfo, existingProxy
58
61
}
59
62
60
63
proxy . getCompletionEntryDetails = ( fileName , position , entryName , formatOptions , source , preferences , data ) => {
61
- if ( fileName === 'disposeLanguageService' ) {
62
- process . exit ( 1 )
63
- return
64
- }
65
64
const program = languageService . getProgram ( )
66
65
const sourceFile = program ?. getSourceFile ( fileName )
67
66
if ( ! program || ! sourceFile ) return
@@ -118,27 +117,24 @@ const plugin: ts.server.PluginModuleFactory = ({ typescript }) => {
118
117
_configuration = info . config
119
118
console . log ( 'receive config' , JSON . stringify ( _configuration ) )
120
119
if ( info . languageService [ thisPluginMarker ] ) return info . languageService
121
- try {
122
- info . languageService . getCompletionEntryDetails ( 'disposeLanguageService' , 0 , '' , undefined , undefined , undefined , undefined )
123
- } catch { }
124
120
125
- const proxy = decorateLanguageService ( info , undefined )
121
+ const proxy = _configuration . enablePlugin === false ? getInitialProxy ( info . languageService ) : decorateLanguageService ( info , undefined )
126
122
123
+ // #region watch enablePlugin setting
127
124
let prevPluginEnabledSetting = _configuration . enablePlugin
128
125
updateConfigListeners . push ( ( ) => {
129
- if ( prevPluginEnabledSetting && ! _configuration . enablePlugin ) {
126
+ if ( ( prevPluginEnabledSetting === true || prevPluginEnabledSetting === undefined ) && ! _configuration . enablePlugin ) {
130
127
// 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 ) {
136
131
// plugin got enabled
137
132
decorateLanguageService ( info , proxy )
138
133
}
139
134
140
135
prevPluginEnabledSetting = _configuration . enablePlugin
141
136
} )
137
+ // #endregion
142
138
143
139
return proxy
144
140
} ,
0 commit comments