From 7ad0a37284d580d7958f490d52e9becc846d09c4 Mon Sep 17 00:00:00 2001 From: Tristan Partin Date: Thu, 16 Nov 2023 11:53:16 -0600 Subject: [PATCH] Fix random file being chosen to create project --- src/extension.ts | 3 ++- src/utils.ts | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/extension.ts b/src/extension.ts index 6bc8543e..7f80ce91 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -11,6 +11,7 @@ import { checkMesonIsConfigured, getOutputChannel, relativeBuildDir, + rootMesonFiles, } from "./utils"; import { DebugConfigurationProviderCppdbg } from "./debug/cppdbg"; import { DebugConfigurationProviderLldb } from "./debug/lldb"; @@ -38,7 +39,7 @@ export async function activate(ctx: vscode.ExtensionContext) { } const root = vscode.workspace.workspaceFolders[0].uri.fsPath; - const mesonFiles = await vscode.workspace.findFiles("**/meson.build"); + const mesonFiles = await rootMesonFiles(); if (mesonFiles.length === 0) { return; } diff --git a/src/utils.ts b/src/utils.ts index 3c304cbc..d7b99134 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -179,3 +179,24 @@ export async function useCompileCommands(buildDir: string) { export function checkMesonIsConfigured(buildDir: string) { return fs.existsSync(path.join(buildDir, "meson-private", "coredata.dat")); } + +export async function rootMesonFiles(): Promise { + const allFiles = (await vscode.workspace.findFiles("**/meson.build")).sort( + (a, b) => a.fsPath.length - b.fsPath.length, + ); + + const rootFiles: vscode.Uri[] = []; + for (const a of allFiles) { + if (rootFiles.length === 0) { + rootFiles.push(a); + continue; + } + + if (!path.dirname(a.fsPath).startsWith(path.dirname(rootFiles[rootFiles.length - 1].fsPath))) { + rootFiles.push(a); + continue; + } + } + + return rootFiles; +}