From c047d4e5da2d467f0b89aef397845201bb52f58b Mon Sep 17 00:00:00 2001 From: Paul LeMarquand Date: Fri, 6 Dec 2024 11:23:25 -0500 Subject: [PATCH] Activate extension when compile_flags.txt or buildServer.json is present (#1240) SourceKit-LSP can handle projects configured with the paired down clang `compile_flags.txt` configuration file, as well as projects configured with the Build Server Protocol's `buildServer.json`. Activate the extension if the folder added to the workspace contains either of these files in the root. Issue: #1087 --- .../test/cmake-compile-flags/CMakeLists.txt | 5 ++++ .../cmake-compile-flags/compile_flags.txt | 8 ++++++ assets/test/cmake-compile-flags/main.cpp | 7 +++++ package.json | 2 ++ src/WorkspaceContext.ts | 8 +++--- .../ExtensionActivation.test.ts | 27 +++++++++++++++++++ 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 assets/test/cmake-compile-flags/CMakeLists.txt create mode 100644 assets/test/cmake-compile-flags/compile_flags.txt create mode 100644 assets/test/cmake-compile-flags/main.cpp diff --git a/assets/test/cmake-compile-flags/CMakeLists.txt b/assets/test/cmake-compile-flags/CMakeLists.txt new file mode 100644 index 000000000..a8b35d019 --- /dev/null +++ b/assets/test/cmake-compile-flags/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 2.4) + +project(hello_world) + +add_executable(app main.cpp) \ No newline at end of file diff --git a/assets/test/cmake-compile-flags/compile_flags.txt b/assets/test/cmake-compile-flags/compile_flags.txt new file mode 100644 index 000000000..af1c2d2a9 --- /dev/null +++ b/assets/test/cmake-compile-flags/compile_flags.txt @@ -0,0 +1,8 @@ +/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ +-isysroot +/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk +-mmacosx-version-min=13.0 +-o +CMakeFiles/app.dir/main.o +-c +main.cpp \ No newline at end of file diff --git a/assets/test/cmake-compile-flags/main.cpp b/assets/test/cmake-compile-flags/main.cpp new file mode 100644 index 000000000..e52a3fb1e --- /dev/null +++ b/assets/test/cmake-compile-flags/main.cpp @@ -0,0 +1,7 @@ +#include + +int main() +{ + std::cout << "Hello World!\n"; + return 0; +} \ No newline at end of file diff --git a/package.json b/package.json index 830713b71..2de3ff0f5 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,8 @@ "onLanguage:swift", "workspaceContains:Package.swift", "workspaceContains:compile_commands.json", + "workspaceContains:compile_flags.txt", + "workspaceContains:buildServer.json", "onDebugResolve:swift-lldb" ], "main": "./dist/src/extension.js", diff --git a/src/WorkspaceContext.ts b/src/WorkspaceContext.ts index fdc26f697..cba4a7c47 100644 --- a/src/WorkspaceContext.ts +++ b/src/WorkspaceContext.ts @@ -349,7 +349,7 @@ export class WorkspaceContext implements vscode.Disposable { } async searchForPackages(folder: vscode.Uri, workspaceFolder: vscode.WorkspaceFolder) { - // add folder if Package.swift/compile_commands.json exists + // add folder if Package.swift/compile_commands.json/compile_flags.txt/buildServer.json exists if (await this.isValidWorkspaceFolder(folder.fsPath)) { await this.addPackageFolder(folder, workspaceFolder); return; @@ -614,13 +614,15 @@ export class WorkspaceContext implements vscode.Disposable { /** * Return if folder is considered a valid root folder ie does it contain a SwiftPM - * Package.swift or a CMake compile_commands.json + * Package.swift or a CMake compile_commands.json, compile_flags.txt, or a BSP buildServer.json. */ async isValidWorkspaceFolder(folder: string): Promise { return ( ((await pathExists(folder, "Package.swift")) && !configuration.disableSwiftPMIntegration) || - (await pathExists(folder, "compile_commands.json")) + (await pathExists(folder, "compile_commands.json")) || + (await pathExists(folder, "compile_flags.txt")) || + (await pathExists(folder, "buildServer.json")) ); } diff --git a/test/integration-tests/ExtensionActivation.test.ts b/test/integration-tests/ExtensionActivation.test.ts index 52913d17a..a878cde5a 100644 --- a/test/integration-tests/ExtensionActivation.test.ts +++ b/test/integration-tests/ExtensionActivation.test.ts @@ -22,6 +22,8 @@ import { deactivateExtension, } from "./utilities/testutilities"; import { WorkspaceContext } from "../../src/WorkspaceContext"; +import { testAssetUri } from "../fixtures"; +import { assertContains } from "./testexplorer/utilities"; suite("Extension Activation/Deactivation Tests", () => { suite("Extension Activation", () => { @@ -97,4 +99,29 @@ suite("Extension Activation/Deactivation Tests", () => { assert.notStrictEqual(workspaceContext, capturedWorkspaceContext); }); }); + + suite("Activates for cmake projects", () => { + let workspaceContext: WorkspaceContext; + + activateExtensionForTest({ + async setup(ctx) { + workspaceContext = ctx; + }, + testAssets: ["cmake", "cmake-compile-flags"], + }); + + test("compile_commands.json", async () => { + const lspWorkspaces = workspaceContext.languageClientManager.subFolderWorkspaces.map( + ({ fsPath }) => fsPath + ); + assertContains(lspWorkspaces, testAssetUri("cmake").fsPath); + }); + + test("compile_flags.txt", async () => { + const lspWorkspaces = workspaceContext.languageClientManager.subFolderWorkspaces.map( + ({ fsPath }) => fsPath + ); + assertContains(lspWorkspaces, testAssetUri("cmake-compile-flags").fsPath); + }); + }); });