|
| 1 | +import { DecorationOptions, TextEditorDecorationType, Uri, window } from 'vscode'; |
| 2 | +import path from 'path'; |
| 3 | +import TextDocumentDecorationProvider from './TextDocumentDecorationProvider'; |
| 4 | +import { PhpClass } from 'parser/php/PhpClass'; |
| 5 | +import { PhpInterface } from 'parser/php/PhpInterface'; |
| 6 | +import ObserverClassInfo from 'common/php/ObserverClassInfo'; |
| 7 | +import PhpDocumentParser from 'common/php/PhpDocumentParser'; |
| 8 | +import { ClasslikeInfo } from 'common/php/ClasslikeInfo'; |
| 9 | +import MarkdownMessageBuilder from 'common/MarkdownMessageBuilder'; |
| 10 | +import Position from 'util/Position'; |
| 11 | +import { NodeKind } from 'parser/php/Parser'; |
| 12 | +import IndexManager from 'indexer/IndexManager'; |
| 13 | +import EventsIndexer from 'indexer/events/EventsIndexer'; |
| 14 | + |
| 15 | +export default class ObserverInstanceDecorationProvider extends TextDocumentDecorationProvider { |
| 16 | + public getType(): TextEditorDecorationType { |
| 17 | + return window.createTextEditorDecorationType({ |
| 18 | + gutterIconPath: path.join(__dirname, 'resources', 'icons', 'observer.svg'), |
| 19 | + gutterIconSize: '80%', |
| 20 | + borderColor: 'rgba(0, 188, 202, 0.5)', |
| 21 | + borderStyle: 'dotted', |
| 22 | + borderWidth: '0 0 1px 0', |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + public async getDecorations(): Promise<DecorationOptions[]> { |
| 27 | + const decorations: DecorationOptions[] = []; |
| 28 | + const phpFile = await PhpDocumentParser.parse(this.document); |
| 29 | + |
| 30 | + const classLikeNode: PhpClass | PhpInterface | undefined = |
| 31 | + phpFile.classes[0] || phpFile.interfaces[0]; |
| 32 | + |
| 33 | + if (!classLikeNode) { |
| 34 | + return decorations; |
| 35 | + } |
| 36 | + |
| 37 | + const observerClassInfo = new ObserverClassInfo(phpFile); |
| 38 | + const classlikeInfo = new ClasslikeInfo(phpFile); |
| 39 | + |
| 40 | + if (this.isObserverInstance(classlikeInfo)) { |
| 41 | + decorations.push(...this.getObserverInstanceDecorations(observerClassInfo, classlikeInfo)); |
| 42 | + } |
| 43 | + |
| 44 | + decorations.push(...this.getEventDispatchDecorations(observerClassInfo)); |
| 45 | + |
| 46 | + return decorations; |
| 47 | + } |
| 48 | + |
| 49 | + private getEventDispatchDecorations(observerClassInfo: ObserverClassInfo): DecorationOptions[] { |
| 50 | + const decorations: DecorationOptions[] = []; |
| 51 | + |
| 52 | + const eventDispatchCalls = observerClassInfo.getEventDispatchCalls(); |
| 53 | + |
| 54 | + if (eventDispatchCalls.length === 0) { |
| 55 | + return decorations; |
| 56 | + } |
| 57 | + |
| 58 | + const eventsIndexData = IndexManager.getIndexData(EventsIndexer.KEY); |
| 59 | + |
| 60 | + if (!eventsIndexData) { |
| 61 | + return decorations; |
| 62 | + } |
| 63 | + |
| 64 | + for (const eventDispatchCall of eventDispatchCalls) { |
| 65 | + const args = eventDispatchCall.arguments; |
| 66 | + |
| 67 | + if (args.length === 0) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + const firstArg = args[0]; |
| 72 | + |
| 73 | + if (!firstArg || firstArg.kind !== NodeKind.String || !firstArg.loc) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + const eventName = (firstArg as any).value; |
| 78 | + |
| 79 | + const event = eventsIndexData.getEventByName(eventName); |
| 80 | + |
| 81 | + if (!event) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + const range = Position.phpAstLocationToVsCodeRange(firstArg.loc); |
| 86 | + const hoverMessage = MarkdownMessageBuilder.create('Event observers'); |
| 87 | + |
| 88 | + for (const observer of event.observers) { |
| 89 | + hoverMessage.appendMarkdown(`- [${observer.instance}](${Uri.file(event.diPath)})\n`); |
| 90 | + } |
| 91 | + |
| 92 | + decorations.push({ |
| 93 | + range, |
| 94 | + hoverMessage: hoverMessage.build(), |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + return decorations; |
| 99 | + } |
| 100 | + |
| 101 | + private getObserverInstanceDecorations( |
| 102 | + observerClassInfo: ObserverClassInfo, |
| 103 | + classlikeInfo: ClasslikeInfo |
| 104 | + ): DecorationOptions[] { |
| 105 | + const decorations: DecorationOptions[] = []; |
| 106 | + |
| 107 | + const observerEvents = observerClassInfo.getObserverEvents(); |
| 108 | + |
| 109 | + if (observerEvents.length === 0) { |
| 110 | + return decorations; |
| 111 | + } |
| 112 | + |
| 113 | + const nameRange = classlikeInfo.getNameRange(); |
| 114 | + |
| 115 | + if (!nameRange) { |
| 116 | + return decorations; |
| 117 | + } |
| 118 | + |
| 119 | + const hoverMessage = MarkdownMessageBuilder.create('Observer Events'); |
| 120 | + |
| 121 | + for (const event of observerEvents) { |
| 122 | + hoverMessage.appendMarkdown(`- [${event.name}](${Uri.file(event.diPath)})\n`); |
| 123 | + } |
| 124 | + |
| 125 | + decorations.push({ |
| 126 | + range: nameRange, |
| 127 | + hoverMessage: hoverMessage.build(), |
| 128 | + }); |
| 129 | + |
| 130 | + return decorations; |
| 131 | + } |
| 132 | + |
| 133 | + private isObserverInstance(classlikeInfo: ClasslikeInfo): boolean { |
| 134 | + const imp = classlikeInfo.getImplements(); |
| 135 | + |
| 136 | + return ( |
| 137 | + imp.includes('Magento\\Framework\\Event\\ObserverInterface') || |
| 138 | + imp.includes('ObserverInterface') |
| 139 | + ); |
| 140 | + } |
| 141 | +} |
0 commit comments