Skip to content

Commit

Permalink
Merge pull request #43 from awslabs/main
Browse files Browse the repository at this point in the history
Sync with upstream
  • Loading branch information
Baccata committed May 31, 2023
2 parents 0964bb7 + b04ece8 commit eeed6dc
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 11 deletions.
Expand Up @@ -69,7 +69,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
loadSmithyBuild(workspaceRoot);
} catch (Exception e) {
LspLog.println("Failure trying to load extensions from workspace root: " + workspaceRoot.getAbsolutePath());
LspLog.println(e.toString());
e.printStackTrace();
}
} else {
LspLog.println("Workspace root was null");
Expand All @@ -78,7 +78,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
if (params.getWorkspaceFolders() == null) {
try {
tempWorkspaceRoot = Files.createTempDirectory("smithy-lsp-workspace").toFile();
System.out.println("Created temporary workspace root: " + tempWorkspaceRoot);
LspLog.println("Created temporary workspace root: " + tempWorkspaceRoot);
tempWorkspaceRoot.deleteOnExit();
WorkspaceFolder workspaceFolder = new WorkspaceFolder(tempWorkspaceRoot.toURI().toString());
params.setWorkspaceFolders(ListUtils.of(workspaceFolder));
Expand All @@ -95,6 +95,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
loadSmithyBuild(root);
} catch (Exception e) {
LspLog.println("Error when loading workspace folder " + ws.toString() + ": " + e);
e.printStackTrace();
}
}

Expand Down
Expand Up @@ -38,6 +38,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionParams;
import org.eclipse.lsp4j.Command;
Expand Down Expand Up @@ -102,6 +103,7 @@ public class SmithyTextDocumentService implements TextDocumentService {
private final List<CompletionItem> baseCompletions = new ArrayList<>();
private Optional<LanguageClient> client;
private final List<Location> noLocations = Collections.emptyList();
@Nullable
private SmithyProject project;
private final File temporaryFolder;

Expand Down Expand Up @@ -142,12 +144,17 @@ public Optional<File> getRoot() {
public void createProject(SmithyBuildExtensions ext, File root) {
Either<Exception, SmithyProject> loaded = SmithyProject.load(ext, root);
if (loaded.isRight()) {
this.project = loaded.getRight();
SmithyProject project = loaded.getRight();
this.project = project;
clearAllDiagnostics();
sendInfo("Project loaded with " + this.project.getExternalJars().size() + " external jars and "
+ this.project.getSmithyFiles().size() + " discovered smithy files");
sendInfo("Project loaded with " + project.getExternalJars().size() + " external jars and "
+ project.getSmithyFiles().size() + " discovered smithy files");
} else {
sendError("Failed to create Smithy project: " + loaded.getLeft().toString());
sendError(
"Failed to create Smithy project. See output panel for details. Uncaught exception: "
+ loaded.getLeft().toString()
);
loaded.getLeft().printStackTrace();
}
}

Expand All @@ -170,6 +177,7 @@ public void createProject(File root) {
LspLog.println("Loaded build extensions " + local + " from " + smithyBuild.getAbsolutePath());
} catch (Exception e) {
LspLog.println("Failed to load config from" + smithyBuild + ": " + e);
e.printStackTrace();
brokenFiles.add(smithyBuild.toString());
}
}
Expand All @@ -178,7 +186,10 @@ public void createProject(File root) {
if (brokenFiles.isEmpty()) {
createProject(result.build(), root);
} else {
sendError("Failed to load the build, following files have problems: \n" + String.join("\n", brokenFiles));
sendError(
"Failed to load the build, the following build files have problems: \n"
+ String.join("\n", brokenFiles)
);
}
}

Expand Down Expand Up @@ -210,6 +221,7 @@ public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completio
} catch (Exception e) {
LspLog.println(
"Failed to identify token for completion in " + params.getTextDocument().getUri() + ": " + e);
e.printStackTrace();
}
return Utils.completableFuture(Either.forLeft(baseCompletions));
}
Expand Down Expand Up @@ -438,7 +450,7 @@ public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> docume
.collect(Collectors.toList())
);
} catch (Exception e) {
e.printStackTrace(System.err);
e.printStackTrace();

return Utils.completableFuture(Collections.emptyList());
}
Expand Down Expand Up @@ -526,6 +538,7 @@ public CompletableFuture<Hover> hover(HoverParams params) {
}
} catch (Exception e) {
LspLog.println("Failed to determine hover content: " + e);
e.printStackTrace();
}

hover.setContents(content);
Expand Down Expand Up @@ -612,6 +625,7 @@ public void didChange(DidChangeTextDocumentParams params) {
} catch (Exception e) {
LspLog.println("Failed to write temporary contents for file " + original + " into temporary file "
+ tempFile + " : " + e);
e.printStackTrace();
}

report(recompile(original, Optional.ofNullable(tempFile)));
Expand Down
Expand Up @@ -63,7 +63,12 @@ public Map<ShapeId, Location> collectDefinitionLocations(Model model) {
this.membersToUpdateMap = new HashMap<>();

for (ModelFile modelFile : this.fileCache.values()) {
collectContainerShapeLocationsInModelFile(modelFile);
try {
collectContainerShapeLocationsInModelFile(modelFile);
} catch (Exception e) {
throw new RuntimeException("Exception while collecting container shape locations in model file: "
+ modelFile.filename, e);
}
}

operationsWithInlineInputOutputMap.forEach((this::collectInlineInputOutputLocations));
Expand Down Expand Up @@ -234,9 +239,12 @@ private Optional<OperationShape> getOperationForInlinedInputOrOutput(Shape shape
) {
String suffix = getOperationInputOrOutputSuffix(shape, preamble);
String shapeName = shape.getId().getName();

String matchingOperationName = shapeName.substring(0, shapeName.length() - suffix.length());
ShapeId matchingOperationId = ShapeId.fromParts(shape.getId().getNamespace(), matchingOperationName);

return model.shapes(OperationShape.class)
.filter(operationShape -> operationShape.getId().getName().equals(matchingOperationName))
.filter(operationShape -> operationShape.getId().equals(matchingOperationId))
.findFirst()
.filter(operation -> shapeWasDefinedInline(operation, shape, modelFile));
}
Expand Down
Expand Up @@ -172,7 +172,13 @@ private static Either<Exception, SmithyProject> load(
return Either.forLeft(model.getLeft());
} else {
model.getRight().getValidationEvents().forEach(LspLog::println);
return Either.forRight(new SmithyProject(imports, smithyFiles, externalJars, root, model.getRight()));

try {
SmithyProject p = new SmithyProject(imports, smithyFiles, externalJars, root, model.getRight());
return Either.forRight(p);
} catch (Exception e) {
return Either.forLeft(e);
}
}
}

Expand Down
Expand Up @@ -149,6 +149,23 @@ public void allowsEmptyStructsWithMixins() throws Exception {
}
}

// https://github.com/awslabs/smithy-language-server/issues/110
// Note: This test is flaky, it may succeed even if the code being tested is incorrect.
@Test
public void handlesSameOperationNameBetweenNamespaces() throws Exception {
Path baseDir = Paths.get(SmithyProjectTest.class.getResource("models/operation-name-conflict").toURI());
Path modelA = baseDir.resolve("a.smithy");
Path modelB = baseDir.resolve("b.smithy");
List<Path> modelFiles = ListUtils.of(modelA, modelB);

try (Harness hs = Harness.create(SmithyBuildExtensions.builder().build(), modelFiles)) {
Map<ShapeId, Location> locationMap = hs.getProject().getLocations();

correctLocation(locationMap, "a#HelloWorld", 4, 0, 13, 1);
correctLocation(locationMap, "b#HelloWorld", 6, 0, 15, 1);
}
}

@Test
public void definitionLocationsV1() throws Exception {
Path baseDir = Paths.get(SmithyProjectTest.class.getResource("models/v1").toURI());
Expand Down
@@ -0,0 +1,14 @@
$version: "2"

namespace a

operation HelloWorld {
input := {
@required
name: String
}
output := {
@required
name: String
}
}
@@ -0,0 +1,16 @@
$version: "2"

namespace b

string Ignored

operation HelloWorld {
input := {
@required
name: String
}
output := {
@required
name: String
}
}

0 comments on commit eeed6dc

Please sign in to comment.