|
| 1 | +package scip |
| 2 | + |
| 3 | +// FlattenDocuments merges elements of the given slice with the same relative path. This allows us to make |
| 4 | +// the assumption post-canonicalization that each index has one representation of a given document path in |
| 5 | +// the database. This function returns a new slice. |
| 6 | +func FlattenDocuments(documents []*Document) []*Document { |
| 7 | + documentMap := make(map[string]*Document, len(documents)) |
| 8 | + for _, document := range documents { |
| 9 | + existing, ok := documentMap[document.RelativePath] |
| 10 | + if !ok { |
| 11 | + documentMap[document.RelativePath] = document |
| 12 | + continue |
| 13 | + } |
| 14 | + if existing.Language != document.Language { |
| 15 | + _ = 0 // TODO - warn? |
| 16 | + } |
| 17 | + |
| 18 | + existing.Symbols = append(existing.Symbols, document.Symbols...) |
| 19 | + existing.Occurrences = append(existing.Occurrences, document.Occurrences...) |
| 20 | + } |
| 21 | + |
| 22 | + flattened := make([]*Document, 0, len(documentMap)) |
| 23 | + for _, document := range documentMap { |
| 24 | + flattened = append(flattened, document) |
| 25 | + } |
| 26 | + |
| 27 | + return flattened |
| 28 | +} |
| 29 | + |
| 30 | +// FlattenSymbol merges elements of the given slice with the same symbol name. This allows us to make the |
| 31 | +// assumption post-canonicalization that each index and document refer to one symbol metadata object uniquely. |
| 32 | +// This function returns a new slice. |
| 33 | +func FlattenSymbols(symbols []*SymbolInformation) []*SymbolInformation { |
| 34 | + symbolMap := make(map[string]*SymbolInformation, len(symbols)) |
| 35 | + for _, symbol := range symbols { |
| 36 | + existing, ok := symbolMap[symbol.Symbol] |
| 37 | + if !ok { |
| 38 | + symbolMap[symbol.Symbol] = symbol |
| 39 | + continue |
| 40 | + } |
| 41 | + |
| 42 | + existing.Documentation = combineDocumentation(existing.Documentation, symbol.Documentation) |
| 43 | + existing.Relationships = append(existing.Relationships, symbol.Relationships...) |
| 44 | + } |
| 45 | + |
| 46 | + flattened := make([]*SymbolInformation, 0, len(symbolMap)) |
| 47 | + for _, symbol := range symbolMap { |
| 48 | + flattened = append(flattened, symbol) |
| 49 | + } |
| 50 | + |
| 51 | + return flattened |
| 52 | +} |
| 53 | + |
| 54 | +// FlattenOccurrences merges elements of the given slice with equivalent bounds. This function returns a new slice. |
| 55 | +func FlattenOccurrences(occurrences []*Occurrence) []*Occurrence { |
| 56 | + if len(occurrences) == 0 { |
| 57 | + return occurrences |
| 58 | + } |
| 59 | + |
| 60 | + _ = SortOccurrences(occurrences) |
| 61 | + flattened := make([]*Occurrence, 0, len(occurrences)) |
| 62 | + flattened = append(flattened, occurrences[0]) |
| 63 | + |
| 64 | + for _, occurrence := range occurrences[1:] { |
| 65 | + top := flattened[len(flattened)-1] |
| 66 | + |
| 67 | + if !rawRangesEqual(top.Range, occurrence.Range) { |
| 68 | + flattened = append(flattened, occurrence) |
| 69 | + continue |
| 70 | + } |
| 71 | + if top.Symbol != occurrence.Symbol { |
| 72 | + flattened = append(flattened, occurrence) |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + if top.SyntaxKind == SyntaxKind_UnspecifiedSyntaxKind { |
| 77 | + // Take first valid syntax kind |
| 78 | + top.SyntaxKind = occurrence.SyntaxKind |
| 79 | + } |
| 80 | + |
| 81 | + // Combine all other fields |
| 82 | + top.SymbolRoles |= occurrence.SymbolRoles |
| 83 | + top.OverrideDocumentation = append(top.OverrideDocumentation, occurrence.OverrideDocumentation...) |
| 84 | + top.Diagnostics = append(top.Diagnostics, occurrence.Diagnostics...) |
| 85 | + } |
| 86 | + |
| 87 | + return flattened |
| 88 | +} |
| 89 | + |
| 90 | +// FlattenRelationship merges elements of the given slice with equivalent symbol names. This function returns a new |
| 91 | +// slice. |
| 92 | +func FlattenRelationship(relationships []*Relationship) []*Relationship { |
| 93 | + relationshipMap := make(map[string][]*Relationship, len(relationships)) |
| 94 | + for _, relationship := range relationships { |
| 95 | + relationshipMap[relationship.Symbol] = append(relationshipMap[relationship.Symbol], relationship) |
| 96 | + } |
| 97 | + |
| 98 | + flattened := make([]*Relationship, 0, len(relationshipMap)) |
| 99 | + for _, relationships := range relationshipMap { |
| 100 | + combined := relationships[0] |
| 101 | + for _, relationship := range relationships[1:] { |
| 102 | + combined.IsReference = combined.IsReference || relationship.IsReference |
| 103 | + combined.IsImplementation = combined.IsImplementation || relationship.IsImplementation |
| 104 | + combined.IsTypeDefinition = combined.IsTypeDefinition || relationship.IsTypeDefinition |
| 105 | + combined.IsDefinition = combined.IsDefinition || relationship.IsDefinition |
| 106 | + } |
| 107 | + |
| 108 | + flattened = append(flattened, combined) |
| 109 | + } |
| 110 | + |
| 111 | + return flattened |
| 112 | +} |
| 113 | + |
| 114 | +// combineDocumentation merges documentation components from two separate symbol information objects. |
| 115 | +func combineDocumentation(existing, additional []string) []string { |
| 116 | + filtered := make([]string, 0, len(additional)) |
| 117 | + for _, s := range additional { |
| 118 | + if !stringSliceContains(existing, s) { |
| 119 | + filtered = append(filtered, s) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return append(existing, filtered...) |
| 124 | +} |
| 125 | + |
| 126 | +func stringSliceContains(slice []string, target string) bool { |
| 127 | + for _, candidate := range slice { |
| 128 | + if target == candidate { |
| 129 | + return true |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return false |
| 134 | +} |
0 commit comments