Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix implicit dialect node position #2369

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,23 @@ private void addDialectsNode(AnalysisContext context, Node rootNode) {
dialectNode.getLocality().getUri(),
dialectNode.getLocality().getRange().getStart());

nodeByPosition.orElse(rootNode).addChild(dialectNode);
addChild(nodeByPosition.orElse(rootNode), dialectNode);
}
}

private void addChild(Node node, Node dialectNode) {
int index = 0;
for (Node child : node.getChildren()) {
if (child.getLocality().getUri().equals(dialectNode.getLocality().getUri())
&& (child.getLocality().getRange().getStart().getLine() >= dialectNode.getLocality().getRange().getStart().getLine())) {
break;
}
index++;
}
dialectNode.setParent(node);
node.getChildren().add(index, dialectNode);
}

private void addCopyNodes(AnalysisContext context, Node rootNode) {
for (Map.Entry<String, Location> copybook : context.getCopybooksRepository().getUsages().entries()) {
String name = copybook.getKey();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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
*
*/
package org.eclipse.lsp.cobol.usecases;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.eclipse.lsp.cobol.common.AnalysisResult;
import org.eclipse.lsp.cobol.common.model.NodeType;
import org.eclipse.lsp.cobol.common.model.tree.Node;
import org.eclipse.lsp.cobol.implicitDialects.cics.nodes.ExecCicsHandleNode;
import org.eclipse.lsp.cobol.test.engine.UseCaseEngine;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/** Tests the CICS implicit dialect node position **/
class TestImplicitDialectsPosition {

private static final String TEXT = " IDENTIFICATION DIVISION.\n"
+ " PROGRAM-ID. CBACT01C.\n"
+ " PROCEDURE DIVISION.\n"
+ " IF 2 > 1 THEN\n"
+ " EXEC CICS HANDLE ABEND LABEL({#HANDLE-ABEND})\n"
+ " END-EXEC.\n"
+ " GO TO {#HANDLE-ABEND}\n"
+ " END-IF.\n"
+ " {#*HANDLE-ABEND}.\n";

private static final String TEXT_WITH_COPY = " IDENTIFICATION DIVISION.\n"
+ " PROGRAM-ID. CBACT01C.\n"
+ " PROCEDURE DIVISION.\n"
+ " IF 2 > 1 \n"
+ " PERFORM {#HANDLE-ABEND}\n"
+ " COPY {~COPY1}.\n"
+ " DISPLAY '1'\n"
+ " END-IF.\n"
+ " {#*HANDLE-ABEND}.\n";

private static final String COPY1 = " EXEC CICS HANDLE ABEND LABEL({#HANDLE-ABEND}) END-EXEC\n";

@Test
void test() {
AnalysisResult result = UseCaseEngine.runTest(TEXT, ImmutableList.of(), ImmutableMap.of());
Node ifNode = result.getRootNode().getDepthFirstStream()
.filter(n -> n.getNodeType() == NodeType.IF)
.findFirst()
.orElse(null);

assertNotNull(ifNode);
Node execCicsNode = ifNode.getChildren().get(2);
assertTrue(execCicsNode instanceof ExecCicsHandleNode);
}

// @Test
// void testCopy() {
// AnalysisResult result = UseCaseEngine.runTest(TEXT_WITH_COPY,
// ImmutableList.of(new CobolText("COPY1", COPY1)),
// ImmutableMap.of());
// Node ifNode = result.getRootNode().getDepthFirstStream()
// .filter(n -> n.getNodeType() == NodeType.IF)
// .findFirst()
// .orElse(null);
//
// assertNotNull(ifNode);
// Node execCicsNode = ifNode.getChildren().get(3);
// assertTrue(execCicsNode instanceof ExecCicsHandleNode);
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. CBACT01C.
PROCEDURE DIVISION.
IF A > B THEN
EXEC CICS HANDLE ABEND LABEL(HANDLE-ABEND)
END-EXEC.
GO TO CALCULATION.
END-IF.

HANDLE-ABEND.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
[
{
"name": "CBACT01C",
"type": "program",
"location": {
"uri": "fake/path",
"start": {
"line": 3,
"character": 8
},
"end": {
"line": 7,
"character": 32
}
},
"children": [
{
"type": "if",
"location": {
"uri": "fake/path",
"start": {
"line": 4,
"character": 12
},
"end": {
"line": 7,
"character": 31
}
}
},
{
"handleType": "LABEL",
"value": "HANDLE-ABEND",
"type": "execcicshandle",
"location": {
"uri": "fake/path",
"start": {
"line": 5,
"character": 14
},
"end": {
"line": 6,
"character": 23
}
}
},
{
"type": "endexec",
"location": {
"uri": "fake/path",
"start": {
"line": 5,
"character": 14
},
"end": {
"line": 6,
"character": 23
}
}
},
{
"targetName": [
"CALCULATION"
],
"type": "goto",
"location": {
"uri": "fake/path",
"start": {
"line": 7,
"character": 14
},
"end": {
"line": 7,
"character": 31
}
}
},
{
"type": "endif",
"location": {
"uri": "fake/path",
"start": {
"line": 4,
"character": 12
},
"end": {
"line": 7,
"character": 31
}
}
}
]
}
]
Loading