From 80a4df641630c7e12d789575584a46a0c6386c3a Mon Sep 17 00:00:00 2001 From: Leonid Baranov Date: Wed, 9 Oct 2024 13:15:46 +0200 Subject: [PATCH 1/2] fix: Fix snippet text for sections and paragraphs --- .../common/mapping/ExtendedDocument.java | 24 +++++++++++++++++++ .../lsp/cobol/core/visitor/CobolVisitor.java | 22 ++++------------- .../test/resources/cfast/case3.result.json | 4 ++-- .../test/resources/cfast/case4.result.json | 4 ++-- .../test/resources/cfast/case46.result.json | 6 ++--- .../test/resources/cfast/case5.result.json | 8 +++---- .../test/resources/cfast/case7.result.json | 2 +- .../test/resources/cfast/case8.result.json | 2 +- .../resources/cfast/case_alter.result.json | 6 ++--- .../case_conditional_perform.result.json | 2 +- .../resources/cfast/case_evaluate.result.json | 2 +- .../cfast/case_evaluate_other.result.json | 2 +- .../case_execCicsHandleAbendEx.result.json | 2 +- .../cfast/case_execSqlWhenever.result.json | 2 +- .../cfast/case_goto_keyword.result.json | 2 +- .../cfast/case_handle_abend_label.result.json | 2 +- .../test/resources/cfast/case_ifs.result.json | 4 ++-- .../resources/cfast/case_ifs_1.result.json | 2 +- .../resources/cfast/case_ifs_2.result.json | 2 +- .../resources/cfast/case_merge.result.json | 14 +++++------ .../cfast/case_perform_thru.result.json | 10 ++++---- .../resources/cfast/case_sort.result.json | 14 +++++------ .../cfast/case_useForDebugging.result.json | 2 +- .../cfast/case_xml_parse.result.json | 6 ++--- 24 files changed, 79 insertions(+), 67 deletions(-) diff --git a/server/common/src/main/java/org/eclipse/lsp/cobol/common/mapping/ExtendedDocument.java b/server/common/src/main/java/org/eclipse/lsp/cobol/common/mapping/ExtendedDocument.java index 0bce883692..6ef49f37cb 100644 --- a/server/common/src/main/java/org/eclipse/lsp/cobol/common/mapping/ExtendedDocument.java +++ b/server/common/src/main/java/org/eclipse/lsp/cobol/common/mapping/ExtendedDocument.java @@ -18,6 +18,7 @@ import java.util.List; +import org.eclipse.lsp.cobol.common.model.Locality; import org.eclipse.lsp4j.Location; import org.eclipse.lsp4j.Position; import org.eclipse.lsp4j.Range; @@ -54,6 +55,29 @@ public String getUri() { return baseText.getUri(); } + /** + * Returns original text, situated between the start and the end lines from the locality range + * @param locality - the text locality + * @return string value that represents a text from the original document + */ + public String getBaseText(Locality locality) { + int startLine = locality.getRange().getStart().getLine(); + int endLine = locality.getRange().getEnd().getLine();; + + if (!baseText.getUri().equals(locality.getUri()) + || baseText.getLines().size() <= endLine) { + return ""; + } + StringBuilder sb = new StringBuilder(); + for (int i = startLine; i <= endLine; i++) { + sb.append(baseText.getLines().get(i).toString()); + if (i != endLine) { + sb.append("\r\n"); + } + } + return sb.toString(); + } + /** * Commit changes */ diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/visitor/CobolVisitor.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/visitor/CobolVisitor.java index 293c8358f6..2696d450eb 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/visitor/CobolVisitor.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/visitor/CobolVisitor.java @@ -18,7 +18,6 @@ import com.google.common.collect.ImmutableList; import lombok.NonNull; import org.antlr.v4.runtime.*; -import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.RuleNode; import org.antlr.v4.runtime.tree.TerminalNode; @@ -110,7 +109,6 @@ public CobolVisitor( @Override public List visitStartRule(StartRuleContext ctx) { // we can skip the other nodes, but not the root - text.setInputStream(ctx.getStart().getInputStream()); try { return ImmutableList.of( retrieveLocality(ctx, extendedDocument, copybooks) @@ -1856,7 +1854,6 @@ public List getErrors() { private static final class TextExtractionState { private final ExtendedDocument extendedDocument; - private CharStream input = null; private ParagraphNode lastParagraphNode = null; private ProcedureSectionNode lastSectionNode = null; private Token firstSectionToken = null; @@ -1870,16 +1867,14 @@ private TextExtractionState(ExtendedDocument extendedDocument) { void update(ProcedureSectionNode section, Token firstToken) { if (lastParagraphNode != null) { - String text = input.getText(new Interval(firstParagraphToken.getStartIndex(), lastSentenseToken.getStopIndex())); - lastParagraphNode.setText(text); lastParagraphNode.getLocality().getRange().setEnd(lastSentensePosition); + lastParagraphNode.setText(extendedDocument.getBaseText(lastParagraphNode.getLocality())); lastParagraphNode = null; firstParagraphToken = null; } if (lastSectionNode != null) { - String text = input.getText(new Interval(firstSectionToken.getStartIndex(), lastSentenseToken.getStopIndex())); lastSectionNode.getLocality().getRange().setEnd(lastSentensePosition); - lastSectionNode.setText(text); + lastSectionNode.setText(extendedDocument.getBaseText(lastSectionNode.getLocality())); } firstSectionToken = firstToken; lastSectionNode = section; @@ -1887,9 +1882,8 @@ void update(ProcedureSectionNode section, Token firstToken) { void update(ParagraphNode paragraphNode, Token firstToken) { if (lastParagraphNode != null) { - String text = input.getText(new Interval(firstParagraphToken.getStartIndex(), lastSentenseToken.getStopIndex())); - lastParagraphNode.setText(text); lastParagraphNode.getLocality().getRange().setEnd(lastSentensePosition); + lastParagraphNode.setText(extendedDocument.getBaseText(lastParagraphNode.getLocality())); } lastParagraphNode = paragraphNode; firstParagraphToken = firstToken; @@ -1901,10 +1895,6 @@ void update(Token lastToken) { .getRange().getEnd(); } - void setInputStream(CharStream input) { - this.input = input; - } - void reset() { lastParagraphNode = null; lastSectionNode = null; @@ -1915,14 +1905,12 @@ void reset() { } void flush() { if (lastParagraphNode != null) { - String text = input.getText(new Interval(firstParagraphToken.getStartIndex(), lastSentenseToken.getStopIndex())); - lastParagraphNode.setText(text); lastParagraphNode.getLocality().getRange().setEnd(lastSentensePosition); + lastParagraphNode.setText(extendedDocument.getBaseText(lastParagraphNode.getLocality())); } if (lastSectionNode != null) { - String text = input.getText(new Interval(firstSectionToken.getStartIndex(), lastSentenseToken.getStopIndex())); - lastSectionNode.setText(text); lastSectionNode.getLocality().getRange().setEnd(lastSentensePosition); + lastSectionNode.setText(extendedDocument.getBaseText(lastSectionNode.getLocality())); } } } diff --git a/server/engine/src/test/resources/cfast/case3.result.json b/server/engine/src/test/resources/cfast/case3.result.json index 83b9354a36..1008840670 100644 --- a/server/engine/src/test/resources/cfast/case3.result.json +++ b/server/engine/src/test/resources/cfast/case3.result.json @@ -29,7 +29,7 @@ } }, { - "snippet": "PARAG1.\r\n DISPLAY \u0027PARAG1\u0027.", + "snippet": " PARAG1.\r\n DISPLAY \u0027PARAG1\u0027.", "name": "PARAG1", "type": "paragraph", "location": { @@ -61,7 +61,7 @@ ] }, { - "snippet": "PARAG2.\r\n DISPLAY \u0027PARAG2\u0027.", + "snippet": " PARAG2.\r\n DISPLAY \u0027PARAG2\u0027.", "name": "PARAG2", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case4.result.json b/server/engine/src/test/resources/cfast/case4.result.json index 6e4ea61400..16e17acabf 100644 --- a/server/engine/src/test/resources/cfast/case4.result.json +++ b/server/engine/src/test/resources/cfast/case4.result.json @@ -46,7 +46,7 @@ } }, { - "snippet": "PARAG1.\r\n DISPLAY \u0027PARAG1\u0027.\r\n PERFORM PARAG2.", + "snippet": " PARAG1.\r\n DISPLAY \u0027PARAG1\u0027.\r\n PERFORM PARAG2.", "name": "PARAG1", "type": "paragraph", "location": { @@ -93,7 +93,7 @@ ] }, { - "snippet": "PARAG2.\r\n DISPLAY \u0027PARAG2\u0027.", + "snippet": " PARAG2.\r\n DISPLAY \u0027PARAG2\u0027.", "name": "PARAG2", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case46.result.json b/server/engine/src/test/resources/cfast/case46.result.json index 504dd8e985..19d191a8d6 100644 --- a/server/engine/src/test/resources/cfast/case46.result.json +++ b/server/engine/src/test/resources/cfast/case46.result.json @@ -15,7 +15,7 @@ }, "children": [ { - "snippet": "P8000-READ-TABLE.\r\n \r\n READ TRAVCONV-FILE INTO TRAVELERS-CONV-RECORD\r\n AT END GO TO P8000-EXIT.\r\n MOVE \u0027Y\u0027 TO OUT-REC-TYPE.\r\n PERFORM X1.", + "snippet": " P8000-READ-TABLE.\r\n \r\n READ TRAVCONV-FILE INTO TRAVELERS-CONV-RECORD\r\n AT END GO TO P8000-EXIT.\r\n MOVE \u0027Y\u0027 TO OUT-REC-TYPE.\r\n PERFORM X1.", "name": "P8000-READ-TABLE", "type": "paragraph", "location": { @@ -121,7 +121,7 @@ ] }, { - "snippet": "X1.\r\n DISPLAY \u0027HAHAH\u0027.", + "snippet": " X1.\r\n DISPLAY \u0027HAHAH\u0027.", "name": "X1", "type": "paragraph", "location": { @@ -153,7 +153,7 @@ ] }, { - "snippet": "P8000-EXIT.\r\n EXIT.", + "snippet": " P8000-EXIT.\r\n EXIT.", "name": "P8000-EXIT", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case5.result.json b/server/engine/src/test/resources/cfast/case5.result.json index 11f23088fa..e5bb767050 100644 --- a/server/engine/src/test/resources/cfast/case5.result.json +++ b/server/engine/src/test/resources/cfast/case5.result.json @@ -47,7 +47,7 @@ }, { "name": "SECT1", - "snippet": "SECT1 SECTION.\r\n PARAG1.\r\n DISPLAY \u0027PARAG1\u0027.\r\n PERFORM PARAG2 OF SECT2.", + "snippet": " SECT1 SECTION.\r\n PARAG1.\r\n DISPLAY \u0027PARAG1\u0027.\r\n PERFORM PARAG2 OF SECT2.", "type": "section", "location": { "uri": "fake/path", @@ -62,7 +62,7 @@ }, "children": [ { - "snippet": "PARAG1.\r\n DISPLAY \u0027PARAG1\u0027.\r\n PERFORM PARAG2 OF SECT2.", + "snippet": " PARAG1.\r\n DISPLAY \u0027PARAG1\u0027.\r\n PERFORM PARAG2 OF SECT2.", "name": "PARAG1", "type": "paragraph", "location": { @@ -113,7 +113,7 @@ }, { "name": "SECT2", - "snippet": "SECT2 SECTION.\r\n PARAG2.\r\n DISPLAY \u0027PARAG2\u0027.", + "snippet": " SECT2 SECTION.\r\n PARAG2.\r\n DISPLAY \u0027PARAG2\u0027.", "type": "section", "location": { "uri": "fake/path", @@ -128,7 +128,7 @@ }, "children": [ { - "snippet": "PARAG2.\r\n DISPLAY \u0027PARAG2\u0027.", + "snippet": " PARAG2.\r\n DISPLAY \u0027PARAG2\u0027.", "name": "PARAG2", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case7.result.json b/server/engine/src/test/resources/cfast/case7.result.json index 41637c2df2..c9e9699bfe 100644 --- a/server/engine/src/test/resources/cfast/case7.result.json +++ b/server/engine/src/test/resources/cfast/case7.result.json @@ -44,7 +44,7 @@ } }, { - "snippet": "PARAG1.\r\n EXIT.", + "snippet": " PARAG1.\r\n EXIT.", "name": "PARAG1", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case8.result.json b/server/engine/src/test/resources/cfast/case8.result.json index 6a8cae6276..c754fa780d 100644 --- a/server/engine/src/test/resources/cfast/case8.result.json +++ b/server/engine/src/test/resources/cfast/case8.result.json @@ -44,7 +44,7 @@ } }, { - "snippet": "PARAG1.\r\n EXIT.", + "snippet": " PARAG1.\r\n EXIT.", "name": "PARAG1", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_alter.result.json b/server/engine/src/test/resources/cfast/case_alter.result.json index 431b3c8b5b..3c649e0ec8 100644 --- a/server/engine/src/test/resources/cfast/case_alter.result.json +++ b/server/engine/src/test/resources/cfast/case_alter.result.json @@ -37,7 +37,7 @@ }, { "name": "SEC1", - "snippet": "SEC1 SECTION.\r\n PAR1.\r\n PAR2.", + "snippet": " SEC1 SECTION.\r\n PAR1.\r\n PAR2.", "type": "section", "location": { "uri": "fake/path", @@ -52,7 +52,7 @@ }, "children": [ { - "snippet": "PAR1.", + "snippet": " PAR1.", "name": "PAR1", "type": "paragraph", "location": { @@ -68,7 +68,7 @@ } }, { - "snippet": "PAR2.", + "snippet": " PAR2.", "name": "PAR2", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_conditional_perform.result.json b/server/engine/src/test/resources/cfast/case_conditional_perform.result.json index 4117928ab4..7edf37900e 100644 --- a/server/engine/src/test/resources/cfast/case_conditional_perform.result.json +++ b/server/engine/src/test/resources/cfast/case_conditional_perform.result.json @@ -60,7 +60,7 @@ } }, { - "snippet": "PAR1.\r\n CONTINUE.", + "snippet": " PAR1.\r\n CONTINUE.", "name": "PAR1", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_evaluate.result.json b/server/engine/src/test/resources/cfast/case_evaluate.result.json index 696e3c4e48..91fdb04366 100644 --- a/server/engine/src/test/resources/cfast/case_evaluate.result.json +++ b/server/engine/src/test/resources/cfast/case_evaluate.result.json @@ -105,7 +105,7 @@ } }, { - "snippet": "PAR1.\r\n CONTINUE.", + "snippet": " PAR1.\r\n CONTINUE.", "name": "PAR1", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_evaluate_other.result.json b/server/engine/src/test/resources/cfast/case_evaluate_other.result.json index b61cf790ef..16c4350b7f 100644 --- a/server/engine/src/test/resources/cfast/case_evaluate_other.result.json +++ b/server/engine/src/test/resources/cfast/case_evaluate_other.result.json @@ -133,7 +133,7 @@ } }, { - "snippet": "PAR1.\r\n CONTINUE.", + "snippet": " PAR1.\r\n CONTINUE.", "name": "PAR1", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_execCicsHandleAbendEx.result.json b/server/engine/src/test/resources/cfast/case_execCicsHandleAbendEx.result.json index b441c7b6ed..7e369208a8 100644 --- a/server/engine/src/test/resources/cfast/case_execCicsHandleAbendEx.result.json +++ b/server/engine/src/test/resources/cfast/case_execCicsHandleAbendEx.result.json @@ -90,7 +90,7 @@ } }, { - "snippet": "HANDLE-ABEND.", + "snippet": " HANDLE-ABEND.", "name": "HANDLE-ABEND", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_execSqlWhenever.result.json b/server/engine/src/test/resources/cfast/case_execSqlWhenever.result.json index 01dd94371f..2809057226 100644 --- a/server/engine/src/test/resources/cfast/case_execSqlWhenever.result.json +++ b/server/engine/src/test/resources/cfast/case_execSqlWhenever.result.json @@ -228,7 +228,7 @@ } }, { - "snippet": "HANDLER.\r\n DISPLAY \"HANDLER\".", + "snippet": " HANDLER.\r\n DISPLAY \"HANDLER\".", "name": "HANDLER", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_goto_keyword.result.json b/server/engine/src/test/resources/cfast/case_goto_keyword.result.json index 56a5c64c47..33269aa59f 100644 --- a/server/engine/src/test/resources/cfast/case_goto_keyword.result.json +++ b/server/engine/src/test/resources/cfast/case_goto_keyword.result.json @@ -46,7 +46,7 @@ } }, { - "snippet": "OPTIONS.\r\n DISPLAY \u0027OPTION\u0027.", + "snippet": " OPTIONS.\r\n DISPLAY \u0027OPTION\u0027.", "name": "OPTIONS", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_handle_abend_label.result.json b/server/engine/src/test/resources/cfast/case_handle_abend_label.result.json index e2a80704ae..8759096ff1 100644 --- a/server/engine/src/test/resources/cfast/case_handle_abend_label.result.json +++ b/server/engine/src/test/resources/cfast/case_handle_abend_label.result.json @@ -46,7 +46,7 @@ }, { "name": "SEC1", - "snippet": "SEC1 SECTION.", + "snippet": " SEC1 SECTION.", "type": "section", "location": { "uri": "fake/path", diff --git a/server/engine/src/test/resources/cfast/case_ifs.result.json b/server/engine/src/test/resources/cfast/case_ifs.result.json index f633796aa0..6736396e61 100644 --- a/server/engine/src/test/resources/cfast/case_ifs.result.json +++ b/server/engine/src/test/resources/cfast/case_ifs.result.json @@ -15,7 +15,7 @@ }, "children": [ { - "snippet": "A000-PARA.\r\n MOVE 25 TO WS-NUM1 WS-NUM3.\r\n MOVE 15 TO WS-NUM2 WS-NUM4.\r\n \r\n IF WS-NUM1 \u003e WS-NUM2 THEN\r\n DISPLAY \u0027IN LOOP 1 - IF BLOCK\u0027\r\n \r\n IF WS-NUM3 \u003d WS-NUM4 THEN\r\n DISPLAY \u0027IN LOOP 2 - IF BLOCK\u0027\r\n ELSE", + "snippet": " A000-PARA.\r\n MOVE 25 TO WS-NUM1 WS-NUM3.\r\n MOVE 15 TO WS-NUM2 WS-NUM4.\r\n \r\n IF WS-NUM1 \u003e WS-NUM2 THEN\r\n DISPLAY \u0027IN LOOP 1 - IF BLOCK\u0027\r\n \r\n IF WS-NUM3 \u003d WS-NUM4 THEN\r\n DISPLAY \u0027IN LOOP 2 - IF BLOCK\u0027\r\n ELSE", "name": "A000-PARA", "type": "paragraph", "location": { @@ -204,7 +204,7 @@ ] }, { - "snippet": "A001-PARA.\r\n STOP RUN.", + "snippet": " A001-PARA.\r\n STOP RUN.", "name": "A001-PARA", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_ifs_1.result.json b/server/engine/src/test/resources/cfast/case_ifs_1.result.json index 3a69aa5649..e269549947 100644 --- a/server/engine/src/test/resources/cfast/case_ifs_1.result.json +++ b/server/engine/src/test/resources/cfast/case_ifs_1.result.json @@ -15,7 +15,7 @@ }, "children": [ { - "snippet": "A000-PARA.\r\n IF NOT WS-NUM1 \u003e WS-NUM2\r\n DISPLAY \u0027HELLO\u0027\r\n GO TO A000-PARA.\r\n STOP RUN.", + "snippet": " A000-PARA.\r\n IF NOT WS-NUM1 \u003e WS-NUM2\r\n DISPLAY \u0027HELLO\u0027\r\n GO TO A000-PARA.\r\n STOP RUN.", "name": "A000-PARA", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_ifs_2.result.json b/server/engine/src/test/resources/cfast/case_ifs_2.result.json index 4889d3039c..110d92654e 100644 --- a/server/engine/src/test/resources/cfast/case_ifs_2.result.json +++ b/server/engine/src/test/resources/cfast/case_ifs_2.result.json @@ -15,7 +15,7 @@ }, "children": [ { - "snippet": "A000-PARA.\r\n IF NOT WS-NUM1 \u003e WS-NUM2\r\n DISPLAY \u0027HELLO\u0027\r\n IF WS-NUM1 \u003e WS-NUM2\r\n GO TO A000-PARA\r\n END-IF\r\n END-IF\r\n PERFORM A000-PARA.\r\n STOP RUN.", + "snippet": " A000-PARA.\r\n IF NOT WS-NUM1 \u003e WS-NUM2\r\n DISPLAY \u0027HELLO\u0027\r\n IF WS-NUM1 \u003e WS-NUM2\r\n GO TO A000-PARA\r\n END-IF\r\n END-IF\r\n PERFORM A000-PARA.\r\n STOP RUN.", "name": "A000-PARA", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_merge.result.json b/server/engine/src/test/resources/cfast/case_merge.result.json index a267f9307a..0f1873923b 100644 --- a/server/engine/src/test/resources/cfast/case_merge.result.json +++ b/server/engine/src/test/resources/cfast/case_merge.result.json @@ -75,7 +75,7 @@ } }, { - "snippet": "PAR1.\r\n DISPLAY \"PAR1\".", + "snippet": " PAR1.\r\n DISPLAY \"PAR1\".", "name": "PAR1", "type": "paragraph", "location": { @@ -107,7 +107,7 @@ ] }, { - "snippet": "PAR2.\r\n DISPLAY \"PAR2\".", + "snippet": " PAR2.\r\n DISPLAY \"PAR2\".", "name": "PAR2", "type": "paragraph", "location": { @@ -139,7 +139,7 @@ ] }, { - "snippet": "PAR3.\r\n DISPLAY \"PAR3\".", + "snippet": " PAR3.\r\n DISPLAY \"PAR3\".", "name": "PAR3", "type": "paragraph", "location": { @@ -171,7 +171,7 @@ ] }, { - "snippet": "INPUT-PAR1.\r\n PERFORM PAR1.", + "snippet": " INPUT-PAR1.\r\n PERFORM PAR1.", "name": "INPUT-PAR1", "type": "paragraph", "location": { @@ -204,7 +204,7 @@ ] }, { - "snippet": "INPUT-PAR2.\r\n PERFORM PAR2.", + "snippet": " INPUT-PAR2.\r\n PERFORM PAR2.", "name": "INPUT-PAR2", "type": "paragraph", "location": { @@ -238,7 +238,7 @@ }, { "name": "SECTION1", - "snippet": "SECTION1 SECTION.\r\n \r\n OUTPUT-PAR1.\r\n PERFORM PAR3.", + "snippet": " SECTION1 SECTION.\r\n \r\n OUTPUT-PAR1.\r\n PERFORM PAR3.", "type": "section", "location": { "uri": "fake/path", @@ -253,7 +253,7 @@ }, "children": [ { - "snippet": "OUTPUT-PAR1.\r\n PERFORM PAR3.", + "snippet": " OUTPUT-PAR1.\r\n PERFORM PAR3.", "name": "OUTPUT-PAR1", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_perform_thru.result.json b/server/engine/src/test/resources/cfast/case_perform_thru.result.json index b8a8dc0376..75a4b1e2d9 100644 --- a/server/engine/src/test/resources/cfast/case_perform_thru.result.json +++ b/server/engine/src/test/resources/cfast/case_perform_thru.result.json @@ -34,7 +34,7 @@ }, { "name": "SEC1", - "snippet": "SEC1 SECTION.\r\n BP.\r\n DISPLAY \u0027BP\u0027.\r\n CP.\r\n DISPLAY \u0027CP\u0027.", + "snippet": " SEC1 SECTION.\r\n BP.\r\n DISPLAY \u0027BP\u0027.\r\n CP.\r\n DISPLAY \u0027CP\u0027.", "type": "section", "location": { "uri": "fake/path", @@ -49,7 +49,7 @@ }, "children": [ { - "snippet": "BP.\r\n DISPLAY \u0027BP\u0027.", + "snippet": " BP.\r\n DISPLAY \u0027BP\u0027.", "name": "BP", "type": "paragraph", "location": { @@ -81,7 +81,7 @@ ] }, { - "snippet": "CP.\r\n DISPLAY \u0027CP\u0027.", + "snippet": " CP.\r\n DISPLAY \u0027CP\u0027.", "name": "CP", "type": "paragraph", "location": { @@ -116,7 +116,7 @@ }, { "name": "SEC2", - "snippet": "SEC2 SECTION.\r\n EP.\r\n DISPLAY \u0027EP\u0027.", + "snippet": " SEC2 SECTION.\r\n EP.\r\n DISPLAY \u0027EP\u0027.", "type": "section", "location": { "uri": "fake/path", @@ -131,7 +131,7 @@ }, "children": [ { - "snippet": "EP.\r\n DISPLAY \u0027EP\u0027.", + "snippet": " EP.\r\n DISPLAY \u0027EP\u0027.", "name": "EP", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_sort.result.json b/server/engine/src/test/resources/cfast/case_sort.result.json index d89f538f8d..fd47d3122e 100644 --- a/server/engine/src/test/resources/cfast/case_sort.result.json +++ b/server/engine/src/test/resources/cfast/case_sort.result.json @@ -95,7 +95,7 @@ } }, { - "snippet": "PAR1.\r\n DISPLAY \"PAR1\".", + "snippet": " PAR1.\r\n DISPLAY \"PAR1\".", "name": "PAR1", "type": "paragraph", "location": { @@ -127,7 +127,7 @@ ] }, { - "snippet": "PAR2.\r\n DISPLAY \"PAR2\".", + "snippet": " PAR2.\r\n DISPLAY \"PAR2\".", "name": "PAR2", "type": "paragraph", "location": { @@ -159,7 +159,7 @@ ] }, { - "snippet": "PAR3.\r\n DISPLAY \"PAR3\".", + "snippet": " PAR3.\r\n DISPLAY \"PAR3\".", "name": "PAR3", "type": "paragraph", "location": { @@ -191,7 +191,7 @@ ] }, { - "snippet": "INPUT-PAR1.\r\n PERFORM PAR1.", + "snippet": " INPUT-PAR1.\r\n PERFORM PAR1.", "name": "INPUT-PAR1", "type": "paragraph", "location": { @@ -224,7 +224,7 @@ ] }, { - "snippet": "INPUT-PAR2.\r\n PERFORM PAR2.", + "snippet": " INPUT-PAR2.\r\n PERFORM PAR2.", "name": "INPUT-PAR2", "type": "paragraph", "location": { @@ -258,7 +258,7 @@ }, { "name": "SECTION1", - "snippet": "SECTION1 SECTION.\r\n \r\n OUTPUT-PAR1.\r\n PERFORM PAR3.", + "snippet": " SECTION1 SECTION.\r\n \r\n OUTPUT-PAR1.\r\n PERFORM PAR3.", "type": "section", "location": { "uri": "fake/path", @@ -273,7 +273,7 @@ }, "children": [ { - "snippet": "OUTPUT-PAR1.\r\n PERFORM PAR3.", + "snippet": " OUTPUT-PAR1.\r\n PERFORM PAR3.", "name": "OUTPUT-PAR1", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_useForDebugging.result.json b/server/engine/src/test/resources/cfast/case_useForDebugging.result.json index 6ec29adda7..b3f38e3321 100644 --- a/server/engine/src/test/resources/cfast/case_useForDebugging.result.json +++ b/server/engine/src/test/resources/cfast/case_useForDebugging.result.json @@ -61,7 +61,7 @@ ] }, { - "snippet": "Some-Routine.\r\n DISPLAY \"SOME-ROUTINE\".", + "snippet": " Some-Routine.\r\n DISPLAY \"SOME-ROUTINE\".", "name": "SOME-ROUTINE", "type": "paragraph", "location": { diff --git a/server/engine/src/test/resources/cfast/case_xml_parse.result.json b/server/engine/src/test/resources/cfast/case_xml_parse.result.json index 162bf1bf33..585d2fc53c 100644 --- a/server/engine/src/test/resources/cfast/case_xml_parse.result.json +++ b/server/engine/src/test/resources/cfast/case_xml_parse.result.json @@ -15,7 +15,7 @@ }, "children": [ { - "snippet": "MAINLINE.\r\n DISPLAY \u0027XML-DOCUMENT\u003d\u0027 XML-STRING(EZ-PTR)\r\n XML PARSE XML-STRING(EZ-PTR) RETURNING NATIONAL\r\n PROCESSING PROCEDURE XML-HANDLER\r\n ON EXCEPTION\r\n GO TO ABEND\r\n NOT ON EXCEPTION\r\n DISPLAY \u0027XML DOCUMENT SUCCESSFULLY PARSED\u0027\r\n END-XML.", + "snippet": " MAINLINE.\r\n DISPLAY \u0027XML-DOCUMENT\u003d\u0027 XML-STRING(EZ-PTR)\r\n XML PARSE XML-STRING(EZ-PTR) RETURNING NATIONAL\r\n PROCESSING PROCEDURE XML-HANDLER\r\n ON EXCEPTION\r\n GO TO ABEND\r\n NOT ON EXCEPTION\r\n DISPLAY \u0027XML DOCUMENT SUCCESSFULLY PARSED\u0027\r\n END-XML.", "name": "MAINLINE", "type": "paragraph", "location": { @@ -165,7 +165,7 @@ ] }, { - "snippet": "XML-HANDLER.\r\n DISPLAY \u0027XML-EVENT\u003d\u0027 XML-EVENT.", + "snippet": " XML-HANDLER.\r\n DISPLAY \u0027XML-EVENT\u003d\u0027 XML-EVENT.", "name": "XML-HANDLER", "type": "paragraph", "location": { @@ -197,7 +197,7 @@ ] }, { - "snippet": "ABEND.\r\n DISPLAY \u0027XML-EVENT\u003d\u0027 XML-EVENT.", + "snippet": " ABEND.\r\n DISPLAY \u0027XML-EVENT\u003d\u0027 XML-EVENT.", "name": "ABEND", "type": "paragraph", "location": { From e3bd1668d21a5138d9c4e4a0377b9c18693e9d75 Mon Sep 17 00:00:00 2001 From: Leonid Baranov Date: Wed, 9 Oct 2024 14:23:16 +0200 Subject: [PATCH 2/2] fix: Fix snippet text for sections and paragraphs --- .../common/mapping/ExtendedDocumentTest.java | 41 +++++++++++++++++++ .../handlers/extended/AnalysisHandler.java | 10 +++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/server/common/src/test/java/org/eclipse/lsp/cobol/common/mapping/ExtendedDocumentTest.java b/server/common/src/test/java/org/eclipse/lsp/cobol/common/mapping/ExtendedDocumentTest.java index 3f5c8f0eb5..96b8d26b0b 100644 --- a/server/common/src/test/java/org/eclipse/lsp/cobol/common/mapping/ExtendedDocumentTest.java +++ b/server/common/src/test/java/org/eclipse/lsp/cobol/common/mapping/ExtendedDocumentTest.java @@ -14,6 +14,7 @@ */ package org.eclipse.lsp.cobol.common.mapping; +import org.eclipse.lsp.cobol.common.model.Locality; import org.eclipse.lsp4j.Location; import org.eclipse.lsp4j.Position; import org.eclipse.lsp4j.Range; @@ -122,4 +123,44 @@ void testIsLineEmptyBetweenColumns() { assertFalse(document.isLineEmptyBetweenColumns(0, 7, 100)); assertTrue(document.isLineEmptyBetweenColumns(1000, 0, 1)); } + + @Test + void testGetBaseTextProperUri() { + document.insertCopybook(5, copybook); + Locality locality = Locality.builder() + .uri(documentUri) + .range(new Range(new Position(3, 5), new Position(4, 8))) + .build(); + String text = document.getBaseText(locality); + + assertEquals(" WORKING-STORAGE SECTION.\r\n" + + " COPY CPY.", text); + } + + @Test + void testGetBaseTextEdge() { + document.insertCopybook(5, copybook); + Locality locality = Locality.builder() + .uri(documentUri) + .range(new Range(new Position(3, 5), new Position(5, 1))) + .build(); + String text = document.getBaseText(locality); + + assertEquals(" WORKING-STORAGE SECTION.\r\n" + + " COPY CPY.\r\n" + + " PROCEDURE DIVISION.", text); + } + + @Test + void testGetBaseTextWrongUri() { + document.insertCopybook(5, copybook); + Locality locality = Locality.builder() + .uri("wrong") + .range(new Range(new Position(3, 5), new Position(4, 8))) + .build(); + String text = document.getBaseText(locality); + + assertEquals("", text); + } + } diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/lsp/handlers/extended/AnalysisHandler.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/lsp/handlers/extended/AnalysisHandler.java index 34c82f3730..20413dae93 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/lsp/handlers/extended/AnalysisHandler.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/lsp/handlers/extended/AnalysisHandler.java @@ -85,12 +85,16 @@ public ExtendedApiResult analysis(AnalysisResultEvent analysisResultEvent) throw int character = analysisResultEvent.getCharacter(); Position position = new Position(line, character); Optional selectedNode = RangeUtils.findNodeByPosition(rootNode, analysisResultEvent.getUri(), position); + + ProgramNode programNode; if (selectedNode.isPresent() && !(selectedNode.get() instanceof RootNode)) { - return cfastBuilder.build((ProgramNode) selectedNode.get() - .getNearestParentByType(PROGRAM).orElse(selectProgramNode(rootNode.findPrograms(), line, character))); + programNode = (ProgramNode) selectedNode.get() + .getNearestParentByType(PROGRAM).orElse(selectProgramNode(rootNode.findPrograms(), line, character)); + } else { + programNode = selectProgramNode(rootNode.findPrograms(), line, character); } - return cfastBuilder.build(selectProgramNode(rootNode.findPrograms(), line, character)); + return cfastBuilder.build(programNode); } private ProgramNode selectProgramNode(List programs, int line, int character) {