Skip to content

Commit

Permalink
Add code action to generate RelaxNG grammar
Browse files Browse the repository at this point in the history
Signed-off-by: Jessica He <jhe@redhat.com>
  • Loading branch information
JessicaJHee committed Dec 21, 2022
1 parent 5db338d commit 92a0eec
Show file tree
Hide file tree
Showing 7 changed files with 1,126 additions and 0 deletions.
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

0 comments on commit 92a0eec

Please sign in to comment.