-
Notifications
You must be signed in to change notification settings - Fork 91
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 454ff11
Showing
40 changed files
with
1,404 additions
and
473 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
35 changes: 35 additions & 0 deletions
35
...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,35 @@ | ||
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; | ||
|
||
public class CatalogFilePathSupportParticipant implements IFilePathSupportParticipant { | ||
|
||
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
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.getExpressions(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; | ||
} | ||
} |
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/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,10 @@ | ||
package org.eclipse.lemminx.extensions.filepath; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.lemminx.dom.DOMDocument; | ||
|
||
public interface IFilePathSupportParticipant { | ||
|
||
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.