From 367641eb2082a62b86f31699a11ed187faefdf67 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Wed, 14 Sep 2022 12:21:02 -0400 Subject: [PATCH] Do not complete paths in attr unless beginning of value looks like a path Fixes redhat-developer/vscode-xml#668 Signed-off-by: David Thompson --- .../FilePathCompletionParticipant.java | 24 ++++++++++ .../lemminx/AbstractCacheBasedTest.java | 1 - .../general/FilePathCompletionTest.java | 45 ++++++++----------- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/general/completion/FilePathCompletionParticipant.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/general/completion/FilePathCompletionParticipant.java index df8af4e812..25bc02cad0 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/general/completion/FilePathCompletionParticipant.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/general/completion/FilePathCompletionParticipant.java @@ -24,6 +24,8 @@ import java.nio.file.Path; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.eclipse.lemminx.commons.BadLocationException; import org.eclipse.lemminx.dom.DOMDocument; @@ -132,6 +134,9 @@ private static void addCompletionItems(String value, ICompletionRequest request, return; } } + } else if (!hasPathBeginning(valuePath)) { + // the user probably didn't intend to complete a path + return; } // On Linux, Mac OS replace '\\' with '/' if (!isWindows) { @@ -259,4 +264,23 @@ private static void createFilePathCompletionItem(File f, Range replaceRange, ICo response.addCompletionItem(item); } + private static boolean hasPathBeginning(String currentText) { + if (currentText.startsWith("/") + || currentText.startsWith("./") + || currentText.startsWith("../") + || currentText.startsWith("..\\") + || currentText.startsWith(".\\")) { + return true; + } + return isAbsoluteWindowsPath(currentText); + } + + private static boolean isAbsoluteWindowsPath(String currentText) { + char driveLetter = currentText.charAt(0); + if (driveLetter < 'A' || (driveLetter > 'Z' && driveLetter < 'a') || (driveLetter) > 'z') { + return false; + } + return currentText.charAt(1) == ':' && currentText.charAt(2) == '\\'; + } + } \ No newline at end of file diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/AbstractCacheBasedTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/AbstractCacheBasedTest.java index 47ff2f058c..101f2f8b5b 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/AbstractCacheBasedTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/AbstractCacheBasedTest.java @@ -45,7 +45,6 @@ public abstract class AbstractCacheBasedTest { @BeforeEach public final void setupCache() throws Exception { - System.out.println(this.getClass().getName() + ": " + uuid); clearCache(); FilesUtils.resetDeployPath(); Assertions.assertNotNull(parentDir); diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/general/FilePathCompletionTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/general/FilePathCompletionTest.java index 3e1764ff75..ebb98a1372 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/general/FilePathCompletionTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/general/FilePathCompletionTest.java @@ -66,15 +66,13 @@ public void testFilePathCompletionFolderABackSlash() throws BadLocationException @Test public void testFilePathCompletionFolderB() throws BadLocationException { String xml = ""; - CompletionItem[] items = getCompletionItemList("/", 0, 16, 17, "xsdB1.xsd", "xmlB1.xml"); - testCompletionFor(xml, 2, items); + testCompletionFor(xml, 0); } @Test public void testFilePathCompletionFolderBBackSlash() throws BadLocationException { String xml = ""; - CompletionItem[] items = getCompletionItemList("\\", 0, 16, 17, "xsdB1.xsd", "xmlB1.xml"); - testCompletionFor(xml, 2, items); + testCompletionFor(xml, 0); } @Test @@ -113,44 +111,42 @@ public void testFilePathCompletionFolderBAbsolutePathWithFileScheme() throws Bad @Test public void testFilePathCompletionNestedA() throws BadLocationException { - String xml = ""; - CompletionItem[] items = getCompletionItemList("/", 0, 16, 17, "NestedB"); + String xml = ""; + CompletionItem[] items = getCompletionItemList("/", 0, 18, 19, "NestedB"); testCompletionFor(xml, 1, items); } @Test public void testFilePathCompletionNestedABackSlash() throws BadLocationException { - String xml = ""; - CompletionItem[] items = getCompletionItemList("\\", 0, 16, 17, "NestedB"); + String xml = ""; + CompletionItem[] items = getCompletionItemList("\\", 0, 18, 19, "NestedB"); testCompletionFor(xml, 1, items); } @Test public void testFilePathCompletionNestedBIncomplete() throws BadLocationException { - String xml = ""; - CompletionItem[] items = getCompletionItemList("/", 0, 24, 28, "nestedXSD.xsd"); + String xml = ""; + CompletionItem[] items = getCompletionItemList("/", 0, 26, 30, "nestedXSD.xsd"); testCompletionFor(xml, 1, items); } @Test public void testFilePathCompletionNestedBIncompleteBackSlash() throws BadLocationException { - String xml = ""; - CompletionItem[] items = getCompletionItemList("\\", 0, 24, 28, "nestedXSD.xsd"); + String xml = ""; + CompletionItem[] items = getCompletionItemList("\\", 0, 26, 30, "nestedXSD.xsd"); testCompletionFor(xml, 1, items); } @Test public void testFilePathCompletionExtraTextInValue() throws BadLocationException { String xml = ""; - CompletionItem[] items = getCompletionItemList("/", 0, 44, 45, "nestedXSD.xsd"); - testCompletionFor(xml, 1, items); + testCompletionFor(xml, 0); } @Test public void testFilePathCompletionExtraTextInValueBackSlash() throws BadLocationException { String xml = ""; - CompletionItem[] items = getCompletionItemList("\\", 0, 44, 45, "nestedXSD.xsd"); - testCompletionFor(xml, 1, items); + testCompletionFor(xml, 0); } @Test @@ -166,11 +162,8 @@ public void testFilePathCompletionExtraTextInValueAbsolute() throws BadLocationE @Test public void testFilePathCompletionExtraTextInValueAbsoluteBackSlash() throws BadLocationException { String filePath = userDirBackSlash + "\\src\\test\\resources\\filePathCompletion\\NestedA\\NestedB\\"; - int filePathLength = filePath.length(); String xml = ""; - CompletionItem[] items = getCompletionItemList("\\", 0, 29 + filePathLength - 1, 29 + filePathLength, - "nestedXSD.xsd"); - testCompletionFor(xml, 1, items); + testCompletionFor(xml, 0); } @Test @@ -262,15 +255,15 @@ public void testFilePathCompletionDTDFolderABackSlash() throws BadLocationExcept @Test public void testFilePathCompletionDTDFolderB() throws BadLocationException { - String xml = ""; - CompletionItem[] items = getCompletionItemList("/", 0, 29, 30, "xsdB1.xsd", "xmlB1.xml"); + String xml = ""; + CompletionItem[] items = getCompletionItemList("/", 0, 31, 32, "xsdB1.xsd", "xmlB1.xml"); testCompletionFor(xml, 2, items); } @Test public void testFilePathCompletionDTDFolderBBackSlash() throws BadLocationException { - String xml = ""; - CompletionItem[] items = getCompletionItemList("\\", 0, 29, 30, "xsdB1.xsd", "xmlB1.xml"); + String xml = ""; + CompletionItem[] items = getCompletionItemList("\\", 0, 31, 32, "xsdB1.xsd", "xmlB1.xml"); testCompletionFor(xml, 2, items); } @@ -288,8 +281,8 @@ public void testFilePathNoCompletionMissingSystemId() throws BadLocationExceptio @Test public void testFilePathCompletionWithSpacesFolder() throws BadLocationException { - String xml = ""; - CompletionItem[] items = getCompletionItemList("/", 0, 16, 17, "a@b", "with%20spaces"); + String xml = ""; + CompletionItem[] items = getCompletionItemList("/", 0, 18, 19, "a@b", "with%20spaces"); testCompletionFor(xml, 2, items); }