Skip to content

Commit

Permalink
feat: Add completion for map group names
Browse files Browse the repository at this point in the history
Add completion for map group names in `cfgeventgroups.xml`.
  • Loading branch information
rvost committed Sep 10, 2024
1 parent 7468b03 commit 0e506bc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private void registerCompletionParticipants(XMLExtensionsRegistry registry, Dayz
completionParticipants.add(new CfgRandomPresetsCompletionParticipant(missionService));
completionParticipants.add(new EventsCompletionParticipant(missionService));
completionParticipants.add(new CfgEventSpawnsCompletionParticipant(missionService));
completionParticipants.add(new CfgEventGroupsCompletionParticipant(missionService));
completionParticipants.add(new CfgEnvironmentCompletionParticipant(missionService));
completionParticipants.add(new MapGroupProtoCompletionParticipant(missionService));
completionParticipants.add(new MapGroupPosCompletionParticipant(missionService));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.rvost.lemminx.dayz.participants.completion;

import io.github.rvost.lemminx.dayz.DayzMissionService;
import io.github.rvost.lemminx.dayz.model.CfgEventGroupsModel;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.services.extensions.completion.CompletionParticipantAdapter;
import org.eclipse.lemminx.services.extensions.completion.ICompletionRequest;
import org.eclipse.lemminx.services.extensions.completion.ICompletionResponse;
import org.eclipse.lsp4j.CompletionItemKind;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;

public class CfgEventGroupsCompletionParticipant extends CompletionParticipantAdapter {
private final DayzMissionService missionService;

public CfgEventGroupsCompletionParticipant(DayzMissionService missionService) {
this.missionService = missionService;
}

@Override
public void onAttributeValue(String valuePrefix, ICompletionRequest request, ICompletionResponse response, CancelChecker cancelChecker) throws Exception {
var doc = request.getXMLDocument();
if (CfgEventGroupsModel.isCfgEventGroups(doc)) {
computeMapGroupCompletion(request, response, doc);
}
}

private void computeMapGroupCompletion(ICompletionRequest request, ICompletionResponse response, DOMDocument document) throws BadLocationException {
var editRange = request.getReplaceRange();
var offset = document.offsetAt(editRange.getStart());
var node = document.findNodeAt(offset);
var attr = node.findAttrAt(offset);

if (CfgEventGroupsModel.TYPE_ATTRIBUTE.equals(attr.getName()) &&
!node.hasAttribute(CfgEventGroupsModel.SPAWNSECONDARY_ATTRIBUTE)) {
var options = missionService.getMapGroups();
options.stream()
.map(option -> CompletionUtils.toCompletionItem(option, request, editRange, CompletionItemKind.Class))
.forEach(response::addCompletionItem);
}
}
}

0 comments on commit 0e506bc

Please sign in to comment.