-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1464 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
8399762
commit 6b12cd0
Showing
48 changed files
with
2,053 additions
and
486 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...rg/eclipse/lemminx/extensions/catalog/participants/CatalogFilePathSupportParticipant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/filepath/FilePathPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...se.lemminx/src/main/java/org/eclipse/lemminx/extensions/filepath/IFilePathExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...nx/src/main/java/org/eclipse/lemminx/extensions/filepath/IFilePathSupportParticipant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.