|
| 1 | +import { BaseCollector } from './BaseCollector'; |
| 2 | +import { Participants } from '@/parser/Participants'; |
| 3 | +import { |
| 4 | + SequenceASTNode, |
| 5 | + ParticipantNode, |
| 6 | + MessageNode, |
| 7 | + CreationNode, |
| 8 | + FragmentNode, |
| 9 | + RetNode, |
| 10 | + GroupNode, |
| 11 | + ToNode, |
| 12 | + FromNode |
| 13 | +} from '@/parser/types/astNode.types'; |
| 14 | + |
| 15 | +export class ParticipantCollector extends BaseCollector<Participants> { |
| 16 | + private participants = new Participants(); |
| 17 | + private groupId?: string; |
| 18 | + |
| 19 | + registerNodeHandlers(): void { |
| 20 | + this.nodeHandlers.set('ParticipantNode', node => this.ParticipantNode(node as ParticipantNode)); |
| 21 | + this.nodeHandlers.set('MessageNode', node => this.MessageNode(node as MessageNode)); |
| 22 | + this.nodeHandlers.set('CreationNode', node => this.CreationNode(node as CreationNode)); |
| 23 | + this.nodeHandlers.set('FragmentNode', node => this.GroupNode(node as FragmentNode)); |
| 24 | + this.nodeHandlers.set('RetNode', node => this.RetNode(node as RetNode)); |
| 25 | + this.nodeHandlers.set('ParametersNode', node => this.ParametersNode(node)); |
| 26 | + this.nodeHandlers.set('ConditionNode', node => this.ConditionNode(node)); |
| 27 | + // Both FromNode and ToNode use the same logic |
| 28 | + this.nodeHandlers.set("FromNode", node => this.FromOrToNode(node as FromNode)); |
| 29 | + this.nodeHandlers.set("ToNode", node => this.FromOrToNode(node as ToNode)); |
| 30 | + } |
| 31 | + |
| 32 | + FromOrToNode(node: ToNode): void { |
| 33 | + if (this.shouldSkip) return; |
| 34 | + let participant = node.getText(); |
| 35 | + const participantInstance = this.participants.Get(participant); |
| 36 | + |
| 37 | + // Skip adding participant position if label is present |
| 38 | + if (participantInstance?.label) { |
| 39 | + this.participants.Add(participant, { isStarter: false }); |
| 40 | + } else if (participantInstance?.assignee) { |
| 41 | + // If the participant has an assignee, calculate the position of the ctor and store it only. |
| 42 | + // Let's say the participant name is `"${assignee}:${type}"`, we need to get the position of ${type} |
| 43 | + // e.g. ret = new A() "ret:A".method() |
| 44 | + const range = node.getRange(); |
| 45 | + const start = range[0] + participantInstance.assignee.length + 2; |
| 46 | + const position: [number, number] = [start, range[1]]; |
| 47 | + const assigneePosition: [number, number] = [ |
| 48 | + range[0] + 1, |
| 49 | + range[0] + participantInstance.assignee.length + 1, |
| 50 | + ]; |
| 51 | + this.participants.Add(participant, { |
| 52 | + isStarter: false, |
| 53 | + position: position, |
| 54 | + assigneePosition: assigneePosition, |
| 55 | + }); |
| 56 | + } else { |
| 57 | + this.participants.Add(participant, { |
| 58 | + isStarter: false, |
| 59 | + position: node.getRange(), |
| 60 | + }); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + ParticipantNode(node: ParticipantNode): void { |
| 65 | + if (this.shouldSkip) return; |
| 66 | + |
| 67 | + this.participants.Add(node.getName(), { |
| 68 | + isStarter: node.isStarter(), |
| 69 | + type: node.getType(), |
| 70 | + stereotype: node.getStereotype(), |
| 71 | + width: node.getWidth(), |
| 72 | + groupId: this.groupId || node.getGroupId(), |
| 73 | + label: node.getLabel(), |
| 74 | + explicit: node.isExplicit(), |
| 75 | + color: node.getColor(), |
| 76 | + comment: node.getComment(), |
| 77 | + position: node.getRange(), |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + MessageNode(node: MessageNode): void { |
| 82 | + if (this.shouldSkip) return; |
| 83 | + |
| 84 | + const from = node.getFrom(); |
| 85 | + const to = node.getTo(); |
| 86 | + |
| 87 | + if (from) { |
| 88 | + this.participants.Add(from, { |
| 89 | + isStarter: false, |
| 90 | + position: node.getRange(), |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + if (to) { |
| 95 | + const participantInstance = this.participants.Get(to); |
| 96 | + if (participantInstance?.label) { |
| 97 | + this.participants.Add(to, { isStarter: false }); |
| 98 | + } else if (participantInstance?.assignee) { |
| 99 | + // Handle assignee position calculation similar to ToCollector |
| 100 | + const range = node.getRange(); |
| 101 | + if (range) { |
| 102 | + const start = range[0] + participantInstance.assignee.length + 2; |
| 103 | + const position: [number, number] = [start, range[1]]; |
| 104 | + const assigneePosition: [number, number] = [ |
| 105 | + range[0] + 1, |
| 106 | + range[0] + participantInstance.assignee.length + 1, |
| 107 | + ]; |
| 108 | + this.participants.Add(to, { |
| 109 | + isStarter: false, |
| 110 | + position: position, |
| 111 | + assigneePosition: assigneePosition, |
| 112 | + }); |
| 113 | + } |
| 114 | + } else { |
| 115 | + this.participants.Add(to, { |
| 116 | + isStarter: false, |
| 117 | + position: node.getRange(), |
| 118 | + }); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + CreationNode(node: CreationNode): void { |
| 124 | + if (this.shouldSkip) return; |
| 125 | + |
| 126 | + const owner = node.getOwner(); |
| 127 | + const assignee = node.getAssignee(); |
| 128 | + const assigneePosition = node.getAssigneePosition(); |
| 129 | + |
| 130 | + const participantInstance = this.participants.Get(owner); |
| 131 | + |
| 132 | + if (!participantInstance?.label) { |
| 133 | + this.participants.Add(owner, { |
| 134 | + isStarter: false, |
| 135 | + position: node.getRange(), |
| 136 | + assignee, |
| 137 | + assigneePosition, |
| 138 | + }); |
| 139 | + } else { |
| 140 | + this.participants.Add(owner, { |
| 141 | + isStarter: false, |
| 142 | + }); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + GroupNode(node: GroupNode): void { |
| 147 | + this.groupId = node.getText(); |
| 148 | + } |
| 149 | + |
| 150 | + RetNode(node: RetNode): void { |
| 151 | + if (node.getAsyncMessage()) { |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + const returnFrom = node.getFrom(); |
| 156 | + const returnTo = node.getTo(); |
| 157 | + |
| 158 | + if (returnFrom) { |
| 159 | + this.participants.Add(returnFrom, { |
| 160 | + isStarter: false, |
| 161 | + position: node.getRange(), |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + if (returnTo) { |
| 166 | + this.participants.Add(returnTo, { |
| 167 | + isStarter: false, |
| 168 | + position: node.getRange(), |
| 169 | + }); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + ParametersNode(_: SequenceASTNode): void { |
| 174 | + this.shouldSkip = true; |
| 175 | + } |
| 176 | + |
| 177 | + ConditionNode(_: SequenceASTNode): void { |
| 178 | + this.shouldSkip = true; |
| 179 | + } |
| 180 | + |
| 181 | + postVisitNode(node: SequenceASTNode): void { |
| 182 | + super.postVisitNode(node); |
| 183 | + |
| 184 | + const nodeType = node.getType(); |
| 185 | + |
| 186 | + // Handle group exit |
| 187 | + if (nodeType === 'GroupNode') { |
| 188 | + this.groupId = undefined; |
| 189 | + } |
| 190 | + |
| 191 | + // Exit blind mode |
| 192 | + if (nodeType === 'ParametersNode' || nodeType === 'ConditionNode') { |
| 193 | + this.shouldSkip = false; |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + traverseNode?(node: SequenceASTNode): void { |
| 198 | + // Default traversal implementation if needed |
| 199 | + } |
| 200 | + |
| 201 | + reset(): void { |
| 202 | + this.participants = new Participants(); |
| 203 | + this.groupId = undefined; |
| 204 | + this.shouldSkip = false; |
| 205 | + this.shouldSkip = false; |
| 206 | + } |
| 207 | + |
| 208 | + result(): Participants { |
| 209 | + return this.participants; |
| 210 | + } |
| 211 | +} |
0 commit comments