1
1
import deepEqual from 'deep-equal' ;
2
2
import WebRequest from 'web-request' ;
3
+ import { join } from 'path' ;
4
+ import { spawnSync } from 'child_process' ;
3
5
import vscode , { ExtensionContext } from 'vscode' ;
4
6
import { LanguageClient , CloseAction , ErrorAction , InitializeError , Message , RevealOutputChannelOn } from 'vscode-languageclient' ;
5
7
@@ -19,6 +21,29 @@ interface LanguageServerConfig {
19
21
readonly flags ?: string [ ] ;
20
22
}
21
23
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
+
22
47
let languageClient : LanguageClient | undefined ;
23
48
let languageServerDisposable : vscode . Disposable | undefined ;
24
49
let latestConfig : LanguageServerConfig | undefined ;
@@ -28,10 +53,52 @@ let crashCount = 0;
28
53
29
54
export function activate ( context : ExtensionContext ) {
30
55
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 ) )
32
58
) ;
33
59
}
34
60
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
+
35
102
async function startLanguageServer ( context : ExtensionContext , config : LanguageServerConfig ) : Promise < void > {
36
103
if ( languageClient ) {
37
104
if ( languageClient . diagnostics ) {
0 commit comments