diff --git a/server/common/src/main/java/org/eclipse/lsp/cobol/common/copybook/CopybookService.java b/server/common/src/main/java/org/eclipse/lsp/cobol/common/copybook/CopybookService.java index 50028a9196..d1494ff507 100644 --- a/server/common/src/main/java/org/eclipse/lsp/cobol/common/copybook/CopybookService.java +++ b/server/common/src/main/java/org/eclipse/lsp/cobol/common/copybook/CopybookService.java @@ -15,7 +15,12 @@ package org.eclipse.lsp.cobol.common.copybook; import java.util.Collection; +import java.util.Collections; +import java.util.Set; + +import com.google.common.collect.ImmutableSet; import lombok.NonNull; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; /** @@ -27,6 +32,8 @@ public interface CopybookService { /** Remove all the stored copybook. */ void invalidateCache(); + void invalidateCache(CopybookId copybookId); + /** * Retrieve and return the copybook by its name. * Returns a CopybookModel and preprocessed errors for the resolved copybook wrapped inside {@link ResultWithErrors}. @@ -35,8 +42,7 @@ public interface CopybookService { * @param copybookName - the name of the copybook to be retrieved * @param programDocumentUri - the currently processing program document * @param documentUri - the currently processing document that contains the copy statement - * @param preprocess - indicates if copybook needs to be preprocessed after resolving - * @param languageId + * @param preprocessor - Cleanup preprocessor that will be used for new copybooks or null * @return a CopybookModel wrapped inside {@link ResultWithErrors} which contains copybook name, its URI and the content. * Wrapped errors are preprocessed errors for the returned CopybookModel. */ @@ -45,7 +51,7 @@ ResultWithErrors resolve( @NonNull CopybookName copybookName, @NonNull String programDocumentUri, @NonNull String documentUri, - boolean preprocess, String languageId); + CleanerPreprocessor preprocessor); /** * Store the copybookModel in cache. Copybook depends on a document from where it is imported. @@ -58,10 +64,9 @@ ResultWithErrors resolve( * Store the copybookModel in cache. Copybook depends on a document from where it is imported. * * @param copybookModel the copybook model - * @param languageId languageId of the source code - * @param doCleanUp is copybook clean up required before storing copybookModel. + * @param preprocessor - Cleanup preprocessor that will be used for new copybooks or null */ - void store(CopybookModel copybookModel, String languageId, boolean doCleanUp); + void store(CopybookModel copybookModel, CleanerPreprocessor preprocessor); /** * Send downloading requests to the Client for copybooks not presented locally, if any. @@ -71,4 +76,13 @@ ResultWithErrors resolve( * @param processingMode copybook processing mode. */ void sendCopybookDownloadRequest(String documentUri, Collection copybookUris, CopybookProcessingMode processingMode); + + /** + * Get the list of copybook used by a document + * + * @param documentUri current document uri. + * @return Set of all the {@link CopybookModel} used by the passed document + */ + Set getCopybookUsage(String documentUri); + } diff --git a/server/common/src/main/java/org/eclipse/lsp/cobol/common/dialects/DialectProcessingContext.java b/server/common/src/main/java/org/eclipse/lsp/cobol/common/dialects/DialectProcessingContext.java index 4289a37e7a..fd04a79e39 100644 --- a/server/common/src/main/java/org/eclipse/lsp/cobol/common/dialects/DialectProcessingContext.java +++ b/server/common/src/main/java/org/eclipse/lsp/cobol/common/dialects/DialectProcessingContext.java @@ -19,6 +19,7 @@ import lombok.Builder; import lombok.Value; import org.eclipse.lsp.cobol.common.AnalysisConfig; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.mapping.ExtendedDocument; import org.eclipse.lsp.cobol.common.model.tree.Node; @@ -29,6 +30,7 @@ public class DialectProcessingContext { AnalysisConfig config; String programDocumentUri; ExtendedDocument extendedDocument; + CleanerPreprocessor preprocessor; @Builder.Default List dialectNodes = new ArrayList<>(); @Builder.Default String languageId = "cobol"; } diff --git a/server/dialect-daco/src/main/java/org/eclipse/lsp/cobol/dialects/daco/DaCoMaidProcessor.java b/server/dialect-daco/src/main/java/org/eclipse/lsp/cobol/dialects/daco/DaCoMaidProcessor.java index c9e5e3e847..5fcf584066 100644 --- a/server/dialect-daco/src/main/java/org/eclipse/lsp/cobol/dialects/daco/DaCoMaidProcessor.java +++ b/server/dialect-daco/src/main/java/org/eclipse/lsp/cobol/dialects/daco/DaCoMaidProcessor.java @@ -211,7 +211,7 @@ private CopyNode createMaidCopybookNode( copybookName, context.getExtendedDocument().getUri(), context.getExtendedDocument().getUri(), - true, context.getLanguageId()); + context.getPreprocessor()); CopybookModel copybookModel = resolvedCopybook.getResult(); DaCoCopyNode cbNode = diff --git a/server/dialect-daco/src/test/java/org/eclipse/lsp/cobol/dialects/daco/utils/UseCaseInitializerService.java b/server/dialect-daco/src/test/java/org/eclipse/lsp/cobol/dialects/daco/utils/UseCaseInitializerService.java index 4245041e33..2be9223e43 100644 --- a/server/dialect-daco/src/test/java/org/eclipse/lsp/cobol/dialects/daco/utils/UseCaseInitializerService.java +++ b/server/dialect-daco/src/test/java/org/eclipse/lsp/cobol/dialects/daco/utils/UseCaseInitializerService.java @@ -35,8 +35,8 @@ import org.eclipse.lsp.cobol.common.file.FileSystemService; import org.eclipse.lsp.cobol.common.file.WorkspaceFileService; import org.eclipse.lsp.cobol.core.engine.dialects.DialectDiscoveryService; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessorImpl; +import org.eclipse.lsp.cobol.dialects.hp.HpTextPreprocessor; +import org.eclipse.lsp.cobol.dialects.ibm.IbmTextPreprocessor; import org.eclipse.lsp.cobol.domain.modules.DatabusModule; import org.eclipse.lsp.cobol.domain.modules.EngineModule; import org.eclipse.lsp.cobol.lsp.DisposableLSPStateService; @@ -81,8 +81,11 @@ protected void configure() { bind(FileSystemService.class).toInstance(new WorkspaceFileService()); bind(CobolLanguageClient.class).toInstance(languageClient); bind(SubroutineService.class).to(SubroutineServiceImpl.class); - bind(TextPreprocessor.class).to(TextPreprocessorImpl.class); - bind(CleanerPreprocessor.class).to(TextPreprocessorImpl.class); + + bind(CleanerPreprocessor.class).annotatedWith(Names.named("cobol")).to(IbmTextPreprocessor.class); + bind(CleanerPreprocessor.class).annotatedWith(Names.named("hpcobol")).to(HpTextPreprocessor.class); + + bind(CleanerPreprocessor.class).to(IbmTextPreprocessor.class); bind(WatcherService.class).to(WatcherServiceImpl.class); bind(DialectDiscoveryService.class).to(ExplicitDialectDiscoveryService.class); bind(CopybookIdentificationService.class) diff --git a/server/dialect-idms/src/main/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookService.java b/server/dialect-idms/src/main/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookService.java index d26974668f..901dd5bce3 100644 --- a/server/dialect-idms/src/main/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookService.java +++ b/server/dialect-idms/src/main/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookService.java @@ -24,6 +24,7 @@ import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.tree.ParseTreeListener; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.copybook.CopybookModel; import org.eclipse.lsp.cobol.common.copybook.CopybookName; @@ -44,6 +45,7 @@ class IdmsCopybookService { private final String programDocumentUri; private final CopybookService copybookService; + private final CleanerPreprocessor preprocessor; private final CopybookProcessingMode copybookProcessingMode; private final ParseTreeListener treeListener; private final MessageService messageService; @@ -95,7 +97,7 @@ private ResultWithErrors> processNodes(CopybookModel copybookModel, i parser.setErrorHandler(new CobolErrorStrategy(messageService)); parser.addParseListener(treeListener); - IdmsCopybookVisitor visitor = new IdmsCopybookVisitor(copybookService, copybookProcessingMode, treeListener, messageService, + IdmsCopybookVisitor visitor = new IdmsCopybookVisitor(copybookService, preprocessor, copybookProcessingMode, treeListener, messageService, programDocumentUri, copybookModel.getUri(), parentLevel, processedCopybooks); ParserRuleContext node = parser.startRule(); diff --git a/server/dialect-idms/src/main/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookVisitor.java b/server/dialect-idms/src/main/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookVisitor.java index 20cec5c51b..6c02f761ad 100644 --- a/server/dialect-idms/src/main/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookVisitor.java +++ b/server/dialect-idms/src/main/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookVisitor.java @@ -29,6 +29,7 @@ import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTreeListener; import org.antlr.v4.runtime.tree.TerminalNode; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.copybook.*; import org.eclipse.lsp.cobol.common.error.SyntaxError; @@ -51,17 +52,17 @@ @RequiredArgsConstructor class IdmsCopybookVisitor extends IdmsCopyParserBaseVisitor> { private final CopybookService copybookService; - private final CopybookProcessingMode copybookProcessingMode; + private final CleanerPreprocessor preprocessor; private final IdmsCopybookService idmsCopybookService; private final String programDocumentUri; private final String documentUri; private final int parentLevel; - private final Set processedCopybooks; @Getter private final List errors = new LinkedList<>(); private int firstCopybookLevel = 0; IdmsCopybookVisitor(CopybookService copybookService, + CleanerPreprocessor preprocessor, CopybookProcessingMode copybookProcessingMode, ParseTreeListener treeListener, MessageService messageService, @@ -70,12 +71,11 @@ class IdmsCopybookVisitor extends IdmsCopyParserBaseVisitor> { int parentLevel, Set processedCopybooks) { this.copybookService = copybookService; - this.copybookProcessingMode = copybookProcessingMode; + this.preprocessor = preprocessor; this.programDocumentUri = programDocumentUri; this.documentUri = documentUri; this.parentLevel = parentLevel; - this.processedCopybooks = processedCopybooks; - idmsCopybookService = new IdmsCopybookService(programDocumentUri, copybookService, + idmsCopybookService = new IdmsCopybookService(programDocumentUri, copybookService, preprocessor, copybookProcessingMode, treeListener, messageService, processedCopybooks); } @@ -90,7 +90,7 @@ public List visitCopyIdmsStatement(IdmsCopyParser.CopyIdmsStatementContext copybookName, programDocumentUri, documentUri, - true, "cobol"); + preprocessor); CopybookModel copybookModel = resolvedCopybook.getResult(); Locality locality = IdmsParserHelper.buildNameRangeLocality(optionsContext, copybookName.getDisplayName(), documentUri); errors.addAll(resolvedCopybook.getErrors()); diff --git a/server/dialect-idms/src/main/java/org/eclipse/lsp/cobol/dialects/idms/IdmsDialect.java b/server/dialect-idms/src/main/java/org/eclipse/lsp/cobol/dialects/idms/IdmsDialect.java index 9f509fae81..c8ee678931 100644 --- a/server/dialect-idms/src/main/java/org/eclipse/lsp/cobol/dialects/idms/IdmsDialect.java +++ b/server/dialect-idms/src/main/java/org/eclipse/lsp/cobol/dialects/idms/IdmsDialect.java @@ -79,11 +79,11 @@ private void insertIdmsCopybook(DialectProcessingContext ctx, ExtendedDocument e Deque copybookStack) { CopybookName copybookName = new CopybookName(cb.getName(), IdmsDialect.NAME); ResultWithErrors resolvedCopybook = copybookService.resolve( - copybookName.toCopybookId(programDocumentUri), - copybookName, - programDocumentUri, - currentUri, - true, "cobol"); + copybookName.toCopybookId(programDocumentUri), + copybookName, + programDocumentUri, + currentUri, + ctx.getPreprocessor()); CopybookModel copybookModel = resolvedCopybook.getResult(); if (copybookModel.getUri() == null || copybookModel.getContent() == null) { diff --git a/server/dialect-idms/src/test/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookServiceTest.java b/server/dialect-idms/src/test/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookServiceTest.java index d119489ed5..27a4b81e6b 100644 --- a/server/dialect-idms/src/test/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookServiceTest.java +++ b/server/dialect-idms/src/test/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookServiceTest.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.Set; import org.antlr.v4.runtime.tree.ParseTreeListener; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.copybook.*; import org.eclipse.lsp.cobol.common.error.SyntaxError; @@ -46,6 +47,8 @@ class IdmsCopybookServiceTest { private ParseTreeListener treeListener; @Mock private MessageService messageService; + @Mock + private CleanerPreprocessor preprocessor; private Set processedCopybooks; private IdmsCopybookService service; @@ -53,7 +56,8 @@ class IdmsCopybookServiceTest { @BeforeEach void init() { processedCopybooks = new HashSet<>(); - service = new IdmsCopybookService("uri", copybookService, CopybookProcessingMode.ENABLED, + service = new IdmsCopybookService("uri", copybookService, preprocessor, + CopybookProcessingMode.ENABLED, treeListener, messageService, processedCopybooks); } diff --git a/server/dialect-idms/src/test/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookVisitorTest.java b/server/dialect-idms/src/test/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookVisitorTest.java index 8d9b2b8b24..0e3d66f4aa 100644 --- a/server/dialect-idms/src/test/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookVisitorTest.java +++ b/server/dialect-idms/src/test/java/org/eclipse/lsp/cobol/dialects/idms/IdmsCopybookVisitorTest.java @@ -17,7 +17,6 @@ import static org.eclipse.lsp.cobol.dialects.idms.IdmsCopyParser.*; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; @@ -25,13 +24,12 @@ import com.google.common.collect.ImmutableList; import java.util.Collections; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.TerminalNode; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.copybook.*; import org.eclipse.lsp.cobol.common.model.NodeType; @@ -55,14 +53,13 @@ class IdmsCopybookVisitorTest { private CopybookService copybookService; @Mock private IdmsCopybookService idmsCopybookService; + @Mock + private CleanerPreprocessor preprocessor; private IdmsCopybookVisitor visitor; @BeforeEach void init() { - Set processedCopybooks = new HashSet<>(); - - visitor = new IdmsCopybookVisitor(copybookService, CopybookProcessingMode.ENABLED, - idmsCopybookService, PROGRAM_DOCUMENT_URI, "documentUri", 1, processedCopybooks); + visitor = new IdmsCopybookVisitor(copybookService, preprocessor, idmsCopybookService, PROGRAM_DOCUMENT_URI, "documentUri", 1); } @Test @@ -72,7 +69,7 @@ void testVisitCopyIdmsStatement() { IdmsCopyParser.CopyIdmsSourceContext sourceContext = mock(IdmsCopyParser.CopyIdmsSourceContext.class); CopybookName expectedCopybookName = new CopybookName("copybook", "IDMS"); when(copybookService.resolve(any(CopybookId.class), any(CopybookName.class), anyString(), anyString(), - anyBoolean(), anyString())) + any())) .thenReturn( new ResultWithErrors<>( new CopybookModel(expectedCopybookName.toCopybookId(PROGRAM_DOCUMENT_URI), expectedCopybookName, null, null), diff --git a/server/dialect-idms/src/test/java/org/eclipse/lsp/cobol/dialects/idms/utils/UseCaseInitializerService.java b/server/dialect-idms/src/test/java/org/eclipse/lsp/cobol/dialects/idms/utils/UseCaseInitializerService.java index 26ceb3e5d2..deee39e00d 100644 --- a/server/dialect-idms/src/test/java/org/eclipse/lsp/cobol/dialects/idms/utils/UseCaseInitializerService.java +++ b/server/dialect-idms/src/test/java/org/eclipse/lsp/cobol/dialects/idms/utils/UseCaseInitializerService.java @@ -35,8 +35,7 @@ import org.eclipse.lsp.cobol.common.file.FileSystemService; import org.eclipse.lsp.cobol.common.file.WorkspaceFileService; import org.eclipse.lsp.cobol.core.engine.dialects.DialectDiscoveryService; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessorImpl; +import org.eclipse.lsp.cobol.dialects.ibm.IbmTextPreprocessor; import org.eclipse.lsp.cobol.domain.modules.DatabusModule; import org.eclipse.lsp.cobol.domain.modules.EngineModule; import org.eclipse.lsp.cobol.lsp.DisposableLSPStateService; @@ -82,8 +81,7 @@ protected void configure() { bind(FileSystemService.class).toInstance(new WorkspaceFileService()); bind(CobolLanguageClient.class).toInstance(languageClient); bind(SubroutineService.class).to(SubroutineServiceImpl.class); - bind(TextPreprocessor.class).to(TextPreprocessorImpl.class); - bind(CleanerPreprocessor.class).to(TextPreprocessorImpl.class); + bind(CleanerPreprocessor.class).to(IbmTextPreprocessor.class); bind(WatcherService.class).to(WatcherServiceImpl.class); bind(DialectDiscoveryService.class).to(ExplicitDialectDiscoveryService.class); bind(CopybookIdentificationService.class) diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/cli/Cli.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/cli/Cli.java index 0a612f8876..4283202828 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/cli/Cli.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/cli/Cli.java @@ -27,6 +27,7 @@ import org.eclipse.lsp.cobol.cli.di.CliModule; import org.eclipse.lsp.cobol.cli.modules.CliClientProvider; import org.eclipse.lsp.cobol.common.AnalysisConfig; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.SubroutineService; import org.eclipse.lsp.cobol.common.benchmark.BenchmarkService; @@ -45,9 +46,9 @@ import org.eclipse.lsp.cobol.core.engine.pipeline.stages.*; import org.eclipse.lsp.cobol.core.engine.processor.AstProcessor; import org.eclipse.lsp.cobol.core.engine.symbols.SymbolsRepository; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; import org.eclipse.lsp.cobol.core.preprocessor.delegates.GrammarPreprocessor; import org.eclipse.lsp.cobol.core.semantics.CopybooksRepository; +import org.eclipse.lsp.cobol.dialects.ibm.IbmTextPreprocessor; import org.eclipse.lsp.cobol.lsp.CobolLanguageId; import org.eclipse.lsp.cobol.service.settings.CachingConfigurationService; import org.eclipse.lsp.cobol.service.settings.layout.CodeLayoutStore; @@ -102,7 +103,7 @@ public Integer call() throws Exception { cliClientProvider.setCpyExt(Arrays.asList(cpyExt)); // Cleaning up - TextPreprocessor preprocessor = diCtx.getInstance(TextPreprocessor.class); + CleanerPreprocessor preprocessor = diCtx.getInstance(IbmTextPreprocessor.class); BenchmarkService benchmarkService = diCtx.getInstance(BenchmarkService.class); if (src == null) { @@ -111,7 +112,7 @@ public Integer call() throws Exception { } String documentUri = src.toURI().toString(); String text = new String(Files.readAllBytes(src.toPath())); - ResultWithErrors resultWithErrors = preprocessor.cleanUpCode(documentUri, text, CobolLanguageId.COBOL); + ResultWithErrors resultWithErrors = preprocessor.cleanUpCode(documentUri, text); AnalysisContext ctx = new AnalysisContext( new ExtendedDocument(resultWithErrors.getResult(), text), @@ -193,6 +194,7 @@ private static Pipeline setupPipeline(Injector diCtx, Action action) { DialectService dialectService = diCtx.getInstance(DialectService.class); MessageService messageService = diCtx.getInstance(MessageService.class); GrammarPreprocessor grammarPreprocessor = diCtx.getInstance(GrammarPreprocessor.class); + CleanerPreprocessor preprocessor = diCtx.getInstance(IbmTextPreprocessor.class); ParseTreeListener parseTreeListener = diCtx.getInstance(ParseTreeListener.class); SymbolsRepository symbolsRepository = diCtx.getInstance(SymbolsRepository.class); SubroutineService subroutineService = diCtx.getInstance(SubroutineService.class); @@ -203,8 +205,8 @@ private static Pipeline setupPipeline(Injector diCtx, Action action) { Pipeline pipeline = new Pipeline(); pipeline.add(new CompilerDirectivesStage(messageService)); - pipeline.add(new DialectProcessingStage(dialectService)); - pipeline.add(new PreprocessorStage(grammarPreprocessor)); + pipeline.add(new DialectProcessingStage(dialectService, preprocessor)); + pipeline.add(new PreprocessorStage(grammarPreprocessor, preprocessor)); if (action == Action.analysis) { pipeline.add(new ImplicitDialectProcessingStage(dialectService)); pipeline.add(new ParserStage(messageService, parseTreeListener)); diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/cli/di/CliModule.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/cli/di/CliModule.java index 0f6a586e80..62a3bed27d 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/cli/di/CliModule.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/cli/di/CliModule.java @@ -26,6 +26,7 @@ import org.eclipse.lsp.cobol.cfg.CFASTBuilder; import org.eclipse.lsp.cobol.cfg.CFASTBuilderImpl; import org.eclipse.lsp.cobol.cli.modules.CliClientProvider; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.LanguageEngineFacade; import org.eclipse.lsp.cobol.common.SubroutineService; import org.eclipse.lsp.cobol.common.action.CodeActionProvider; @@ -40,8 +41,8 @@ import org.eclipse.lsp.cobol.core.engine.dialects.DialectDiscoveryService; import org.eclipse.lsp.cobol.core.messages.LocaleStoreImpl; import org.eclipse.lsp.cobol.core.messages.PropertiesMessageService; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessorImpl; +import org.eclipse.lsp.cobol.dialects.hp.HpTextPreprocessor; +import org.eclipse.lsp.cobol.dialects.ibm.IbmTextPreprocessor; import org.eclipse.lsp.cobol.core.preprocessor.delegates.GrammarPreprocessor; import org.eclipse.lsp.cobol.core.preprocessor.delegates.GrammarPreprocessorImpl; import org.eclipse.lsp.cobol.core.preprocessor.delegates.copybooks.GrammarPreprocessorListenerFactory; @@ -90,7 +91,8 @@ public class CliModule extends AbstractModule { @Override protected void configure() { - bind(TextPreprocessor.class).to(TextPreprocessorImpl.class); + bind(CleanerPreprocessor.class).annotatedWith(Names.named("cobol")).to(IbmTextPreprocessor.class); + bind(CleanerPreprocessor.class).annotatedWith(Names.named("hpcobol")).to(HpTextPreprocessor.class); bind(CobolLineReader.class).annotatedWith(Names.named("hpcobol")).to(HPCobolLineReaderImpl.class); bind(CobolLineReader.class).annotatedWith(Names.named("cobol")).to(CobolLineReaderImpl.class); diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/dialects/DialectService.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/dialects/DialectService.java index 0f4403643a..36e6a19624 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/dialects/DialectService.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/dialects/DialectService.java @@ -23,6 +23,7 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import org.eclipse.lsp.cobol.common.AnalysisConfig; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.DialectRegistryItem; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.copybook.CopybookModel; @@ -43,7 +44,6 @@ import org.eclipse.lsp.cobol.core.engine.dialects.hp.HpDialect; import org.eclipse.lsp.cobol.implicitDialects.cics.CICSDialect; import org.eclipse.lsp.cobol.implicitDialects.sql.Db2SqlDialect; -import org.eclipse.lsp.cobol.lsp.CobolLanguageId; import org.eclipse.lsp4j.Location; import org.eclipse.lsp4j.Position; import org.eclipse.lsp4j.Range; @@ -369,15 +369,15 @@ public List getWatchingFolderSettings() { * Add pre-defined copybooks from dialects to the copybook repository. * * @param config {@link AnalysisConfig} - * @param languageId + * @param preprocessor - dialect specific preprocessor */ - public void addDialectPredefinedCopybooks(AnalysisConfig config, CobolLanguageId languageId) { + public void addDialectPredefinedCopybooks(AnalysisConfig config, CleanerPreprocessor preprocessor) { List dialects = new ArrayList<>(); config.getDialects().forEach(dialect -> getDialectByName(dialect).ifPresent(dialects::add)); dialects.addAll(getActiveImplicitDialects(config)); for (CobolDialect dialect : dialects) { List predefinedCopybook = dialect.getPredefinedCopybook(config); - predefinedCopybook.forEach(model -> copybookService.store(model, languageId.getId(), true)); + predefinedCopybook.forEach(model -> copybookService.store(model, preprocessor)); } } } diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/dialects/hp/HpDialect.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/dialects/hp/HpDialect.java index b8dc5ca1f0..e3522fa8b2 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/dialects/hp/HpDialect.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/dialects/hp/HpDialect.java @@ -79,7 +79,7 @@ private void insertHpCopybook(DialectProcessingContext context, CopybookDescript CopybookName copybookName = new CopybookName(descriptor.getName()); CopybookModel model = copybookService.resolve(copybookName.toCopybookId(context.getProgramDocumentUri()), copybookName, context.getProgramDocumentUri(), - context.getExtendedDocument().getUri(), false, "hp-cobol") + context.getExtendedDocument().getUri(), null) .unwrap(errors::addAll); Location nameLocation = new Location(context.getExtendedDocument().getUri(), descriptor.getNameRange()); diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/pipeline/stages/DialectProcessingStage.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/pipeline/stages/DialectProcessingStage.java index 4fdfbfda8b..9ad8d28b3a 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/pipeline/stages/DialectProcessingStage.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/pipeline/stages/DialectProcessingStage.java @@ -17,6 +17,7 @@ import java.util.HashSet; import java.util.Set; import lombok.RequiredArgsConstructor; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.dialects.DialectOutcome; import org.eclipse.lsp.cobol.common.dialects.DialectProcessingContext; import org.eclipse.lsp.cobol.common.error.SyntaxError; @@ -32,6 +33,7 @@ public class DialectProcessingStage implements Stage { private final DialectService dialectService; + private final CleanerPreprocessor preprocessor; @Override public StageResult run(AnalysisContext context, StageResult prevStageResult) { @@ -47,12 +49,13 @@ public String getName() { } private DialectOutcome processDialects(AnalysisContext ctx) { - dialectService.addDialectPredefinedCopybooks(ctx.getConfig(), ctx.getLanguageId()); + dialectService.addDialectPredefinedCopybooks(ctx.getConfig(), preprocessor); DialectProcessingContext dialectProcessingContext = DialectProcessingContext.builder() .config(ctx.getConfig()) .programDocumentUri(ctx.getExtendedDocument().getUri()) .extendedDocument(ctx.getExtendedDocument()) + .preprocessor(preprocessor) .build(); dialectProcessingContext.getExtendedDocument().commitTransformations(); diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/pipeline/stages/PreprocessorStage.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/pipeline/stages/PreprocessorStage.java index 8f536a6703..5a861ff0a9 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/pipeline/stages/PreprocessorStage.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/pipeline/stages/PreprocessorStage.java @@ -17,6 +17,7 @@ import java.util.ArrayList; import java.util.List; import lombok.RequiredArgsConstructor; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.dialects.DialectOutcome; import org.eclipse.lsp.cobol.common.error.SyntaxError; import org.eclipse.lsp.cobol.common.mapping.ExtendedDocument; @@ -36,6 +37,7 @@ @RequiredArgsConstructor public class PreprocessorStage implements Stage { private final GrammarPreprocessor grammarPreprocessor; + private final CleanerPreprocessor preprocessor; @Override public StageResult run(AnalysisContext context, StageResult prevStageResult) { @@ -67,9 +69,10 @@ private void applyDialectCopybooks(CopybooksRepository copybooksRepository, List private CopybooksRepository runPreprocessor(String programDocumentUri, AnalysisContext ctx) { List preprocessorErrors = new ArrayList<>(); ExtendedDocument extendedDocument = ctx.getExtendedDocument(); - CopybooksRepository copybooks = - grammarPreprocessor.preprocess(new PreprocessorContext(programDocumentUri, extendedDocument, - ctx.getConfig().getCopybookProcessingMode(), new CopybookHierarchy(), new CopybooksRepository(), ctx.getLanguageId())) + PreprocessorContext context = new PreprocessorContext(programDocumentUri, extendedDocument, + ctx.getConfig().getCopybookProcessingMode(), new CopybookHierarchy(), new CopybooksRepository()); + + CopybooksRepository copybooks = grammarPreprocessor.preprocess(context, preprocessor) .unwrap(preprocessorErrors::addAll); extendedDocument.commitTransformations(); diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/TextPreprocessor.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/TextPreprocessor.java deleted file mode 100644 index 2af19ab312..0000000000 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/TextPreprocessor.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2020 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.core.preprocessor; - -import org.eclipse.lsp.cobol.common.ResultWithErrors; -import org.eclipse.lsp.cobol.common.mapping.ExtendedText; -import org.eclipse.lsp.cobol.lsp.CobolLanguageId; - -/** - * This interface describes the text preprocessor which prepares the given string for analysis by - * the grammar - */ -public interface TextPreprocessor { - - /** - * Check and clean of the code as per cobol program structure. - * - * @param documentUri unique resource identifier of the processed document - * @param cobolCode the content of the document that should be processed - * @param languageId - * @return modified code wrapped object and list of syntax error that might send back to the - * client - */ - ResultWithErrors cleanUpCode(String documentUri, String cobolCode, CobolLanguageId languageId); -} diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/GrammarPreprocessor.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/GrammarPreprocessor.java index 59803982ea..772e7c8b9f 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/GrammarPreprocessor.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/GrammarPreprocessor.java @@ -15,6 +15,7 @@ package org.eclipse.lsp.cobol.core.preprocessor.delegates; import lombok.NonNull; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.core.semantics.CopybooksRepository; @@ -26,8 +27,9 @@ public interface GrammarPreprocessor { * applying related semantic analysis * * @param context - preprocessor context + * @param preprocessor - dialect specific preprocessor * @return extended document with copybooks and related errors */ @NonNull - ResultWithErrors preprocess(@NonNull PreprocessorContext context); + ResultWithErrors preprocess(@NonNull PreprocessorContext context, @NonNull CleanerPreprocessor preprocessor); } diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/GrammarPreprocessorImpl.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/GrammarPreprocessorImpl.java index da871563d8..715a5c2c2f 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/GrammarPreprocessorImpl.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/GrammarPreprocessorImpl.java @@ -21,6 +21,7 @@ import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.tree.ParseTreeWalker; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.error.SyntaxError; import org.eclipse.lsp.cobol.common.mapping.ExtendedDocument; @@ -55,12 +56,12 @@ public GrammarPreprocessorImpl( @NonNull @Override - public ResultWithErrors preprocess(@NonNull PreprocessorContext context) { + public ResultWithErrors preprocess(@NonNull PreprocessorContext context, @NonNull CleanerPreprocessor preprocessor) { List errors = new ArrayList<>(); String replacedCode = replace(context.getCurrentDocument(), context.getHierarchy()).unwrap(errors::addAll); - return preprocess(context, replacedCode).accumulateErrors(errors); + return preprocess(context, preprocessor, replacedCode).accumulateErrors(errors); } private ResultWithErrors replace(ExtendedDocument extendedDocument, CopybookHierarchy hierarchy) { @@ -76,11 +77,12 @@ private ResultWithErrors replace(ExtendedDocument extendedDocument, Copy private ResultWithErrors preprocess( PreprocessorContext context, + CleanerPreprocessor preprocessor, String code) { ThreadInterruptionUtil.checkThreadInterrupted(); BufferedTokenStream tokens = makeTokens(code); - GrammarPreprocessorListener listener = listenerFactory.create(context); + GrammarPreprocessorListener listener = listenerFactory.create(context, preprocessor); CobolPreprocessor parser = new CobolPreprocessor(tokens); parser.removeErrorListeners(); diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/PreprocessorContext.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/PreprocessorContext.java index 7ca03aeedd..51888df92f 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/PreprocessorContext.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/PreprocessorContext.java @@ -20,7 +20,6 @@ import org.eclipse.lsp.cobol.common.mapping.ExtendedDocument; import org.eclipse.lsp.cobol.core.preprocessor.CopybookHierarchy; import org.eclipse.lsp.cobol.core.semantics.CopybooksRepository; -import org.eclipse.lsp.cobol.lsp.CobolLanguageId; /** * Preprocessor context @@ -32,5 +31,4 @@ public class PreprocessorContext { @NonNull CopybookProcessingMode copybookProcessingMode; @NonNull CopybookHierarchy hierarchy; @NonNull CopybooksRepository copybooksRepository; - @NonNull CobolLanguageId languageId; } diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/copybooks/CopybookPreprocessorService.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/copybooks/CopybookPreprocessorService.java index 9356473de8..f582a57a88 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/copybooks/CopybookPreprocessorService.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/copybooks/CopybookPreprocessorService.java @@ -23,6 +23,7 @@ import lombok.extern.slf4j.Slf4j; import org.antlr.v4.runtime.ParserRuleContext; import org.apache.commons.lang3.tuple.Pair; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.copybook.*; import org.eclipse.lsp.cobol.common.error.SyntaxError; @@ -42,7 +43,6 @@ import org.eclipse.lsp.cobol.core.preprocessor.delegates.util.LocalityUtils; import org.eclipse.lsp.cobol.core.semantics.CopybooksRepository; import org.eclipse.lsp.cobol.core.visitor.VisitorHelper; -import org.eclipse.lsp.cobol.lsp.CobolLanguageId; import org.eclipse.lsp4j.Location; import org.eclipse.lsp4j.Position; import org.eclipse.lsp4j.Range; @@ -68,10 +68,10 @@ class CopybookPreprocessorService { private final CopybookHierarchy hierarchy; private final ReplacingService replacingService; private final CopybookErrorService copybookErrorService; + private final CleanerPreprocessor preprocessor; private static final String HYPHEN = "-"; private static final String UNDERSCORE = "_"; - private final CobolLanguageId languageId; CopybookPreprocessorService(String programDocumentUri, GrammarPreprocessor grammarPreprocessor, @@ -82,7 +82,7 @@ class CopybookPreprocessorService { CopybookHierarchy hierarchy, MessageService messageService, ReplacingService replacingService, - CobolLanguageId languageId) { + CleanerPreprocessor preprocessor) { this.programDocumentUri = programDocumentUri; this.grammarPreprocessor = grammarPreprocessor; this.currentDocument = currentDocument; @@ -92,7 +92,7 @@ class CopybookPreprocessorService { this.hierarchy = hierarchy; this.replacingService = replacingService; this.copybookErrorService = new CopybookErrorService(messageService); - this.languageId = languageId; + this.preprocessor = preprocessor; } void addCopybook(ParserRuleContext ctx, CobolPreprocessor.CopySourceContext copySource, @@ -165,9 +165,9 @@ private ExtendedDocument processCopybookWithReplacement(List hierarchy.replaceCopybook(copybookDocument, replacingService::applyReplacing, errors); - PreprocessorContext copybookContext = new PreprocessorContext(programDocumentUri, copybookDocument, copybookConfig, hierarchy, copybooks, languageId); + PreprocessorContext copybookContext = new PreprocessorContext(programDocumentUri, copybookDocument, copybookConfig, hierarchy, copybooks); List copybookErrors = new LinkedList<>(); - grammarPreprocessor.preprocess(copybookContext).unwrap(copybookErrors::addAll); + grammarPreprocessor.preprocess(copybookContext, preprocessor).unwrap(copybookErrors::addAll); errors.addAll(copybookErrors); List distinct = errors.stream().distinct().collect(Collectors.toList()); @@ -241,8 +241,7 @@ private CopybookModel read(CopybookName copybookName, String documentUri) { copybookName, programDocumentUri, documentUri, - true, - languageId.getId()); + preprocessor); CopybookModel copybookModel = resolvedCopybook.getResult(); if (copybookModel.getContent() == null) { return null; diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/copybooks/GrammarPreprocessorListenerFactory.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/copybooks/GrammarPreprocessorListenerFactory.java index 0e7ed54f50..ba7d8ac022 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/copybooks/GrammarPreprocessorListenerFactory.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/copybooks/GrammarPreprocessorListenerFactory.java @@ -16,6 +16,7 @@ package org.eclipse.lsp.cobol.core.preprocessor.delegates.copybooks; import lombok.NonNull; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.core.preprocessor.delegates.PreprocessorContext; /** A factory for {@link GrammarPreprocessorListener} */ @@ -25,7 +26,8 @@ public interface GrammarPreprocessorListenerFactory { * the grammar * * @param context - preprocessor context + * @param preprocessor - dialect specific preprocessor * @return a new listener */ - GrammarPreprocessorListenerImpl create(@NonNull PreprocessorContext context); + GrammarPreprocessorListenerImpl create(@NonNull PreprocessorContext context, CleanerPreprocessor preprocessor); } diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/copybooks/GrammarPreprocessorListenerImpl.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/copybooks/GrammarPreprocessorListenerImpl.java index 27a50fe581..e95d527321 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/copybooks/GrammarPreprocessorListenerImpl.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/delegates/copybooks/GrammarPreprocessorListenerImpl.java @@ -24,6 +24,7 @@ import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.antlr.v4.runtime.ParserRuleContext; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.copybook.CopybookProcessingMode; import org.eclipse.lsp.cobol.common.copybook.CopybookService; @@ -61,6 +62,7 @@ public class GrammarPreprocessorListenerImpl extends CobolPreprocessorBaseListen @Inject GrammarPreprocessorListenerImpl( @Assisted PreprocessorContext context, + @Assisted CleanerPreprocessor preprocessor, GrammarPreprocessor grammarPreprocessor, CopybookService copybookService, MessageService messageService, @@ -76,7 +78,7 @@ public class GrammarPreprocessorListenerImpl extends CobolPreprocessorBaseListen context.getHierarchy(), messageService, replacingService, - context.getLanguageId()); + preprocessor); this.replacingService = replacingService; } diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/TrueDialectService.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/TrueDialectService.java index 151f3af8de..94ae3db05e 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/TrueDialectService.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/TrueDialectService.java @@ -16,7 +16,9 @@ import com.google.inject.Inject; import com.google.inject.Singleton; +import com.google.inject.name.Named; import org.antlr.v4.runtime.tree.ParseTreeListener; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.SubroutineService; import org.eclipse.lsp.cobol.common.message.MessageService; import org.eclipse.lsp.cobol.core.engine.dialects.DialectService; @@ -24,8 +26,8 @@ import org.eclipse.lsp.cobol.core.engine.pipeline.stages.*; import org.eclipse.lsp.cobol.core.engine.processor.AstProcessor; import org.eclipse.lsp.cobol.core.engine.symbols.SymbolsRepository; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; import org.eclipse.lsp.cobol.core.preprocessor.delegates.GrammarPreprocessor; +import org.eclipse.lsp.cobol.dialects.hp.HpCleanupStage; import org.eclipse.lsp.cobol.dialects.ibm.IbmCleanupStage; import org.eclipse.lsp.cobol.lsp.CobolLanguageId; import org.eclipse.lsp.cobol.service.settings.CachingConfigurationService; @@ -41,8 +43,12 @@ public class TrueDialectService { private final Map pipelineMap; + + private final Map preprocessorMap; + @Inject - public TrueDialectService(TextPreprocessor preprocessor, + public TrueDialectService(@Named("hpcobol") CleanerPreprocessor hpPreprocessor, + @Named("cobol") CleanerPreprocessor ibmPreprocessor, GrammarPreprocessor grammarPreprocessor, MessageService messageService, ParseTreeListener treeListener, @@ -52,17 +58,41 @@ public TrueDialectService(TextPreprocessor preprocessor, AstProcessor astProcessor, SymbolsRepository symbolsRepository, CodeLayoutStore codeLayoutStore) { + preprocessorMap = new HashMap<>(); + preprocessorMap.put(CobolLanguageId.COBOL, ibmPreprocessor); + preprocessorMap.put(CobolLanguageId.HP_COBOL, hpPreprocessor); + pipelineMap = new HashMap<>(); - Pipeline pipeline = new Pipeline(); - pipeline.add(new IbmCleanupStage(preprocessor)); - pipeline.add(new DialectCompilerDirectiveStage(dialectService)); - pipeline.add(new CompilerDirectivesStage(messageService)); - pipeline.add(new DialectProcessingStage(dialectService)); - pipeline.add(new PreprocessorStage(grammarPreprocessor)); - pipeline.add(new ImplicitDialectProcessingStage(dialectService)); - pipeline.add(new ParserStage(messageService, treeListener)); - pipeline.add( + Pipeline ibmPipeline = new Pipeline(); + ibmPipeline.add(new IbmCleanupStage(ibmPreprocessor)); + ibmPipeline.add(new DialectCompilerDirectiveStage(dialectService)); + ibmPipeline.add(new CompilerDirectivesStage(messageService)); + ibmPipeline.add(new DialectProcessingStage(dialectService, ibmPreprocessor)); + ibmPipeline.add(new PreprocessorStage(grammarPreprocessor, ibmPreprocessor)); + ibmPipeline.add(new ImplicitDialectProcessingStage(dialectService)); + ibmPipeline.add(new ParserStage(messageService, treeListener)); + ibmPipeline.add( + new TransformTreeStage( + symbolsRepository, + messageService, + subroutineService, + cachingConfigurationService, + dialectService, + astProcessor, + codeLayoutStore)); + + pipelineMap.put(CobolLanguageId.COBOL, ibmPipeline); + + Pipeline hpPipeline = new Pipeline(); + hpPipeline.add(new HpCleanupStage(hpPreprocessor)); + hpPipeline.add(new DialectCompilerDirectiveStage(dialectService)); + hpPipeline.add(new CompilerDirectivesStage(messageService)); + hpPipeline.add(new DialectProcessingStage(dialectService, hpPreprocessor)); + hpPipeline.add(new PreprocessorStage(grammarPreprocessor, hpPreprocessor)); + hpPipeline.add(new ImplicitDialectProcessingStage(dialectService)); + hpPipeline.add(new ParserStage(messageService, treeListener)); + hpPipeline.add( new TransformTreeStage( symbolsRepository, messageService, @@ -72,8 +102,7 @@ public TrueDialectService(TextPreprocessor preprocessor, astProcessor, codeLayoutStore)); - pipelineMap.put(CobolLanguageId.COBOL, pipeline); - pipelineMap.put(CobolLanguageId.HP_COBOL, pipeline); + pipelineMap.put(CobolLanguageId.HP_COBOL, hpPipeline); } /** @@ -84,4 +113,13 @@ public TrueDialectService(TextPreprocessor preprocessor, public Pipeline getPipeline(CobolLanguageId languageId) { return pipelineMap.get(languageId); } + + /** + * Returns the cleanup preprocessor for a dialect, based on a given language id + * @param languageId a language id of a dialect + * @return the cleanup preprocessor + */ + public CleanerPreprocessor getPreprocessor(CobolLanguageId languageId) { + return preprocessorMap.get(languageId); + } } diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/hp/HpCleanupStage.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/hp/HpCleanupStage.java index f734e31409..d83743ed7f 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/hp/HpCleanupStage.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/hp/HpCleanupStage.java @@ -14,22 +14,22 @@ */ package org.eclipse.lsp.cobol.dialects.hp; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.mapping.ExtendedDocument; import org.eclipse.lsp.cobol.common.mapping.ExtendedText; import org.eclipse.lsp.cobol.core.engine.analysis.AnalysisContext; import org.eclipse.lsp.cobol.core.engine.pipeline.Stage; import org.eclipse.lsp.cobol.core.engine.pipeline.StageResult; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; /** * Cleanup preprocessor pipeline stage */ public class HpCleanupStage implements Stage { - private final TextPreprocessor preprocessor; + private final CleanerPreprocessor preprocessor; - public HpCleanupStage(TextPreprocessor preprocessor) { + public HpCleanupStage(CleanerPreprocessor preprocessor) { this.preprocessor = preprocessor; } @@ -37,8 +37,7 @@ public HpCleanupStage(TextPreprocessor preprocessor) { public StageResult run(AnalysisContext context, StageResult prevStageResult) { ResultWithErrors resultWithErrors = preprocessor.cleanUpCode( context.getDocumentUri(), - context.getText(), - context.getLanguageId()); + context.getText()); context.getAccumulatedErrors().addAll(resultWithErrors.getErrors()); context.setExtendedDocument(new ExtendedDocument(resultWithErrors.getResult(), context.getText())); diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/TextPreprocessorImpl.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/hp/HpTextPreprocessor.java similarity index 76% rename from server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/TextPreprocessorImpl.java rename to server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/hp/HpTextPreprocessor.java index 00a9f9893e..b4d6c55053 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/core/preprocessor/TextPreprocessorImpl.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/hp/HpTextPreprocessor.java @@ -12,24 +12,26 @@ * Broadcom, Inc. - initial API and implementation * */ -package org.eclipse.lsp.cobol.core.preprocessor; +package org.eclipse.lsp.cobol.dialects.hp; import com.google.inject.Inject; import com.google.inject.Singleton; -import java.util.ArrayList; -import java.util.List; import lombok.extern.slf4j.Slf4j; import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.error.SyntaxError; import org.eclipse.lsp.cobol.common.mapping.ExtendedDocument; import org.eclipse.lsp.cobol.common.mapping.ExtendedText; +import org.eclipse.lsp.cobol.core.preprocessor.CobolLine; import org.eclipse.lsp.cobol.core.preprocessor.delegates.reader.CobolLineReaderService; import org.eclipse.lsp.cobol.core.preprocessor.delegates.rewriter.CobolLineReWriterService; import org.eclipse.lsp.cobol.core.preprocessor.delegates.transformer.CobolLineTransformationService; import org.eclipse.lsp.cobol.core.preprocessor.delegates.writer.CobolLineWriterService; import org.eclipse.lsp.cobol.lsp.CobolLanguageId; +import java.util.ArrayList; +import java.util.List; + /** * This class re-writes the content of the analyzing file to simplify the processing by the grammar, * e.g. removes comments or cleans-up the comment and sequence areas. See the delegates for more @@ -37,14 +39,14 @@ */ @Slf4j @Singleton -public class TextPreprocessorImpl implements TextPreprocessor, CleanerPreprocessor { +public class HpTextPreprocessor implements CleanerPreprocessor { private final CobolLineReaderService readerService; private final CobolLineWriterService writerService; private final CobolLineTransformationService transformationService; private final CobolLineReWriterService indicatorProcessorService; @Inject - public TextPreprocessorImpl( + public HpTextPreprocessor( CobolLineReaderService readerService, CobolLineWriterService writerService, CobolLineTransformationService transformationService, @@ -56,18 +58,13 @@ public TextPreprocessorImpl( } @Override - public ResultWithErrors cleanUpCode(String documentUri, String cobolCode, CobolLanguageId languageId) { + public ResultWithErrors cleanUpCode(String documentUri, String cobolCode) { List errors = new ArrayList<>(); - List lines = readerService.getCobolLineReader(languageId).processLines(documentUri, cobolCode).unwrap(errors::addAll); - List transformedLines = transformationService.getTransformer(languageId).transformLines(documentUri, lines).unwrap(errors::addAll); - List rewrittenLines = indicatorProcessorService.getLineReWriter(languageId).processLines(transformedLines); + List lines = readerService.getCobolLineReader(CobolLanguageId.HP_COBOL).processLines(documentUri, cobolCode).unwrap(errors::addAll); + List transformedLines = transformationService.getTransformer(CobolLanguageId.HP_COBOL).transformLines(documentUri, lines).unwrap(errors::addAll); + List rewrittenLines = indicatorProcessorService.getLineReWriter(CobolLanguageId.HP_COBOL).processLines(transformedLines); - ExtendedDocument code = writerService.getCobolLineWriter(languageId).serialize(rewrittenLines, documentUri); + ExtendedDocument code = writerService.getCobolLineWriter(CobolLanguageId.HP_COBOL).serialize(rewrittenLines, documentUri); return new ResultWithErrors<>(code.getCurrentText(), errors); } - - @Override - public ResultWithErrors cleanUpCode(String documentUri, String cobolCode) { - return cleanUpCode(documentUri, cobolCode, CobolLanguageId.COBOL); - } } diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/ibm/IbmCleanupStage.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/ibm/IbmCleanupStage.java index e494d12ffc..a0161c2b35 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/ibm/IbmCleanupStage.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/ibm/IbmCleanupStage.java @@ -14,22 +14,22 @@ */ package org.eclipse.lsp.cobol.dialects.ibm; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.mapping.ExtendedDocument; import org.eclipse.lsp.cobol.common.mapping.ExtendedText; import org.eclipse.lsp.cobol.core.engine.analysis.AnalysisContext; import org.eclipse.lsp.cobol.core.engine.pipeline.Stage; import org.eclipse.lsp.cobol.core.engine.pipeline.StageResult; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; /** * Cleanup preprocessor pipeline stage */ public class IbmCleanupStage implements Stage { - private final TextPreprocessor preprocessor; + private final CleanerPreprocessor preprocessor; - public IbmCleanupStage(TextPreprocessor preprocessor) { + public IbmCleanupStage(CleanerPreprocessor preprocessor) { this.preprocessor = preprocessor; } @@ -37,8 +37,7 @@ public IbmCleanupStage(TextPreprocessor preprocessor) { public StageResult run(AnalysisContext context, StageResult prevStageResult) { ResultWithErrors resultWithErrors = preprocessor.cleanUpCode( context.getDocumentUri(), - context.getText(), - context.getLanguageId()); + context.getText()); context.getAccumulatedErrors().addAll(resultWithErrors.getErrors()); context.setExtendedDocument(new ExtendedDocument(resultWithErrors.getResult(), context.getText())); diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/ibm/IbmTextPreprocessor.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/ibm/IbmTextPreprocessor.java new file mode 100644 index 0000000000..acbe7fb3e3 --- /dev/null +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/dialects/ibm/IbmTextPreprocessor.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2020 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.dialects.ibm; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import java.util.ArrayList; +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; +import org.eclipse.lsp.cobol.common.ResultWithErrors; +import org.eclipse.lsp.cobol.common.error.SyntaxError; +import org.eclipse.lsp.cobol.common.mapping.ExtendedDocument; +import org.eclipse.lsp.cobol.common.mapping.ExtendedText; +import org.eclipse.lsp.cobol.core.preprocessor.CobolLine; +import org.eclipse.lsp.cobol.core.preprocessor.delegates.reader.CobolLineReaderService; +import org.eclipse.lsp.cobol.core.preprocessor.delegates.rewriter.CobolLineReWriterService; +import org.eclipse.lsp.cobol.core.preprocessor.delegates.transformer.CobolLineTransformationService; +import org.eclipse.lsp.cobol.core.preprocessor.delegates.writer.CobolLineWriterService; +import org.eclipse.lsp.cobol.lsp.CobolLanguageId; + +/** + * This class re-writes the content of the analyzing file to simplify the processing by the grammar, + * e.g. removes comments or cleans-up the comment and sequence areas. See the delegates for more + * details. + */ +@Slf4j +@Singleton +public class IbmTextPreprocessor implements CleanerPreprocessor { + private final CobolLineReaderService readerService; + private final CobolLineWriterService writerService; + private final CobolLineTransformationService transformationService; + private final CobolLineReWriterService indicatorProcessorService; + + @Inject + public IbmTextPreprocessor( + CobolLineReaderService readerService, + CobolLineWriterService writerService, + CobolLineTransformationService transformationService, + CobolLineReWriterService indicatorProcessorService) { + this.readerService = readerService; + this.writerService = writerService; + this.transformationService = transformationService; + this.indicatorProcessorService = indicatorProcessorService; + } + + @Override + public ResultWithErrors cleanUpCode(String documentUri, String cobolCode) { + List errors = new ArrayList<>(); + List lines = readerService.getCobolLineReader(CobolLanguageId.COBOL).processLines(documentUri, cobolCode).unwrap(errors::addAll); + List transformedLines = transformationService.getTransformer(CobolLanguageId.COBOL).transformLines(documentUri, lines).unwrap(errors::addAll); + List rewrittenLines = indicatorProcessorService.getLineReWriter(CobolLanguageId.COBOL).processLines(transformedLines); + + ExtendedDocument code = writerService.getCobolLineWriter(CobolLanguageId.COBOL).serialize(rewrittenLines, documentUri); + return new ResultWithErrors<>(code.getCurrentText(), errors); + } +} diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/domain/modules/EngineModule.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/domain/modules/EngineModule.java index 9719211d86..0d45878016 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/domain/modules/EngineModule.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/domain/modules/EngineModule.java @@ -21,6 +21,7 @@ import com.google.inject.assistedinject.FactoryModuleBuilder; import com.google.inject.name.Names; import org.antlr.v4.runtime.tree.ParseTreeListener; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.benchmark.BenchmarkService; import org.eclipse.lsp.cobol.common.benchmark.BenchmarkServiceImpl; import org.eclipse.lsp.cobol.common.message.LocaleStore; @@ -28,8 +29,8 @@ import org.eclipse.lsp.cobol.core.engine.CobolLanguageEngine; import org.eclipse.lsp.cobol.core.messages.LocaleStoreImpl; import org.eclipse.lsp.cobol.core.messages.PropertiesMessageService; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessorImpl; +import org.eclipse.lsp.cobol.dialects.hp.HpTextPreprocessor; +import org.eclipse.lsp.cobol.dialects.ibm.IbmTextPreprocessor; import org.eclipse.lsp.cobol.core.preprocessor.delegates.GrammarPreprocessor; import org.eclipse.lsp.cobol.core.preprocessor.delegates.GrammarPreprocessorImpl; import org.eclipse.lsp.cobol.core.preprocessor.delegates.copybooks.GrammarPreprocessorListenerFactory; @@ -64,7 +65,9 @@ public class EngineModule extends AbstractModule { protected void configure() { bind(CobolLanguageEngine.class); bind(BenchmarkService.class).to(BenchmarkServiceImpl.class); - bind(TextPreprocessor.class).to(TextPreprocessorImpl.class); + bind(CleanerPreprocessor.class).annotatedWith(Names.named("cobol")).to(IbmTextPreprocessor.class); + bind(CleanerPreprocessor.class).annotatedWith(Names.named("hpcobol")).to(HpTextPreprocessor.class); + bind(GrammarPreprocessor.class).to(GrammarPreprocessorImpl.class); install(new FactoryModuleBuilder().build(GrammarPreprocessorListenerFactory.class)); install(new FactoryModuleBuilder().build(ReplacePreprocessorFactory.class)); diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/lsp/analysis/AsyncAnalysisService.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/lsp/analysis/AsyncAnalysisService.java index 1859d2b18b..ba8866d8dc 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/lsp/analysis/AsyncAnalysisService.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/lsp/analysis/AsyncAnalysisService.java @@ -22,13 +22,14 @@ import lombok.extern.slf4j.Slf4j; import org.eclipse.lsp.cobol.common.SubroutineService; import org.eclipse.lsp.cobol.common.copybook.CopybookService; +import org.eclipse.lsp.cobol.dialects.TrueDialectService; +import org.eclipse.lsp.cobol.lsp.CobolLanguageId; import org.eclipse.lsp.cobol.lsp.LspEventCancelCondition; import org.eclipse.lsp.cobol.lsp.LspEventDependency; import org.eclipse.lsp.cobol.lsp.SourceUnitGraph; import org.eclipse.lsp.cobol.service.AnalysisService; import org.eclipse.lsp.cobol.service.CobolDocumentModel; import org.eclipse.lsp.cobol.service.DocumentModelService; -import org.eclipse.lsp.cobol.service.copybooks.CopybookServiceImpl; import org.eclipse.lsp.cobol.service.delegates.communications.Communications; /** @@ -38,6 +39,7 @@ @Singleton public class AsyncAnalysisService implements AnalysisStateNotifier { private static final ExecutorService SINGLE_THREAD_EXECUTOR = Executors.newSingleThreadExecutor(r -> new Thread(r, "LSP workspace service")); + private final TrueDialectService dialectService; private final DocumentModelService documentModelService; private final AnalysisService analysisService; private final CopybookService copybookService; @@ -60,11 +62,13 @@ public Thread newThread(Runnable r) { @Inject - public AsyncAnalysisService(DocumentModelService documentModelService, + public AsyncAnalysisService(TrueDialectService dialectService, + DocumentModelService documentModelService, AnalysisService analysisService, CopybookService copybookService, SubroutineService subroutineService, Communications communications) { + this.dialectService = dialectService; this.documentModelService = documentModelService; this.analysisService = analysisService; this.copybookService = copybookService; @@ -220,20 +224,18 @@ public void reanalyseCopybooksAssociatedPrograms(List uris, String copyb for (String uri : openedUris) { String languageId = documentModelService.get(uri).getLanguageId(); //TODO: update cache directly from workspace document graph - if (copybookService instanceof CopybookServiceImpl) { - CopybookServiceImpl copybookServiceImpl = (CopybookServiceImpl) copybookService; - copybookServiceImpl.getCopybookUsage(uri).stream() - .filter(model -> Objects.nonNull(model.getUri())) - .filter(model -> model.getUri().equals(copybookUri)) - .forEach( - copybookModel -> { - copybookServiceImpl.invalidateCache(copybookModel.getCopybookId()); - if (copybookContent != null) { - copybookModel.setContent(copybookContent); - copybookServiceImpl.store(copybookModel, languageId, true); - } - }); - } + copybookService.getCopybookUsage(uri).stream() + .filter(model -> Objects.nonNull(model.getUri())) + .filter(model -> model.getUri().equals(copybookUri)) + .forEach( + copybookModel -> { + copybookService.invalidateCache(copybookModel.getCopybookId()); + if (copybookContent != null) { + copybookModel.setContent(copybookContent); + copybookService.store(copybookModel, dialectService.getPreprocessor(CobolLanguageId.MAPPER.get(languageId))); + } + }); + subroutineService.invalidateCache(); LOG.info("Cache invalidated"); CobolDocumentModel document = documentModelService.get(uri); diff --git a/server/engine/src/main/java/org/eclipse/lsp/cobol/service/copybooks/CopybookServiceImpl.java b/server/engine/src/main/java/org/eclipse/lsp/cobol/service/copybooks/CopybookServiceImpl.java index 442d755b60..9233cf9d4c 100644 --- a/server/engine/src/main/java/org/eclipse/lsp/cobol/service/copybooks/CopybookServiceImpl.java +++ b/server/engine/src/main/java/org/eclipse/lsp/cobol/service/copybooks/CopybookServiceImpl.java @@ -32,6 +32,7 @@ import java.util.concurrent.ExecutionException; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.copybook.*; import org.eclipse.lsp.cobol.common.error.SyntaxError; @@ -40,9 +41,7 @@ import org.eclipse.lsp.cobol.common.mapping.OriginalLocation; import org.eclipse.lsp.cobol.common.utils.ImplicitCodeUtils; import org.eclipse.lsp.cobol.common.utils.ThreadInterruptionUtil; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; import org.eclipse.lsp.cobol.core.semantics.CopybooksRepository; -import org.eclipse.lsp.cobol.lsp.CobolLanguageId; import org.eclipse.lsp.cobol.lsp.jrpc.CobolLanguageClient; import org.eclipse.lsp.cobol.service.UriDecodeService; @@ -60,7 +59,6 @@ public class CopybookServiceImpl implements CopybookService { private final Provider clientProvider; private final UriDecodeService uriDecodeService; private final FileSystemService files; - public final TextPreprocessor preprocessor; private static final String COBOL = "COBOL"; private final Map> copybooksForDownloading = @@ -71,12 +69,10 @@ public class CopybookServiceImpl implements CopybookService { @Inject public CopybookServiceImpl(Provider clientProvider, FileSystemService files, - TextPreprocessor preprocessor, CopybookCache copybookCache, UriDecodeService uriDecodeService) { this.files = files; this.clientProvider = clientProvider; - this.preprocessor = preprocessor; this.copybookCache = copybookCache; this.uriDecodeService = uriDecodeService; } @@ -93,7 +89,7 @@ public void invalidateCache() { /** * Removes cache for the passed {@link CopybookId} - * @param copybookId + * @param copybookId is a copybook identifier */ public void invalidateCache(CopybookId copybookId) { copybookCache.invalidate(copybookId); @@ -113,8 +109,7 @@ public void invalidateCache(CopybookId copybookId) { * @param copybookName - the name of the copybook to be retrieved * @param programDocumentUri - the currently processing program document * @param documentUri - the currently processing document that contains the copy statement - * @param preprocess - indicates if copybook needs to be preprocessed after resolving - * @param languageId + * @param preprocessor - Cleanup preprocessor that will be used for new copybooks or null * @return a CopybookModel wrapped inside {@link ResultWithErrors} which contains copybook name, its URI and the content */ public ResultWithErrors resolve( @@ -122,12 +117,11 @@ public ResultWithErrors resolve( @NonNull CopybookName copybookName, @NonNull String programDocumentUri, @NonNull String documentUri, - boolean preprocess, String languageId) { + CleanerPreprocessor preprocessor) { try { ThreadInterruptionUtil.checkThreadInterrupted(); - CopybookModel copybookModel = getFromCache(programDocumentUri, copybookId, copybookName, - preprocess, CobolLanguageId.MAPPER.get(languageId)); + CopybookModel copybookModel = getFromCache(programDocumentUri, copybookId, copybookName, preprocessor); copybookUsage.computeIfAbsent(programDocumentUri, k -> new HashSet<>()).add(copybookModel); List errors = Optional.ofNullable(copybookModel.getUri()) @@ -142,11 +136,11 @@ public ResultWithErrors resolve( } private CopybookModel getFromCache(String programDocumentUri, CopybookId copybookId, - CopybookName copybookName, boolean preprocess, CobolLanguageId languageId) throws ExecutionException { + CopybookName copybookName, CleanerPreprocessor preprocessor) throws ExecutionException { return copybookCache.get(copybookId, () -> { CopybookModel copybookModel = resolveSync(copybookName, programDocumentUri); - if (preprocess && copybookModel.getUri() != null) { - ResultWithErrors copybookModelResultWithErrors = cleanupCopybook(copybookModel, languageId); + if (preprocessor != null && copybookModel.getUri() != null) { + ResultWithErrors copybookModelResultWithErrors = cleanupCopybook(copybookModel, preprocessor); copybookModel = copybookModelResultWithErrors.getResult(); preprocessCopybookErrors.put(copybookModel.getUri(), copybookModelResultWithErrors.getErrors()); } @@ -160,9 +154,9 @@ public void store(CopybookModel copybookModel) { } @Override - public void store(CopybookModel copybookModel, String languageId, boolean doCleanUp) { - if (doCleanUp) { - ResultWithErrors processedCopybook = cleanupCopybook(copybookModel, CobolLanguageId.MAPPER.get(languageId)); + public void store(CopybookModel copybookModel, CleanerPreprocessor preprocessor) { + if (preprocessor != null) { + ResultWithErrors processedCopybook = cleanupCopybook(copybookModel, preprocessor); copybookModel = processedCopybook.getResult(); preprocessCopybookErrors.put(copybookModel.getUri(), processedCopybook.getErrors()); } @@ -196,8 +190,8 @@ private Optional tryResolvePredefinedCopybook(CopybookName copybo } } - private ResultWithErrors cleanupCopybook(CopybookModel dirtyCopybook, CobolLanguageId languageId) { - ResultWithErrors textTransformationsResultWithErrors = preprocessor.cleanUpCode(dirtyCopybook.getUri(), dirtyCopybook.getContent(), languageId); + private ResultWithErrors cleanupCopybook(CopybookModel dirtyCopybook, CleanerPreprocessor preprocessor) { + ResultWithErrors textTransformationsResultWithErrors = preprocessor.cleanUpCode(dirtyCopybook.getUri(), dirtyCopybook.getContent()); String cleanText = CharMatcher.whitespace().trimTrailingFrom(textTransformationsResultWithErrors.getResult().toString()); CopybookModel copybookModel = new CopybookModel(dirtyCopybook.getCopybookId(), dirtyCopybook.getCopybookName(), dirtyCopybook.getUri(), cleanText); return new ResultWithErrors<>(copybookModel, adjustErrorLocation(dirtyCopybook, textTransformationsResultWithErrors.getErrors())); @@ -302,7 +296,7 @@ public void sendCopybookDownloadRequest(String documentUri, Collection c * Get the list of copybook used by a document * * @param documentUri current document uri. - * @return List of all the {@link CopybookModel} used by the passed document + * @return Set of all the {@link CopybookModel} used by the passed document */ public Set getCopybookUsage(String documentUri) { return Collections.unmodifiableSet(copybookUsage.getOrDefault(documentUri, ImmutableSet.of())); diff --git a/server/engine/src/test/java/org/eclipse/lsp/cobol/ClientServerTestModule.java b/server/engine/src/test/java/org/eclipse/lsp/cobol/ClientServerTestModule.java index 1dfae58abe..ec0d19f577 100644 --- a/server/engine/src/test/java/org/eclipse/lsp/cobol/ClientServerTestModule.java +++ b/server/engine/src/test/java/org/eclipse/lsp/cobol/ClientServerTestModule.java @@ -24,6 +24,7 @@ import com.google.inject.name.Names; import org.eclipse.lsp.cobol.cfg.CFASTBuilder; import org.eclipse.lsp.cobol.cfg.CFASTBuilderImpl; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.SubroutineService; import org.eclipse.lsp.cobol.common.action.CodeActionProvider; import org.eclipse.lsp.cobol.common.copybook.CopybookService; @@ -35,7 +36,6 @@ import org.eclipse.lsp.cobol.core.engine.dialects.DialectDiscoveryService; import org.eclipse.lsp.cobol.core.messages.LocaleStoreImpl; import org.eclipse.lsp.cobol.core.messages.PropertiesMessageService; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; import org.eclipse.lsp.cobol.lsp.CobolWorkspaceServiceImpl; import org.eclipse.lsp.cobol.lsp.DisposableLSPStateService; import org.eclipse.lsp.cobol.lsp.jrpc.CobolLanguageClient; @@ -102,7 +102,7 @@ protected void configure() { bind(SubroutineService.class).to(SubroutineServiceImpl.class); bind(Occurrences.class).to(ElementOccurrences.class); bind(CFASTBuilder.class).to(CFASTBuilderImpl.class); - bind(TextPreprocessor.class).to(MockTextPreprocessor.class); + bind(CleanerPreprocessor.class).to(MockTextPreprocessor.class); bind(CopybookIdentificationService.class) .annotatedWith(Names.named("contentStrategy")) .to(CopybookIdentificationServiceBasedOnContent.class); diff --git a/server/engine/src/test/java/org/eclipse/lsp/cobol/core/engine/CobolLanguageEngineTest.java b/server/engine/src/test/java/org/eclipse/lsp/cobol/core/engine/CobolLanguageEngineTest.java index 48686b315e..b04a4d5ca8 100644 --- a/server/engine/src/test/java/org/eclipse/lsp/cobol/core/engine/CobolLanguageEngineTest.java +++ b/server/engine/src/test/java/org/eclipse/lsp/cobol/core/engine/CobolLanguageEngineTest.java @@ -24,10 +24,7 @@ import com.google.common.collect.ImmutableList; import org.antlr.v4.runtime.tree.ParseTreeListener; -import org.eclipse.lsp.cobol.common.AnalysisConfig; -import org.eclipse.lsp.cobol.common.AnalysisResult; -import org.eclipse.lsp.cobol.common.ResultWithErrors; -import org.eclipse.lsp.cobol.common.SubroutineService; +import org.eclipse.lsp.cobol.common.*; import org.eclipse.lsp.cobol.common.benchmark.BenchmarkService; import org.eclipse.lsp.cobol.common.benchmark.BenchmarkSession; import org.eclipse.lsp.cobol.common.dialects.DialectOutcome; @@ -45,7 +42,6 @@ import org.eclipse.lsp.cobol.core.engine.errors.ErrorFinalizerService; import org.eclipse.lsp.cobol.core.engine.processor.AstProcessor; import org.eclipse.lsp.cobol.core.engine.symbols.SymbolsRepository; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; import org.eclipse.lsp.cobol.core.preprocessor.delegates.GrammarPreprocessor; import org.eclipse.lsp.cobol.core.semantics.CopybooksRepository; import org.eclipse.lsp.cobol.core.strategy.CobolErrorStrategy; @@ -62,7 +58,7 @@ /** * This test checks the logic of {@link CobolLanguageEngine}. It should first run {@link - * TextPreprocessor} to clean up the given COBOL text, then apply syntax and semantic analysis. If + * CleanerPreprocessor} to clean up the given COBOL text, then apply syntax and semantic analysis. If * the text doesn't have any semantic elements, it should return an empty {@link Node} */ class CobolLanguageEngineTest { @@ -70,7 +66,7 @@ class CobolLanguageEngineTest { private static final String TEXT = " IDENTIFICATION DIVISION."; private static final String URI = "document.cbl"; - private final TextPreprocessor preprocessor = mock(TextPreprocessor.class); + private final CleanerPreprocessor preprocessor = mock(CleanerPreprocessor.class); private final GrammarPreprocessor grammarPreprocessor = mock(GrammarPreprocessor.class); private final MessageService mockMessageService = mock(MessageService.class); private final ErrorMessageHelper mockErrUtil = mock(ErrorMessageHelper.class); @@ -91,7 +87,7 @@ void testLanguageEngineRun() { BenchmarkService benchmarkService = mock(BenchmarkService.class); when(benchmarkService.startSession()).thenReturn(new BenchmarkSession()); - TrueDialectService trueDialectService = new TrueDialectService(preprocessor, grammarPreprocessor, mockMessageService, treeListener, mock(SubroutineService.class), + TrueDialectService trueDialectService = new TrueDialectService(preprocessor, preprocessor, grammarPreprocessor, mockMessageService, treeListener, mock(SubroutineService.class), null, dialectService, astProcessor, symbolsRepository, store); CobolLanguageEngine engine = @@ -132,10 +128,10 @@ void testLanguageEngineRun() { .thenReturn(new ResultWithErrors<>(new DialectOutcome(context), ImmutableList.of())); when(dialectService.processImplicitDialects(any(), anyList(), any())) .thenReturn(new ResultWithErrors<>(new DialectOutcome(context), ImmutableList.of())); - when(preprocessor.cleanUpCode(URI, TEXT, CobolLanguageId.COBOL)) + when(preprocessor.cleanUpCode(URI, TEXT)) .thenReturn(new ResultWithErrors<>(new ExtendedText(TEXT, URI), ImmutableList.of())); - when(grammarPreprocessor.preprocess(any())).thenReturn(new ResultWithErrors<>(new CopybooksRepository(), ImmutableList.of())); + when(grammarPreprocessor.preprocess(any(), any())).thenReturn(new ResultWithErrors<>(new CopybooksRepository(), ImmutableList.of())); Range sourceRange = ParserUtils.isHwParserEnabled() ? new Range(new Position(0, 0), new Position(0, 32)) @@ -170,7 +166,7 @@ void testLanguageEngineRunWhenNativeServerWithDialects() { BenchmarkService benchmarkService = mock(BenchmarkService.class); when(benchmarkService.startSession()).thenReturn(mock(BenchmarkSession.class)); - TrueDialectService trueDialectService = new TrueDialectService(preprocessor, grammarPreprocessor, mockMessageService, treeListener, mock(SubroutineService.class), + TrueDialectService trueDialectService = new TrueDialectService(preprocessor, preprocessor, grammarPreprocessor, mockMessageService, treeListener, mock(SubroutineService.class), null, dialectService, astProcessor, symbolsRepository, store); CobolLanguageEngine engine = diff --git a/server/engine/src/test/java/org/eclipse/lsp/cobol/core/preprocessor/TestCommentLines.java b/server/engine/src/test/java/org/eclipse/lsp/cobol/core/preprocessor/TestCommentLines.java index ddb7b8d5c1..8d7999252c 100644 --- a/server/engine/src/test/java/org/eclipse/lsp/cobol/core/preprocessor/TestCommentLines.java +++ b/server/engine/src/test/java/org/eclipse/lsp/cobol/core/preprocessor/TestCommentLines.java @@ -24,6 +24,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; + +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.error.SyntaxError; import org.eclipse.lsp.cobol.common.message.MessageService; import org.eclipse.lsp.cobol.core.preprocessor.delegates.reader.CobolLineReader; @@ -38,7 +40,7 @@ import org.eclipse.lsp.cobol.core.preprocessor.delegates.writer.CobolLineWriter; import org.eclipse.lsp.cobol.core.preprocessor.delegates.writer.CobolLineWriterImpl; import org.eclipse.lsp.cobol.core.preprocessor.delegates.writer.CobolLineWriterService; -import org.eclipse.lsp.cobol.lsp.CobolLanguageId; +import org.eclipse.lsp.cobol.dialects.ibm.IbmTextPreprocessor; import org.eclipse.lsp.cobol.service.settings.layout.CodeLayoutStore; import org.junit.jupiter.api.Test; @@ -88,10 +90,10 @@ void test() { CobolLineReWriterService indicatorProcessorService = mock(CobolLineReWriterService.class); when(indicatorProcessorService.getLineReWriter(any())).thenReturn(indicatorProcessor); - TextPreprocessor textPreprocessor = - new TextPreprocessorImpl(cobolLineReaderService, writerService, transformationService, indicatorProcessorService); + CleanerPreprocessor textPreprocessor = + new IbmTextPreprocessor(cobolLineReaderService, writerService, transformationService, indicatorProcessorService); String actual = - textPreprocessor.cleanUpCode(DOCUMENT_URI, TEXT, CobolLanguageId.COBOL).unwrap(accumulatedErrors::addAll).toString(); + textPreprocessor.cleanUpCode(DOCUMENT_URI, TEXT).unwrap(accumulatedErrors::addAll).toString(); assertEquals(EXPECTED, actual); assertTrue(accumulatedErrors.isEmpty()); } diff --git a/server/engine/src/test/java/org/eclipse/lsp/cobol/core/preprocessor/TestLinesConcatenation.java b/server/engine/src/test/java/org/eclipse/lsp/cobol/core/preprocessor/TestLinesConcatenation.java index 77c6586809..df9b7cf8da 100644 --- a/server/engine/src/test/java/org/eclipse/lsp/cobol/core/preprocessor/TestLinesConcatenation.java +++ b/server/engine/src/test/java/org/eclipse/lsp/cobol/core/preprocessor/TestLinesConcatenation.java @@ -22,6 +22,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; + +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.error.SyntaxError; import org.eclipse.lsp.cobol.common.mapping.ExtendedText; import org.eclipse.lsp.cobol.common.message.MessageService; @@ -37,7 +39,7 @@ import org.eclipse.lsp.cobol.core.preprocessor.delegates.writer.CobolLineWriter; import org.eclipse.lsp.cobol.core.preprocessor.delegates.writer.CobolLineWriterImpl; import org.eclipse.lsp.cobol.core.preprocessor.delegates.writer.CobolLineWriterService; -import org.eclipse.lsp.cobol.lsp.CobolLanguageId; +import org.eclipse.lsp.cobol.dialects.ibm.IbmTextPreprocessor; import org.eclipse.lsp.cobol.service.settings.layout.CodeLayoutStore; import org.junit.jupiter.api.Test; @@ -92,8 +94,8 @@ void test() { CobolLineReWriterService indicatorProcessorService = mock(CobolLineReWriterService.class); when(indicatorProcessorService.getLineReWriter(any())).thenReturn(indicatorProcessor); - TextPreprocessor textPreprocessor = new TextPreprocessorImpl(cobolLineReaderService, writerService, transformationService, indicatorProcessorService); - ExtendedText extendedText = textPreprocessor.cleanUpCode(DOCUMENT_URI, TEXT, CobolLanguageId.COBOL).unwrap(accumulatedErrors::addAll); + CleanerPreprocessor textPreprocessor = new IbmTextPreprocessor(cobolLineReaderService, writerService, transformationService, indicatorProcessorService); + ExtendedText extendedText = textPreprocessor.cleanUpCode(DOCUMENT_URI, TEXT).unwrap(accumulatedErrors::addAll); assertEquals(EXPECTED, extendedText.toString()); assertTrue(accumulatedErrors.isEmpty()); } diff --git a/server/engine/src/test/java/org/eclipse/lsp/cobol/lsp/analysis/AsyncAnalysisServiceTest.java b/server/engine/src/test/java/org/eclipse/lsp/cobol/lsp/analysis/AsyncAnalysisServiceTest.java index 94b2951624..638d8cd204 100644 --- a/server/engine/src/test/java/org/eclipse/lsp/cobol/lsp/analysis/AsyncAnalysisServiceTest.java +++ b/server/engine/src/test/java/org/eclipse/lsp/cobol/lsp/analysis/AsyncAnalysisServiceTest.java @@ -22,6 +22,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import org.eclipse.lsp.cobol.common.SubroutineService; +import org.eclipse.lsp.cobol.dialects.TrueDialectService; import org.eclipse.lsp.cobol.lsp.SourceUnitGraph; import org.eclipse.lsp.cobol.service.AnalysisService; import org.eclipse.lsp.cobol.service.CobolDocumentModel; @@ -54,8 +55,8 @@ class AsyncAnalysisServiceTest { @BeforeEach void setUp() { - asyncAnalysisService = new AsyncAnalysisService(documentModelService, analysisService, copybookService, - subroutineService, communication); + asyncAnalysisService = new AsyncAnalysisService(mock(TrueDialectService.class), + documentModelService, analysisService, copybookService, subroutineService, communication); } @Test diff --git a/server/engine/src/test/java/org/eclipse/lsp/cobol/positive/TypingTest.java b/server/engine/src/test/java/org/eclipse/lsp/cobol/positive/TypingTest.java index 912d444451..abcf91af0f 100644 --- a/server/engine/src/test/java/org/eclipse/lsp/cobol/positive/TypingTest.java +++ b/server/engine/src/test/java/org/eclipse/lsp/cobol/positive/TypingTest.java @@ -28,11 +28,11 @@ import java.util.stream.IntStream; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.time.DurationFormatUtils; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.error.SyntaxError; import org.eclipse.lsp.cobol.common.mapping.ExtendedText; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessorImpl; +import org.eclipse.lsp.cobol.dialects.ibm.IbmTextPreprocessor; import org.eclipse.lsp.cobol.core.preprocessor.delegates.reader.CobolLineReaderImpl; import org.eclipse.lsp.cobol.core.preprocessor.delegates.reader.CobolLineReaderService; import org.eclipse.lsp.cobol.core.preprocessor.delegates.rewriter.CobolLineIndicatorProcessorImpl; @@ -102,14 +102,14 @@ private String getCleanText(CobolText cobolText) { when(transformationService.getTransformer(any())).thenReturn(continuationLineTransformation); when(indicatorProcessorService.getLineReWriter(any())).thenReturn(cobolLineIndicatorProcessor); - TextPreprocessor preprocessor = - new TextPreprocessorImpl( + CleanerPreprocessor preprocessor = + new IbmTextPreprocessor( readerService, writerService, transformationService, indicatorProcessorService); ResultWithErrors cleanTextResult = - preprocessor.cleanUpCode(cobolText.getFileName(), cobolText.getFullText(), CobolLanguageId.COBOL); + preprocessor.cleanUpCode(cobolText.getFileName(), cobolText.getFullText()); for (SyntaxError error : cleanTextResult.getErrors()) LOG.error(error.toString()); return cleanTextResult.getResult().toString(); } diff --git a/server/engine/src/test/java/org/eclipse/lsp/cobol/service/CobolTextDocumentServiceTest.java b/server/engine/src/test/java/org/eclipse/lsp/cobol/service/CobolTextDocumentServiceTest.java index 0270f209ef..15acd8fff2 100644 --- a/server/engine/src/test/java/org/eclipse/lsp/cobol/service/CobolTextDocumentServiceTest.java +++ b/server/engine/src/test/java/org/eclipse/lsp/cobol/service/CobolTextDocumentServiceTest.java @@ -22,6 +22,7 @@ import org.eclipse.lsp.cobol.cfg.CFASTBuilder; import org.eclipse.lsp.cobol.common.SubroutineService; import org.eclipse.lsp.cobol.common.copybook.CopybookService; +import org.eclipse.lsp.cobol.dialects.TrueDialectService; import org.eclipse.lsp.cobol.lsp.*; import org.eclipse.lsp.cobol.lsp.analysis.AsyncAnalysisService; import org.eclipse.lsp.cobol.lsp.events.notifications.DidChangeNotification; @@ -96,7 +97,7 @@ void setupService() { DisposableLSPStateService disposableLSPStateService = new CobolLSPServerStateService(); CopybookService copybookService = mock(CopybookService.class); SubroutineService subroutineService = mock(SubroutineService.class); - AsyncAnalysisService asyncAnalysisService = new AsyncAnalysisService(documentModelService, analysisService, copybookService, subroutineService, communications); + AsyncAnalysisService asyncAnalysisService = new AsyncAnalysisService(mock(TrueDialectService.class), documentModelService, analysisService, copybookService, subroutineService, communications); CompletionHandler completionHandler = new CompletionHandler(asyncAnalysisService, completions, documentModelService, uriDecodeService); FormattingHandler formattingHandler = new FormattingHandler(documentModelService, formations, asyncAnalysisService, uriDecodeService); diff --git a/server/engine/src/test/java/org/eclipse/lsp/cobol/service/copybooks/CopybookServiceTest.java b/server/engine/src/test/java/org/eclipse/lsp/cobol/service/copybooks/CopybookServiceTest.java index b7970f597c..e5a8b65dac 100644 --- a/server/engine/src/test/java/org/eclipse/lsp/cobol/service/copybooks/CopybookServiceTest.java +++ b/server/engine/src/test/java/org/eclipse/lsp/cobol/service/copybooks/CopybookServiceTest.java @@ -29,6 +29,8 @@ import java.net.URI; import java.nio.file.Path; import java.util.Collections; + +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.copybook.*; import org.eclipse.lsp.cobol.common.error.ErrorSeverity; @@ -37,7 +39,6 @@ import org.eclipse.lsp.cobol.common.mapping.ExtendedText; import org.eclipse.lsp.cobol.common.mapping.OriginalLocation; import org.eclipse.lsp.cobol.common.utils.PredefinedCopybooks; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; import org.eclipse.lsp.cobol.lsp.jrpc.CobolLanguageClient; import org.eclipse.lsp.cobol.service.UriDecodeService; import org.eclipse.lsp.cobol.service.providers.ClientProvider; @@ -66,7 +67,7 @@ class CopybookServiceTest { private static final String COPYBOOK_3_NAME = "SQLCA_DB2"; private final CobolLanguageClient client = mock(CobolLanguageClient.class); private final FileSystemService files = mock(FileSystemService.class); - private final TextPreprocessor preprocessor = mock(TextPreprocessor.class); + private final CleanerPreprocessor preprocessor = mock(CleanerPreprocessor.class); private final Path cpyPath = mock(Path.class); private final Path parentPath = mock(Path.class); private final UriDecodeService uriDecodeService = new UriDecodeService(); @@ -107,7 +108,7 @@ void testRequestWhileCopybookAnalysisActiveProcessed() { copybookName, DOCUMENT_URI, DOCUMENT_URI, - false, "cobol") + null) .getResult(); assertEquals( @@ -135,7 +136,7 @@ void testResponseIfFileNotExists() { copybookName, DOCUMENT_URI, DOCUMENT_URI, - false, "cobol") + null) .getResult(); assertEquals( @@ -160,7 +161,7 @@ void testRequestWhenUriNotFoundProcessed() { copybookName, DOCUMENT_URI, DOCUMENT_URI, - false, "cobol") + null) .getResult(); assertEquals( @@ -184,7 +185,7 @@ void testNoNewClientCallsOnDidChange() { copybookName, DOCUMENT_URI, DOCUMENT_URI, - false, languageId) + null) .getResult(); CopybookModel copybookModelSkipped = copybookService @@ -193,7 +194,7 @@ void testNoNewClientCallsOnDidChange() { copybookName, DOCUMENT_URI, DOCUMENT_URI, - false, languageId) + null) .getResult(); verify(files, times(1)).getContentByPath(cpyPath); @@ -226,7 +227,7 @@ void testCacheInvalidation() { copybookName, DOCUMENT_URI, DOCUMENT_URI, - false, languageId) + null) .getResult(); assertEquals( new CopybookModel( @@ -242,7 +243,7 @@ void testCacheInvalidation() { copybookName, DOCUMENT_URI, DOCUMENT_URI, - false, languageId) + null) .getResult(); assertEquals( new CopybookModel( @@ -270,7 +271,7 @@ void testServiceRespondsIfClientSendsInvalidResult() { copybookName, DOCUMENT_URI, DOCUMENT_URI, - false, languageId) + null) .getResult(); assertEquals( @@ -302,7 +303,7 @@ void testServiceSendsDownloadingRequestForAnalysisFinishedEvent() { copybookInvalid, DOCUMENT_URI, DOCUMENT_URI, - false, languageId) + null) .getResult(); CopybookName copybookValid = createCopybook(VALID_CPY_NAME); CopybookModel validCpy = @@ -312,7 +313,7 @@ void testServiceSendsDownloadingRequestForAnalysisFinishedEvent() { copybookValid, DOCUMENT_URI, DOCUMENT_URI, - false, languageId) + null) .getResult(); // Second document parsed CopybookName copybookInvalid2 = createCopybook(INVALID_2_CPY_NAME); @@ -323,7 +324,7 @@ void testServiceSendsDownloadingRequestForAnalysisFinishedEvent() { copybookInvalid2, DOCUMENT_2_URI, DOCUMENT_2_URI, - false, languageId) + null) .getResult(); // Check that all copybook models are correct @@ -371,13 +372,13 @@ void testSqldaCopybookResolutionDoesNotRelyOnBackend() { copybookName, DOCUMENT_URI, DOCUMENT_URI, - false, languageId), + null), copybookService.resolve( CopybookId.fromString(copybookName.getDisplayName()), copybookName, DOCUMENT_URI, DOCUMENT_URI, - false, languageId)); + null)); } /** @@ -406,7 +407,7 @@ void testServiceSendsDownloadingRequestForAllNotResolvedCopybooks() { copybookInvalid, DOCUMENT_URI, DOCUMENT_URI, - false, languageId) + null) .getResult(); CopybookName copybookParent = createCopybook(PARENT_CPY_NAME); CopybookModel parentCpy = @@ -416,7 +417,7 @@ void testServiceSendsDownloadingRequestForAllNotResolvedCopybooks() { copybookParent, DOCUMENT_URI, DOCUMENT_URI, - false, languageId) + null) .getResult(); // Nested copybook declaration CopybookName copybookNested = createCopybook(NESTED_CPY_NAME); @@ -427,7 +428,7 @@ void testServiceSendsDownloadingRequestForAllNotResolvedCopybooks() { copybookNested, DOCUMENT_URI, PARENT_CPY_URI, - false, languageId) + null) .getResult(); // Check that all copybook models are correct @@ -473,7 +474,7 @@ void testPredefinedCopybooksResolvedInsteadOfStaticOnes() { new CopybookName(copybookName), DOCUMENT_URI, DOCUMENT_URI, - false, languageId) + null) .getResult(); // Assert the copybook was resolved from the workspace @@ -489,7 +490,7 @@ private CopybookServiceImpl createCopybookService() { ClientProvider provider = new ClientProvider(); provider.setClient(client); return new CopybookServiceImpl( - provider, files, preprocessor, new CopybookCache(3, 3, "HOURS"), uriDecodeService); + provider, files, new CopybookCache(3, 3, "HOURS"), uriDecodeService); } private CopybookName createCopybook(String displayName) { @@ -507,7 +508,7 @@ void store() { copybookName, DOCUMENT_URI, DOCUMENT_URI, - false, languageId) + null) .getResult(); CopybookModel resolve; resolve = @@ -517,10 +518,10 @@ void store() { copybookName, DOCUMENT_2_URI, DOCUMENT_2_URI, - false, languageId) + null) .getResult(); assertNull(resolve.getContent()); - copybookService.store(copybookModel, "cobol", false); + copybookService.store(copybookModel, null); resolve = copybookService .resolve( @@ -528,7 +529,7 @@ void store() { copybookName, DOCUMENT_URI, DOCUMENT_URI, - false, languageId) + null) .getResult(); assertEquals(CONTENT, resolve.getContent()); } @@ -544,7 +545,7 @@ void whenErrorInPreprocessOfCopybook_thenResolveReturnsPreprocessErrors() { .suggestion("some suggestion") .severity(ErrorSeverity.ERROR) .build(); - when(preprocessor.cleanUpCode(anyString(), anyString(), any())) + when(preprocessor.cleanUpCode(anyString(), anyString())) .thenReturn( new ResultWithErrors<>( new ExtendedText(copybookContent, null), @@ -556,7 +557,7 @@ void whenErrorInPreprocessOfCopybook_thenResolveReturnsPreprocessErrors() { copybookName, DOCUMENT_URI, DOCUMENT_URI, - true, languageId); + preprocessor); CopybookModel copybookModel = resolvedCopybook.getResult(); assertEquals(copybookModel.getContent(), copybookContent); diff --git a/server/engine/src/test/java/org/eclipse/lsp/cobol/service/mocks/MockTextPreprocessor.java b/server/engine/src/test/java/org/eclipse/lsp/cobol/service/mocks/MockTextPreprocessor.java index 0dc9f51a87..4688604df1 100644 --- a/server/engine/src/test/java/org/eclipse/lsp/cobol/service/mocks/MockTextPreprocessor.java +++ b/server/engine/src/test/java/org/eclipse/lsp/cobol/service/mocks/MockTextPreprocessor.java @@ -15,17 +15,16 @@ package org.eclipse.lsp.cobol.service.mocks; import com.google.common.collect.ImmutableList; +import org.eclipse.lsp.cobol.common.CleanerPreprocessor; import org.eclipse.lsp.cobol.common.ResultWithErrors; import org.eclipse.lsp.cobol.common.mapping.ExtendedText; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; -import org.eclipse.lsp.cobol.lsp.CobolLanguageId; /** * Mock Text Processor class */ -public class MockTextPreprocessor implements TextPreprocessor { +public class MockTextPreprocessor implements CleanerPreprocessor { @Override - public ResultWithErrors cleanUpCode(String documentUri, String cobolCode, CobolLanguageId languageId) { + public ResultWithErrors cleanUpCode(String documentUri, String cobolCode) { return new ResultWithErrors<>(new ExtendedText(cobolCode, documentUri), ImmutableList.of()); } } diff --git a/server/engine/src/test/java/org/eclipse/lsp/cobol/usecases/engine/UseCaseInitializerService.java b/server/engine/src/test/java/org/eclipse/lsp/cobol/usecases/engine/UseCaseInitializerService.java index 9ee3cc8628..5856805252 100644 --- a/server/engine/src/test/java/org/eclipse/lsp/cobol/usecases/engine/UseCaseInitializerService.java +++ b/server/engine/src/test/java/org/eclipse/lsp/cobol/usecases/engine/UseCaseInitializerService.java @@ -36,8 +36,7 @@ import org.eclipse.lsp.cobol.common.file.WorkspaceFileService; import org.eclipse.lsp.cobol.core.engine.dialects.DialectDiscoveryFolderService; import org.eclipse.lsp.cobol.core.engine.dialects.DialectDiscoveryService; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessor; -import org.eclipse.lsp.cobol.core.preprocessor.TextPreprocessorImpl; +import org.eclipse.lsp.cobol.dialects.ibm.IbmTextPreprocessor; import org.eclipse.lsp.cobol.domain.modules.DatabusModule; import org.eclipse.lsp.cobol.domain.modules.EngineModule; import org.eclipse.lsp.cobol.lsp.DisposableLSPStateService; @@ -84,8 +83,7 @@ protected void configure() { bind(FileSystemService.class).toInstance(new WorkspaceFileService()); bind(CobolLanguageClient.class).toInstance(languageClient); bind(SubroutineService.class).to(SubroutineServiceImpl.class); - bind(TextPreprocessor.class).to(TextPreprocessorImpl.class); - bind(CleanerPreprocessor.class).to(TextPreprocessorImpl.class); + bind(CleanerPreprocessor.class).to(IbmTextPreprocessor.class); bind(WatcherService.class).to(WatcherServiceImpl.class); bind(DialectDiscoveryService.class).to(DialectDiscoveryFolderService.class); bind(CodeActions.class); diff --git a/server/test/src/main/java/org/eclipse/lsp/cobol/test/engine/UseCaseUtils.java b/server/test/src/main/java/org/eclipse/lsp/cobol/test/engine/UseCaseUtils.java index 76f28c1398..ed9c844e54 100644 --- a/server/test/src/main/java/org/eclipse/lsp/cobol/test/engine/UseCaseUtils.java +++ b/server/test/src/main/java/org/eclipse/lsp/cobol/test/engine/UseCaseUtils.java @@ -125,19 +125,15 @@ public static AnalysisResult analyze(UseCase useCase, String languageId) { CopybookService copybookService = injector.getInstance(CopybookService.class); PredefinedCopybookUtils.loadPredefinedCopybooks(useCase.getSqlBackend(), useCase.getCopybooks(), useCase.documentUri) - .forEach(pc -> copybookService.store(pc, languageId, true)); + .forEach(pc -> copybookService.store(pc, preprocessor)); useCase.getCopybooks() .forEach(cobolText -> { String copybookText = cobolText.getFullText(); - if (cobolText.isPreprocess()) { - copybookText = preprocessor.cleanUpCode("uri", cobolText.getFullText()) - .getResult().toString(); - } cobolText = new CobolText(cobolText.getFileName().toUpperCase(), cobolText.getDialectType(), copybookText); - copybookService.store(UseCaseUtils.toCopybookModel(cobolText, useCase.documentUri), languageId, true); + copybookService.store(UseCaseUtils.toCopybookModel(cobolText, useCase.documentUri), preprocessor); }); SubroutineService subroutines = injector.getInstance(SubroutineService.class);