Skip to content

Commit 466a6df

Browse files
committed
fix: Store symbols in a repository
Store symbols in a repository, to be later used for completion, references, definition and highlights. Signed-off-by: ap891843 <[email protected]>
1 parent ac8e757 commit 466a6df

37 files changed

+340
-258
lines changed

server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/CobolLanguageEngine.java

+28-22
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
import org.eclipse.lsp.cobol.core.CobolParser;
4646
import org.eclipse.lsp.cobol.core.engine.dialects.DialectService;
4747
import org.eclipse.lsp.cobol.core.engine.processor.AstProcessor;
48-
import org.eclipse.lsp.cobol.core.engine.symbols.SymbolService;
48+
import org.eclipse.lsp.cobol.core.engine.symbols.SymbolAccumulatorService;
49+
import org.eclipse.lsp.cobol.core.engine.symbols.SymbolTable;
50+
import org.eclipse.lsp.cobol.core.engine.symbols.SymbolsRepository;
4951
import org.eclipse.lsp.cobol.core.model.*;
5052
import org.eclipse.lsp.cobol.core.model.tree.*;
5153
import org.eclipse.lsp.cobol.core.model.tree.logic.*;
@@ -68,9 +70,7 @@
6870
import org.eclipse.lsp.cobol.service.delegates.validations.AnalysisResult;
6971
import org.eclipse.lsp4j.Location;
7072

71-
import java.util.ArrayList;
72-
import java.util.List;
73-
import java.util.Map;
73+
import java.util.*;
7474
import java.util.function.Function;
7575
import java.util.function.Predicate;
7676
import java.util.stream.Stream;
@@ -95,9 +95,10 @@ public class CobolLanguageEngine {
9595
private final SubroutineService subroutineService;
9696
private final CachingConfigurationService cachingConfigurationService;
9797
private final DialectService dialectService;
98-
private final SymbolService symbolService;
98+
private final SymbolAccumulatorService symbolAccumulatorService;
9999
private final AstProcessor astProcessor;
100100
private final InjectService injectService;
101+
private final SymbolsRepository symbolsRepository;
101102

102103
@Inject
103104
public CobolLanguageEngine(
@@ -107,18 +108,20 @@ public CobolLanguageEngine(
107108
SubroutineService subroutineService,
108109
CachingConfigurationService cachingConfigurationService,
109110
DialectService dialectService,
110-
SymbolService symbolService,
111+
SymbolAccumulatorService symbolAccumulatorService,
111112
AstProcessor astProcessor,
112-
InjectService injectService) {
113+
InjectService injectService,
114+
SymbolsRepository symbolsRepository) {
113115
this.preprocessor = preprocessor;
114116
this.messageService = messageService;
115117
this.treeListener = treeListener;
116118
this.subroutineService = subroutineService;
117119
this.cachingConfigurationService = cachingConfigurationService;
118120
this.dialectService = dialectService;
119-
this.symbolService = symbolService;
121+
this.symbolAccumulatorService = symbolAccumulatorService;
120122
this.astProcessor = astProcessor;
121123
this.injectService = injectService;
124+
this.symbolsRepository = symbolsRepository;
122125
}
123126

124127
/**
@@ -139,7 +142,6 @@ public ResultWithErrors<AnalysisResult> run(
139142
Timing.Builder timingBuilder = Timing.builder();
140143

141144
timingBuilder.getDialectsTimer().start();
142-
symbolService.reset(documentUri);
143145
List<SyntaxError> accumulatedErrors = new ArrayList<>();
144146
TextTransformations cleanText =
145147
preprocessor.cleanUpCode(documentUri, text).unwrap(accumulatedErrors::addAll);
@@ -252,7 +254,7 @@ public ResultWithErrors<AnalysisResult> run(
252254
embeddedCodeParts,
253255
messageService,
254256
subroutineService,
255-
symbolService,
257+
symbolsRepository,
256258
dialectOutcome.getDialectNodes(),
257259
cachingConfigurationService);
258260
List<Node> syntaxTree = visitor.visit(tree);
@@ -290,10 +292,14 @@ public ResultWithErrors<AnalysisResult> run(
290292
timing.getLateErrorProcessingTime());
291293
}
292294

295+
Map<String, SymbolTable> symbolTableMap = Collections.synchronizedMap(new HashMap<>(symbolAccumulatorService.getProgramSymbols()));
296+
symbolsRepository.updateSymbols(symbolTableMap);
297+
symbolAccumulatorService.reset(documentUri);
298+
293299
return new ResultWithErrors<>(
294300
AnalysisResult.builder()
295301
.rootNode(rootNode)
296-
.symbolTableMap(symbolService.getProgramSymbols())
302+
.symbolTableMap(symbolTableMap)
297303
.build(),
298304
accumulatedErrors.stream().map(this::constructErrorMessage).collect(toList()));
299305
}
@@ -307,63 +313,63 @@ private void registerProcessors(AnalysisConfig analysisConfig, ProcessingContext
307313
new ProcessorDescription(
308314
SectionNode.class,
309315
ProcessingPhase.TRANSFORMATION,
310-
new ProcessNodeWithVariableDefinitions(symbolService)));
316+
new ProcessNodeWithVariableDefinitions(symbolAccumulatorService)));
311317
ctx.register(
312318
new ProcessorDescription(
313319
FileEntryNode.class, ProcessingPhase.TRANSFORMATION, new FileEntryProcess()));
314320
ctx.register(
315321
new ProcessorDescription(
316322
FileDescriptionNode.class,
317323
ProcessingPhase.TRANSFORMATION,
318-
new FileDescriptionProcess(symbolService)));
324+
new FileDescriptionProcess(symbolAccumulatorService)));
319325
ctx.register(
320326
new ProcessorDescription(
321327
DeclarativeProcedureSectionNode.class,
322328
ProcessingPhase.TRANSFORMATION,
323-
new DeclarativeProcedureSectionRegister(symbolService)));
329+
new DeclarativeProcedureSectionRegister(symbolAccumulatorService, symbolsRepository)));
324330
// Phase DEFINITION
325331
ctx.register(
326332
new ProcessorDescription(
327-
ParagraphsNode.class, ProcessingPhase.DEFINITION, new DefineCodeBlock(symbolService)));
333+
ParagraphsNode.class, ProcessingPhase.DEFINITION, new DefineCodeBlock(symbolAccumulatorService)));
328334
ctx.register(
329335
new ProcessorDescription(
330336
SectionNameNode.class,
331337
ProcessingPhase.DEFINITION,
332-
new SectionNameRegister(symbolService)));
338+
new SectionNameRegister(symbolAccumulatorService)));
333339
ctx.register(
334340
new ProcessorDescription(
335341
ParagraphNameNode.class,
336342
ProcessingPhase.DEFINITION,
337-
new ParagraphNameRegister(symbolService)));
343+
new ParagraphNameRegister(symbolAccumulatorService)));
338344
ctx.register(
339345
new ProcessorDescription(
340346
ProcedureDivisionBodyNode.class,
341347
ProcessingPhase.DEFINITION,
342-
new DefineCodeBlock(symbolService)));
348+
new DefineCodeBlock(symbolAccumulatorService)));
343349
// Phase USAGE
344350
ctx.register(
345351
new ProcessorDescription(
346-
CodeBlockUsageNode.class, ProcessingPhase.USAGE, new CodeBlockUsage(symbolService)));
352+
CodeBlockUsageNode.class, ProcessingPhase.USAGE, new CodeBlockUsage(symbolAccumulatorService)));
347353
ctx.register(
348354
new ProcessorDescription(
349355
RootNode.class, ProcessingPhase.USAGE, new RootNodeUpdateCopyNodesByPositionInTree()));
350356
ctx.register(
351357
new ProcessorDescription(
352358
QualifiedReferenceNode.class,
353359
ProcessingPhase.USAGE,
354-
new QualifiedReferenceUpdateVariableUsage(symbolService)));
360+
new QualifiedReferenceUpdateVariableUsage(symbolAccumulatorService)));
355361

356362
// ENRICHMENT
357363
ctx.register(
358364
new ProcessorDescription(
359365
SectionNameNode.class,
360366
ProcessingPhase.VALIDATION,
361-
new SectionNameNodeEnricher(symbolService)));
367+
new SectionNameNodeEnricher(symbolAccumulatorService)));
362368
ctx.register(
363369
new ProcessorDescription(
364370
CodeBlockUsageNode.class,
365371
ProcessingPhase.VALIDATION,
366-
new CodeBlockUsageNodeEnricher(symbolService)));
372+
new CodeBlockUsageNodeEnricher(symbolAccumulatorService)));
367373

368374
// Phase VALIDATION
369375
ctx.register(

server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/dialects/DialectService.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.eclipse.lsp.cobol.core.engine.dialects.daco.DaCoDialect;
3232
import org.eclipse.lsp.cobol.core.engine.dialects.daco.DaCoMaidProcessor;
3333
import org.eclipse.lsp.cobol.core.engine.dialects.idms.IdmsDialect;
34-
import org.eclipse.lsp.cobol.core.engine.symbols.SymbolService;
34+
import org.eclipse.lsp.cobol.core.engine.symbols.SymbolAccumulatorService;
3535
import org.eclipse.lsp.cobol.service.copybooks.CopybookService;
3636

3737
import java.util.*;
@@ -47,7 +47,7 @@ public class DialectService {
4747
public DialectService(
4848
CopybookService copybookService,
4949
ParseTreeListener treeListener,
50-
SymbolService symbolService,
50+
SymbolAccumulatorService symbolAccumulatorService,
5151
MessageService messageService) {
5252
dialectSuppliers = new HashMap<>();
5353

@@ -58,7 +58,7 @@ public DialectService(
5858
new DaCoDialect(
5959
messageService,
6060
new DaCoMaidProcessor(copybookService, treeListener, messageService),
61-
symbolService);
61+
symbolAccumulatorService);
6262
dialectSuppliers.put(dialect.getName(), dialect);
6363

6464
dialectSuppliers.put(dialect.getName(), dialect);

server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/dialects/daco/DaCoCopyFromProcessor.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.eclipse.lsp.cobol.common.processor.ProcessingContext;
2525
import org.eclipse.lsp.cobol.common.processor.Processor;
2626
import org.eclipse.lsp.cobol.core.engine.dialects.daco.nodes.DaCoCopyFromNode;
27-
import org.eclipse.lsp.cobol.core.engine.symbols.SymbolService;
27+
import org.eclipse.lsp.cobol.core.engine.symbols.SymbolAccumulatorService;
2828
import org.eclipse.lsp.cobol.core.model.tree.variables.*;
2929

3030
import java.util.List;
@@ -34,10 +34,10 @@
3434

3535
/** Handle Copy From node */
3636
public class DaCoCopyFromProcessor implements Processor<DaCoCopyFromNode> {
37-
private final SymbolService symbolService;
37+
private final SymbolAccumulatorService symbolAccumulatorService;
3838

39-
public DaCoCopyFromProcessor(SymbolService symbolService) {
40-
this.symbolService = symbolService;
39+
public DaCoCopyFromProcessor(SymbolAccumulatorService symbolAccumulatorService) {
40+
this.symbolAccumulatorService = symbolAccumulatorService;
4141
}
4242

4343
@Override
@@ -68,7 +68,7 @@ private void astPostprocessing(DaCoCopyFromNode node, List<SyntaxError> errors)
6868
return;
6969
}
7070
copyFrom(node.getLevel(), protoCandidates.get(0), node.getParent(), node);
71-
symbolService.registerVariablesInProgram(node.getParent());
71+
symbolAccumulatorService.registerVariablesInProgram(node.getParent());
7272
}
7373

7474
private void copyFrom(

server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/dialects/daco/DaCoDialect.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.eclipse.lsp.cobol.core.engine.dialects.daco.nodes.DaCoCopyFromNode;
3838
import org.eclipse.lsp.cobol.core.engine.dialects.daco.provider.DaCoImplicitCodeProvider;
3939
import org.eclipse.lsp.cobol.core.engine.dialects.idms.IdmsDialect;
40-
import org.eclipse.lsp.cobol.core.engine.symbols.SymbolService;
40+
import org.eclipse.lsp.cobol.core.engine.symbols.SymbolAccumulatorService;
4141
import org.eclipse.lsp.cobol.core.strategy.CobolErrorStrategy;
4242
import org.eclipse.lsp4j.Position;
4343
import org.eclipse.lsp4j.Range;
@@ -54,7 +54,7 @@ public final class DaCoDialect implements CobolDialect {
5454

5555
private final MessageService messageService;
5656
private final DaCoMaidProcessor maidProcessor;
57-
private final SymbolService symbolService;
57+
private final SymbolAccumulatorService symbolAccumulatorService;
5858

5959
/**
6060
* Gets the name of the dialect
@@ -136,6 +136,6 @@ public List<ProcessorDescription> getProcessors() {
136136
return Collections.singletonList(
137137
new ProcessorDescription(
138138
DaCoCopyFromNode.class, ProcessingPhase.POST_DEFINITION,
139-
new DaCoCopyFromProcessor(symbolService)));
139+
new DaCoCopyFromProcessor(symbolAccumulatorService)));
140140
}
141141
}

server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/symbols/SymbolService.java renamed to server/engine/src/main/java/org/eclipse/lsp/cobol/core/engine/symbols/SymbolAccumulatorService.java

+6-80
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,35 @@
1818
import com.google.common.collect.ImmutableList;
1919
import com.google.common.collect.Multimap;
2020
import com.google.inject.Singleton;
21-
import lombok.Value;
2221
import org.eclipse.lsp.cobol.common.error.ErrorSeverity;
2322
import org.eclipse.lsp.cobol.common.error.ErrorSource;
2423
import org.eclipse.lsp.cobol.common.error.SyntaxError;
2524
import org.eclipse.lsp.cobol.common.message.MessageTemplate;
26-
import org.eclipse.lsp.cobol.common.model.Context;
27-
import org.eclipse.lsp.cobol.common.model.tree.Node;
2825
import org.eclipse.lsp.cobol.common.model.NodeType;
26+
import org.eclipse.lsp.cobol.common.model.tree.Node;
2927
import org.eclipse.lsp.cobol.common.model.tree.ProgramNode;
30-
import org.eclipse.lsp.cobol.core.model.VariableUsageUtils;
31-
import org.eclipse.lsp.cobol.core.model.tree.*;
3228
import org.eclipse.lsp.cobol.common.model.tree.variable.VariableNode;
3329
import org.eclipse.lsp.cobol.common.model.tree.variable.VariableUsageNode;
34-
import org.eclipse.lsp.cobol.core.preprocessor.delegates.injector.ImplicitCodeUtils;
35-
import org.eclipse.lsp.cobol.service.CobolDocumentModel;
36-
import org.eclipse.lsp.cobol.service.delegates.validations.AnalysisResult;
30+
import org.eclipse.lsp.cobol.core.model.VariableUsageUtils;
31+
import org.eclipse.lsp.cobol.core.model.tree.*;
3732
import org.eclipse.lsp4j.Location;
38-
import org.eclipse.lsp4j.TextDocumentPositionParams;
3933

4034
import java.util.*;
4135
import java.util.function.Function;
42-
import java.util.function.Predicate;
4336
import java.util.stream.Collectors;
4437

4538
import static org.eclipse.lsp.cobol.common.model.tree.Node.hasType;
46-
import static org.eclipse.lsp.cobol.common.utils.RangeUtils.findNodeByPosition;
4739

4840
/** Service to handle symbol information and dependencies */
4941
@Singleton
50-
public class SymbolService {
42+
public class SymbolAccumulatorService {
5143
private final Map<String, SymbolTable> programSymbols;
5244

53-
public SymbolService() {
45+
public SymbolAccumulatorService() {
5446
this.programSymbols = Collections.synchronizedMap(new HashMap<>());
5547
}
5648

57-
public SymbolService(Map<String, SymbolTable> symbolTableMap) {
49+
public SymbolAccumulatorService(Map<String, SymbolTable> symbolTableMap) {
5850
this.programSymbols = symbolTableMap;
5951
}
6052

@@ -67,36 +59,6 @@ public SymbolService(Map<String, SymbolTable> symbolTableMap) {
6759
public void addVariableDefinition(ProgramNode programNode, VariableNode node) {
6860
createOrGetSymbolTable(programNode).getVariables().put(node.getName(), node);
6961
}
70-
/**
71-
* Find element using a position
72-
*
73-
* @param document the document to search in
74-
* @param position the position to check
75-
* @return element at specified position
76-
*/
77-
public Optional<Context> findElementByPosition(
78-
CobolDocumentModel document, TextDocumentPositionParams position) {
79-
AnalysisResult result = document.getAnalysisResult();
80-
if (result.getRootNode() == null) {
81-
return Optional.empty();
82-
}
83-
Optional<Node> node = findNodeByPosition(result.getRootNode(), position.getTextDocument().getUri(), position.getPosition());
84-
return node.filter(Context.class::isInstance)
85-
.map(Context.class::cast)
86-
.map(this::constructElementsExcludingImplicits);
87-
}
88-
89-
private Context constructElementsExcludingImplicits(Context ctx) {
90-
List<Location> definitions =
91-
ctx.getDefinitions().stream().filter(uriNotImplicit()).collect(Collectors.toList());
92-
List<Location> usages =
93-
ctx.getUsages().stream().filter(uriNotImplicit()).collect(Collectors.toList());
94-
return new Element("", definitions, usages);
95-
}
96-
97-
private static Predicate<Location> uriNotImplicit() {
98-
return i -> !ImplicitCodeUtils.isImplicit(i.getUri());
99-
}
10062

10163
/**
10264
* * Register variable definitions into nearest ProgramNode
@@ -287,25 +249,6 @@ public List<Location> getSectionLocations(
287249
.orElse(ImmutableList.of());
288250
}
289251

290-
/**
291-
* Get paragraphs data
292-
*
293-
* @param programNode the program node
294-
* @return map of paragraphs
295-
*/
296-
public Map<String, CodeBlockReference> getParagraphMap(ProgramNode programNode) {
297-
return createOrGetSymbolTable(programNode).getParagraphMap();
298-
}
299-
/**
300-
* Get section data
301-
*
302-
* @param programNode the program node
303-
* @return map of sections
304-
*/
305-
public Map<String, CodeBlockReference> getSectionMap(ProgramNode programNode) {
306-
return createOrGetSymbolTable(programNode).getSectionMap();
307-
}
308-
309252
/**
310253
* Extract all accumulated symbols information
311254
*
@@ -347,16 +290,6 @@ private Map<String, VariableNode> getMapOfGlobalVariables(ProgramNode programNod
347290
return result;
348291
}
349292

350-
/**
351-
* Get variables data
352-
*
353-
* @param programNode the program node
354-
* @return map of variables
355-
*/
356-
public Multimap<String, VariableNode> getVariables(ProgramNode programNode) {
357-
return createOrGetSymbolTable(programNode).getVariables();
358-
}
359-
360293
/**
361294
* Remove program related symbols
362295
*
@@ -369,11 +302,4 @@ public void reset(String documentUri) {
369302
.forEach(programSymbols::remove);
370303
programSymbols.remove(documentUri);
371304
}
372-
373-
@Value
374-
private static class Element implements Context {
375-
String name;
376-
List<Location> definitions;
377-
List<Location> usages;
378-
}
379305
}

0 commit comments

Comments
 (0)