fix: restart LS on lib/core change, client re-init

Closes #670

Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
This commit is contained in:
Akos Kitta 2023-01-09 10:36:15 +01:00 committed by Akos Kitta
parent 197cea2a60
commit 432f3654df

View File

@ -1,15 +1,17 @@
import { Mutex } from 'async-mutex'; import { DisposableCollection } from '@theia/core/lib/common/disposable';
import { inject, injectable } from '@theia/core/shared/inversify'; import { inject, injectable } from '@theia/core/shared/inversify';
import { Mutex } from 'async-mutex';
import { import {
ArduinoDaemon, ArduinoDaemon,
BoardsService, BoardsService,
ExecutableService, ExecutableService,
} from '../../common/protocol'; } from '../../common/protocol';
import { HostedPluginEvents } from '../hosted-plugin-events';
import { SketchContribution, URI } from './contribution';
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl'; import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
import { BoardsConfig } from '../boards/boards-config'; import { BoardsConfig } from '../boards/boards-config';
import { BoardsServiceProvider } from '../boards/boards-service-provider'; import { BoardsServiceProvider } from '../boards/boards-service-provider';
import { HostedPluginEvents } from '../hosted-plugin-events';
import { NotificationCenter } from '../notification-center';
import { SketchContribution, URI } from './contribution';
@injectable() @injectable()
export class InoLanguage extends SketchContribution { export class InoLanguage extends SketchContribution {
@ -28,8 +30,12 @@ export class InoLanguage extends SketchContribution {
@inject(BoardsServiceProvider) @inject(BoardsServiceProvider)
private readonly boardsServiceProvider: BoardsServiceProvider; private readonly boardsServiceProvider: BoardsServiceProvider;
@inject(NotificationCenter)
private readonly notificationCenter: NotificationCenter;
private readonly toDispose = new DisposableCollection();
private readonly languageServerStartMutex = new Mutex();
private languageServerFqbn?: string; private languageServerFqbn?: string;
private languageServerStartMutex = new Mutex();
override onReady(): void { override onReady(): void {
const start = ( const start = (
@ -43,27 +49,41 @@ export class InoLanguage extends SketchContribution {
} }
} }
}; };
this.boardsServiceProvider.onBoardsConfigChanged(start); const forceRestart = () => {
this.hostedPluginEvents.onPluginsDidStart(() => start(this.boardsServiceProvider.boardsConfig, true);
start(this.boardsServiceProvider.boardsConfig) };
); this.toDispose.pushAll([
this.hostedPluginEvents.onPluginsWillUnload( this.boardsServiceProvider.onBoardsConfigChanged(start),
() => (this.languageServerFqbn = undefined) this.hostedPluginEvents.onPluginsDidStart(() =>
); start(this.boardsServiceProvider.boardsConfig)
this.preferences.onPreferenceChanged( ),
({ preferenceName, oldValue, newValue }) => { this.hostedPluginEvents.onPluginsWillUnload(
if (oldValue !== newValue) { () => (this.languageServerFqbn = undefined)
switch (preferenceName) { ),
case 'arduino.language.log': this.preferences.onPreferenceChanged(
case 'arduino.language.realTimeDiagnostics': ({ preferenceName, oldValue, newValue }) => {
start(this.boardsServiceProvider.boardsConfig, true); if (oldValue !== newValue) {
switch (preferenceName) {
case 'arduino.language.log':
case 'arduino.language.realTimeDiagnostics':
forceRestart();
}
} }
} }
} ),
); this.notificationCenter.onLibraryDidInstall(() => forceRestart()),
this.notificationCenter.onLibraryDidUninstall(() => forceRestart()),
this.notificationCenter.onPlatformDidInstall(() => forceRestart()),
this.notificationCenter.onPlatformDidUninstall(() => forceRestart()),
this.notificationCenter.onDidReinitialize(() => forceRestart()),
]);
start(this.boardsServiceProvider.boardsConfig); start(this.boardsServiceProvider.boardsConfig);
} }
onStop(): void {
this.toDispose.dispose();
}
private async startLanguageServer( private async startLanguageServer(
fqbn: string, fqbn: string,
name: string | undefined, name: string | undefined,