Skip to content

Commit 5289af9

Browse files
authored
Merge pull request wso2#874 from madushajg/bi-multi-project
Improve detecting project path in AI panel
2 parents 66d595a + d226d81 commit 5289af9

File tree

7 files changed

+36
-37
lines changed

7 files changed

+36
-37
lines changed

workspaces/ballerina/ballerina-extension/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@
689689
},
690690
{
691691
"command": "BI.project-explorer.add",
692-
"title": "Add Construct",
692+
"title": "Add Project",
693693
"icon": "$(add)",
694694
"group": "navigation",
695695
"category": "BI"

workspaces/ballerina/ballerina-extension/src/features/ai/service/documentation/doc_generator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ import { DocGenerationRequest } from '@wso2/ballerina-core';
2020
import { getServiceDeclaration } from '../../testGenerator';
2121
import { generateDocumentation, DocumentationGenerationRequest } from './documentation';
2222
import { getProjectSource, getOpenAPISpecification } from '../../utils';
23-
import { StateMachine } from '../../../../stateMachine';
23+
import { getCurrentProjectRoot } from '../../../../utils/project-utils';
2424

2525
// Main documentation generator function that handles all the logic
2626
export async function generateDocumentationForService(params: DocGenerationRequest): Promise<void> {
2727
try {
2828
// Get the project root
29-
const projectPath = StateMachine.context().projectPath;
29+
const projectPath = await getCurrentProjectRoot();
3030

3131
// Get the project source files
3232
const projectSource = await getProjectSource(projectPath);

workspaces/ballerina/ballerina-extension/src/features/ai/service/test/function_tests.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { generateTest, getDiagnostics } from "../../testGenerator";
2020
import { URI } from "vscode-uri";
2121
import * as fs from "fs";
2222
import { CopilotEventHandler, createWebviewEventHandler } from "../event";
23-
import { StateMachine } from "../../../../stateMachine";
23+
import { getCurrentProjectRoot } from "../../../../utils/project-utils";
2424

2525
// Core function test generation that emits events
2626
export async function generateFunctionTestsCore(
@@ -39,7 +39,15 @@ export async function generateFunctionTestsCore(
3939
content: `\n\n<progress>Generating tests for the function ${functionIdentifier}. This may take a moment.</progress>`,
4040
});
4141

42-
const projectPath = StateMachine.context().projectPath;
42+
let projectPath: string;
43+
try {
44+
projectPath = await getCurrentProjectRoot();
45+
} catch (error) {
46+
console.error("Error getting current project root:", error);
47+
eventHandler({ type: "error", content: getErrorMessage(error) });
48+
return;
49+
}
50+
4351
const response = await generateTest(projectPath, {
4452
targetType: TestGenerationTarget.Function,
4553
targetIdentifier: functionIdentifier,

workspaces/ballerina/ballerina-extension/src/features/ai/service/test/test_plan.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { TestGenerationTarget, TestPlanGenerationRequest, Command } from "@wso2/
2121
import { generateTest, getDiagnostics } from "../../testGenerator";
2222
import { CopilotEventHandler, createWebviewEventHandler } from "../event";
2323
import { AIPanelAbortController } from "../../../../../src/rpc-managers/ai-panel/utils";
24-
import { StateMachine } from "../../../../stateMachine";
24+
import { getCurrentProjectRoot } from "../../../../utils/project-utils";
2525

2626
export interface TestPlanResponse {
2727
testPlan: string;
@@ -168,7 +168,14 @@ export async function generateTestPlanCore(
168168
type: "content_block",
169169
content: `\n\n<progress>Generating tests for the ${target} service. This may take a moment.</progress>`,
170170
});
171-
const projectPath = StateMachine.context().projectPath;
171+
let projectPath: string;
172+
try {
173+
projectPath = await getCurrentProjectRoot();
174+
} catch (error) {
175+
console.error("Error getting current project root:", error);
176+
eventHandler({ type: "error", content: getErrorMessage(error) });
177+
return;
178+
}
172179
const testResp = await generateTest(projectPath, {
173180
targetType: TestGenerationTarget.Service,
174181
targetIdentifier: target,

workspaces/ballerina/ballerina-extension/src/features/ai/testGenerator.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import * as os from 'os';
2828
import { writeBallerinaFileDidOpenTemp } from '../../utils/modification';
2929
import { closeAllBallerinaFiles } from './utils';
3030
import { generateTestFromLLM, TestGenerationRequest1 } from './service/test/test';
31-
import { findBallerinaPackageRoot } from '../../utils';
3231

3332
const TEST_GEN_REQUEST_TIMEOUT = 100000;
3433

@@ -220,10 +219,9 @@ export async function getResourceAccessorDef(projectRoot: string, resourceMethod
220219
}
221220

222221
export async function getDiagnostics(
223-
projectRoot: string,
222+
ballerinaProjectRoot: string,
224223
generatedTestSource: TestGenerationResponse
225224
): Promise<ProjectDiagnostics> {
226-
const ballerinaProjectRoot = await findBallerinaPackageRoot(projectRoot);
227225
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'temp-bal-test-gen-'));
228226
fs.cpSync(ballerinaProjectRoot, tempDir, { recursive: true });
229227
const tempTestFolderPath = path.join(tempDir, 'tests');
@@ -245,12 +243,7 @@ export async function getDiagnostics(
245243
};
246244
}
247245

248-
async function getProjectSource(dirPath: string): Promise<ProjectSource | null> {
249-
const projectRoot = await findBallerinaPackageRoot(dirPath);
250-
251-
if (!projectRoot) {
252-
return null;
253-
}
246+
async function getProjectSource(projectRoot: string): Promise<ProjectSource | null> {
254247

255248
const projectSource: ProjectSource = {
256249
sourceFiles: [],
@@ -298,14 +291,9 @@ async function getProjectSource(dirPath: string): Promise<ProjectSource | null>
298291
return projectSource;
299292
}
300293

301-
async function getProjectSourceWithTests(dirPath: string): Promise<ProjectSource | null> {
302-
const projectRoot = await findBallerinaPackageRoot(dirPath);
303-
304-
if (!projectRoot) {
305-
return null;
306-
}
294+
async function getProjectSourceWithTests(projectRoot: string): Promise<ProjectSource | null> {
307295

308-
const projectSourceWithTests: ProjectSource = await getProjectSource(dirPath);
296+
const projectSourceWithTests: ProjectSource = await getProjectSource(projectRoot);
309297

310298
// Read tests
311299
const testsDir = path.join(projectRoot, 'tests');

workspaces/ballerina/ballerina-extension/src/features/ai/utils.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -278,17 +278,11 @@ async function showNoBallerinaSourceWarningMessage() {
278278

279279
import { ProjectSource, ProjectModule, OpenAPISpec } from '@wso2/ballerina-core';
280280
import { langClient } from './activator';
281-
import { findBallerinaPackageRoot } from '../../utils';
282281

283282
/**
284283
* Gets the project source including all .bal files and modules
285284
*/
286-
export async function getProjectSource(dirPath: string): Promise<ProjectSource | null> {
287-
const projectRoot = await findBallerinaPackageRoot(dirPath);
288-
289-
if (!projectRoot) {
290-
return null;
291-
}
285+
export async function getProjectSource(projectRoot: string): Promise<ProjectSource | null> {
292286

293287
const projectSource: ProjectSource = {
294288
sourceFiles: [],
@@ -339,14 +333,9 @@ export async function getProjectSource(dirPath: string): Promise<ProjectSource |
339333
/**
340334
* Gets the project source including test files
341335
*/
342-
export async function getProjectSourceWithTests(dirPath: string): Promise<ProjectSource | null> {
343-
const projectRoot = await findBallerinaPackageRoot(dirPath);
344-
345-
if (!projectRoot) {
346-
return null;
347-
}
336+
export async function getProjectSourceWithTests(projectRoot: string): Promise<ProjectSource | null> {
348337

349-
const projectSourceWithTests: ProjectSource = await getProjectSource(dirPath);
338+
const projectSourceWithTests: ProjectSource = await getProjectSource(projectRoot);
350339

351340
// Read tests
352341
const testsDir = path.join(projectRoot, 'tests');

workspaces/ballerina/ballerina-extension/src/stateMachine.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,13 @@ async function handleSingleWorkspaceFolder(workspaceURI: Uri, isSwitching: boole
879879
targetPackage = packages[0];
880880
}
881881

882+
if (!targetPackage && packages.length > 1) {
883+
// If the user has not selected a package, select the first package
884+
// This is a temporary solution until we provide the support for multi root workspaces
885+
// Ref: https://github.com/wso2/product-ballerina-integrator/issues/1465
886+
targetPackage = packages[0];
887+
}
888+
882889
if (targetPackage) {
883890
const packagePath = path.isAbsolute(targetPackage)
884891
? targetPackage

0 commit comments

Comments
 (0)