这几天折腾使用Visual Studio Code 编写 ESP8266 non_os 代码,基本设置都没问题,但是就是自动补全折腾很久没弄好。在折腾了一圈插件之后(包括Auto Import, C++ Intellisense, CTags Support, scope4code, c/c++ Clang Command Adapter,platformIO IDE),还是没法实现结构体及其成员的自动完成。
折腾了一晚上,第二天继续折腾,最后发现其实ms官方的 ms-vscode.cpptools 已经很好用了。自动补全和跳转定义都没问题,所以暂时把其它的插件全部禁用。
自动补全的关键是 includePath 的设置,默认的情况下includePath 在 c_cpp_properties.json 中设置,但是只针对当前工作区有效,网上的文章都只讨论 c_cpp_properties.json 的设置,其实Visual Studio Code官方从2017年12月29日开始就已经引入了全局设置的参数 Customizing Default Settings,当和 c_cpp_properties.json 冲突时,以全局的设置为准。
首选项-设置,从设置页面找到链接打开setting.json,添加如下内容 (本例适用于机智云官方的 ESP8266 SoC 代码,其他项目请自行修改路径):
//如下内容适用于机智云官方的 ESP8266 SoC 代码,其他项目请自行修改路径
"[cpp]": {
"editor.quickSuggestions": true
},
"[c]": {
"editor.quickSuggestions": true
},
"C_Cpp.default.includePath": [
"${workspaceFolder}",
"${workspaceFolder}/src",
"${workspaceFolder}/app/driver",
"${workspaceFolder}/app/include",
"${workspaceFolder}/app/Gizwits",
"${workspaceFolder}/app/include/driver",
"${workspaceFolder}/include",
"${workspaceFolder}/app/Utils"
],
"C_Cpp.default.browse.databaseFilename": "${workspaceFolder}/.vscode/.browse.c_cpp.db"
这样就可以了。
亲测结构体及结构体成员都能很好地自动完成。
注意:很多网上的文章都使用了 “${workspaceRoot}”,但是已经被官方弃用了,现在应该使用“${workspaceFolder}”以便支持工作区的多目录特性。
附全局参数列表:
New VS Code settings
The following C_Cpp.default.*
settings map to each of the properties in a configuration block of c_cpp_properties.json. Namely:
C_Cpp.default.includePath : string[]
C_Cpp.default.defines : string[]
C_Cpp.default.compileCommands : string
C_Cpp.default.macFrameworkPath : string[]
C_Cpp.default.forcedIncludes : string[]
C_Cpp.default.intelliSenseMode : string
C_Cpp.default.compilerPath : string
C_Cpp.default.cStandard : c89 | c99 | c11
C_Cpp.default.cppStandard : c++98 | c++03 | c++11 | c++14 | c++17
C_Cpp.default.browse.path : string[]
C_Cpp.default.browse.databaseFilename : string
C_Cpp.default.browse.limitSymbolsToIncludedHeaders : boolean