Restart the language server when the board is changed

This commit is contained in:
Miro Spönemann 2019-10-02 13:16:10 +02:00
parent 065f9f042b
commit aa4f216544
3 changed files with 26 additions and 21 deletions

View File

@ -1,10 +1,8 @@
import { injectable, inject, postConstruct } from 'inversify';
import { BaseLanguageClientContribution, NotificationType } from '@theia/languages/lib/browser';
import { BaseLanguageClientContribution } from '@theia/languages/lib/browser';
import { BoardsServiceClientImpl } from '../boards/boards-service-client-impl';
import { BoardsConfig } from '../boards/boards-config';
const SELECTED_BOARD = new NotificationType<BoardsConfig.Config, void>('arduino/selectedBoard');
@injectable()
export class ArduinoLanguageClientContribution extends BaseLanguageClientContribution {
@ -19,27 +17,24 @@ export class ArduinoLanguageClientContribution extends BaseLanguageClientContrib
return ['**/*.ino'];
}
private cancelationToken?: CancelationToken;
@inject(BoardsServiceClientImpl)
protected readonly boardsServiceClient: BoardsServiceClientImpl;
protected boardConfig?: BoardsConfig.Config;
@postConstruct()
protected init() {
this.boardsServiceClient.onBoardsConfigChanged(this.selectBoard.bind(this));
}
async selectBoard(config: BoardsConfig.Config): Promise<void> {
// The board configuration may change multiple times before the language client is activated.
const token: CancelationToken = {};
this.cancelationToken = token;
const lc = await this.languageClient;
if (this.cancelationToken === token) {
lc.sendNotification(SELECTED_BOARD, config);
this.cancelationToken = undefined;
}
selectBoard(config: BoardsConfig.Config): void {
this.boardConfig = config;
// Force a restart to send the new board config to the language server
this.restart();
}
protected getStartParameters(): BoardsConfig.Config | undefined {
return this.boardConfig;
}
}
type CancelationToken = {}

View File

@ -46,7 +46,8 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
bind(BackendApplicationContribution).toService(ArduinoDaemon);
// Language server
bind(LanguageServerContribution).to(ArduinoLanguageServerContribution).inSingletonScope();
bind(ArduinoLanguageServerContribution).toSelf().inSingletonScope();
bind(LanguageServerContribution).toService(ArduinoLanguageServerContribution);
// Library service
const libraryServiceConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService }) => {
@ -64,10 +65,9 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
});
bind(ConnectionContainerModule).toConstantValue(sketchesServiceConnectionModule);
// Config service
bind(ConfigServiceImpl).toSelf().inSingletonScope();
bind(ConfigService).toService(ConfigServiceImpl);
// Config service
const configServiceConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService }) => {
bindBackendService(ConfigServicePath, ConfigService);
});

View File

@ -2,7 +2,8 @@ import * as which from 'which';
import * as os from 'os';
import { join, delimiter } from 'path';
import { injectable } from 'inversify';
import { BaseLanguageServerContribution, IConnection } from '@theia/languages/lib/node';
import { BaseLanguageServerContribution, IConnection, LanguageServerStartOptions } from '@theia/languages/lib/node';
import { Board } from '../../common/protocol/boards-service';
@injectable()
export class ArduinoLanguageServerContribution extends BaseLanguageServerContribution {
@ -22,12 +23,21 @@ export class ArduinoLanguageServerContribution extends BaseLanguageServerContrib
return this.description.name;
}
async start(clientConnection: IConnection): Promise<void> {
async start(clientConnection: IConnection, options: LanguageServerStartOptions): Promise<void> {
const clangd = await this.resolveExecutable('clangd');
const languageServer = await this.resolveExecutable('arduino-language-server');
const cli = await this.resolveExecutable('arduino-cli');
// Add '-log' argument to enable logging to files
const args: string[] = ['-clangd', clangd, '-cli', cli];
if (options.parameters && options.parameters.selectedBoard) {
const board = options.parameters.selectedBoard as Board;
if (board.fqbn) {
args.push('-fqbn', board.fqbn);
}
if (board.name) {
args.push('-board-name', `"${board.name}"`);
}
}
console.log(`Starting language server ${languageServer} ${args.join(' ')}`);
const serverConnection = await this.createProcessStreamConnectionAsync(languageServer, args);
this.forward(clientConnection, serverConnection);