Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2f61dd9

Browse files
Merge pull request #3 from arduino/cortex-debug--cli
Exposed a command to start the debugger from the IDE.
2 parents 141a3f6 + a503954 commit 2f61dd9

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

‎package.json‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
"theme": "light"
6767
},
6868
"activationEvents": [
69+
"onLanguage:ino",
70+
"onCommand:arduino.debug.start",
6971
"onCommand:arduino.languageserver.start"
7072
],
7173
"contributes": {
@@ -93,8 +95,13 @@
9395
"commands": [
9496
{
9597
"command": "arduino.languageserver.start",
96-
"title": "Arduino: Start/Restart Language Server",
97-
"description": "Start the language server or restart if there is a running instance"
98+
"title": "Start Language Server",
99+
"category": "Arduino"
100+
},
101+
{
102+
"command": "arduino.debug.start",
103+
"title": "Start Debug",
104+
"category": "Arduino"
98105
}
99106
]
100107
}

‎src/extension.ts‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import deepEqual from 'deep-equal';
22
import WebRequest from 'web-request';
3+
import { join } from 'path';
4+
import { spawnSync } from 'child_process';
35
import vscode, { ExtensionContext } from 'vscode';
46
import { LanguageClient, CloseAction, ErrorAction, InitializeError, Message, RevealOutputChannelOn } from 'vscode-languageclient';
57

@@ -19,6 +21,29 @@ interface LanguageServerConfig {
1921
readonly flags?: string[];
2022
}
2123

24+
interface DebugConfig {
25+
readonly cliPath: string;
26+
readonly board: {
27+
readonly fqbn: string;
28+
readonly name?: string;
29+
}
30+
readonly sketchPath: string;
31+
}
32+
33+
interface DebugInfo {
34+
readonly executable: string;
35+
readonly toolchain: string;
36+
readonly toolchain_path: string;
37+
readonly toolchain_prefix: string;
38+
readonly server: string;
39+
readonly server_path: string;
40+
readonly server_configuration: {
41+
readonly path: string;
42+
readonly script: string;
43+
readonly scripts_dir: string;
44+
}
45+
}
46+
2247
let languageClient: LanguageClient | undefined;
2348
let languageServerDisposable: vscode.Disposable | undefined;
2449
let latestConfig: LanguageServerConfig | undefined;
@@ -28,10 +53,52 @@ let crashCount = 0;
2853

2954
export function activate(context: ExtensionContext) {
3055
context.subscriptions.push(
31-
vscode.commands.registerCommand('arduino.languageserver.start', (config: LanguageServerConfig) => startLanguageServer(context, config))
56+
vscode.commands.registerCommand('arduino.languageserver.start', (config: LanguageServerConfig) => startLanguageServer(context, config)),
57+
vscode.commands.registerCommand('arduino.debug.start', (config: DebugConfig) => startDebug(context, config))
3258
);
3359
}
3460

61+
async function startDebug(_: ExtensionContext, config: DebugConfig): Promise<boolean> {
62+
let info: DebugInfo | undefined = undefined;
63+
try {
64+
const args = ['debug', '-I', '-b', config.board.fqbn, config.sketchPath, '--format', 'json'];
65+
const rawInfo = spawnSync(config.cliPath, args, { encoding: 'utf8' }).stdout.trim();
66+
info = JSON.parse(rawInfo);
67+
} catch (err) {
68+
const message = err instanceof Error ? err.stack || err.message : 'Unknown error';
69+
vscode.window.showErrorMessage(message);
70+
return false;
71+
}
72+
if (!info) {
73+
return false;
74+
}
75+
const debugConfig = {
76+
cwd: '${workspaceRoot}',
77+
name: 'Arduino',
78+
request: 'launch',
79+
type: 'cortex-debug',
80+
executable: info.executable,
81+
servertype: info.server,
82+
serverpath: join(info.server_path, info.server),
83+
armToolchainPath: info.toolchain_path,
84+
configFiles: [
85+
info.server_configuration.script
86+
]
87+
};
88+
// Create the `launch.json` if it does not exist. Otherwise, update the existing.
89+
const configuration = vscode.workspace.getConfiguration();
90+
const launchConfig = {
91+
version: '0.2.0',
92+
'configurations': [
93+
{
94+
...debugConfig
95+
}
96+
]
97+
};
98+
await configuration.update('launch', launchConfig, false);
99+
return vscode.debug.startDebugging(undefined, debugConfig);
100+
}
101+
35102
async function startLanguageServer(context: ExtensionContext, config: LanguageServerConfig): Promise<void> {
36103
if (languageClient) {
37104
if (languageClient.diagnostics) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /