Skip to content

Commit

Permalink
XML Files support with settings
Browse files Browse the repository at this point in the history
Fixes #1464

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Nov 30, 2023
1 parent def27a7 commit 9bce7b9
Show file tree
Hide file tree
Showing 42 changed files with 1,771 additions and 480 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import org.eclipse.lemminx.client.InvalidPathWarner;
import org.eclipse.lemminx.client.PathFeature;
import org.eclipse.lemminx.extensions.catalog.participants.XMLCatalogCodeLensParticipant;
import org.eclipse.lemminx.extensions.catalog.participants.XMLCatalogDiagnosticsParticipant;
import org.eclipse.lemminx.extensions.catalog.participants.XMLCatalogDocumentLinkParticipant;
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager;
import org.eclipse.lemminx.extensions.contentmodel.settings.ContentModelSettings;
import org.eclipse.lemminx.services.IXMLNotificationService;
Expand Down Expand Up @@ -51,7 +54,8 @@ public XMLCatalogPlugin() {
@Override
public void doSave(ISaveContext context) {
Object initializationOptionsSettings = context.getSettings();
ContentModelSettings cmSettings = ContentModelSettings.getContentModelXMLSettings(initializationOptionsSettings);
ContentModelSettings cmSettings = ContentModelSettings
.getContentModelXMLSettings(initializationOptionsSettings);
if (cmSettings == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*******************************************************************************
* Copyright (c) 2023 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.catalog.participants;

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

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.extensions.filepath.IFilePathExpression;
import org.eclipse.lemminx.extensions.filepath.IFilePathSupportParticipant;
import org.eclipse.lemminx.extensions.filepath.settings.FilePathExpression;
import org.eclipse.lemminx.utils.DOMUtils;

/**
* Catalog file path support for catalog.xml to provide completion for @uri
* attribute.
*/
public class CatalogFilePathSupportParticipant implements IFilePathSupportParticipant {

private static final List<IFilePathExpression> CATALOG_FILE_PATH_EXPRESSIONS;

static {
CATALOG_FILE_PATH_EXPRESSIONS = Arrays.asList(new FilePathExpression("@uri"));
}

@Override
public List<IFilePathExpression> collectFilePathExpressions(DOMDocument document) {
if (!DOMUtils.isCatalog(document)) {
return Collections.emptyList();
}
// The DOM document is an XML catalog, returns their expressions.
return CATALOG_FILE_PATH_EXPRESSIONS;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.extensions.catalog;
package org.eclipse.lemminx.extensions.catalog.participants;

import java.util.Arrays;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
* Red Hat Inc. - initial API and implementation
*******************************************************************************/

package org.eclipse.lemminx.extensions.catalog;
package org.eclipse.lemminx.extensions.catalog.participants;

import java.text.MessageFormat;
import java.util.List;

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.extensions.catalog.CatalogEntry;
import org.eclipse.lemminx.extensions.catalog.CatalogUtils;
import org.eclipse.lemminx.extensions.catalog.XMLCatalogErrorCode;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings;
import org.eclipse.lemminx.services.extensions.diagnostics.IDiagnosticsParticipant;
import org.eclipse.lemminx.utils.DOMUtils;
Expand Down Expand Up @@ -45,7 +48,7 @@ public void doDiagnostics(DOMDocument xmlDocument, List<Diagnostic> diagnostics,
// appending it in the case when original URI does not start with 'file://'.
// Ex: originalURI ="foo/bar.xsd" -> path ="file://foo/bar.xsd"
String path = CatalogUtils.getResolvedLocation(xmlDocument, catalogEntry);
if (!FilesUtils.isValidPath(FilesUtils.getPath(path)) && URIUtils.isFileResource(path)) {
if (path != null && !FilesUtils.isValidPath(FilesUtils.getPath(path)) && URIUtils.isFileResource(path)) {
Range range = XMLPositionUtility.selectValueWithoutQuote(catalogEntry.getLinkRange());
String msg = MessageFormat.format(ERROR_STRING, catalogEntry.getResolvedURI());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
* Red Hat Inc. - initial API and implementation
*******************************************************************************/

package org.eclipse.lemminx.extensions.catalog;
package org.eclipse.lemminx.extensions.catalog.participants;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.extensions.catalog.CatalogEntry;
import org.eclipse.lemminx.extensions.catalog.CatalogUtils;
import org.eclipse.lemminx.services.extensions.IDocumentLinkParticipant;
import org.eclipse.lemminx.utils.XMLPositionUtility;
import org.eclipse.lsp4j.DocumentLink;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*******************************************************************************
* Copyright (c) 2019 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.filepath;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.extensions.filepath.participants.FilePathCompletionParticipant;
import org.eclipse.lemminx.extensions.filepath.settings.FilePathExpression;
import org.eclipse.lemminx.extensions.filepath.settings.FilePathMapping;
import org.eclipse.lemminx.extensions.filepath.settings.FilePathSupportSettings;
import org.eclipse.lemminx.services.extensions.IXMLExtension;
import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lemminx.services.extensions.save.ISaveContext;
import org.eclipse.lsp4j.InitializeParams;

/**
* File Path support plugin.
*/
public class FilePathPlugin implements IXMLExtension {

private static final Logger LOGGER = Logger.getLogger(FilePathPlugin.class.getName());

private final FilePathCompletionParticipant completionParticipant;
private FilePathSupportSettings filePathsSettings;

private List<IFilePathSupportParticipant> filePathSupportParticipants;

public FilePathPlugin() {
completionParticipant = new FilePathCompletionParticipant(this);
}

@Override
public void doSave(ISaveContext context) {
if (context.getType() != ISaveContext.SaveContextType.DOCUMENT) {
// Settings
updateSettings(context);
}
}

private void updateSettings(ISaveContext saveContext) {
Object initializationOptionsSettings = saveContext.getSettings();
FilePathSupportSettings settings = FilePathSupportSettings
.getFilePathsSettings(initializationOptionsSettings);
updateSettings(settings, saveContext);
}

private void updateSettings(FilePathSupportSettings settings, ISaveContext context) {
this.filePathsSettings = settings;
}

@Override
public void start(InitializeParams params, XMLExtensionsRegistry registry) {
registry.registerCompletionParticipant(completionParticipant);
}

@Override
public void stop(XMLExtensionsRegistry registry) {
registry.unregisterCompletionParticipant(completionParticipant);
}

public FilePathSupportSettings getFilePathsSettings() {
return filePathsSettings;
}

/**
* Return the list of {@link FilePathExpression} for the given document and an
* empty list otherwise.
*
* @param xmlDocument the DOM document
*
* @return the list of {@link FilePathExpression} for the given document and an
* empty list otherwise.
*/
public List<IFilePathExpression> findFilePathExpressions(DOMDocument xmlDocument) {
List<IFilePathExpression> expressions = new ArrayList<>();
fillFromParticipants(xmlDocument, expressions);
fillFromUserSettings(xmlDocument, expressions);
return expressions;
}

private void fillFromParticipants(DOMDocument xmlDocument, List<IFilePathExpression> allExpressions) {
for (IFilePathSupportParticipant participant : getFilePathSupportParticipants()) {
List<IFilePathExpression> expressions = participant.collectFilePathExpressions(xmlDocument);
if (expressions != null && !expressions.isEmpty()) {
allExpressions.addAll(expressions);
}
}
}

private void fillFromUserSettings(DOMDocument xmlDocument, List<IFilePathExpression> expressions) {
FilePathSupportSettings settings = getFilePathsSettings();
if (settings == null) {
return;
}

List<FilePathMapping> mappings = settings.getFilePathMappings();
fillFromMappings(xmlDocument, mappings, expressions);
}

private static void fillFromMappings(DOMDocument xmlDocument, List<FilePathMapping> mappings,
List<IFilePathExpression> expressions) {
if (mappings == null) {
return;
}

for (FilePathMapping filePaths : mappings) {
if (filePaths.matches(xmlDocument.getDocumentURI())) {
expressions.addAll(filePaths.getExpressions());
}
}
}

public List<IFilePathSupportParticipant> getFilePathSupportParticipants() {
if (filePathSupportParticipants == null) {
loadFilePathSupportParticipants();
}
return filePathSupportParticipants;
}

private synchronized void loadFilePathSupportParticipants() {
if (filePathSupportParticipants != null) {
return;
}
List<IFilePathSupportParticipant> participants = new ArrayList<>();
Iterator<IFilePathSupportParticipant> extensions = ServiceLoader.load(IFilePathSupportParticipant.class)
.iterator();
while (extensions.hasNext()) {
try {
participants.add(extensions.next());
} catch (ServiceConfigurationError e) {
LOGGER.log(Level.SEVERE, "Error while instantiating file path support participant", e);
}
}
filePathSupportParticipants = participants;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*******************************************************************************
* Copyright (c) 2023 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.filepath;

import java.nio.file.Path;

import org.w3c.dom.Node;

/**
* File path support expression API.
*/
public interface IFilePathExpression {

/**
* Returns true if the given DOM node matches the file path expression and false
* otherwise.
*
* @param node the DOM node.
*
* @return true if the given DOM node matches the file path expression and false
* otherwise.
*/
boolean match(Node node);

/**
* Returns the separator character (ex: ';') used to separate multiple files
* declaration (ex:
* file1.xml;file2.xml) and null otherwise.
*
* @return the separator character (ex: ';') used to separate multiple files
* declaration (ex:
* file1.xml;file2.xml) and null otherwise.
*/
Character getSeparator();

/**
* Returns true if given file path is allowed for the file path completion and
* false otherwise.
*
* @param path the file path.
*
* @return true if given file path is allowed for the file path completion and
* false otherwise.
*/
boolean acceptPath(Path path);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2023 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.filepath;

import java.util.List;

import org.eclipse.lemminx.dom.DOMDocument;

/**
* File path support participant API.
*
* <p>
* This API provides the capability to contribute to
* mark some DOM nodes as file type to have file path completion inside the DOM
* node.
* </p>
*/
public interface IFilePathSupportParticipant {

/**
* Returns the file path expressions used to mark DOM nodes as file type for the
* given DOM document and null or empty otherwise.
*
* @param document the DOM document.
*
* @return the file path expressions used to mark DOM nodes as file type for the
* given DOM document and null or empty otherwise.
*/
List<IFilePathExpression> collectFilePathExpressions(DOMDocument document);
}
Loading

0 comments on commit 9bce7b9

Please sign in to comment.