@@ -12,12 +12,15 @@ import {
1212 resolveInterpreter ,
1313} from './common/python' ;
1414import { restartServer } from './common/server' ;
15- import { checkIfConfigurationChanged , getInterpreterFromSetting } from './common/settings' ;
15+ import { checkIfConfigurationChanged , getInterpreterFromSetting , getServerEnabled } from './common/settings' ;
1616import { loadServerDefaults } from './common/setup' ;
17+ import { LS_SERVER_RESTART_DELAY } from './common/constants' ;
1718import { getLSClientTraceLevel } from './common/utilities' ;
1819import { createOutputChannel , onDidChangeConfiguration , registerCommand } from './common/vscodeapi' ;
1920
2021let lsClient : LanguageClient | undefined ;
22+ let isRestarting = false ;
23+ let restartTimer : NodeJS . Timeout | undefined ;
2124export async function activate ( context : vscode . ExtensionContext ) : Promise < void > {
2225 // This is required to get server name and module. This should be
2326 // the first thing that we do in this extension.
@@ -49,28 +52,52 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
4952 traceVerbose ( `Full Server Info: ${ JSON . stringify ( serverInfo ) } ` ) ;
5053
5154 const runServer = async ( ) => {
52- const interpreter = getInterpreterFromSetting ( serverId ) ;
53- if ( interpreter && interpreter . length > 0 ) {
54- if ( checkVersion ( await resolveInterpreter ( interpreter ) ) ) {
55- traceVerbose ( `Using interpreter from ${ serverInfo . module } .interpreter: ${ interpreter . join ( ' ' ) } ` ) ;
56- lsClient = await restartServer ( serverId , serverName , outputChannel , lsClient ) ;
55+ if ( isRestarting ) {
56+ if ( restartTimer ) {
57+ clearTimeout ( restartTimer ) ;
5758 }
59+ restartTimer = setTimeout ( runServer , LS_SERVER_RESTART_DELAY ) ;
5860 return ;
5961 }
62+ isRestarting = true ;
63+ try {
64+ if ( ! getServerEnabled ( serverId ) ) {
65+ if ( lsClient ) {
66+ try {
67+ await lsClient . stop ( ) ;
68+ } catch ( ex ) {
69+ traceError ( `Server: Stop failed: ${ ex } ` ) ;
70+ }
71+ lsClient = undefined ;
72+ }
73+ return ;
74+ }
6075
61- const interpreterDetails = await getInterpreterDetails ( ) ;
62- if ( interpreterDetails . path ) {
63- traceVerbose ( `Using interpreter from Python extension: ${ interpreterDetails . path . join ( ' ' ) } ` ) ;
64- lsClient = await restartServer ( serverId , serverName , outputChannel , lsClient ) ;
65- return ;
66- }
76+ const interpreter = getInterpreterFromSetting ( serverId ) ;
77+ if ( interpreter && interpreter . length > 0 ) {
78+ if ( checkVersion ( await resolveInterpreter ( interpreter ) ) ) {
79+ traceVerbose ( `Using interpreter from ${ serverInfo . module } .interpreter: ${ interpreter . join ( ' ' ) } ` ) ;
80+ lsClient = await restartServer ( serverId , serverName , outputChannel , lsClient ) ;
81+ }
82+ return ;
83+ }
6784
68- traceError (
69- 'Python interpreter missing:\r\n' +
70- '[Option 1] Select python interpreter using the ms-python.python.\r\n' +
71- `[Option 2] Set an interpreter using "${ serverId } .interpreter" setting.\r\n` +
72- 'Please use Python 3.8 or greater.' ,
73- ) ;
85+ const interpreterDetails = await getInterpreterDetails ( ) ;
86+ if ( interpreterDetails . path ) {
87+ traceVerbose ( `Using interpreter from Python extension: ${ interpreterDetails . path . join ( ' ' ) } ` ) ;
88+ lsClient = await restartServer ( serverId , serverName , outputChannel , lsClient ) ;
89+ return ;
90+ }
91+
92+ traceError (
93+ 'Python interpreter missing:\r\n' +
94+ '[Option 1] Select python interpreter using the ms-python.python.\r\n' +
95+ `[Option 2] Set an interpreter using "${ serverId } .interpreter" setting.\r\n` +
96+ 'Please use Python 3.8 or greater.' ,
97+ ) ;
98+ } finally {
99+ isRestarting = false ;
100+ }
74101 } ;
75102
76103 context . subscriptions . push (
@@ -101,6 +128,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
101128
102129export async function deactivate ( ) : Promise < void > {
103130 if ( lsClient ) {
104- await lsClient . stop ( ) ;
131+ try {
132+ await lsClient . stop ( ) ;
133+ } catch ( ex ) {
134+ traceError ( `Server: Stop failed: ${ ex } ` ) ;
135+ }
105136 }
106137}
0 commit comments