diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/commons/ParentProcessWatcher.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/commons/ParentProcessWatcher.java index 1df863748c..4d7a3f0726 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/commons/ParentProcessWatcher.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/commons/ParentProcessWatcher.java @@ -19,11 +19,11 @@ import java.util.logging.Level; import java.util.logging.Logger; +import com.google.common.io.Closeables; + import org.eclipse.lsp4j.jsonrpc.MessageConsumer; import org.eclipse.lsp4j.services.LanguageServer; -import com.google.common.io.Closeables; - /** * Watches the parent process PID and invokes exit if it is no longer available. * This implementation waits for periods of inactivity to start querying the PIDs. @@ -38,7 +38,7 @@ public final class ParentProcessWatcher implements Runnable, Function= 0; + public static final boolean isWindows = System.getProperty("os.name").toLowerCase().indexOf("win") >= 0; private static final long INACTIVITY_DELAY_SECS = 30 *1000; private static final int POLL_DELAY_SECS = 10; diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/general/FilePathPlugin.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/general/FilePathPlugin.java new file mode 100644 index 0000000000..19c487adc8 --- /dev/null +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/general/FilePathPlugin.java @@ -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) { + + } + + +} \ No newline at end of file diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/general/completion/FilePathCompletionParticipant.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/general/completion/FilePathCompletionParticipant.java new file mode 100644 index 0000000000..68ad888490 --- /dev/null +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/general/completion/FilePathCompletionParticipant.java @@ -0,0 +1,189 @@ +/******************************************************************************* +* 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.commons.BadLocationException; +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.CompletionSortTextHelper; +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); + + if(parentAttributeValuePath == null) { + return; + } + + File f = new File(parentAttributeValuePath.toString()); + + if(f.isFile()) { + return; + } + String slash = StringUtils.getFilePathSlash(currentValue); + + Range replaceRange = adjustReplaceRange(xmlDocument, fullRange, currentValue, slash); + + createNextValidCompletionPaths(parentAttributeValuePath, currentValue, replaceRange, response, xsdFilter); + } + + private Range adjustReplaceRange(DOMDocument xmlDocument, Range fullRange, String attributeValue, String slash) { + //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 startOffset; + try { + startOffset = xmlDocument.offsetAt(fullRange.getStart()) + 1; + } catch (BadLocationException e) { + return null; + } + int lastSlashIndex = attributeValue.lastIndexOf(slash); + if(lastSlashIndex > -1) { + try { + replaceStart = xmlDocument.positionAt(startOffset + lastSlashIndex); + } catch (BadLocationException e) { + return null; + } + } + Range replaceRange = new Range(); + if(replaceStart != null) { + replaceRange.setStart(replaceStart); + } + else { + replaceRange.setStart(replaceEnd); + } + replaceRange.setEnd(replaceEnd); + + return replaceRange; + } + + 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; + // } + insertText = slash + fName; + item.setLabel(insertText); + CompletionItemKind kind = f.isFile()? CompletionItemKind.File : CompletionItemKind.Folder; + item.setKind(kind); + + item.setSortText(CompletionSortTextHelper.getSortText(kind)); + item.setFilterText(insertText); + item.setTextEdit(new TextEdit(replaceRange, insertText)); + response.addCompletionItem(item); + } + + + + + +} \ No newline at end of file diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/CompletionSortTextHelper.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/CompletionSortTextHelper.java new file mode 100644 index 0000000000..cc34bd6653 --- /dev/null +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/CompletionSortTextHelper.java @@ -0,0 +1,56 @@ +/******************************************************************************* +* 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.utils; + +import org.eclipse.lsp4j.CompletionItemKind; + +/** + * CompletionSortText class to get sortText for CompletionItem's + */ +public class CompletionSortTextHelper { + + private int i; + private final String base; + + public CompletionSortTextHelper(CompletionItemKind kind) { + this.base = getSortText(kind); + i = 0; + } + + public String next() { + i++; + return base + Integer.toString(i); + } + + public String next(CompletionItemKind kind) { + String tempBase = getSortText(kind); + i++; + return tempBase + Integer.toString(i); + } + + public static String getSortText(CompletionItemKind kind) { + switch (kind) { + case Variable: + case Property: // DOMElement + case Enum: + case EnumMember: + case Value: + return "aa"; + case File: + return "ab"; + case Folder: + return "ac"; + default: + return "zz"; + } + } + +} \ No newline at end of file diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/StringUtils.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/StringUtils.java index ee7a31a2a9..dcade66494 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/StringUtils.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/StringUtils.java @@ -10,6 +10,11 @@ */ package org.eclipse.lsp4xml.utils; +import static org.eclipse.lsp4xml.commons.ParentProcessWatcher.isWindows; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collection; @@ -24,6 +29,7 @@ public class StringUtils { public static final String TRUE = "true"; public static final String FALSE = "false"; public static final Collection TRUE_FALSE_ARRAY = Arrays.asList(TRUE, FALSE); + public static String SLASH = isWindows ? "\\" : "/"; private StringUtils() { } @@ -229,4 +235,91 @@ public static String getDefaultString(String text) { return ""; } + /** + * Will return a Path object + * @param parentDirectory The directory that the given path is relative to + * @param givenPath Path that could be absolute or relative + * @return + */ + public static Path getNormalizedPath(String parentDirectory, String givenPath) { + + Path p; + if(givenPath == null) { + return null; + } + String newPath; + File f; + + if(parentDirectory.endsWith(SLASH)) { + parentDirectory = parentDirectory.substring(0, parentDirectory.length() - 1); + } + + //The following 2 are for when the given path is already valid + + + p = getPathIfExists(givenPath); + if(p != null) { + //givenPath is absolute + return p; + } + + + String combinedPath = parentDirectory + SLASH + givenPath; + p = getPathIfExists(combinedPath); + if(p != null) { + return p; + } + + //The following 2 are for when the given path ends with and invalid + //file / folder. + + + int lastIndexOfSlash = givenPath.lastIndexOf(SLASH); + if(lastIndexOfSlash == -1) { + return null; + } + + //in case the given path is incomplete + givenPath = givenPath.substring(0, lastIndexOfSlash); + + + p = getPathIfExists(givenPath); + if(p != null) { + //givenPath is absolute + return p; + } + + + combinedPath = parentDirectory + SLASH + givenPath; + p = getPathIfExists(combinedPath); + if(p != null) { + return p; + } + + + return null; + } + + private static Path getPathIfExists(String path) { + Path p = Paths.get(path).normalize(); + String newPath = p.toString(); + File f = new File(newPath); + if(f.exists()) { + return p; + } + return null; + } + + public static String getFilePathSlash(String text) { + if(text.contains("\\")) { + return "\\"; + } + else if(text.contains("/")) { + return "/"; + } + else { + return SLASH; + } + } + } diff --git a/org.eclipse.lsp4xml/src/main/resources/META-INF/services/org.eclipse.lsp4xml.services.extensions.IXMLExtension b/org.eclipse.lsp4xml/src/main/resources/META-INF/services/org.eclipse.lsp4xml.services.extensions.IXMLExtension index 99c16bdb19..478b4228e5 100644 --- a/org.eclipse.lsp4xml/src/main/resources/META-INF/services/org.eclipse.lsp4xml.services.extensions.IXMLExtension +++ b/org.eclipse.lsp4xml/src/main/resources/META-INF/services/org.eclipse.lsp4xml.services.extensions.IXMLExtension @@ -5,4 +5,5 @@ org.eclipse.lsp4xml.extensions.dtd.DTDPlugin org.eclipse.lsp4xml.extensions.xsl.XSLPlugin org.eclipse.lsp4xml.extensions.catalog.XMLCatalogPlugin org.eclipse.lsp4xml.extensions.xsi.XSISchemaPlugin -org.eclipse.lsp4xml.extensions.prolog.PrologPlugin \ No newline at end of file +org.eclipse.lsp4xml.extensions.prolog.PrologPlugin +org.eclipse.lsp4xml.extensions.general.FilePathPlugin \ No newline at end of file diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/general/FilePathCompletionTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/general/FilePathCompletionTest.java new file mode 100644 index 0000000000..ae53b5acb7 --- /dev/null +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/general/FilePathCompletionTest.java @@ -0,0 +1,83 @@ +/******************************************************************************* +* 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 static org.eclipse.lsp4xml.XMLAssert.c; +import static org.eclipse.lsp4xml.XMLAssert.te; + +import org.eclipse.lsp4j.CompletionItem; +import org.eclipse.lsp4xml.XMLAssert; +import org.eclipse.lsp4xml.commons.BadLocationException; +import org.junit.Test; + +/** + * FilePathCompletionTest + * + * Test folders are in org.eclipse.lsp4xml/src/test/resources/filePathCompletion/ + */ +public class FilePathCompletionTest { + + @Test + public void testFilePathCompletion() throws BadLocationException { + String xml = ""; + testCompletionFor(xml, c("/folderA", te(0, 10, 0, 11, "/folderA"), "/folderA"), c("/folderB", te(0, 10, 0, 11, "/folderB"), "/folderB"), c("/NestedA", te(0, 10, 0, 11, "/NestedA"), "/NestedA")); + } + + @Test + public void testFilePathCompletionFolderA() throws BadLocationException { + String xml = ""; + testCompletionFor(xml, c("/xsdA1.xsd", te(0, 18, 0, 19, "/xsdA1.xsd"), "/xsdA1.xsd"), c("/xsdA2.xsd", te(0, 18, 0, 19, "/xsdA2.xsd"), "/xsdA2.xsd")); + } + + @Test + public void testFilePathCompletionFolderB() throws BadLocationException { + String xml = ""; + testCompletionFor(xml, 1, c("/xsdB1.xsd", te(0, 16, 0, 17, "/xsdB1.xsd"), "/xsdB1.xsd")); + } + + @Test + public void testFilePathCompletionFolderBAbsolutePath() throws BadLocationException { + String xml = ""; + testCompletionFor(xml, 1, c("/xsdB1.xsd", te(0, 54, 0, 55, "/xsdB1.xsd"), "/xsdB1.xsd")); + } + + @Test + public void testFilePathCompletionNestedA() throws BadLocationException { + String xml = ""; + testCompletionFor(xml, 1, c("/NestedB", te(0, 16, 0, 17, "/NestedB"), "/NestedB")); + } + + @Test + public void testFilePathCompletionNestedBIncomplete() throws BadLocationException { + String xml = ""; + testCompletionFor(xml, 1, c("/nestedXSD.xsd", te(0, 24, 0, 28, "/nestedXSD.xsd"), "/nestedXSD.xsd")); + } + + @Test + public void testFilePathCompletionBadFolder() throws BadLocationException { + String xml = ""; + testCompletionFor(xml, 0); + } + + @Test + public void testFilePathCompletionNotValue() throws BadLocationException { + String xml = ""; + testCompletionFor(xml, 3, c("/folderA", te(0, 9, 0, 9, "/folderA"), "/folderA"), c("/folderB", te(0, 9, 0, 9, "/folderB"), "/folderB"), c("/NestedA", te(0, 9, 0, 9, "/NestedA"), "/NestedA")); + } + + private void testCompletionFor(String xml, CompletionItem... expectedItems) throws BadLocationException { + testCompletionFor(xml, null, expectedItems); + } + + private void testCompletionFor(String xml, Integer expectedItemCount, CompletionItem... expectedItems) throws BadLocationException{ + XMLAssert.testCompletionFor(xml, null, "src/test/resources/filePathCompletion/main.xml", expectedItemCount, expectedItems); + } +} \ No newline at end of file diff --git a/org.eclipse.lsp4xml/src/test/resources/filePathCompletion/NestedA/NestedB/nestedXSD.xsd b/org.eclipse.lsp4xml/src/test/resources/filePathCompletion/NestedA/NestedB/nestedXSD.xsd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/org.eclipse.lsp4xml/src/test/resources/filePathCompletion/folderA/xsdA1.xsd b/org.eclipse.lsp4xml/src/test/resources/filePathCompletion/folderA/xsdA1.xsd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/org.eclipse.lsp4xml/src/test/resources/filePathCompletion/folderA/xsdA2.xsd b/org.eclipse.lsp4xml/src/test/resources/filePathCompletion/folderA/xsdA2.xsd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/org.eclipse.lsp4xml/src/test/resources/filePathCompletion/folderB/ignore.xml b/org.eclipse.lsp4xml/src/test/resources/filePathCompletion/folderB/ignore.xml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/org.eclipse.lsp4xml/src/test/resources/filePathCompletion/folderB/xsdB1.xsd b/org.eclipse.lsp4xml/src/test/resources/filePathCompletion/folderB/xsdB1.xsd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/org.eclipse.lsp4xml/src/test/resources/filePathCompletion/main.xml b/org.eclipse.lsp4xml/src/test/resources/filePathCompletion/main.xml new file mode 100644 index 0000000000..e69de29bb2