Skip to content

Commit

Permalink
Account for line sep length
Browse files Browse the repository at this point in the history
  • Loading branch information
milesziemer committed Jun 13, 2024
1 parent cfe22db commit 4024cee
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,8 @@ private static int[] computeLineIndicies(StringBuilder buffer) {
List<Integer> indicies = new ArrayList<>();
indicies.add(0);
while ((next = buffer.indexOf(System.lineSeparator(), off)) != -1) {
indicies.add(next + 1);
off = next + 1;
indicies.add(next + System.lineSeparator().length());
off = next + System.lineSeparator().length();
++matchCount;
}
return indicies.stream().mapToInt(Integer::intValue).toArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private ProjectLoader() {
* @return The loaded project
*/
public static Project loadDetached(String uri, String text) {
LOGGER.info("Loading detached project at " + uri);
String asPath = UriAdapter.toPath(uri);
ValidatedResult<Model> modelResult = Model.assembler()
.addUnparsedModel(asPath, text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.logging.Logger;

/**
Expand All @@ -28,7 +29,7 @@ private UriAdapter() {
*/
public static String toPath(String uri) {
if (uri.startsWith("file://")) {
return uri.replaceFirst("file://", "");
return Paths.get(URI.create(uri)).toString();
} else if (isSmithyJarFile(uri)) {
String decoded = decode(uri);
return fixJarScheme(decoded);
Expand All @@ -42,12 +43,12 @@ public static String toPath(String uri) {
* correct scheme for jars
*/
public static String toUri(String path) {
if (path.startsWith("/")) {
return "file://" + path;
} else if (path.startsWith("jar:file")) {
if (path.startsWith("jar:file")) {
return path.replaceFirst("jar:file", "smithyjar");
} else {
} else if (path.startsWith("smithyjar:")) {
return path;
} else {
return Paths.get(path).toUri().toString();
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/test/java/software/amazon/smithy/lsp/TestWorkspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public String getUri(String filename) {
*/
public void addModel(String relativePath, String model) {
try {
Files.write(root.resolve(relativePath), model.getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve(relativePath), model.replace("\n", System.lineSeparator())
.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -172,7 +173,8 @@ protected void writeModels(Path toDir) {

private static void writeModels(Path toDir, Map<String, String> models) throws Exception {
for (Map.Entry<String, String> entry : models.entrySet()) {
Files.write(toDir.resolve(entry.getKey()), entry.getValue().getBytes(StandardCharsets.UTF_8));
Files.write(toDir.resolve(entry.getKey()),
entry.getValue().replace("\n", System.lineSeparator()).getBytes(StandardCharsets.UTF_8));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
package software.amazon.smithy.lsp.project;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasSize;
import static software.amazon.smithy.lsp.project.ProjectTest.toPath;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
Expand All @@ -24,7 +24,7 @@ public class ProjectConfigLoaderTest {
@Test
public void loadsConfigWithEnvVariable() {
System.setProperty("FOO", "bar");
Path root = Paths.get(getClass().getResource("env-config").getPath());
Path root = toPath(getClass().getResource("env-config"));
Result<ProjectConfig, List<Exception>> result = ProjectConfigLoader.loadFromRoot(root);

assertThat(result.isOk(), is(true));
Expand All @@ -40,7 +40,7 @@ public void loadsConfigWithEnvVariable() {

@Test
public void loadsLegacyConfig() {
Path root = Paths.get(getClass().getResource("legacy-config").getPath());
Path root = toPath(getClass().getResource("legacy-config"));
Result<ProjectConfig, List<Exception>> result = ProjectConfigLoader.loadFromRoot(root);

assertThat(result.isOk(), is(true));
Expand All @@ -55,7 +55,7 @@ public void loadsLegacyConfig() {

@Test
public void prefersNonLegacyConfig() {
Path root = Paths.get(getClass().getResource("legacy-config-with-conflicts").getPath());
Path root = toPath(getClass().getResource("legacy-config-with-conflicts"));
Result<ProjectConfig, List<Exception>> result = ProjectConfigLoader.loadFromRoot(root);

assertThat(result.isOk(), is(true));
Expand All @@ -70,7 +70,7 @@ public void prefersNonLegacyConfig() {

@Test
public void mergesBuildExts() {
Path root = Paths.get(getClass().getResource("build-exts").getPath());
Path root = toPath(getClass().getResource("build-exts"));
Result<ProjectConfig, List<Exception>> result = ProjectConfigLoader.loadFromRoot(root);

assertThat(result.isOk(), is(true));
Expand Down
48 changes: 35 additions & 13 deletions src/test/java/software/amazon/smithy/lsp/project/ProjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
import static software.amazon.smithy.lsp.UtilMatchers.anOptionalOf;
import static software.amazon.smithy.lsp.document.DocumentTest.string;

import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.lsp.TestWorkspace;
Expand All @@ -43,7 +46,7 @@
public class ProjectTest {
@Test
public void loadsFlatProject() {
Path root = Paths.get(getClass().getResource("flat").getPath());
Path root = toPath(getClass().getResource("flat"));
Project project = ProjectLoader.load(root).unwrap();

assertThat(project.getRoot(), equalTo(root));
Expand All @@ -56,7 +59,7 @@ public void loadsFlatProject() {

@Test
public void loadsProjectWithMavenDep() {
Path root = Paths.get(getClass().getResource("maven-dep").getPath());
Path root = toPath(getClass().getResource("maven-dep"));
Project project = ProjectLoader.load(root).unwrap();

assertThat(project.getRoot(), equalTo(root));
Expand All @@ -69,7 +72,7 @@ public void loadsProjectWithMavenDep() {

@Test
public void loadsProjectWithSubdir() {
Path root = Paths.get(getClass().getResource("subdirs").getPath());
Path root = toPath(getClass().getResource("subdirs"));
Project project = ProjectLoader.load(root).unwrap();

assertThat(project.getRoot(), equalTo(root));
Expand All @@ -89,7 +92,7 @@ public void loadsProjectWithSubdir() {

@Test
public void loadsModelWithUnknownTrait() {
Path root = Paths.get(getClass().getResource("unknown-trait").getPath());
Path root = toPath(getClass().getResource("unknown-trait"));
Project project = ProjectLoader.load(root).unwrap();

assertThat(project.getRoot(), equalTo(root));
Expand All @@ -106,7 +109,7 @@ public void loadsModelWithUnknownTrait() {

@Test
public void loadsWhenModelHasInvalidSyntax() {
Path root = Paths.get(getClass().getResource("invalid-syntax").getPath());
Path root = toPath(getClass().getResource("invalid-syntax"));
Project project = ProjectLoader.load(root).unwrap();

assertThat(project.getRoot(), equalTo(root));
Expand Down Expand Up @@ -140,7 +143,7 @@ public void loadsWhenModelHasInvalidSyntax() {

@Test
public void loadsProjectWithMultipleNamespaces() {
Path root = Paths.get(getClass().getResource("multiple-namespaces").getPath());
Path root = Paths.get(getClass().getResource("multiple-namespaces").getFile());
Project project = ProjectLoader.load(root).unwrap();

assertThat(project.getSources(), hasItem(root.resolve("model")));
Expand Down Expand Up @@ -176,7 +179,7 @@ public void loadsProjectWithMultipleNamespaces() {

@Test
public void loadsProjectWithExternalJars() {
Path root = Paths.get(getClass().getResource("external-jars").getPath());
Path root = toPath(getClass().getResource("external-jars"));
Result<Project, List<Exception>> result = ProjectLoader.load(root);

assertThat(result.isOk(), is(true));
Expand All @@ -200,23 +203,23 @@ public void loadsProjectWithExternalJars() {

@Test
public void failsLoadingInvalidSmithyBuildJson() {
Path root = Paths.get(getClass().getResource("broken/missing-version").getPath());
Path root = toPath(getClass().getResource("broken/missing-version"));
Result<Project, List<Exception>> result = ProjectLoader.load(root);

assertThat(result.isErr(), is(true));
}

@Test
public void failsLoadingUnparseableSmithyBuildJson() {
Path root = Paths.get(getClass().getResource("broken/parse-failure").getPath());
Path root = toPath(getClass().getResource("broken/parse-failure"));
Result<Project, List<Exception>> result = ProjectLoader.load(root);

assertThat(result.isErr(), is(true));
}

@Test
public void doesntFailLoadingProjectWithNonExistingSource() {
Path root = Paths.get(getClass().getResource("broken/source-doesnt-exist").getPath());
Path root = toPath(getClass().getResource("broken/source-doesnt-exist"));
Result<Project, List<Exception>> result = ProjectLoader.load(root);

assertThat(result.isErr(), is(false));
Expand All @@ -226,23 +229,23 @@ public void doesntFailLoadingProjectWithNonExistingSource() {

@Test
public void failsLoadingUnresolvableMavenDependency() {
Path root = Paths.get(getClass().getResource("broken/unresolvable-maven-dependency").getPath());
Path root = toPath(getClass().getResource("broken/unresolvable-maven-dependency"));
Result<Project, List<Exception>> result = ProjectLoader.load(root);

assertThat(result.isErr(), is(true));
}

@Test
public void failsLoadingUnresolvableProjectDependency() {
Path root = Paths.get(getClass().getResource("broken/unresolvable-maven-dependency").getPath());
Path root = toPath(getClass().getResource("broken/unresolvable-maven-dependency"));
Result<Project, List<Exception>> result = ProjectLoader.load(root);

assertThat(result.isErr(), is(true));
}

@Test
public void loadsProjectWithUnNormalizedDirs() {
Path root = Paths.get(getClass().getResource("unnormalized-dirs").getPath());
Path root = toPath(getClass().getResource("unnormalized-dirs"));
Project project = ProjectLoader.load(root).unwrap();

assertThat(project.getRoot(), equalTo(root));
Expand Down Expand Up @@ -520,6 +523,17 @@ public void changingFileWithArrayDependenciesWithDependencies() {

String uri = workspace.getUri("model-0.smithy");
Document document = project.getDocument(uri);
if (document == null) {
String smithyFilesPaths = String.join(System.lineSeparator(), project.getSmithyFiles().keySet());
String smithyFilesUris = project.getSmithyFiles().keySet().stream()
.map(UriAdapter::toUri)
.collect(Collectors.joining(System.lineSeparator()));
Logger logger = Logger.getLogger(getClass().getName());
logger.severe("Not found uri: " + uri);
logger.severe("Not found path: " + UriAdapter.toPath(uri));
logger.severe("PATHS: " + smithyFilesPaths);
logger.severe("URIS: " + smithyFilesUris);
}
document.applyEdit(RangeAdapter.point(document.end()), "\n");

project.updateModelWithoutValidating(uri);
Expand Down Expand Up @@ -592,4 +606,12 @@ public void removingArrayApply() {
assertThat(bar.hasTrait("tags"), is(true));
assertThat(bar.expectTrait(TagsTrait.class).getTags(), containsInAnyOrder("bar"));
}

public static Path toPath(URL url) {
try {
return Paths.get(url.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 4024cee

Please sign in to comment.