Skip to content

Commit aa4f216

Browse files
committedOct 2, 2019
Restart the language server when the board is changed
1 parent 065f9f0 commit aa4f216

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { injectable, inject, postConstruct } from 'inversify';
2-
import { BaseLanguageClientContribution, NotificationType } from '@theia/languages/lib/browser';
2+
import { BaseLanguageClientContribution } from '@theia/languages/lib/browser';
33
import { BoardsServiceClientImpl } from '../boards/boards-service-client-impl';
44
import { BoardsConfig } from '../boards/boards-config';
55

6-
const SELECTED_BOARD = new NotificationType<BoardsConfig.Config, void>('arduino/selectedBoard');
7-
86
@injectable()
97
export class ArduinoLanguageClientContribution extends BaseLanguageClientContribution {
108

@@ -19,27 +17,24 @@ export class ArduinoLanguageClientContribution extends BaseLanguageClientContrib
1917
return ['**/*.ino'];
2018
}
2119

22-
private cancelationToken?: CancelationToken;
23-
2420
@inject(BoardsServiceClientImpl)
2521
protected readonly boardsServiceClient: BoardsServiceClientImpl;
2622

23+
protected boardConfig?: BoardsConfig.Config;
24+
2725
@postConstruct()
2826
protected init() {
2927
this.boardsServiceClient.onBoardsConfigChanged(this.selectBoard.bind(this));
3028
}
3129

32-
async selectBoard(config: BoardsConfig.Config): Promise<void> {
33-
// The board configuration may change multiple times before the language client is activated.
34-
const token: CancelationToken = {};
35-
this.cancelationToken = token;
36-
const lc = await this.languageClient;
37-
if (this.cancelationToken === token) {
38-
lc.sendNotification(SELECTED_BOARD, config);
39-
this.cancelationToken = undefined;
40-
}
30+
selectBoard(config: BoardsConfig.Config): void {
31+
this.boardConfig = config;
32+
// Force a restart to send the new board config to the language server
33+
this.restart();
4134
}
4235

43-
}
36+
protected getStartParameters(): BoardsConfig.Config | undefined {
37+
return this.boardConfig;
38+
}
4439

45-
type CancelationToken = {}
40+
}

‎arduino-ide-extension/src/node/arduino-backend-module.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
4646
bind(BackendApplicationContribution).toService(ArduinoDaemon);
4747

4848
// Language server
49-
bind(LanguageServerContribution).to(ArduinoLanguageServerContribution).inSingletonScope();
49+
bind(ArduinoLanguageServerContribution).toSelf().inSingletonScope();
50+
bind(LanguageServerContribution).toService(ArduinoLanguageServerContribution);
5051

5152
// Library service
5253
const libraryServiceConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService }) => {
@@ -64,10 +65,9 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
6465
});
6566
bind(ConnectionContainerModule).toConstantValue(sketchesServiceConnectionModule);
6667

68+
// Config service
6769
bind(ConfigServiceImpl).toSelf().inSingletonScope();
6870
bind(ConfigService).toService(ConfigServiceImpl);
69-
70-
// Config service
7171
const configServiceConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService }) => {
7272
bindBackendService(ConfigServicePath, ConfigService);
7373
});

‎arduino-ide-extension/src/node/language/arduino-language-server-contribution.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as which from 'which';
22
import * as os from 'os';
33
import { join, delimiter } from 'path';
44
import { injectable } from 'inversify';
5-
import { BaseLanguageServerContribution, IConnection } from '@theia/languages/lib/node';
5+
import { BaseLanguageServerContribution, IConnection, LanguageServerStartOptions } from '@theia/languages/lib/node';
6+
import { Board } from '../../common/protocol/boards-service';
67

78
@injectable()
89
export class ArduinoLanguageServerContribution extends BaseLanguageServerContribution {
@@ -22,12 +23,21 @@ export class ArduinoLanguageServerContribution extends BaseLanguageServerContrib
2223
return this.description.name;
2324
}
2425

25-
async start(clientConnection: IConnection): Promise<void> {
26+
async start(clientConnection: IConnection, options: LanguageServerStartOptions): Promise<void> {
2627
const clangd = await this.resolveExecutable('clangd');
2728
const languageServer = await this.resolveExecutable('arduino-language-server');
2829
const cli = await this.resolveExecutable('arduino-cli');
2930
// Add '-log' argument to enable logging to files
3031
const args: string[] = ['-clangd', clangd, '-cli', cli];
32+
if (options.parameters && options.parameters.selectedBoard) {
33+
const board = options.parameters.selectedBoard as Board;
34+
if (board.fqbn) {
35+
args.push('-fqbn', board.fqbn);
36+
}
37+
if (board.name) {
38+
args.push('-board-name', `"${board.name}"`);
39+
}
40+
}
3141
console.log(`Starting language server ${languageServer} ${args.join(' ')}`);
3242
const serverConnection = await this.createProcessStreamConnectionAsync(languageServer, args);
3343
this.forward(clientConnection, serverConnection);

0 commit comments

Comments
 (0)