Skip to content

Notify running LS about build complete. #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@
"title": "Restart Language Server",
"category": "Arduino"
},
{
"command": "arduino.languageserver.notifyBuildDidComplete",
"title": "Notify Build Did Complete",
"category": "Arduino"
},
{
"command": "arduino.debug.start",
"title": "Start Debug",
Expand Down
30 changes: 22 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import deepmerge from 'deepmerge';
import { Mutex } from 'async-mutex';
import vscode, { ExtensionContext } from 'vscode';
import { LanguageClient, CloseAction, ErrorAction, InitializeError, Message, RevealOutputChannelOn } from 'vscode-languageclient';
import { DidCompleteBuildNotification, DidCompleteBuildParams } from './protocol';

interface LanguageServerConfig {
readonly lsPath: string;
Expand Down Expand Up @@ -72,10 +73,10 @@ export function activate(context: ExtensionContext) {
const started = await startLanguageServer(context, config);
languageServerIsRunning = started;
return languageServerIsRunning ? config.board.fqbn : undefined;
} catch (e) {
console.log(e);
} catch (err) {
console.error('Failed to start the language server.', err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you prefer the console.error over the showError function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question; the original code does not show the error if the LS failed to start. I did not want to change this behavior, but I could do it.

Note, if we use vscode.window.showErrorMessage here, it will be a notification message in IDE2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only effective change here is that I use console.error instead of console.log with an additional Failed to start the language server. message.

languageServerIsRunning = false;
throw e;
throw err;
} finally {
unlock();
}
Expand All @@ -94,7 +95,14 @@ export function activate(context: ExtensionContext) {
return vscode.commands.executeCommand('arduino.languageserver.start', latestConfig);
}
}),
vscode.commands.registerCommand('arduino.debug.start', (config: DebugConfig) => startDebug(context, config))
vscode.commands.registerCommand('arduino.debug.start', (config: DebugConfig) => startDebug(context, config)),
vscode.commands.registerCommand('arduino.languageserver.notifyBuildDidComplete', (params: DidCompleteBuildParams) => {
if (languageClient) {
languageClient.sendNotification(DidCompleteBuildNotification.TYPE, params);
} else {
vscode.window.showWarningMessage('Language server is not running.');
}
})
);
}

Expand All @@ -108,8 +116,7 @@ async function startDebug(_: ExtensionContext, config: DebugConfig): Promise<boo
rawStdout = stdout.trim();
rawStdErr = stderr.trim();
} catch (err) {
const message = err instanceof Error ? err.stack || err.message : 'Unknown error';
vscode.window.showErrorMessage(message);
showError(err);
return false;
}
if (!rawStdout) {
Expand All @@ -125,7 +132,8 @@ async function startDebug(_: ExtensionContext, config: DebugConfig): Promise<boo
try {
info = JSON.parse(rawStdout);
} catch (err) {
vscode.window.showErrorMessage(err);
console.error(`Could not parse JSON: <${rawStdout}>`);
showError(err);
}
if (!info) {
return false;
Expand Down Expand Up @@ -190,7 +198,7 @@ async function startLanguageServer(context: ExtensionContext, config: LanguageSe

async function buildLanguageClient(config: LanguageServerConfig): Promise<LanguageClient> {
const { lsPath: command, clangdPath, cliDaemonAddr, cliDaemonInstance, board, flags, env, log } = config;
const args = ['-clangd', clangdPath, '-cli-daemon-addr', cliDaemonAddr, '-cli-daemon-instance', cliDaemonInstance, '-fqbn', board.fqbn];
const args = ['-clangd', clangdPath, '-cli-daemon-addr', cliDaemonAddr, '-cli-daemon-instance', cliDaemonInstance, '-fqbn', board.fqbn, '-skip-libraries-discovery-on-rebuild'];
if (board.name) {
args.push('-board-name', board.name);
}
Expand Down Expand Up @@ -252,6 +260,12 @@ async function buildLanguageClient(config: LanguageServerConfig): Promise<Langua
);
}

function showError(err: unknown): void {
console.error(err);
const message = err instanceof Error ? err.message : typeof err === 'string' ? err : String(err);
vscode.window.showErrorMessage(message);
}

/**
* Instead of writing the `launch.json` to the workspace, the file is written to the temporary binary output location.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/protocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NotificationType, DocumentUri } from 'vscode-languageclient';

export interface DidCompleteBuildParams {
readonly buildOutputUri: DocumentUri;
}
export namespace DidCompleteBuildNotification {
export const TYPE = new NotificationType<DidCompleteBuildParams, void>('ino/didCompleteBuild');
}