diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLLanguageServer.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLLanguageServer.java
index 7fe5630bca..0189ca6fa4 100644
--- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLLanguageServer.java
+++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLLanguageServer.java
@@ -51,6 +51,7 @@
import org.eclipse.lsp4xml.settings.InitializationOptionsSettings;
import org.eclipse.lsp4xml.settings.LogsSettings;
import org.eclipse.lsp4xml.settings.ServerSettings;
+import org.eclipse.lsp4xml.settings.SharedSettings;
import org.eclipse.lsp4xml.settings.XMLGeneralClientSettings;
import org.eclipse.lsp4xml.settings.XMLExperimentalCapabilities;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;
@@ -208,6 +209,10 @@ public XMLLanguageService getXMLLanguageService() {
return xmlLanguageService;
}
+ public SharedSettings getSettings() {
+ return xmlTextDocumentService.getSharedSettings();
+ }
+
public ScheduledFuture> schedule(Runnable command, int delay, TimeUnit unit) {
return delayer.schedule(command, delay, unit);
}
diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/ServerSettings.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/ServerSettings.java
index e13315fa0b..953f69ec38 100644
--- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/ServerSettings.java
+++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/ServerSettings.java
@@ -17,9 +17,10 @@
*/
public class ServerSettings {
+ public static final String DEFAULT_WORK_DIR = "~/.lsp4xml";
+
private String workDir;
-
/**
* @return the workDir
*/
@@ -34,7 +35,16 @@ public void setWorkDir(String workDir) {
this.workDir = workDir;
}
+ /**
+ * Returns a normalized workDir that was defined in the client preferences.
+ *
+ * If null or empty, returns a default path.
+ *
+ */
public String getNormalizedWorkDir() {
+ if(workDir == null || workDir.isEmpty()) {
+ workDir = DEFAULT_WORK_DIR;
+ }
return FilesUtils.normalizePath(workDir);
}
diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/FilesUtils.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/FilesUtils.java
index b32cff597f..60ec349d30 100644
--- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/FilesUtils.java
+++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/FilesUtils.java
@@ -10,6 +10,7 @@
*/
package org.eclipse.lsp4xml.utils;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
@@ -18,6 +19,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
+import java.util.regex.Matcher;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
@@ -30,6 +32,8 @@ public class FilesUtils {
public static final String LSP4XML_WORKDIR_KEY = "lsp4xml.workdir";
private static String cachePathSetting = null;
+ public static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");
+ public static final String SLASH = File.separator;
public static String getCachePathSetting() {
return cachePathSetting;
@@ -62,15 +66,26 @@ public static void resetDeployPath() {
/**
* Given a file path as a string, will normalize it
* and return the normalized string if valid, or null if not.
+ *
+ * The '~' home symbol will be converted into the actual home path.
+ * Slashes will be corrected depending on the OS.
*/
public static String normalizePath(String pathString) {
- if(pathString != null && !pathString.isEmpty()) {
- pathString = pathString.replaceFirst("^~", System.getProperty("user.home"));
- Path p = Paths.get(pathString);
- pathString = p.normalize().toString();
- return pathString;
- }
- return null;
+
+ if (pathString != null && !pathString.isEmpty()) {
+ if (pathString.indexOf("~") == 0) {
+ pathString = System.getProperty("user.home") + (pathString.length() > 1? pathString.substring(1):"");
+ }
+ pathString = pathString.replace("/", File.separator);
+ pathString = pathString.replace("\\", File.separator);
+ if(isWindows){
+ pathString = pathString.replaceAll("^(\\\\)+", "");
+ }
+ Path p = Paths.get(pathString);
+ pathString = p.normalize().toString();
+ return pathString;
+ }
+ return null;
}
private static Path getDeployedBasePath() {
diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/services/XMLFormatterTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/services/XMLFormatterTest.java
index a198bf6a69..5d1df6223c 100644
--- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/services/XMLFormatterTest.java
+++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/services/XMLFormatterTest.java
@@ -397,7 +397,7 @@ public void testCommentFormatSameLine() throws BadLocationException {
String expected =
"" + lineSeparator() +
" Content" + lineSeparator() +
- " \n";
+ " " + lineSeparator();
XMLFormattingOptions formattingOptions = createDefaultFormattingOptions();
formattingOptions.setJoinCommentLines(true);
@@ -1544,8 +1544,8 @@ public void testUseSingleQuotesNoQuotesSplit() throws BadLocationException {
String content =
" ";
String expected =
- "";
format(content, expected, formattingOptions);
}
diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/settings/SettingsTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/settings/SettingsTest.java
index 69cf345b44..be237477d6 100644
--- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/settings/SettingsTest.java
+++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/settings/SettingsTest.java
@@ -19,13 +19,13 @@
import org.junit.Assert;
import org.junit.Test;
-import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.File;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
+import static org.eclipse.lsp4xml.utils.FilesUtils.SLASH;
/**
* Tests for settings.
@@ -33,12 +33,12 @@
public class SettingsTest {
private static String testFolder = "TestXMLCacheFolder";
- private static String targetTestFolder = "target/generated-test-sources";
+ private static String targetTestFolder = "target" + SLASH +"generated-test-sources";
@After
public void cleanup() {
- String path = System.getProperty("user.dir") + "/" + targetTestFolder + "/" + testFolder;
+ String path = System.getProperty("user.dir") + SLASH + targetTestFolder + SLASH + testFolder;
File f = new File(path);
if (f.exists()) {
@@ -49,7 +49,8 @@ public void cleanup() {
private final String json = "{\r\n" + //
" \"settings\": {\r\n" + //
// Content model settings
- " \"xml\": {\r\n" + " \"fileAssociations\": [\r\n" + //
+ " \"xml\": {\r\n" +
+ " \"fileAssociations\": [\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\spring-beans-3.0.xsd\",\r\n" + //
" \"pattern\": \"**/test*.xml\"\r\n" + //
@@ -75,9 +76,13 @@ public void cleanup() {
" \"formatComments\": true,\r\n" + //
" \"joinCommentLines\": true,\r\n" + //
" \"quotations\": " + XMLFormattingOptions.DOUBLE_QUOTES_VALUE + "\r\n" + //
- " },\r\n" + " \"server\": {\r\n" + //
+ " },\r\n" +
+ " \"server\": {\r\n" + //
" \"workDir\": \"~/" + testFolder + "/Nested\"\r\n" + //
- " }\r\n" + " }\r\n" + " }\r\n" + "}";
+ " }\r\n" +
+ " }\r\n" +
+ " }\r\n" +
+ "}";
@Test
public void initializationOptionsSettings() {
@@ -174,12 +179,12 @@ public void cachePathSettings() {
String originalUserHome = System.getProperty("user.home");
String userDir = System.getProperty("user.dir");
try {
- System.setProperty("user.home", userDir + "/" + targetTestFolder); // .../org.eclipse.lsp4xml/target/generated-test-sources/
+ System.setProperty("user.home", userDir + SLASH + targetTestFolder); // .../org.eclipse.lsp4xml/target/generated-test-sources/
languageServer.updateSettings(initializationOptionsSettings);
//Ensure the expanded absolute path is being used.
- Assert.assertEquals(System.getProperty("user.home") + "/" + testFolder + "/Nested", FilesUtils.getCachePathSetting());
+ Assert.assertEquals(System.getProperty("user.home") + SLASH + testFolder + SLASH + "Nested", FilesUtils.getCachePathSetting());
} catch (Exception e) {
fail();
} finally {
@@ -187,8 +192,5 @@ public void cachePathSettings() {
FilesUtils.setCachePathSetting(null);
System.setProperty("user.home", originalUserHome);
}
-
-
-
}
}
diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/utils/FilesUtilsTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/utils/FilesUtilsTest.java
index 0de987ae3c..b49073c8c1 100644
--- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/utils/FilesUtilsTest.java
+++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/utils/FilesUtilsTest.java
@@ -18,6 +18,8 @@
import org.junit.Test;
+import static org.eclipse.lsp4xml.utils.FilesUtils.*;
+
/**
* FilesUtilsTest
*/
@@ -27,18 +29,27 @@ public class FilesUtilsTest {
public void testFilesCachePathPreference() throws Exception {
System.clearProperty(FilesUtils.LSP4XML_WORKDIR_KEY);
String newBasePathString = System.getProperty("user.home");
- String newSubPathString = "New/Sub/Path";
+ String newSubPathString = Paths.get("New", "Sub", "Path").toString();
Path newSubPath = Paths.get(newSubPathString);
FilesUtils.setCachePathSetting(newBasePathString);
Path finalPath = FilesUtils.getDeployedPath(newSubPath);
- assertEquals(newBasePathString + "/" + newSubPathString, finalPath.toString());
+ assertEquals(newBasePathString + SLASH + newSubPathString, finalPath.toString());
}
@Test
public void normalizePathTest() {
- assertEquals(System.getProperty("user.home") + "/Test/Folder", FilesUtils.normalizePath("~/Test/Folder"));
- assertEquals("/Test/~/Folder", FilesUtils.normalizePath("/Test/~/Folder"));
- assertEquals("~/Test/Folder", FilesUtils.normalizePath("./~/Test/Folder"));
- assertEquals("/Folder", FilesUtils.normalizePath("/Test/../Folder"));
+ assertEquals(System.getProperty("user.home") + SLASH + "Test" + SLASH +"Folder", FilesUtils.normalizePath("~/Test/Folder"));
+ assertEquals((isWindows ? "" : SLASH) + "Test" + SLASH + "~" + SLASH + "Folder", FilesUtils.normalizePath("/Test/~/Folder"));
+ assertEquals("~" + SLASH + "Test" + SLASH + "Folder", FilesUtils.normalizePath("./~/Test/Folder"));
+ assertEquals((isWindows ? "" : SLASH) + "Folder", FilesUtils.normalizePath("/Test/../Folder"));
+ assertEquals((isWindows ? "" : SLASH) + "Users" + SLASH + "Nikolas", FilesUtils.normalizePath("\\Users\\Nikolas\\"));
+ }
+
+ @Test
+ public void testLeadingSlashes() {
+ String slashes = "\\\\\\";
+ assertEquals((isWindows ? "" : SLASH) + "D:" + SLASH + "Users" + SLASH + "Nikolas" + SLASH + "Desktop", FilesUtils.normalizePath(slashes + "D:\\Users\\Nikolas\\Desktop"));
+ assertEquals((isWindows ? "" : SLASH) + "D:" + SLASH + "Users" + SLASH + "Nikolas" + SLASH + "Desktop", FilesUtils.normalizePath("\\D:\\Users\\Nikolas\\Desktop"));
+
}
}
\ No newline at end of file
diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/utils/ProjectUtils.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/utils/ProjectUtils.java
index d4110863c0..a92ad22af9 100644
--- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/utils/ProjectUtils.java
+++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/utils/ProjectUtils.java
@@ -24,6 +24,7 @@ public class ProjectUtils {
*/
public static Path getProjectDirectory() {
String currPath = ProjectUtils.class.getClassLoader().getResource("").getFile();
+ currPath = FilesUtils.normalizePath(currPath);
Path dir = Paths.get(currPath);
while (!Files.exists(dir.resolve("pom.xml")) && dir.getParent() != null) {
dir = dir.getParent();