22// Licensed under the MIT license.
33
44import * as ccp from "cocopa" ;
5- import * as fs from "fs" ;
65import * as os from "os" ;
76import * as path from "path" ;
8- import * as tp from "typed-promisify" ;
97
108import * as constants from "../common/constants" ;
119import { arduinoChannel } from "../common/outputChannel" ;
@@ -45,11 +43,6 @@ export function isCompilerParserEnabled(dc?: DeviceContext) {
4543 *
4644 * Possible enhancements:
4745 *
48- * * Parse c++ standard from arduino command line
49- *
50- * Arduino currently sets the C++ standard during compilation with the
51- * flag -std=gnu++11
52- *
5346 * * Order of includes: Perhaps insert the internal includes at the front
5447 * as at least for the forcedIncludes IntelliSense seems to take the
5548 * order into account.
@@ -79,34 +72,30 @@ export function makeCompilerParserContext(dc: DeviceContext): ICoCoPaContext {
7972
8073 // Normalize compiler and include paths (resolve ".." and ".")
8174 runner . result . normalize ( ) ;
82-
83- runner . result . includes = await removeInvalidDirs ( runner . result . includes ) ;
75+ // Remove invalid paths
76+ await runner . result . cleanup ( ) ;
8477
8578 // Search for Arduino.h in the include paths - we need it for a
8679 // forced include - users expect Arduino symbols to be available
8780 // in main sketch without having to include the header explicitly
88- const ardHeader = await locateArduinoHeader ( runner . result . includes ) ;
89- const forcedIncludes = ardHeader
90- ? [ ardHeader ]
81+ const ardHeader = await runner . result . findFile ( "Arduino.h" ) ;
82+ const forcedIncludes = ardHeader . length > 0
83+ ? ardHeader
9184 : undefined ;
92- if ( ! ardHeader ) {
85+ if ( ! forcedIncludes ) {
9386 arduinoChannel . warning ( "Unable to locate \"Arduino.h\" within IntelliSense include paths." ) ;
9487 }
9588
96- // TODO: check what kind of result we've got: gcc or other architecture:
97- // and instantiate content accordingly (to be implemented within cocopa)
89+ // The C++ standard is set to the following default value if no compiler flag has been found.
9890 const content = new ccp . CCppPropertiesContentResult ( runner . result ,
9991 constants . C_CPP_PROPERTIES_CONFIG_NAME ,
10092 ccp . CCppPropertiesISMode . Gcc_X64 ,
10193 ccp . CCppPropertiesCStandard . C11 ,
102- // as of 1.8.11 arduino is on C++11
10394 ccp . CCppPropertiesCppStandard . Cpp11 ,
10495 forcedIncludes ) ;
10596 try {
106- const cmd = os . platform ( ) === "darwin"
107- ? "Cmd + Alt + I"
108- : "Ctrl + Alt + I" ;
109- const help = `To manually rebuild your IntelliSense configuration run "${ cmd } "` ;
97+ const cmd = os . platform ( ) === "darwin" ? "Cmd" : "Ctrl" ;
98+ const help = `To manually rebuild your IntelliSense configuration run "${ cmd } +Alt+I"` ;
11099 const pPath = path . join ( ArduinoWorkspace . rootPath , constants . CPP_CONFIG_FILE ) ;
111100 const prop = new ccp . CCppProperties ( ) ;
112101 prop . read ( pPath ) ;
@@ -127,27 +116,6 @@ export function makeCompilerParserContext(dc: DeviceContext): ICoCoPaContext {
127116 }
128117} ;
129118
130- // TODO: move to cocopa
131- /**
132- * Filter directory list by directories by their existence.
133- * @param dirs Directories to be checked.
134- * @returns The list of directories which exist.
135- */
136- async function removeInvalidDirs ( dirs : string [ ] ) {
137- const fsstat = tp . promisify ( fs . stat ) ;
138- const res : string [ ] = [ ] ;
139- for ( const d of dirs ) {
140- try {
141- const s = await fsstat ( d ) ;
142- if ( s . isDirectory ( ) ) {
143- res . push ( d ) ;
144- }
145- } catch ( e ) {
146- }
147- }
148- return res ;
149- }
150-
151119/**
152120 * Assembles compiler parser engines which then will be used to find the main
153121 * sketch's compile command and parse the infomation from it required for
@@ -165,55 +133,6 @@ function makeCompilerParserEngines(dc: DeviceContext) {
165133 return [ gccParserEngine ] ;
166134}
167135
168- /**
169- * Search directories recursively for a file.
170- * @param dir Directory where the search should begin.
171- * @param what The file we're looking for.
172- * @returns The path of the directory which contains the file else undefined.
173- */
174- async function findDirContaining ( dir : string , what : string ) : Promise < string | undefined > {
175- const readdir = tp . promisify ( fs . readdir ) ;
176- const fsstat = tp . promisify ( fs . stat ) ;
177-
178- let entries : string [ ] ;
179- try {
180- entries = await readdir ( dir ) ;
181- } catch ( e ) {
182- return undefined ;
183- }
184- for ( const entry of entries ) {
185- const p = path . join ( dir , entry ) ;
186- const s = await fsstat ( p ) ;
187- if ( s . isDirectory ( ) ) {
188- const result = await findDirContaining ( p , what ) ;
189- if ( result ) {
190- return result ;
191- }
192- } else if ( entry === what ) {
193- return dir ;
194- }
195- }
196- return undefined ;
197- } ;
198-
199- /**
200- * Tries to find the main Arduino header (i.e. Arduino.h) in the given include
201- * paths.
202- * @param includes Array containing all include paths in which we should look
203- * for Arduino.h
204- * @returns The full path of the main Arduino header.
205- */
206- async function locateArduinoHeader ( includes : string [ ] ) {
207- const header = "Arduino.h" ;
208- for ( const i of includes ) {
209- const result = await findDirContaining ( i , header ) ;
210- if ( result ) {
211- return path . join ( result , header ) ;
212- }
213- }
214- return undefined ;
215- }
216-
217136/**
218137 * Possible states of AnalysisManager's state machine.
219138 */
0 commit comments