Skip to content

Commit 82534fd

Browse files
committed
fix: grouped plugin messages
1 parent c244464 commit 82534fd

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

src/decorator/PluginClassDecorationProvider.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,19 @@ import path from 'path';
1010
import MarkdownMessageBuilder from 'common/MarkdownMessageBuilder';
1111
import PhpNamespace from 'common/PhpNamespace';
1212
import TextDocumentDecorationProvider from './TextDocumentDecorationProvider';
13-
import PhpParser from 'parser/php/Parser';
1413
import Position from 'util/Position';
1514
import PluginSubjectInfo from 'common/php/PluginSubjectInfo';
1615
import PluginInfo from 'common/php/PluginInfo';
1716
import Magento from 'util/Magento';
1817
import { ClasslikeInfo } from 'common/php/ClasslikeInfo';
1918
import { PhpClass } from 'parser/php/PhpClass';
2019
import { PhpInterface } from 'parser/php/PhpInterface';
20+
import { PhpMethod } from 'parser/php/PhpMethod';
2121
import IndexManager from 'indexer/IndexManager';
2222
import AutoloadNamespaceIndexer from 'indexer/autoload-namespace/AutoloadNamespaceIndexer';
2323
import { AutoloadNamespaceIndexData } from 'indexer/autoload-namespace/AutoloadNamespaceIndexData';
2424
import { DiPlugin } from 'indexer/di/types';
2525
import PhpDocumentParser from 'common/php/PhpDocumentParser';
26-
import Common from 'util/Common';
2726

2827
export default class PluginClassDecorationProvider extends TextDocumentDecorationProvider {
2928
public getType(): TextEditorDecorationType {
@@ -74,55 +73,57 @@ export default class PluginClassDecorationProvider extends TextDocumentDecoratio
7473
hoverMessage,
7574
});
7675

77-
const promises = classPlugins.map(async plugin => {
76+
const methodPlugins: Record<string, { plugin: DiPlugin; subjectMethod: PhpMethod }[]> = {};
77+
78+
for (const plugin of classPlugins) {
7879
const fileUri = await namespaceIndexData.findClassByNamespace(
7980
PhpNamespace.fromString(plugin.type)
8081
);
8182

8283
if (!fileUri) {
83-
return;
84+
continue;
8485
}
8586

8687
const pluginPhpFile = await PhpDocumentParser.parseUri(this.document, fileUri);
8788
const pluginInfo = new PluginInfo(pluginPhpFile);
8889
const pluginMethods = pluginInfo.getPluginMethods(pluginPhpFile.classes[0]);
8990

90-
return pluginMethods.map(method => {
91+
for (const method of pluginMethods) {
9192
const subjectMethodName = Magento.pluginMethodToMethodName(method.name);
92-
93-
if (!subjectMethodName) {
94-
return;
95-
}
96-
9793
const subjectMethod = classlikeInfo.getMethodByName(classLikeNode, subjectMethodName);
9894

9995
if (!subjectMethod) {
100-
return;
96+
continue;
10197
}
10298

103-
if (typeof subjectMethod.ast.name === 'string' || !subjectMethod.ast.name.loc) {
104-
return;
105-
}
99+
methodPlugins[subjectMethod.name] = methodPlugins[subjectMethod.name] || [];
100+
methodPlugins[subjectMethod.name].push({
101+
plugin,
102+
subjectMethod,
103+
});
104+
}
105+
}
106106

107-
const range = new Range(
108-
Position.phpAstPositionToVsCodePosition(subjectMethod.ast.name.loc.start),
109-
Position.phpAstPositionToVsCodePosition(subjectMethod.ast.name.loc.end)
110-
);
107+
for (const [, plugins] of Object.entries(methodPlugins)) {
108+
const hoverMessage = await this.getInterceptorHoverMessage(
109+
plugins.map(p => p.plugin),
110+
namespaceIndexData
111+
);
112+
const subjectMethod = plugins[0].subjectMethod;
111113

112-
const message = MarkdownMessageBuilder.create('Interceptors');
113-
const link = `[${plugin.type}](${fileUri ? Uri.file(fileUri.fsPath) : '#'})`;
114-
message.appendMarkdown(`- ${link} [(di.xml)](${Uri.file(plugin.diPath)})\n`);
114+
if (typeof subjectMethod.ast.name === 'string' || !subjectMethod.ast.name.loc) {
115+
continue;
116+
}
117+
const range = new Range(
118+
Position.phpAstPositionToVsCodePosition(subjectMethod.ast.name.loc.start),
119+
Position.phpAstPositionToVsCodePosition(subjectMethod.ast.name.loc.end)
120+
);
115121

116-
return {
117-
range,
118-
hoverMessage: message.build(),
119-
};
122+
decorations.push({
123+
range,
124+
hoverMessage,
120125
});
121-
});
122-
123-
const pluginMethodsDecorations = await Promise.all(promises);
124-
125-
decorations.push(...(pluginMethodsDecorations.flat().filter(Boolean) as DecorationOptions[]));
126+
}
126127

127128
return decorations;
128129
}
@@ -138,9 +139,8 @@ export default class PluginClassDecorationProvider extends TextDocumentDecoratio
138139
PhpNamespace.fromString(interceptor.type)
139140
);
140141

141-
message.appendMarkdown(
142-
`- [${interceptor.type}](${fileUri ? Uri.file(fileUri.fsPath) : '#'})\n`
143-
);
142+
const link = `[${interceptor.type}](${fileUri ? Uri.file(fileUri.fsPath) : '#'})`;
143+
message.appendMarkdown(`- ${link} [(di.xml)](${Uri.file(interceptor.diPath)})\n`);
144144
}
145145

146146
return message.build();

src/util/Magento.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default class Magento {
99
return /^around|^before|^after/.test(method);
1010
}
1111

12-
public static pluginMethodToMethodName(method: string): string | undefined {
12+
public static pluginMethodToMethodName(method: string): string {
1313
return lowerFirst(method.replace(/^around|^before|^after/, ''));
1414
}
1515

0 commit comments

Comments
 (0)