VSC w/ ESP-IDF: how do I configure LSP to have autocompletion for bridged headers?

Hi :wave: I wonder if it's somehow possible to have autocompletion I get for regular Swift code:

but for the C functions imported in BridgingHeader.h, like led_driver_set_brightness() or led_driver_set_saturation(). Per Using SourceKit-LSP with Embedded Projects I've attempted to specify -import-bridging-header to the LSP like so, but I couldn't get it working.

.vscode/settings.json:

"swift.buildArguments": ["-import-bridging-header", "/Users/[redacted]/swift-matter-examples/led-blink/main/BridgingHeader.h"]

or via .sourcekit-lsp/config.json:

{
  "swiftPM": {
    "swiftCompilerFlags": "-import-bridging-header", "/Users/[redacted]/swift-matter-examples/led-blink/main/BridgingHeader.h"]
  }
}

Not sure if that matters, but I am able to successfully compile and upload the blink example to my ESP32C6. As far as I remember, in Xcode the autocomplete does suggest things imported through bridging header, so my guess is that it should be possible. Any help would be greatly appraciated.

5 Likes

any updates after the new release of VS Code extension?

IDK don't know if this works on the embedded headers, but if you add something like this with your SDK name, it might work same as Static Linux build:

{
  "swiftPM": {
    "swiftSDK": "x86_64-swift-linux-musl"
  }
}

in VSCode project, named .sourcekit-lsp/config.json

I asked a similar question here elsewhere in regards to another project that had a similar structure, albeit it uses makefiles instead of cmake.

TL:DR it appears that what we were trying was for SwiftPM project, which we are not using.

In your case, you appear to be using CMAKE, so you can seemingly, in fact, achieve autocomplete using CMAKE_EXPORT_COMPILE_COMMANDS to create the compilation database.

For those using makefiles, you apparently need to craft the compilation database json yourself.