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 7fe5630bc..0189ca6fa 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 e13315fa0..953f69ec3 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 b32cff597..258b47b87 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; @@ -62,10 +63,17 @@ 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")); + 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); Path p = Paths.get(pathString); pathString = p.normalize().toString(); return pathString; 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 a198bf6a6..5d1df6223 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 69cf345b4..e5e5cd24d 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 java.io.File.separator; /** * 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" + separator +"generated-test-sources"; @After public void cleanup() { - String path = System.getProperty("user.dir") + "/" + targetTestFolder + "/" + testFolder; + String path = System.getProperty("user.dir") + separator + targetTestFolder + separator + 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 + separator + 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") + separator + testFolder + separator + "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 0de987ae3..fffb75827 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 @@ -17,6 +17,7 @@ import java.nio.file.Paths; import org.junit.Test; +import static java.io.File.separator; /** * FilesUtilsTest @@ -27,18 +28,19 @@ 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(Paths.get(newBasePathString, newSubPathString).toString(), 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(Paths.get(System.getProperty("user.home"), "Test", "Folder").toString(), FilesUtils.normalizePath("~/Test/Folder")); + assertEquals(Paths.get(separator, "Test", "~", "Folder").toString(), FilesUtils.normalizePath("/Test/~/Folder")); + assertEquals(Paths.get("~", "Test", "Folder").toString(), FilesUtils.normalizePath("./~/Test/Folder")); + assertEquals(Paths.get(separator, "Folder").toString(), FilesUtils.normalizePath("/Test/../Folder")); + assertEquals(Paths.get(separator, "Users", "Nikolas").toString(), FilesUtils.normalizePath("\\Users\\Nikolas\\")); } } \ 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 d4110863c..f6e7382ab 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 @@ -10,6 +10,7 @@ package org.eclipse.lsp4xml.utils; +import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -23,7 +24,7 @@ public class ProjectUtils { * @return the current lsp4xml project directory */ public static Path getProjectDirectory() { - String currPath = ProjectUtils.class.getClassLoader().getResource("").getFile(); + String currPath = new File(ProjectUtils.class.getClassLoader().getResource("").getPath()).toString(); Path dir = Paths.get(currPath); while (!Files.exists(dir.resolve("pom.xml")) && dir.getParent() != null) { dir = dir.getParent();