Skip to content

Commit

Permalink
feat: filter CFAST programs based on the selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ishche committed Sep 16, 2024
1 parent dc3bab5 commit 4319105
Show file tree
Hide file tree
Showing 30 changed files with 451 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ jest.mock("vscode", () => ({
registerCodeActionsProvider: jest.fn(),
registerCompletionItemProvider: jest.fn(),
},
Position: class {
constructor(private line: number, private character: number) {}
},
window: {
visibleTextEditors: [],
setStatusBarMessage: jest
.fn()
.mockImplementation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { EXP_LANGUAGE_ID, HP_LANGUAGE_ID } from "../../constants";

jest.mock("../../services/reporter/TelemetryService");
jest.mock("../../services/copybook/CopybookURI");

jest.mock("vscode", () => ({
extensions: {
getExtension: jest.fn().mockReturnValue({ extensionPath: "/test" }),
Expand All @@ -43,6 +44,9 @@ jest.mock("vscode", () => ({
fsPath: "/storagePath",
}),
},
Position: class {
constructor(private line: number, private character: number) {}
},
RelativePattern: jest.fn().mockReturnValue(undefined),
}));
jest.mock("vscode-languageclient/node", () => ({
Expand Down Expand Up @@ -116,9 +120,13 @@ describe("LanguageClientService positive scenario", () => {

LanguageClient.prototype.sendRequest = () =>
Promise.resolve(expectedResult);
expect(await languageClientService.retrieveAnalysis("test", "text")).toBe(
expectedResult,
);
expect(
await languageClientService.retrieveAnalysis(
"test",
"text",
new vscode.Position(0, 0),
),
).toBe(expectedResult);
});

test("Test LanguageClientService starts language client", async () => {
Expand Down
15 changes: 14 additions & 1 deletion clients/cobol-lsp-vscode-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,23 @@ export async function activate(
},
version: API_VERSION,
analysis(uri: string, text: string): Promise<any> {
return languageClientService.retrieveAnalysis(uri, text);
return languageClientService.retrieveAnalysis(
uri,
text,
findPosition(uri),
);
},
};
}
function findPosition(uri: string): vscode.Position {
for (const e of vscode.window.visibleTextEditors) {
if (e.document.uri.toString() === uri) {
return e.selection.start;
}
}
outputChannel.appendLine("Cant find editor for " + uri + " the first program/function will be used.")
return new vscode.Position(0, 0);
}

export function deactivate() {
return languageClientService.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,19 @@ export class LanguageClientService {
);
}

public async retrieveAnalysis(uri: string, text: string): Promise<any> {
public async retrieveAnalysis(
uri: string,
text: string,
position: vscode.Position,
): Promise<any> {
const params = {
uri,
text,
line: position.line,
character: position.character,
};
const languageClient = this.getLanguageClient();
return languageClient.sendRequest("extended/analysis", { uri, text });
return languageClient.sendRequest("extended/analysis", params);
}

public invalidateConfiguration() {
Expand Down
210 changes: 210 additions & 0 deletions clients/cobol-lsp-vscode-extension/src/test/suite/cfast.spec.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*/
import * as assert from "assert";
import * as helper from "./testHelper";
import * as vscode from "vscode";

suite("Integration Test Suite: CFAST generation", function () {
suiteSetup(async function () {
this.timeout(0);
helper.updateConfig("basic.json");
await helper.activate();
});

this.afterEach(async () => await helper.closeAllEditors()).timeout(
helper.TEST_TIMEOUT,
);

test("CFAST generation call", async () => {
await helper.showDocument("CFAST.cbl");
const editor = helper.getEditor("CFAST.cbl");
const cobolExtension = vscode.extensions.getExtension(
"broadcommfd.cobol-language-support",
);
if (cobolExtension === undefined) {
assert.fail("can't find extension: broadcommfd.cobol-language-support");
}
helper.moveCursor(editor, new vscode.Position(1, 1));
const result = await cobolExtension.exports.analysis(
editor.document.uri.toString(),
editor.document.getText(),
);
assert.strictEqual(
result.controlFlowAST.length,
1,
"Expects only one program",
);
assert.strictEqual(
result.controlFlowAST[0].name,
"MAIN",
"Program is to be MAIN",
);
});

test("CFAST generation call for nested program", async () => {
await helper.showDocument("CFAST.cbl");
const editor = helper.getEditor("CFAST.cbl");
const cobolExtension = vscode.extensions.getExtension(
"broadcommfd.cobol-language-support",
);
if (cobolExtension === undefined) {
assert.fail("can't find extension: broadcommfd.cobol-language-support");
}
helper.moveCursor(editor, new vscode.Position(23, 1));
const result = await cobolExtension.exports.analysis(
editor.document.uri.toString(),
editor.document.getText(),
);
assert.strictEqual(
result.controlFlowAST.length,
1,
"Expects only one program",
);
assert.strictEqual(
result.controlFlowAST[0].name,
"NESTED1",
"Program is to be NESTED1",
);
});

test("CFAST generation call for MAIN2 program", async () => {
await helper.showDocument("CFAST.cbl");
const editor = helper.getEditor("CFAST.cbl");
const cobolExtension = vscode.extensions.getExtension(
"broadcommfd.cobol-language-support",
);
if (cobolExtension === undefined) {
assert.fail("can't find extension: broadcommfd.cobol-language-support");
}
helper.moveCursor(editor, new vscode.Position(30, 1));
const result = await cobolExtension.exports.analysis(
editor.document.uri.toString(),
editor.document.getText(),
);
assert.strictEqual(
result.controlFlowAST.length,
1,
"Expects only one program",
);
assert.strictEqual(
result.controlFlowAST[0].name,
"MAIN2",
"Program is to be MAIN2",
);
});

test("CFAST generation: MAIN program after nested click", async () => {
await helper.showDocument("CFAST.cbl");
const editor = helper.getEditor("CFAST.cbl");
const cobolExtension = vscode.extensions.getExtension(
"broadcommfd.cobol-language-support",
);
if (cobolExtension === undefined) {
assert.fail("can't find extension: broadcommfd.cobol-language-support");
}
helper.moveCursor(editor, new vscode.Position(25, 1));
const result = await cobolExtension.exports.analysis(
editor.document.uri.toString(),
editor.document.getText(),
);
assert.strictEqual(
result.controlFlowAST.length,
1,
"Expects only one program",
);
assert.strictEqual(
result.controlFlowAST[0].name,
"MAIN",
"Program is to be MAIN",
);
});

test("CFAST generation: before MAIN program click", async () => {
await helper.showDocument("CFAST.cbl");
const editor = helper.getEditor("CFAST.cbl");
const cobolExtension = vscode.extensions.getExtension(
"broadcommfd.cobol-language-support",
);
if (cobolExtension === undefined) {
assert.fail("can't find extension: broadcommfd.cobol-language-support");
}
helper.moveCursor(editor, new vscode.Position(25, 1));
const result = await cobolExtension.exports.analysis(
editor.document.uri.toString(),
editor.document.getText(),
);
assert.strictEqual(
result.controlFlowAST.length,
1,
"Expects only one program",
);
assert.strictEqual(
result.controlFlowAST[0].name,
"MAIN",
"Program is to be MAIN",
);
});

test("CFAST generation: before MAIN2 after MAIN programs click", async () => {
await helper.showDocument("CFAST.cbl");
const editor = helper.getEditor("CFAST.cbl");
const cobolExtension = vscode.extensions.getExtension(
"broadcommfd.cobol-language-support",
);
if (cobolExtension === undefined) {
assert.fail("can't find extension: broadcommfd.cobol-language-support");
}
helper.moveCursor(editor, new vscode.Position(28, 1));
const result = await cobolExtension.exports.analysis(
editor.document.uri.toString(),
editor.document.getText(),
);
assert.strictEqual(
result.controlFlowAST.length,
1,
"Expects only one program",
);
assert.strictEqual(
result.controlFlowAST[0].name,
"MAIN2",
"Program is to be MAIN2",
);
});

test("CFAST generation: after MAIN2 programs click", async () => {
await helper.showDocument("CFAST.cbl");
const editor = helper.getEditor("CFAST.cbl");
const cobolExtension = vscode.extensions.getExtension(
"broadcommfd.cobol-language-support",
);
if (cobolExtension === undefined) {
assert.fail("can't find extension: broadcommfd.cobol-language-support");
}
helper.moveCursor(editor, new vscode.Position(34, 1));
const result = await cobolExtension.exports.analysis(
editor.document.uri.toString(),
editor.document.getText(),
);
assert.strictEqual(
result.controlFlowAST.length,
1,
"Expects only one program",
);
assert.strictEqual(
result.controlFlowAST[0].name,
"MAIN2",
"Program is to be MAIN2",
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ suite("Integration Test Suite: Copybooks", function () {

test("TC174952 Copybook - not exist, but dynamically appears", async () => {
await helper.showDocument("VAR.cbl");
let editor = helper.get_editor("VAR.cbl");
let editor = helper.getEditor("VAR.cbl");
await helper.waitForDiagnostics(editor.document.uri);
const diagnostics = vscode.languages.getDiagnostics(editor.document.uri);
assert.strictEqual(diagnostics.length, 2);
Expand All @@ -135,7 +135,7 @@ suite("Integration Test Suite: Copybooks", function () {

test("TC174952 / TC174953 Copybook - definition not exist, but dynamically appears", async () => {
await helper.showDocument("USERC1F.cbl");
let editor = helper.get_editor("USERC1F.cbl");
let editor = helper.getEditor("USERC1F.cbl");
await helper.waitForDiagnostics(editor.document.uri);
let diagnostics = vscode.languages.getDiagnostics(editor.document.uri);
helper.assertRangeIsEqual(
Expand Down
Loading

0 comments on commit 4319105

Please sign in to comment.