|
| 1 | +using TypeCobol.Compiler; |
| 2 | +using TypeCobol.Compiler.CodeElements; |
| 3 | +using TypeCobol.Compiler.Nodes; |
| 4 | +using TypeCobol.Compiler.Scanner; |
| 5 | +using TypeCobol.LanguageServer.VsCodeProtocol; |
| 6 | + |
| 7 | +using Range = TypeCobol.LanguageServer.VsCodeProtocol.Range; |
| 8 | + |
| 9 | +namespace TypeCobol.LanguageServer.Processor |
| 10 | +{ |
| 11 | + public class CompletionProcessor |
| 12 | + { |
| 13 | + private readonly SignatureCompletionContext _signatureCompletionContext; |
| 14 | + |
| 15 | + public CompletionProcessor(SignatureCompletionContext signatureCompletionContext) |
| 16 | + { |
| 17 | + _signatureCompletionContext = signatureCompletionContext; |
| 18 | + } |
| 19 | + |
| 20 | + public List<CompletionItem> ComputeProposals(CompilationUnit compilationUnit, Position position) |
| 21 | + { |
| 22 | + List<CompletionItem> items; |
| 23 | + |
| 24 | + var wrappedCodeElements = TypeCobolServer.CodeElementFinder(compilationUnit, position); |
| 25 | + if (wrappedCodeElements == null) |
| 26 | + return null; |
| 27 | + |
| 28 | + //Try to get a significant token for completion and return the codeelement containing the matching token. |
| 29 | + CodeElement matchingCodeElement = CodeElementMatcher.MatchCompletionCodeElement(position, |
| 30 | + wrappedCodeElements, |
| 31 | + out var userFilterToken, out var lastSignificantToken); //Magic happens here |
| 32 | + |
| 33 | + if (lastSignificantToken != null) |
| 34 | + { |
| 35 | + |
| 36 | + switch (lastSignificantToken.TokenType) |
| 37 | + { |
| 38 | + case TokenType.PERFORM: |
| 39 | + items = new CompletionAfterPerform(userFilterToken).ComputeProposals(compilationUnit, matchingCodeElement); |
| 40 | + break; |
| 41 | + case TokenType.CALL: |
| 42 | + _signatureCompletionContext.Candidates.Clear(); //Clear to avoid key collision |
| 43 | + items = new CompletionForProcedure(userFilterToken, _signatureCompletionContext.Candidates).ComputeProposals(compilationUnit, matchingCodeElement); |
| 44 | + items.AddRange(new CompletionForLibrary(userFilterToken).ComputeProposals(compilationUnit, matchingCodeElement)); |
| 45 | + break; |
| 46 | + case TokenType.TYPE: |
| 47 | + items = new CompletionForType(userFilterToken).ComputeProposals(compilationUnit, matchingCodeElement); |
| 48 | + items.AddRange(new CompletionForLibrary(userFilterToken).ComputeProposals(compilationUnit, matchingCodeElement)); |
| 49 | + break; |
| 50 | + case TokenType.QualifiedNameSeparator: |
| 51 | + items = new CompletionForQualifiedName(userFilterToken, lastSignificantToken, position, _signatureCompletionContext.Candidates).ComputeProposals(compilationUnit, matchingCodeElement); |
| 52 | + break; |
| 53 | + case TokenType.INPUT: |
| 54 | + case TokenType.OUTPUT: |
| 55 | + case TokenType.IN_OUT: |
| 56 | + items = new CompletionForProcedureParameter(userFilterToken, lastSignificantToken, position, _signatureCompletionContext.BestMatch).ComputeProposals(compilationUnit, matchingCodeElement); |
| 57 | + break; |
| 58 | + case TokenType.DISPLAY: |
| 59 | + Predicate<DataDefinition> excludeNonDisplayable = dataDefinition => |
| 60 | + dataDefinition.Usage != DataUsage.ProcedurePointer // invalid usages in DISPLAY statement |
| 61 | + && dataDefinition.Usage != DataUsage.FunctionPointer |
| 62 | + && dataDefinition.Usage != DataUsage.ObjectReference |
| 63 | + && dataDefinition.Usage != DataUsage.Index |
| 64 | + && dataDefinition.CodeElement?.LevelNumber != null |
| 65 | + && dataDefinition.CodeElement.LevelNumber.Value < 88; |
| 66 | + // Ignore level 88. Note that dataDefinition.CodeElement != null condition also filters out IndexDefinition which is invalid in the context of DISPLAY |
| 67 | + // Filtering dataDefinition without LevelNumber also excludes FileDescription which are invalid for a DISPLAY |
| 68 | + items = new CompletionForVariable(userFilterToken, excludeNonDisplayable).ComputeProposals(compilationUnit, matchingCodeElement); |
| 69 | + break; |
| 70 | + case TokenType.MOVE: |
| 71 | + Predicate<DataDefinition> excludeLevel88 = dataDefinition => |
| 72 | + (dataDefinition.CodeElement?.LevelNumber != null && dataDefinition.CodeElement.LevelNumber.Value < 88) |
| 73 | + || |
| 74 | + (dataDefinition.CodeElement == null && dataDefinition is IndexDefinition); |
| 75 | + //Ignore 88 level variable |
| 76 | + items = new CompletionForVariable(userFilterToken, excludeLevel88).ComputeProposals(compilationUnit, matchingCodeElement); |
| 77 | + break; |
| 78 | + case TokenType.TO: |
| 79 | + items = new CompletionForTo(userFilterToken, lastSignificantToken).ComputeProposals(compilationUnit, matchingCodeElement); |
| 80 | + break; |
| 81 | + case TokenType.INTO: |
| 82 | + Predicate<DataDefinition> onlyAlpha = dataDefinition => dataDefinition.CodeElement != null && |
| 83 | + (dataDefinition.DataType == DataType.Alphabetic || |
| 84 | + dataDefinition.DataType == DataType.Alphanumeric || |
| 85 | + dataDefinition.DataType == DataType.AlphanumericEdited); |
| 86 | + items = new CompletionForVariable(userFilterToken, onlyAlpha).ComputeProposals(compilationUnit, matchingCodeElement); |
| 87 | + break; |
| 88 | + case TokenType.SET: |
| 89 | + Predicate<DataDefinition> keepCompatibleTypes = dataDefinition => dataDefinition.CodeElement?.Type == CodeElementType.DataConditionEntry //Level 88 Variable |
| 90 | + || dataDefinition.DataType == DataType.Numeric //Numeric Integer Variable |
| 91 | + || dataDefinition.Usage == DataUsage.Pointer |
| 92 | + || dataDefinition.Usage == DataUsage.Pointer32; //Or usage is pointer/pointer-32 |
| 93 | + items = new CompletionForVariable(userFilterToken, keepCompatibleTypes).ComputeProposals(compilationUnit, matchingCodeElement); |
| 94 | + break; |
| 95 | + case TokenType.OF: |
| 96 | + items = new CompletionForOf(userFilterToken, position).ComputeProposals(compilationUnit, matchingCodeElement); |
| 97 | + break; |
| 98 | + default: |
| 99 | + // Unable to suggest anything |
| 100 | + items = new List<CompletionItem>(); |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + //If no known keyword has been found, let's try to get the context and return available variables. |
| 107 | + if (matchingCodeElement == null && wrappedCodeElements.Any()) |
| 108 | + { |
| 109 | + userFilterToken = |
| 110 | + wrappedCodeElements.First().ArrangedConsumedTokens.FirstOrDefault( |
| 111 | + t => |
| 112 | + position.character <= t.StopIndex + 1 && position.character > t.StartIndex |
| 113 | + && t.Line == position.line + 1 |
| 114 | + && t.TokenType == TokenType.UserDefinedWord); //Get the userFilterToken to filter the results |
| 115 | + items = new CompletionForVariable(userFilterToken, _ => true).ComputeProposals(compilationUnit, wrappedCodeElements.First()); |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + //Return a default text to inform the user that completion is not available after the given token |
| 120 | + items = new List<CompletionItem>(1) |
| 121 | + { |
| 122 | + new CompletionItem() { label = "Completion is not available in this context", insertText = string.Empty } |
| 123 | + }; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (userFilterToken != null) |
| 128 | + { |
| 129 | + //Add the range object to let the client know the position of the user filter token |
| 130 | + var range = Range.FromPositions(userFilterToken.Line - 1, userFilterToken.StartIndex, userFilterToken.Line - 1, userFilterToken.StopIndex + 1); |
| 131 | + //-1 on line to 0 based / +1 on stop index to include the last character |
| 132 | + items.ForEach(c => |
| 133 | + { |
| 134 | + if (c.data != null && c.data.GetType().IsArray) |
| 135 | + ((object[])c.data)[0] = range; |
| 136 | + else |
| 137 | + c.data = range; |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + return items; |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments