forked from eclipse-lemminx/lemminx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Need to still handle ~ (home) and get SortTextHelper from XSDCompletion pr Fixes eclipse-lemminx#345 Signed-off-by: Nikolas Komonen <[email protected]>
- Loading branch information
1 parent
f9d5253
commit 660caa5
Showing
5 changed files
with
288 additions
and
4 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
46 changes: 46 additions & 0 deletions
46
org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/general/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,46 @@ | ||
/******************************************************************************* | ||
* 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 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.lsp4xml.extensions.general; | ||
|
||
import org.eclipse.lsp4j.InitializeParams; | ||
import org.eclipse.lsp4xml.extensions.general.completion.FilePathCompletionParticipant; | ||
import org.eclipse.lsp4xml.services.extensions.IXMLExtension; | ||
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry; | ||
import org.eclipse.lsp4xml.services.extensions.save.ISaveContext; | ||
|
||
/** | ||
* FilePathPlugin | ||
*/ | ||
public class FilePathPlugin implements IXMLExtension { | ||
|
||
private final FilePathCompletionParticipant completionParticipant; | ||
|
||
public FilePathPlugin() { | ||
completionParticipant = new FilePathCompletionParticipant(); | ||
} | ||
|
||
@Override | ||
public void start(InitializeParams params, XMLExtensionsRegistry registry) { | ||
registry.registerCompletionParticipant(completionParticipant); | ||
} | ||
|
||
@Override | ||
public void stop(XMLExtensionsRegistry registry) { | ||
registry.unregisterCompletionParticipant(completionParticipant); | ||
} | ||
|
||
@Override | ||
public void doSave(ISaveContext context) { | ||
|
||
} | ||
|
||
|
||
} |
165 changes: 165 additions & 0 deletions
165
...java/org/eclipse/lsp4xml/extensions/general/completion/FilePathCompletionParticipant.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,165 @@ | ||
/******************************************************************************* | ||
* 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 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.lsp4xml.extensions.general.completion; | ||
|
||
import static org.eclipse.lsp4xml.utils.StringUtils.getNormalizedPath; | ||
|
||
import java.io.File; | ||
import java.io.FilenameFilter; | ||
import java.net.URI; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import org.eclipse.lsp4j.CompletionItem; | ||
import org.eclipse.lsp4j.CompletionItemKind; | ||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.lsp4j.Range; | ||
import org.eclipse.lsp4j.TextEdit; | ||
import org.eclipse.lsp4xml.dom.DOMDocument; | ||
import org.eclipse.lsp4xml.services.extensions.ICompletionParticipant; | ||
import org.eclipse.lsp4xml.services.extensions.ICompletionRequest; | ||
import org.eclipse.lsp4xml.services.extensions.ICompletionResponse; | ||
import org.eclipse.lsp4xml.settings.SharedSettings; | ||
import org.eclipse.lsp4xml.utils.StringUtils; | ||
|
||
/** | ||
* FilePathCompletionParticipant | ||
*/ | ||
public class FilePathCompletionParticipant implements ICompletionParticipant { | ||
|
||
private static final FilenameFilter xsdFilter = new FilenameFilter() { | ||
|
||
@Override | ||
public boolean accept(File dir, String name) { | ||
if(name.matches(".+\\.[^\\s]+$") == false) { | ||
return true; | ||
} | ||
return name.toLowerCase().endsWith(".xsd"); | ||
} | ||
}; | ||
|
||
@Override | ||
public void onTagOpen(ICompletionRequest completionRequest, ICompletionResponse completionResponse) throws Exception { | ||
|
||
} | ||
|
||
@Override | ||
public void onXMLContent(ICompletionRequest request, ICompletionResponse response) throws Exception { | ||
|
||
} | ||
|
||
@Override | ||
public void onAttributeName(boolean generateValue, Range fullRange, ICompletionRequest request, | ||
ICompletionResponse response, SharedSettings settings) throws Exception { | ||
|
||
} | ||
|
||
@Override | ||
public void onAttributeValue(String valuePrefix, Range fullRange, boolean addQuotes, ICompletionRequest request, | ||
ICompletionResponse response, SharedSettings settings) throws Exception { | ||
|
||
DOMDocument xmlDocument = request.getXMLDocument(); | ||
// +- 1 since it includes the quotations | ||
int startOffset = xmlDocument.offsetAt(fullRange.getStart()) + 1; | ||
int endOffset = xmlDocument.offsetAt(fullRange.getEnd()) - 1; | ||
String currentValue = xmlDocument.getText().substring(startOffset, endOffset); | ||
|
||
String uriString = xmlDocument.getTextDocument().getUri(); | ||
String directoryOfFile = Paths.get(uriString).getParent().toString(); | ||
URI uri = URI.create(directoryOfFile); | ||
|
||
String scheme = uri.getScheme(); | ||
if(!(scheme == null || "file".equals(scheme))) { | ||
return; | ||
} | ||
|
||
directoryOfFile = uri.getPath(); | ||
|
||
Path parentAttributeValuePath = getNormalizedPath(directoryOfFile, currentValue); | ||
|
||
File f = new File(parentAttributeValuePath.toString()); | ||
if(parentAttributeValuePath == null || f.isFile()) { | ||
return; | ||
} | ||
String slash = StringUtils.getFilePathSlash(currentValue); | ||
|
||
//In the case the currently typed file/directory needs to be overwritten | ||
Position replaceStart = null; | ||
Position currentEnd = fullRange.getEnd(); | ||
Position replaceEnd = new Position(currentEnd.getLine(), currentEnd.getCharacter() - 1); | ||
int lastSlashIndex = currentValue.lastIndexOf(slash); | ||
if(lastSlashIndex > -1) { | ||
replaceStart = xmlDocument.positionAt(startOffset + lastSlashIndex); | ||
} | ||
Range replaceRange = new Range(); | ||
if(replaceStart != null) { | ||
replaceRange.setStart(replaceStart); | ||
} | ||
else { | ||
replaceRange.setStart(replaceEnd); | ||
} | ||
replaceRange.setEnd(replaceEnd); | ||
|
||
createNextValidCompletionPaths(parentAttributeValuePath, currentValue, replaceRange, response, xsdFilter); | ||
} | ||
|
||
private void createNextValidCompletionPaths(Path pathToAttributeDirectory, String attributePath, Range replaceRange, ICompletionResponse response, | ||
FilenameFilter filter) { | ||
|
||
String slash = StringUtils.getFilePathSlash(attributePath); | ||
|
||
File[] proposedFiles = gatherFiles(pathToAttributeDirectory, filter); | ||
if (proposedFiles != null) { | ||
for (File child : proposedFiles) { | ||
if (child != null) { | ||
createFilePathCompletionItem(pathToAttributeDirectory, attributePath, child, replaceRange, response, slash); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private File[] gatherFiles(Path pathOfDirectory, FilenameFilter filter) { | ||
|
||
File f = new File(pathOfDirectory.toString()); | ||
if(f.isDirectory()) { | ||
return f.listFiles(filter); | ||
} | ||
return null; | ||
} | ||
|
||
private void createFilePathCompletionItem(Path pathOfDirectory, String attributePath, File f, Range replaceRange, ICompletionResponse response, String slash) { | ||
CompletionItem item = new CompletionItem(); | ||
String fName = f.getName(); | ||
String insertText; | ||
if(f.isDirectory()) { | ||
insertText = slash + fName + slash; | ||
} | ||
else if(f.isFile()) { | ||
insertText = slash + fName; | ||
} | ||
else { | ||
return; | ||
} | ||
|
||
item.setLabel(insertText); | ||
item.setKind(f.isFile()? CompletionItemKind.File : CompletionItemKind.Folder); | ||
Sor | ||
item.setSortText(sortText); | ||
item.setFilterText(insertText); | ||
item.setTextEdit(new TextEdit(replaceRange, insertText)); | ||
response.addCompletionItem(item); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} |
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