Skip to content

Commit

Permalink
Add support for ~/ and ~\ in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
me-johnomar authored and valentjn committed Aug 22, 2020
1 parent a78d7e2 commit 32eb5dd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
18 changes: 15 additions & 3 deletions ltexls-core/src/main/java/org/bsplines/ltexls/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.checkerframework.checker.initialization.qual.UnknownInitialization;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.eclipse.lsp4j.DiagnosticSeverity;
Expand All @@ -25,6 +26,7 @@ public class Settings {
Arrays.asList("AutoLink", "Code");
private static final List<String> defaultIgnoreMarkdownNodeTypes =
Arrays.asList("CodeBlock", "FencedCodeBlock", "IndentedCodeBlock");
private static final Pattern tildePathPattern = Pattern.compile("^~($|/|\\\\)");

private @Nullable Boolean enabled = null;
private @Nullable String languageShortCode = null;
Expand Down Expand Up @@ -87,6 +89,16 @@ public Settings(JsonElement jsonSettings) {
setSettings(jsonSettings);
}

private static String normalizePath(String path) {
@Nullable String homeDirPath = System.getProperty("user.home");

if (homeDirPath != null) {
path = tildePathPattern.matcher(path).replaceFirst(homeDirPath + "$1");
}

return path;
}

private static Map<String, List<String>> copyMapOfLists(Map<String, List<String>> map) {
Map<String, List<String>> mapCopy = new HashMap<>();

Expand Down Expand Up @@ -551,15 +563,15 @@ public String getMotherTongueShortCode() {
}

public String getLanguageModelRulesDirectory() {
return getDefault(this.languageModelRulesDirectory, "");
return normalizePath(getDefault(this.languageModelRulesDirectory, ""));
}

public String getNeuralNetworkModelRulesDirectory() {
return getDefault(this.neuralNetworkModelRulesDirectory, "");
return normalizePath(getDefault(this.neuralNetworkModelRulesDirectory, ""));
}

public String getWord2VecModelRulesDirectory() {
return getDefault(this.word2VecModelRulesDirectory, "");
return normalizePath(getDefault(this.word2VecModelRulesDirectory, ""));
}

public Integer getSentenceCacheSize() {
Expand Down
12 changes: 11 additions & 1 deletion ltexls-core/src/test/java/org/bsplines/ltexls/SettingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,14 @@ public void testProperties() {
Assertions.assertEquals(DiagnosticSeverity.Error, settings.getDiagnosticSeverity());
settings2 = compareSettings(settings, settings2);
}
}

@Test
public void testTildeExpansion() {
Settings settings = new Settings();
String originalDirPath = "~/tildeExpansion";
settings = settings.withLanguageModelRulesDirectory(originalDirPath);
String expandedDirPath = settings.getLanguageModelRulesDirectory();
Assertions.assertTrue(expandedDirPath.endsWith("tildeExpansion"));
Assertions.assertNotEquals(originalDirPath, expandedDirPath);
}
}

0 comments on commit 32eb5dd

Please sign in to comment.