-
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
def27a7
commit acdfe02
Showing
34 changed files
with
1,268 additions
and
467 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...c/main/java/org/eclipse/lemminx/extensions/catalog/CatalogFilePathExpressionProvider.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,35 @@ | ||
package org.eclipse.lemminx.extensions.catalog; | ||
|
||
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.IFilePathExpressionProvider; | ||
import org.eclipse.lemminx.extensions.filepath.settings.FilePathExpression; | ||
import org.eclipse.lemminx.utils.DOMUtils; | ||
|
||
public class CatalogFilePathExpressionProvider implements IFilePathExpressionProvider { | ||
|
||
private static final List<IFilePathExpression> CATALOG_FILE_PATH_EXPRESSIONS; | ||
|
||
static { | ||
CATALOG_FILE_PATH_EXPRESSIONS = Arrays.asList(createUriExpression()); | ||
} | ||
|
||
private static FilePathExpression createUriExpression() { | ||
FilePathExpression uri = new FilePathExpression(); | ||
uri.setXPath("@uri"); | ||
return uri; | ||
} | ||
|
||
@Override | ||
public List<IFilePathExpression> getExpressions(DOMDocument document) { | ||
if (!DOMUtils.isCatalog(document)) { | ||
return Collections.emptyList(); | ||
} | ||
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
138 changes: 138 additions & 0 deletions
138
...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,138 @@ | ||
/******************************************************************************* | ||
* 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.List; | ||
|
||
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; | ||
|
||
/** | ||
* FilePathPlugin | ||
*/ | ||
public class FilePathPlugin implements IXMLExtension { | ||
|
||
private final FilePathCompletionParticipant completionParticipant; | ||
private FilePathSupportSettings filePathsSettings; | ||
private XMLExtensionsRegistry registry; | ||
|
||
private static class ContributedProviderCache extends ArrayList<IFilePathExpressionProvider> { | ||
|
||
} | ||
|
||
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) { | ||
this.registry = 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<>(); | ||
fillFromContribution(xmlDocument, expressions); | ||
fillFromUserSettings(xmlDocument, expressions); | ||
return expressions; | ||
} | ||
|
||
private void fillFromContribution(DOMDocument xmlDocument, List<IFilePathExpression> expressions) { | ||
ContributedProviderCache cache = registry.getComponent(ContributedProviderCache.class); | ||
if (cache != null) { | ||
for (IFilePathExpressionProvider provider : cache) { | ||
List<IFilePathExpression> providerExpressions = provider.getExpressions(xmlDocument); | ||
if (providerExpressions != null && !providerExpressions.isEmpty()) { | ||
expressions.addAll(providerExpressions); | ||
} | ||
} | ||
} | ||
} | ||
|
||
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 static void registerFilePathExpressionProvider(IFilePathExpressionProvider provider, | ||
XMLExtensionsRegistry registry) { | ||
ContributedProviderCache cache = registry.getComponent(ContributedProviderCache.class); | ||
if (cache == null) { | ||
cache = new ContributedProviderCache(); | ||
registry.registerComponent(cache); | ||
} | ||
cache.add(provider); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...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,15 @@ | ||
package org.eclipse.lemminx.extensions.filepath; | ||
|
||
import java.nio.file.Path; | ||
|
||
import org.w3c.dom.Node; | ||
|
||
public interface IFilePathExpression { | ||
|
||
boolean match(Node node); | ||
|
||
Character getSeparator(); | ||
|
||
boolean acceptPath(Path path); | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...nx/src/main/java/org/eclipse/lemminx/extensions/filepath/IFilePathExpressionProvider.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,10 @@ | ||
package org.eclipse.lemminx.extensions.filepath; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.lemminx.dom.DOMDocument; | ||
|
||
public interface IFilePathExpressionProvider { | ||
|
||
List<IFilePathExpression> getExpressions(DOMDocument document); | ||
} |
32 changes: 32 additions & 0 deletions
32
...mminx/src/main/java/org/eclipse/lemminx/extensions/filepath/SimpleFilePathExpression.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,32 @@ | ||
package org.eclipse.lemminx.extensions.filepath; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import org.w3c.dom.Node; | ||
|
||
public class SimpleFilePathExpression implements IFilePathExpression { | ||
|
||
@Override | ||
public boolean match(Node node) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Character getSeparator() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean acceptPath(Path path) { | ||
if (!Files.isDirectory(path)) { | ||
return acceptFile(path); | ||
} | ||
return true; | ||
} | ||
|
||
protected boolean acceptFile(Path path) { | ||
return false; | ||
} | ||
|
||
} |
Oops, something went wrong.