1- const debounce = require ( 'lodash.debounce' ) ;
1+ import { Mutex } from 'async-mutex' ;
22import { MAIN_MENU_BAR , MenuContribution , MenuModelRegistry , SelectionService , ILogger } from '@theia/core' ;
33import {
44 ContextMenuRenderer ,
@@ -201,7 +201,6 @@ export class ArduinoFrontendContribution implements FrontendApplicationContribut
201201 if ( selectedBoard ) {
202202 const { name, fqbn } = selectedBoard ;
203203 if ( fqbn ) {
204- await this . hostedPluginSupport . didStart ;
205204 this . startLanguageServer ( fqbn , name ) ;
206205 }
207206 }
@@ -225,33 +224,44 @@ export class ArduinoFrontendContribution implements FrontendApplicationContribut
225224 } ) ;
226225 }
227226
228- protected startLanguageServer = debounce ( ( fqbn : string , name : string | undefined ) => this . doStartLanguageServer ( fqbn , name ) ) ;
229- protected async doStartLanguageServer ( fqbn : string , name : string | undefined ) : Promise < void > {
230- this . logger . info ( `Starting language server: ${ fqbn } ` ) ;
231- const log = this . arduinoPreferences . get ( 'arduino.language.log' ) ;
232- let currentSketchPath : string | undefined = undefined ;
233- if ( log ) {
234- const currentSketch = await this . sketchServiceClient . currentSketch ( ) ;
235- if ( currentSketch ) {
236- currentSketchPath = await this . fileSystem . fsPath ( new URI ( currentSketch . uri ) ) ;
227+ protected languageServerFqbn ?: string ;
228+ protected languageServerStartMutex = new Mutex ( ) ;
229+ protected async startLanguageServer ( fqbn : string , name : string | undefined ) : Promise < void > {
230+ const release = await this . languageServerStartMutex . acquire ( ) ;
231+ try {
232+ if ( fqbn === this . languageServerFqbn ) {
233+ // NOOP
234+ return ;
237235 }
238- }
239- const { clangdUri, cliUri, lsUri } = await this . executableService . list ( ) ;
240- const [ clangdPath , cliPath , lsPath ] = await Promise . all ( [
241- this . fileSystem . fsPath ( new URI ( clangdUri ) ) ,
242- this . fileSystem . fsPath ( new URI ( cliUri ) ) ,
243- this . fileSystem . fsPath ( new URI ( lsUri ) ) ,
244- ] ) ;
245- this . commandRegistry . executeCommand ( 'arduino.languageserver.start' , {
246- lsPath,
247- cliPath,
248- clangdPath,
249- log : currentSketchPath ? currentSketchPath : log ,
250- board : {
251- fqbn,
252- name : name ? `"${ name } "` : undefined
236+ await this . hostedPluginSupport . didStart ;
237+ this . logger . info ( `Starting language server: ${ fqbn } ` ) ;
238+ const log = this . arduinoPreferences . get ( 'arduino.language.log' ) ;
239+ let currentSketchPath : string | undefined = undefined ;
240+ if ( log ) {
241+ const currentSketch = await this . sketchServiceClient . currentSketch ( ) ;
242+ if ( currentSketch ) {
243+ currentSketchPath = await this . fileSystem . fsPath ( new URI ( currentSketch . uri ) ) ;
244+ }
253245 }
254- } ) ;
246+ const { clangdUri, cliUri, lsUri } = await this . executableService . list ( ) ;
247+ const [ clangdPath , cliPath , lsPath ] = await Promise . all ( [
248+ this . fileSystem . fsPath ( new URI ( clangdUri ) ) ,
249+ this . fileSystem . fsPath ( new URI ( cliUri ) ) ,
250+ this . fileSystem . fsPath ( new URI ( lsUri ) ) ,
251+ ] ) ;
252+ this . languageServerFqbn = await this . commandRegistry . executeCommand ( 'arduino.languageserver.start' , {
253+ lsPath,
254+ cliPath,
255+ clangdPath,
256+ log : currentSketchPath ? currentSketchPath : log ,
257+ board : {
258+ fqbn,
259+ name : name ? `"${ name } "` : undefined
260+ }
261+ } ) ;
262+ } finally {
263+ release ( ) ;
264+ }
255265 }
256266
257267 registerToolbarItems ( registry : TabBarToolbarRegistry ) : void {
0 commit comments