Skip to content

Commit

Permalink
Refactor "Register catalog" code lens to be in catalog package
Browse files Browse the repository at this point in the history
Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 authored and angelozerr committed Dec 16, 2022
1 parent 990bffe commit 486955a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*******************************************************************************
* 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
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.extensions.catalog;

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

import org.eclipse.lemminx.client.ClientCommands;
import org.eclipse.lemminx.commons.config.ConfigurationItemEdit;
import org.eclipse.lemminx.commons.config.ConfigurationItemEditType;
import org.eclipse.lemminx.commons.config.ConfigurationItemValueKind;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager;
import org.eclipse.lemminx.services.extensions.codelens.ICodeLensParticipant;
import org.eclipse.lemminx.services.extensions.codelens.ICodeLensRequest;
import org.eclipse.lemminx.utils.DOMUtils;
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;

/**
* This code lens participant creates the "(un)register catalog" code lens in
* catalog files.
*
* When clicked, this catalog runs the client command
* <code>"xml.update.configuration"</code>
* to update the workspace settings to register the catalog for use.
* The client needs to implement the <code>"xml.update.configuration"</code>
* command in order for this to work.
*/
public class XMLCatalogCodeLensParticipant implements ICodeLensParticipant {

private final ContentModelManager contentModelManager;

public XMLCatalogCodeLensParticipant(ContentModelManager contentModelManager) {
this.contentModelManager = contentModelManager;
}

@Override
public void doCodeLens(ICodeLensRequest request, List<CodeLens> lenses, CancelChecker cancelChecker) {
// Register/unregister catalog
createRegisterCatalogLenses(request, lenses);
}

private void createRegisterCatalogLenses(ICodeLensRequest request, List<CodeLens> lenses) {
DOMDocument document = request.getDocument();
if (!DOMUtils.isCatalog(document)) {
return;
}
String documentURI = contentModelManager.expandSystemId(document.getDocumentURI());
Range range = XMLPositionUtility.selectRootStartTag(document);
String[] catalogs = contentModelManager.getCatalogs();
if (catalogs == null || !Arrays.asList(catalogs).contains(documentURI)) {
// When a catalog is not registered in settings.json, [Register Catalog]
// CodeLens appears:

// [Register Catalog]
// <catalog ...>
ConfigurationItemEdit configurationItemEdit = new ConfigurationItemEdit("xml.catalogs",
documentURI, ConfigurationItemEditType.Add, ConfigurationItemValueKind.File);

Command command = new Command("Register Catalog", ClientCommands.UPDATE_CONFIGURATION,
Arrays.asList(configurationItemEdit));
lenses.add(new CodeLens(range, command, null));
} else {
// When a catalog is already registered in settings.json, [Unregister Catalog]
// CodeLens appears:

// [Unregister Catalog]
// <catalog ...>
ConfigurationItemEdit configurationItemEdit = new ConfigurationItemEdit("xml.catalogs",
documentURI, ConfigurationItemEditType.Delete, ConfigurationItemValueKind.File);
Command command = new Command("Unregister Catalog", ClientCommands.UPDATE_CONFIGURATION,
Arrays.asList(configurationItemEdit));
lenses.add(new CodeLens(range, command, null));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

import org.eclipse.lemminx.client.InvalidPathWarner;
import org.eclipse.lemminx.client.PathFeature;
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager;
import org.eclipse.lemminx.extensions.contentmodel.settings.ContentModelSettings;
import org.eclipse.lemminx.services.IXMLNotificationService;
import org.eclipse.lemminx.services.extensions.IDocumentLinkParticipant;
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.diagnostics.IDiagnosticsParticipant;
import org.eclipse.lemminx.services.extensions.save.ISaveContext;
import org.eclipse.lemminx.utils.FilesUtils;
Expand All @@ -37,6 +39,7 @@ public class XMLCatalogPlugin implements IXMLExtension {
private XMLCatalogURIResolverExtension uiResolver;
private final IDocumentLinkParticipant documentLinkParticipant;
private final IDiagnosticsParticipant diagnosticsParticipant;
private ICodeLensParticipant codeLensParticipant;

private InvalidPathWarner pathWarner;

Expand All @@ -57,6 +60,8 @@ public void doSave(ISaveContext context) {

@Override
public void start(InitializeParams params, XMLExtensionsRegistry registry) {
ContentModelManager contentModelManager = registry.getComponent(ContentModelManager.class);
codeLensParticipant = new XMLCatalogCodeLensParticipant(contentModelManager);
uiResolver = new XMLCatalogURIResolverExtension(registry);
registry.getResolverExtensionManager().registerResolver(uiResolver);
IXMLNotificationService notificationService = registry.getNotificationService();
Expand All @@ -65,12 +70,14 @@ public void start(InitializeParams params, XMLExtensionsRegistry registry) {
}
registry.registerDocumentLinkParticipant(documentLinkParticipant);
registry.registerDiagnosticsParticipant(diagnosticsParticipant);
registry.registerCodeLensParticipant(codeLensParticipant);
}

@Override
public void stop(XMLExtensionsRegistry registry) {
registry.getResolverExtensionManager().unregisterResolver(uiResolver);
registry.unregisterDiagnosticsParticipant(diagnosticsParticipant);
registry.unregisterCodeLensParticipant(codeLensParticipant);
}

private void validateCatalogPaths(ContentModelSettings cmSettings) {
Expand All @@ -81,7 +88,7 @@ private void validateCatalogPaths(ContentModelSettings cmSettings) {
Set<String> invalidCatalogs = Arrays.stream(catalogs).filter(c -> {
return Files.notExists(FilesUtils.getPath(c));
}).collect(Collectors.toSet());

if (invalidCatalogs.size() > 0) {
this.pathWarner.onInvalidFilePath(invalidCatalogs, PathFeature.CATALOGS);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@

import static org.eclipse.lemminx.client.ClientCommands.OPEN_BINDING_WIZARD;
import static org.eclipse.lemminx.client.ClientCommands.OPEN_URI;
import static org.eclipse.lemminx.client.ClientCommands.UPDATE_CONFIGURATION;

import static org.eclipse.lemminx.extensions.contentmodel.commands.CheckBoundGrammarCommand.canBindWithGrammar;

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

import org.eclipse.lemminx.client.CodeLensKind;
import org.eclipse.lemminx.commons.config.ConfigurationItemEdit;
import org.eclipse.lemminx.commons.config.ConfigurationItemEditType;
import org.eclipse.lemminx.commons.config.ConfigurationItemValueKind;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMRange;
import org.eclipse.lemminx.extensions.contentmodel.commands.AssociateGrammarCommand;
Expand Down Expand Up @@ -86,8 +81,6 @@ public void doCodeLens(ICodeLensRequest request, List<CodeLens> lenses, CancelCh
createReferencedGrammarLenses(request, lenses);
// "Bind to grammar/schema..."
createBindToGrammarSchemaLenses(request, lenses);
// Register/unregister catalog
createRegisterCatalogLenses(request, lenses);
}

private void createReferencedGrammarLenses(ICodeLensRequest request, List<CodeLens> lenses) {
Expand All @@ -114,40 +107,6 @@ private void createReferencedGrammarLenses(ICodeLensRequest request, List<CodeLe
}
}

private void createRegisterCatalogLenses(ICodeLensRequest request, List<CodeLens> lenses) {
DOMDocument document = request.getDocument();
if (!DOMUtils.isCatalog(document)) {
return;
}
String documentURI = contentModelManager.expandSystemId(document.getDocumentURI());
Range range = XMLPositionUtility.selectRootStartTag(document);
String[] catalogs = contentModelManager.getCatalogs();
if (catalogs == null || !Arrays.asList(catalogs).contains(documentURI)) {
// When a catalog is not registered in settings.json, [Register Catalog]
// CodeLens appears:

// [Register Catalog]
// <catalog ...>
ConfigurationItemEdit configurationItemEdit = new ConfigurationItemEdit("xml.catalogs",
documentURI, ConfigurationItemEditType.Add, ConfigurationItemValueKind.File);

Command command = new Command("Register Catalog", UPDATE_CONFIGURATION,
Arrays.asList(configurationItemEdit));
lenses.add(new CodeLens(range, command, null));
} else {
// When a catalog is already registered in settings.json, [Unregister Catalog]
// CodeLens appears:

// [Unregister Catalog]
// <catalog ...>
ConfigurationItemEdit configurationItemEdit = new ConfigurationItemEdit("xml.catalogs",
documentURI, ConfigurationItemEditType.Delete, ConfigurationItemValueKind.File);
Command command = new Command("Unregister Catalog", UPDATE_CONFIGURATION,
Arrays.asList(configurationItemEdit));
lenses.add(new CodeLens(range, command, null));
}
}

private static boolean canSupportOpenUri(ICodeLensRequest request) {
return request.isSupportedByClient(CodeLensKind.OpenUri);
}
Expand Down

0 comments on commit 486955a

Please sign in to comment.