Skip to content

feat: event name autocomplete #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the "magento-toolbox" extension will be documented in thi

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [Unreleased]

- Added: Event name autocomplete

## [1.5.0] - 2025-04-06
- Added: Class namespace autocomplete in XML files
- Added: Module name autocomplete in module.xml files
Expand Down
2 changes: 2 additions & 0 deletions src/completion/XmlCompletionProviderProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ModuleCompletionProvider } from './xml/ModuleCompletionProvider';
import { NamespaceCompletionProvider } from './xml/NamespaceCompletionProvider';
import { AclCompletionProvider } from './xml/AclCompletionProvider';
import { TemplateCompletionProvider } from './xml/TemplateCompletionProvider';
import { EventCompletionProvider } from './xml/EventCompletionProvider';

export class XmlCompletionProviderProcessor
extends XmlSuggestionProviderProcessor<CompletionItem>
Expand All @@ -19,6 +20,7 @@ export class XmlCompletionProviderProcessor
new NamespaceCompletionProvider(),
new AclCompletionProvider(),
new TemplateCompletionProvider(),
new EventCompletionProvider(),
]);
}

Expand Down
47 changes: 47 additions & 0 deletions src/completion/xml/EventCompletionProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { TextDocument, CompletionItem, CompletionItemKind, Range } from 'vscode';
import IndexManager from 'indexer/IndexManager';
import { XmlSuggestionProvider, CombinedCondition } from 'common/xml/XmlSuggestionProvider';
import { XMLElement, XMLAttribute } from '@xml-tools/ast';
import { AttributeNameMatches } from 'common/xml/suggestion/condition/AttributeNameMatches';
import { ElementNameMatches } from 'common/xml/suggestion/condition/ElementNameMatches';
import EventsIndexer from 'indexer/events/EventsIndexer';

export class EventCompletionProvider extends XmlSuggestionProvider<CompletionItem> {
public getFilePatterns(): string[] {
return ['**/etc/events.xml'];
}

public getAttributeValueConditions(): CombinedCondition[] {
return [[new ElementNameMatches('event'), new AttributeNameMatches('name')]];
}

public getConfigKey(): string | undefined {
return 'provideXmlDefinitions';
}

public getSuggestionItems(
value: string,
range: Range,
document: TextDocument,
element: XMLElement,
attribute?: XMLAttribute
): CompletionItem[] {
const eventIndexData = IndexManager.getIndexData(EventsIndexer.KEY);

if (!eventIndexData) {
return [];
}

const events = eventIndexData.getEventsByPrefix(value);

if (!events) {
return [];
}

return events.map(event => {
const item = new CompletionItem(event.name, CompletionItemKind.Value);
item.range = range;
return item;
});
}
}
6 changes: 6 additions & 0 deletions src/indexer/events/EventsIndexData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Memoize } from 'typescript-memoize';
import EventsIndexer from './EventsIndexer';
import { Event } from './types';
import { AbstractIndexData } from 'indexer/AbstractIndexData';
import { uniqBy } from 'lodash-es';

export class EventsIndexData extends AbstractIndexData<Event[]> {
@Memoize({ tags: [EventsIndexer.KEY] })
Expand All @@ -17,6 +18,11 @@ export class EventsIndexData extends AbstractIndexData<Event[]> {
return this.getEvents().find(event => event.name === eventName);
}

public getEventsByPrefix(prefix: string): Event[] {
const events = this.getEvents().filter(event => event.name.startsWith(prefix));
return uniqBy(events, 'name');
}

public findEventsByObserverInstance(observerInstance: string): Event[] {
return this.getEvents().filter(event =>
event.observers.some(observer => observer.instance === observerInstance)
Expand Down