Skip to content

Commit

Permalink
fix: fix wrong variable reference in a continued line #2360
Browse files Browse the repository at this point in the history
Signed-off-by: ap891843 <aman.prashant@broadcom.com>
  • Loading branch information
ap891843 committed Aug 12, 2024
1 parent 7c00c92 commit 61085eb
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ public void replace(Range range, String newText) {
dirty = true;
}

/**
* Replaces given range of text with a new text
* @param range - range of text to replace
* @param textLine - a new Extended text
*/
public void replace(Range range, ExtendedTextLine textLine) {
Range updatedRange = updateRangeDueToChanges(range);
currentText.clear(updatedRange);
currentText.append(updatedRange.getStart().getLine(), textLine);
dirty = true;
}

/**
* Cleares a range of text
* @param range - a range of text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private ExtendedTextLine() {
this(line, new Position(lineNumber, 0), uri);
}

ExtendedTextLine(String line, Position start, String uri) {
public ExtendedTextLine(String line, Position start, String uri) {
for (int i = 0; i < line.length(); i++) {
char character = line.charAt(i);
checkCharacter(character);
Expand Down Expand Up @@ -127,7 +127,7 @@ ExtendedTextLine subline(int start, int end) {
* Appends the line with given line
* @param line - line that will be added to the end of this line
*/
void append(ExtendedTextLine line) {
public void append(ExtendedTextLine line) {
characters.addAll(line.characters);
characters.forEach(c -> c.setParent(this));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.lsp.cobol.common.mapping.ExtendedDocument;
import org.eclipse.lsp.cobol.common.mapping.ExtendedTextLine;
import org.eclipse.lsp.cobol.core.model.CobolLineTypeEnum;
import org.eclipse.lsp.cobol.core.preprocessor.CobolLine;
import org.eclipse.lsp.cobol.core.preprocessor.delegates.rewriter.CobolLineReWriter;
Expand All @@ -45,8 +48,8 @@ public abstract class CobolLineWriter {
*/
public ExtendedDocument serialize(List<CobolLine> lines, String documentUri) {
final StringBuilder sb = new StringBuilder();
final Map<Range, String> acc = new HashMap<>();
StringBuilder clSb = new StringBuilder();
final Map<Range, ExtendedTextLine> acc = new HashMap<>();
ExtendedTextLine clSb = null;
Position start = null;
lines.sort(Comparator.comparingInt(CobolLine::getNumber));
for (final CobolLine line : lines) {
Expand All @@ -57,8 +60,8 @@ public ExtendedDocument serialize(List<CobolLine> lines, String documentUri) {
Position stop =
new Position(line.getNumber() - 1, sb.length() - sb.lastIndexOf("\n") - 1);
Range range = new Range(start, stop);
acc.put(range, clSb.toString());
clSb = new StringBuilder();
acc.put(range, clSb);
clSb = null;
start = null;
}
process(sb, line);
Expand All @@ -76,7 +79,14 @@ public ExtendedDocument serialize(List<CobolLine> lines, String documentUri) {
start = new Position(predecessor.getNumber(), col);
}
process(sb, line);
clSb.append(removeStartingQuote(line));
String unquotedContinuedLine = removeStartingQuote(line);
Position unquotedExtendedLinePOsition = new Position(line.getNumber(), line.toString().indexOf(unquotedContinuedLine));
ExtendedTextLine extendedTextLine = new ExtendedTextLine(unquotedContinuedLine, unquotedExtendedLinePOsition, documentUri);
if (Objects.nonNull(clSb)) {
clSb.append(extendedTextLine);
} else {
clSb = extendedTextLine;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ class CobolLineWriterImplTest extends AbstractCobolLinePreprocessorTest {
"CBL DATA(24)\n"
+ " PERFORM BBAB-MOVE-TO-DETAIL-MAP\n"
+ " MOVE -1 TO SNAMEDL\n"
+ " MOVE 'PRESS \"CLEAR\" OR \"ENTER\" TO RETURN TO THE MENU WHEN FINISHED'\n\n"
+ " MOVE \"PRESS 'CLEAR' OR 'ENTER' TO RETURN TO THE MENU WHEN FINISHED\"\n\n"
+ " MOVE 'PRESS \"CLEAR\" OR \"ENTER\" TO RETURN TO THE MENU WHEN FINISHED'\n"
+ " \n"
+ " \n"
+ " MOVE \"PRESS 'CLEAR' OR 'ENTER' TO RETURN TO THE MENU WHEN FINISHED\"\n"
+ " \n"
+ " MOVE 'asd' NEXT LINE\n";

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,33 @@ class TestContinuationLine {
+ " MOVE \"TEST\" to {$DATA-T}.\n"
+ " DISPLAY {$WRK-XN-160-1}.";

private static final String TEXT4 = " IDENTIFICATION DIVISION.\n"
+ " PROGRAM-ID. TEST1.\n"
+ " DATA DIVISION.\n"
+ " WORKING-STORAGE SECTION.\n"
+ " 01 {$*CCVS-C-1}. NC2354.2\n"
+ " 02 FILLER PIC IS X(99) VALUE IS \" FEATURE PANC2354.2\n"
+ " - \"SS PARAGRAPH-NAME NC2354.2\n"
+ " - \" REMARKS\". NC2354.2\n"
+ " 01 {$*HYPHEN-LINE}. NC2354.2\n"
+ " 02 FILLER PIC IS X VALUE IS SPACE. NC2354.2\n"
+ " 02 FILLER PIC IS X(65) VALUE IS \"************************NC2354.2\n"
+ " - \"*****************************************\". NC2354.2\n"
+ " 02 FILLER PIC IS X(54) VALUE IS \"************************NC2354.2\n"
+ " - \"******************************\".";
private static final String TEXT4 =
" IDENTIFICATION DIVISION.\n"
+ " PROGRAM-ID. TEST1.\n"
+ " DATA DIVISION.\n"
+ " WORKING-STORAGE SECTION.\n"
+ " 01 {$*CCVS-C-1}. NC2354.2\n"
+ " 02 FILLER PIC IS X(99) VALUE IS \" FEATURE PANC2354.2\n"
+ " - \"SS PARAGRAPH-NAME NC2354.2\n"
+ " - \" REMARKS\". NC2354.2\n"
+ " 01 {$*HYPHEN-LINE}. NC2354.2\n"
+ " 02 FILLER PIC IS X VALUE IS SPACE. NC2354.2\n"
+ " 02 FILLER PIC IS X(65) VALUE IS \"************************NC2354.2\n"
+ " - \"*****************************************\". NC2354.2\n"
+ " 02 FILLER PIC IS X(54) VALUE IS \"************************NC2354.2\n"
+ " - \"******************************\".";

private static final String CONT_LINE_DATA_REF =
" IDENTIFICATION DIVISION.\n"
+ " PROGRAM-ID. test12.\n"
+ " DATA DIVISION.\n"
+ " WORKING-STORAGE SECTION.\n"
+ " 01 {$*tst} pic x. \n"
+ " PROCEDURE DIVISION.\n"
+ " move 'this is a long mssg ...................................\n"
+ " - 'message continues...........................................\n"
+ " - '' to {$tst}.\n"
+ " STOP RUN.";

@Test
void testWhenContinuousLineStartsWithOneQuote_thenOneLiteralIsIdentified() {
Expand All @@ -130,24 +143,33 @@ void testWhenContinuousLineStartsWithTwoQuotes_thenTwoLiteralsAreIdentified() {

@Test
void
testWhenContinuousLineStartsWithOneQuoteAndContinuedLineIsOpenLiteral_thenOneLiteralIdentified() {
testWhenContinuousLineStartsWithOneQuoteAndContinuedLineIsOpenLiteral_thenOneLiteralIdentified() {
UseCaseEngine.runTest(TEXT3, ImmutableList.of(), ImmutableMap.of());
}

@Test
void
testWhenThereAre2ContinuousLines() {
void testWhenThereAre2ContinuousLines() {
AnalysisResult result = UseCaseEngine.runTest(TEXT4, ImmutableList.of(), ImmutableMap.of());
Optional<VariableNode> node = result.getRootNode().getDepthFirstStream().filter(n -> n.getNodeType() == NodeType.VARIABLE)
.map(VariableNode.class::cast)
.filter(v -> v.getName().equals("HYPHEN-LINE"))
.findFirst();
Optional<VariableNode> node =
result
.getRootNode()
.getDepthFirstStream()
.filter(n -> n.getNodeType() == NodeType.VARIABLE)
.map(VariableNode.class::cast)
.filter(v -> v.getName().equals("HYPHEN-LINE"))
.findFirst();

assertTrue(node.isPresent());
assertEquals(Locality.builder()
.uri("file:c:/workspace/document.cbl")
.range(new Range(new Position(8, 7), new Position(13, 44)))
.build(), node.get().getLocality());
assertEquals(
Locality.builder()
.uri("file:c:/workspace/document.cbl")
.range(new Range(new Position(8, 7), new Position(13, 44)))
.build(),
node.get().getLocality());
}

@Test
void testDataRefInContinuedLine() {
UseCaseEngine.runTest(CONT_LINE_DATA_REF, ImmutableList.of(), ImmutableMap.of());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.eclipse.lsp.cobol.test.engine.UseCaseEngine;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.junit.jupiter.api.Test;

Expand All @@ -38,7 +37,8 @@ class TestPseudoTextContentConstraint {
+ "3 WORKING-STORAGE SECTION.\n";
private static final String TEXT =
BASE
+ "5 COPY {~REPL} REPLACING {_==asasasasasasasasasasasasa|1_}\n"
+ "5 COPY {~REPL}\n"
+ " REPLACING {_==asasasasasasasasasasasasa\n"
+ " - cccccccccccccccccccccccccccccccccccccccccccccccccccc\n"
+ " - oooooooooooooooooooooooooooooooooooooooooooooooooooo\n"
+ " - bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
Expand All @@ -47,8 +47,8 @@ class TestPseudoTextContentConstraint {
+ " - IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII\n"
+ " - BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
+ " - MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM\n"
+ " - == BY ====.\n"
+ "8 PROCEDURE DIVISION.\n";
+ " - == BY ====|1_}.\n"
+ "8 PROCEDURE DIVISION.";
private static final String PSEUDO_COPY_TEXT =
BASE
+ "5 COPY {~REPL} REPLACING {_==COPY== BY ====|1_}.\n"
Expand Down Expand Up @@ -80,7 +80,7 @@ void testMax322CharsAllowedInPseudoText() {
ImmutableMap.of(
"1",
new Diagnostic(
new Range(new Position(4, 27), new Position(13, 22)),
new Range(),
"Max 322 chars allowed for each individual word in pseudo text",
DiagnosticSeverity.Error,
ErrorSource.EXTENDED_DOCUMENT.getText())));
Expand Down

0 comments on commit 61085eb

Please sign in to comment.