@@ -5,6 +5,7 @@ import { spawnSync } from 'child_process';
5
5
import deepEqual from 'deep-equal' ;
6
6
import WebRequest from 'web-request' ;
7
7
import deepmerge from 'deepmerge' ;
8
+ import { Mutex } from 'async-mutex' ;
8
9
import vscode , { ExtensionContext } from 'vscode' ;
9
10
import { LanguageClient , CloseAction , ErrorAction , InitializeError , Message , RevealOutputChannelOn } from 'vscode-languageclient' ;
10
11
@@ -54,10 +55,21 @@ let languageClient: LanguageClient | undefined;
54
55
let languageServerDisposable : vscode . Disposable | undefined ;
55
56
let latestConfig : LanguageServerConfig | undefined ;
56
57
let crashCount = 0 ;
58
+ const languageServerStartMutex = new Mutex ( ) ;
59
+ export let languageServerIsRunning = false ; // TODO: use later for `start`, `stop`, and `restart` language server.
57
60
58
61
export function activate ( context : ExtensionContext ) {
59
62
context . subscriptions . push (
60
- vscode . commands . registerCommand ( 'arduino.languageserver.start' , ( config : LanguageServerConfig ) => startLanguageServer ( context , config ) ) ,
63
+ vscode . commands . registerCommand ( 'arduino.languageserver.start' , async ( config : LanguageServerConfig ) => {
64
+ const unlock = await languageServerStartMutex . acquire ( ) ;
65
+ try {
66
+ const started = await startLanguageServer ( context , config ) ;
67
+ languageServerIsRunning = started ;
68
+ return languageServerIsRunning ? config . board . fqbn : undefined ;
69
+ } finally {
70
+ unlock ( ) ;
71
+ }
72
+ } ) ,
61
73
vscode . commands . registerCommand ( 'arduino.debug.start' , ( config : DebugConfig ) => startDebug ( context , config ) )
62
74
) ;
63
75
}
@@ -129,7 +141,7 @@ async function startDebug(_: ExtensionContext, config: DebugConfig): Promise<boo
129
141
return vscode . debug . startDebugging ( undefined , mergedDebugConfig ) ;
130
142
}
131
143
132
- async function startLanguageServer ( context : ExtensionContext , config : LanguageServerConfig ) : Promise < void > {
144
+ async function startLanguageServer ( context : ExtensionContext , config : LanguageServerConfig ) : Promise < boolean > {
133
145
if ( languageClient ) {
134
146
if ( languageClient . diagnostics ) {
135
147
languageClient . diagnostics . clear ( ) ;
@@ -147,6 +159,8 @@ async function startLanguageServer(context: ExtensionContext, config: LanguageSe
147
159
148
160
languageServerDisposable = languageClient . start ( ) ;
149
161
context . subscriptions . push ( languageServerDisposable ) ;
162
+ await languageClient . onReady ( ) ;
163
+ return true ;
150
164
}
151
165
152
166
async function buildLanguageClient ( config : LanguageServerConfig ) : Promise < LanguageClient > {
0 commit comments