Skip to content

Commit

Permalink
Feature request: buttons to associate XML documents with schema files
Browse files Browse the repository at this point in the history
Fixes eclipse#395

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jun 3, 2021
1 parent 4497ae5 commit 8bf302f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.lemminx.extensions.contentmodel.commands.XMLValidationFileCommand;
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager;
import org.eclipse.lemminx.extensions.contentmodel.participants.ContentModelCodeActionParticipant;
import org.eclipse.lemminx.extensions.contentmodel.participants.ContentModelCodeLensParticipant;
import org.eclipse.lemminx.extensions.contentmodel.participants.ContentModelCompletionParticipant;
import org.eclipse.lemminx.extensions.contentmodel.participants.ContentModelDocumentLinkParticipant;
import org.eclipse.lemminx.extensions.contentmodel.participants.ContentModelHoverParticipant;
Expand All @@ -36,6 +37,7 @@
import org.eclipse.lemminx.services.extensions.ITypeDefinitionParticipant;
import org.eclipse.lemminx.services.extensions.IXMLExtension;
import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lemminx.services.extensions.codelens.ICodeLensParticipant;
import org.eclipse.lemminx.services.extensions.commands.IXMLCommandService;
import org.eclipse.lemminx.services.extensions.diagnostics.IDiagnosticsParticipant;
import org.eclipse.lemminx.services.extensions.save.ISaveContext;
Expand Down Expand Up @@ -68,6 +70,8 @@ public class ContentModelPlugin implements IXMLExtension {

private ContentModelSymbolsProviderParticipant symbolsProviderParticipant;

private final ICodeLensParticipant codeLensParticipant;

ContentModelManager contentModelManager;

private ContentModelSettings cmSettings;
Expand All @@ -80,6 +84,7 @@ public ContentModelPlugin() {
diagnosticsParticipant = new ContentModelDiagnosticsParticipant(this);
codeActionParticipant = new ContentModelCodeActionParticipant();
typeDefinitionParticipant = new ContentModelTypeDefinitionParticipant();
codeLensParticipant = new ContentModelCodeLensParticipant();
}

@Override
Expand Down Expand Up @@ -176,7 +181,8 @@ public void start(InitializeParams params, XMLExtensionsRegistry registry) {
registry.registerTypeDefinitionParticipant(typeDefinitionParticipant);
symbolsProviderParticipant = new ContentModelSymbolsProviderParticipant(contentModelManager);
registry.registerSymbolsProviderParticipant(symbolsProviderParticipant);

registry.registerCodeLensParticipant(codeLensParticipant);

// Register custom commands to re-validate XML files
IXMLCommandService commandService = registry.getCommandService();
if (commandService != null) {
Expand All @@ -198,7 +204,8 @@ public void stop(XMLExtensionsRegistry registry) {
registry.unregisterDocumentLinkParticipant(documentLinkParticipant);
registry.unregisterTypeDefinitionParticipant(typeDefinitionParticipant);
registry.unregisterSymbolsProviderParticipant(symbolsProviderParticipant);

registry.unregisterCodeLensParticipant(codeLensParticipant);

// Un-register custom commands to re-validate XML files
IXMLCommandService commandService = registry.getCommandService();
if (commandService != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*******************************************************************************
* Copyright (c) 2021 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;

import java.util.Arrays;
import java.util.List;

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.services.extensions.codelens.ICodeLensParticipant;
import org.eclipse.lemminx.services.extensions.codelens.ICodeLensRequest;
import org.eclipse.lemminx.utils.XMLPositionUtility;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;

public class ContentModelCodeLensParticipant implements ICodeLensParticipant {

private static final String COMMAND_ID = "xml.associate.grammar.selectFile";

@Override
public void doCodeLens(ICodeLensRequest request, List<CodeLens> lenses, CancelChecker cancelChecker) {
DOMDocument document = request.getDocument();
DOMElement documentElement = document.getDocumentElement();
if (documentElement == null || document.hasGrammar()) {
return;
}
String documentURI = document.getDocumentURI();
Range range = XMLPositionUtility.selectRootStartTag(document);

lenses.add(createAssociateLens(documentURI, "Associate with XSD and schemaLocation", "XSD", "schemaLocation",
range));
lenses.add(createAssociateLens(documentURI, "Associate with XSD and xml-model", "XSD", "xml-model", range));
lenses.add(createAssociateLens(documentURI, "Associate with DTD and DOCTYPE", "DTD", "DOCTYPE", range));
lenses.add(createAssociateLens(documentURI, "Associate with DTD and xml-model", "DTD", "xml-model", range));
}

private static CodeLens createAssociateLens(String documentURI, String title, String type, String bindingKind,
Range range) {
Command command = new Command(title, COMMAND_ID, Arrays.asList(documentURI, type, bindingKind));
return new CodeLens(range, command, null);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.xerces.xni.XMLLocator;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMDocumentType;
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.dom.DOMRange;
import org.eclipse.lemminx.extensions.contentmodel.participants.codeactions.ElementDeclUnterminatedCodeAction;
Expand Down

0 comments on commit 8bf302f

Please sign in to comment.