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 codelens and command to register/unregister catalog #1390

Merged
merged 1 commit into from
Dec 11, 2022
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
Expand Up @@ -132,10 +132,21 @@ private static enum TriggeredBy {
/**
* Save context.
*/
class SaveContext extends AbstractSaveContext {
public class SaveContext extends AbstractSaveContext {

private final Collection<ModelTextDocument<DOMDocument>> documentsToValidate;

private boolean isRefreshCodeLenses;

public boolean isRefreshCodeLenses() {
return isRefreshCodeLenses;
}

public void setRefreshCodeLenses(boolean isRefreshCodeLenses) {
this.isRefreshCodeLenses = isRefreshCodeLenses;
return;
}

public SaveContext(Object settings) {
super(settings);
this.documentsToValidate = new ArrayList<>();
Expand Down Expand Up @@ -588,6 +599,9 @@ void doSave(String uri) {
void doSave(SaveContext context) {
getXMLLanguageService().doSave(context);
context.triggerValidationIfNeeded();
if (context.isRefreshCodeLenses()) {
xmlLanguageServer.getLanguageClient().refreshCodeLenses();
}
}

private void triggerValidationFor(Collection<ModelTextDocument<DOMDocument>> documents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ private ClientCommands() {
*/
public static final String OPEN_URI = "xml.open.uri";

/**
* Update configuration
*/
public static final String UPDATE_CONFIGURATION = "xml.update.configuration";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.commons.config;

import org.eclipse.lsp4j.ConfigurationItem;

/**
* Class representing a change to a client's config.
*/
public class ConfigurationItemEdit extends ConfigurationItem {
private Object value;
private ConfigurationItemEditType editType;
private ConfigurationItemValueKind valueKind;

/**
*
* @param section config section to change
* @param value the value for the change
* @param editType the type of edit operation being made
* @param valueKind the kind of value for the configuration item
*/
public ConfigurationItemEdit(String section, Object value, ConfigurationItemEditType editType,
ConfigurationItemValueKind valueKind) {
super.setSection(section);
this.value = value;
this.editType = editType;
this.valueKind = valueKind;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
ConfigurationItemEdit other = (ConfigurationItemEdit) obj;
if (editType == null) {
if (other.editType != null)
return false;
} else if (!editType.equals(other.editType))
return false;
if (valueKind == null) {
if (other.valueKind != null)
return false;
} else if (!valueKind.equals(other.valueKind))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((editType == null) ? 0 : editType.hashCode());
result = prime * result + ((valueKind == null) ? 0 : valueKind.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.commons.config;

/**
* Configuration item edit type. Represents a configuration edit operation
*/
public enum ConfigurationItemEditType {
Add, Delete
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.commons.config;

/**
* Configuration item value kind. Represents the type of data that is being sent
*/
public enum ConfigurationItemValueKind {
File
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.Objects;

import org.eclipse.lemminx.XMLTextDocumentService.SaveContext;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.extensions.contentmodel.commands.AssociateGrammarCommand;
import org.eclipse.lemminx.extensions.contentmodel.commands.CheckBoundGrammarCommand;
Expand Down Expand Up @@ -137,6 +138,9 @@ private void updateSettings(ContentModelSettings settings, ISaveContext context)
if (catalogPathsChanged) {
// Validate all opened XML files
validateAllOpenedDocument(context);
if (context instanceof SaveContext) {
((SaveContext) context).setRefreshCodeLenses(true);
}
}
}
if (settings.getFileAssociations() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,25 @@ public ContentModelProvider getModelProviderByURI(String uri) {
return null;
}

/**
* Expand system Id
*
* @param path path to be expanded
* @return the expanded system Id
*/
public String expandSystemId(String path) {
return catalogResolverExtension.expandSystemId(path);
}

/**
* Get XML catalogs.
*
* @return list of catalogs
*/
public String[] getCatalogs() {
return catalogResolverExtension.getCatalogs();
}

/**
* Set up XML catalogs.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@

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 @@ -81,6 +86,8 @@ 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 @@ -107,6 +114,40 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public boolean setCatalogs(String[] catalogs) {
return !Objects.equals(oldCatalogs, newCatalogs);
}

private String expandSystemId(String path) {
public String expandSystemId(String path) {
try {
return XMLEntityManager.expandSystemId(path, rootUri, false);
} catch (MalformedURIException e) {
Expand All @@ -129,4 +129,14 @@ public void refreshCatalogs() {
setCatalogs(catalogResolver.getCatalogList());
}
}

/**
* Get the XML catalogs.
*/
public String[] getCatalogs() {
if (catalogResolver != null) {
return catalogResolver.getCatalogList();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import static org.eclipse.lemminx.XMLAssert.testCodeLensFor;
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 java.io.IOException;
import java.util.Collections;
import java.util.function.Consumer;

Expand All @@ -26,6 +28,7 @@
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLFileAssociation;
import org.eclipse.lemminx.services.XMLLanguageService;
import org.eclipse.lemminx.utils.FilesUtils;
import org.junit.jupiter.api.Test;

/**
Expand Down Expand Up @@ -192,6 +195,66 @@ public void referencedGrammarUsingFileAssociationEmptyDocument() throws BadLocat
cl(r(0, 0, 0, 0), " (with file association)", OPEN_URI));
}

public void unregisterCatalog() throws BadLocationException, IOException {
Consumer<XMLLanguageService> configuration = ls -> {
ContentModelManager contentModelManager = ls.getComponent(ContentModelManager.class);
contentModelManager.setCatalogs(new String[] { "src/test/resources/catalogs/catalog.xml" });
};

String xml = FilesUtils.readString(FilesUtils.getPath("src/test/resources/catalogs/catalog.xml"));

testCodeLensFor(xml, "src/test/resources/catalogs/catalog.xml", new XMLLanguageService(), //
Collections.singletonList(CodeLensKind.OpenUri), //
configuration, //
cl(r(0, 0, 0, 0), "urn:oasis:names:tc:entity:xmlns:xml:catalog (with embedded catalog.xsd)", OPEN_URI),
cl(r(1, 1, 1, 8), "Unregister Catalog", UPDATE_CONFIGURATION));
}

@Test
public void registerCatalogEmptyCatalog() throws BadLocationException, IOException {

String xml = FilesUtils.readString(FilesUtils.getPath("src/test/resources/catalogs/catalog.xml"));

testCodeLensFor(xml, "src/test/resources/catalogs/catalog.xml", new XMLLanguageService(), //
Collections.singletonList(CodeLensKind.OpenUri), //
cl(r(0, 0, 0, 0), "urn:oasis:names:tc:entity:xmlns:xml:catalog (with embedded catalog.xsd)", OPEN_URI),
cl(r(1, 1, 1, 8), "Register Catalog", UPDATE_CONFIGURATION));
}

@Test
public void registerCatalogWithOtherExistingCatalog() throws BadLocationException, IOException {
Consumer<XMLLanguageService> configuration = ls -> {
ContentModelManager contentModelManager = ls.getComponent(ContentModelManager.class);
contentModelManager.setCatalogs(new String[] { "src/test/resources/catalogs/catalog-public.xml" });
};

String xml = FilesUtils.readString(FilesUtils.getPath("src/test/resources/catalogs/catalog.xml"));

testCodeLensFor(xml, "src/test/resources/catalogs/catalog.xml", new XMLLanguageService(), //
Collections.singletonList(CodeLensKind.OpenUri), //
configuration, //
cl(r(0, 0, 0, 0), "urn:oasis:names:tc:entity:xmlns:xml:catalog (with embedded catalog.xsd)", OPEN_URI),
cl(r(1, 1, 1, 8), "Register Catalog", UPDATE_CONFIGURATION));
}

@Test
public void unregisterCatalogWithExistingCatalog() throws BadLocationException, IOException {
Consumer<XMLLanguageService> configuration = ls -> {
ContentModelManager contentModelManager = ls.getComponent(ContentModelManager.class);
contentModelManager.setCatalogs(new String[] { //
"src/test/resources/catalogs/catalog-public.xml", //
"src/test/resources/catalogs/catalog.xml" });
};

String xml = FilesUtils.readString(FilesUtils.getPath("src/test/resources/catalogs/catalog.xml"));

testCodeLensFor(xml, "src/test/resources/catalogs/catalog.xml", new XMLLanguageService(), //
Collections.singletonList(CodeLensKind.OpenUri), //
configuration, //
cl(r(0, 0, 0, 0), "urn:oasis:names:tc:entity:xmlns:xml:catalog (with embedded catalog.xsd)", OPEN_URI),
cl(r(1, 1, 1, 8), "Unregister Catalog", UPDATE_CONFIGURATION));
}

private static XMLFileAssociation[] createXSDAssociationsNoNamespaceSchemaLocationLike(String baseSystemId) {
XMLFileAssociation resources = new XMLFileAssociation();
resources.setPattern("**/*resources*.xml");
Expand Down