1
- import { posix } from 'path' ;
1
+ import { stat } from 'fs' ;
2
+ import { basename } from 'path' ;
3
+ import { promisify } from 'util' ;
2
4
import { spawnSync } from 'child_process' ;
3
5
import deepEqual from 'deep-equal' ;
4
6
import WebRequest from 'web-request' ;
@@ -9,14 +11,17 @@ interface LanguageServerConfig {
9
11
readonly lsPath : string ;
10
12
readonly cliPath : string ;
11
13
readonly clangdPath : string ;
12
- /**
13
- * Filesystem path pointing to the folder that contains the `compile_commands.json` file.
14
- */
15
- readonly compileCommandsPath ?: string ;
16
14
readonly board : {
17
15
readonly fqbn : string ;
18
16
readonly name ?: string ;
19
17
}
18
+ /**
19
+ * `true` if the LS should generate log files into the default location. The default location is `cwd` of the process. It's very often the same
20
+ * as the workspace root of the IDE, aka the sketch folder.
21
+ * When it is a string, it is the folder where the log files should be generated. If the path is invalid (does not exist, not a folder),
22
+ * the log files will be generated into the default location.
23
+ */
24
+ readonly log ?: boolean | string ;
20
25
readonly env ?: any ;
21
26
readonly flags ?: string [ ] ;
22
27
}
@@ -75,7 +80,7 @@ async function startDebug(_: ExtensionContext, config: DebugConfig): Promise<boo
75
80
if ( ! rawStdout ) {
76
81
if ( rawStdErr ) {
77
82
if ( rawStdErr . indexOf ( 'compiled sketch not found in' ) !== - 1 ) {
78
- vscode . window . showErrorMessage ( `Sketch '${ posix . basename ( config . sketchPath ) } ' was not compiled. Please compile the sketch and start debugging again.` ) ;
83
+ vscode . window . showErrorMessage ( `Sketch '${ basename ( config . sketchPath ) } ' was not compiled. Please compile the sketch and start debugging again.` ) ;
79
84
} else {
80
85
vscode . window . showErrorMessage ( rawStdErr ) ;
81
86
}
@@ -129,32 +134,44 @@ async function startLanguageServer(context: ExtensionContext, config: LanguageSe
129
134
}
130
135
if ( ! languageClient || ! deepEqual ( latestConfig , config ) ) {
131
136
latestConfig = config ;
132
- languageClient = buildLanguageClient ( config ) ;
137
+ languageClient = await buildLanguageClient ( config ) ;
133
138
crashCount = 0 ;
134
139
}
135
140
136
141
languageServerDisposable = languageClient . start ( ) ;
137
142
context . subscriptions . push ( languageServerDisposable ) ;
138
143
}
139
144
140
- function buildLanguageClient ( config : LanguageServerConfig ) : LanguageClient {
145
+ async function buildLanguageClient ( config : LanguageServerConfig ) : Promise < LanguageClient > {
141
146
if ( ! serverOutputChannel ) {
142
147
serverOutputChannel = vscode . window . createOutputChannel ( 'Arduino Language Server' ) ;
143
148
}
144
149
if ( ! serverTraceChannel ) {
145
150
serverTraceChannel = vscode . window . createOutputChannel ( 'Arduino Language Server (trace)' ) ;
146
151
}
147
- const { lsPath : command , clangdPath, cliPath, board, flags, env, compileCommandsPath } = config ;
152
+ const { lsPath : command , clangdPath, cliPath, board, flags, env, log } = config ;
148
153
const args = [ '-clangd' , clangdPath , '-cli' , cliPath , '-fqbn' , board . fqbn ] ;
149
154
if ( board . name ) {
150
155
args . push ( '-board-name' , board . name ) ;
151
156
}
152
- if ( compileCommandsPath ) {
153
- args . push ( '-compile-commands-dir' , compileCommandsPath ) ;
154
- }
155
157
if ( flags && flags . length ) {
156
158
args . push ( ...flags ) ;
157
159
}
160
+ if ( ! ! log ) {
161
+ args . push ( '-log' ) ;
162
+ let logPath : string | undefined = undefined ;
163
+ if ( typeof log === 'string' ) {
164
+ try {
165
+ const stats = await promisify ( stat ) ( log ) ;
166
+ if ( stats . isDirectory ( ) ) {
167
+ logPath = log ;
168
+ }
169
+ } catch { }
170
+ }
171
+ if ( logPath ) {
172
+ args . push ( '-logpath' , logPath ) ;
173
+ }
174
+ }
158
175
return new LanguageClient (
159
176
'ino' ,
160
177
'Arduino Language Server' ,
@@ -165,7 +182,7 @@ function buildLanguageClient(config: LanguageServerConfig): LanguageClient {
165
182
} ,
166
183
{
167
184
initializationOptions : { } ,
168
- documentSelector : [ 'ino' ] ,
185
+ documentSelector : [ 'ino' , 'c' , 'cpp' , 'h' , 'hpp' ] ,
169
186
uriConverters : {
170
187
code2Protocol : ( uri : vscode . Uri ) : string => ( uri . scheme ? uri : uri . with ( { scheme : 'file' } ) ) . toString ( ) ,
171
188
protocol2Code : ( uri : string ) => vscode . Uri . parse ( uri )
0 commit comments