Skip to content
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

Add code action to generate RelaxNG grammar #1426

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.extensions.contentmodel.participants.codeactions.nogrammarconstraints;

import static org.eclipse.lemminx.extensions.contentmodel.participants.codeactions.nogrammarconstraints.NoGrammarConstraintsCodeAction.createXmlModelEdit;

import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.extensions.generators.FileContentGeneratorSettings;
import org.eclipse.lemminx.extensions.generators.xml2relaxng.RelaxNGGeneratorSettings;
import org.eclipse.lemminx.settings.SharedSettings;
import org.eclipse.lsp4j.TextDocumentEdit;

/**
* Code action resolver participant used to:
*
* <ul>
* <li>generate the RelaxNG file for the given DOM document</li>
* <li>generate the association relaxng in the XML to bind it with the
* generated RelaxNG schema</li>
*
* </ul>
*
*/
public class GenerateRelaxNGSchemaCodeActionResolver extends AbstractGenerateGrammarAndAssociationResolveCodeActionParticipant {

public static final String PARTICIPANT_ID = GenerateRelaxNGSchemaCodeActionResolver.class.getName();

@Override
protected TextDocumentEdit createFileEdit(String grammarFileName, DOMDocument document,
SharedSettings sharedSettings) throws BadLocationException {
return createXmlModelEdit(grammarFileName, null, document, sharedSettings);
}

@Override
protected FileContentGeneratorSettings getFileContentGeneratorSettings() {
return new RelaxNGGeneratorSettings();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.extensions.generators.FileContentGeneratorManager;
import org.eclipse.lemminx.extensions.generators.xml2dtd.DTDGeneratorSettings;
import org.eclipse.lemminx.extensions.generators.xml2relaxng.RelaxNGGeneratorSettings;
import org.eclipse.lemminx.extensions.generators.xml2xsd.XMLSchemaGeneratorSettings;
import org.eclipse.lemminx.services.data.DataEntryField;
import org.eclipse.lemminx.services.extensions.codeaction.ICodeActionParticipant;
Expand Down Expand Up @@ -70,6 +71,8 @@ public NoGrammarConstraintsCodeAction() {
new GenerateDocTypeCodeActionResolver());
resolveCodeActionParticipants.put(GenerateXMLModelWithDTDCodeActionResolver.PARTICIPANT_ID,
new GenerateXMLModelWithDTDCodeActionResolver());
resolveCodeActionParticipants.put(GenerateRelaxNGSchemaCodeActionResolver.PARTICIPANT_ID,
new GenerateRelaxNGSchemaCodeActionResolver());
}

@Override
Expand Down Expand Up @@ -125,6 +128,22 @@ public void doCodeAction(ICodeActionRequest request, List<CodeAction> codeAction
GenerateXMLModelWithDTDCodeActionResolver.PARTICIPANT_ID, request);
codeActions.add(dtdWithXmlModelAction);

// ---------- Relax NG

String rngURI = getGrammarURI(document.getDocumentURI(), "rng");
String rngFileName = getFileName(rngURI);
String rngTemplate = null;
if (!request.canSupportResolve()) {
SharedSettings sharedSettings = request.getSharedSettings();
FileContentGeneratorManager generator = request.getComponent(FileContentGeneratorManager.class);
rngTemplate = generator.generate(document, sharedSettings, new RelaxNGGeneratorSettings(), cancelChecker);
}

// xml-model
CodeAction relaxNGWithXmlModelAction = createRelaxNGCodeAction(rngURI, rngFileName, rngTemplate, request,
cancelChecker);
codeActions.add(relaxNGWithXmlModelAction);

// ---------- Open Binding Wizard
SharedSettings sharedSettings = request.getSharedSettings();
if (sharedSettings.isBindingWizardSupport()) {
Expand Down Expand Up @@ -192,6 +211,20 @@ private CodeAction createDocTypeCodeAction(String dtdURI, String dtdFileName, St
return createGrammarFileAndBindIt(title, dtdURI, dtdTemplate, dtdWithDocType, diagnostic);
}

private CodeAction createRelaxNGCodeAction(String rngURI, String rngFileName, String rngTemplate,
ICodeActionRequest request, CancelChecker cancelChecker) throws BadLocationException {
Diagnostic diagnostic = request.getDiagnostic();
DOMDocument document = request.getDocument();
String title = "Generate '" + rngFileName + "' and bind with RelaxNG";
if (request.canSupportResolve()) {
return createGenerateFileUnresolvedCodeAction(rngURI, title, diagnostic, document,
GenerateRelaxNGSchemaCodeActionResolver.PARTICIPANT_ID);
}
SharedSettings sharedSettings = request.getSharedSettings();
TextDocumentEdit relaxNGWithXMLModelEdit = createXmlModelEdit(rngFileName, null, document, sharedSettings);
return createGrammarFileAndBindIt(title, rngURI, rngTemplate, relaxNGWithXMLModelEdit, diagnostic);
}

private static CodeAction createGenerateFileUnresolvedCodeAction(String generateFileURI, String title,
Diagnostic diagnostic, DOMDocument document, String participantId) {
CodeAction codeAction = new CodeAction(title);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.eclipse.lemminx.extensions.generators.xml2dtd.XML2DTDGenerator;
import org.eclipse.lemminx.extensions.generators.xml2xsd.XML2XMLSchemaGenerator;
import org.eclipse.lemminx.extensions.generators.xml2xsd.XMLSchemaGeneratorSettings;
import org.eclipse.lemminx.extensions.generators.xml2relaxng.XML2RelaxNGGenerator;
import org.eclipse.lemminx.extensions.generators.xml2relaxng.RelaxNGGeneratorSettings;
import org.eclipse.lemminx.services.IXMLFullFormatter;
import org.eclipse.lemminx.settings.SharedSettings;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
Expand Down Expand Up @@ -52,6 +54,7 @@ public FileContentGeneratorManager(IXMLFullFormatter formatter) {
private void registerDefaultGenerators() {
registerGenerator(new XML2DTDGenerator(), DTDGeneratorSettings.class);
registerGenerator(new XML2XMLSchemaGenerator(), XMLSchemaGeneratorSettings.class);
registerGenerator(new XML2RelaxNGGenerator(), RelaxNGGeneratorSettings.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.extensions.generators.xml2relaxng;

import org.eclipse.lemminx.extensions.generators.FileContentGeneratorSettings;

/**
* RelaxNG generator settings
*
*/
public class RelaxNGGeneratorSettings extends FileContentGeneratorSettings {

}
Loading