Skip to content

Commit

Permalink
Activate extension when compile_flags.txt or buildServer.json is present
Browse files Browse the repository at this point in the history
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
  • Loading branch information
plemarquand committed Dec 3, 2024
1 parent 36cd225 commit 63f2767
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 4 deletions.
5 changes: 5 additions & 0 deletions assets/test/cmake-compile-flags/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 2.4)

project(hello_world)

add_executable(app main.cpp)
8 changes: 8 additions & 0 deletions assets/test/cmake-compile-flags/compile_flags.txt
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions assets/test/cmake-compile-flags/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>

int main()
{
std::cout << "Hello World!\n";
return 0;
}
1 change: 1 addition & 0 deletions assets/test/dependencies/Packages/swift-markdown
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -1335,4 +1337,4 @@
"vscode-languageclient": "^9.0.1",
"xml2js": "^0.6.2"
}
}
}
8 changes: 5 additions & 3 deletions src/WorkspaceContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<boolean> {
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"))
);
}

Expand Down
27 changes: 27 additions & 0 deletions test/integration-tests/ExtensionActivation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 63f2767

Please sign in to comment.