Skip to content

Commit

Permalink
Upgrade completions, definition, hover (#166)
Browse files Browse the repository at this point in the history
* Upgrade completions, definition, hover

This commit is a rewrite of how language features (i.e. completions,
definition, hover) are implemented. It improves the accuracy and expands
the functionality of each feature significantly. Improvements include:
- Completions
    - Trait values
    - Builtin control keys and metadata
    - Namespaces, based on other namespaces in the project
    - Keywords
    - Member names (like inside resources, maps)
    - Member values (like inside the list of operation errors, resource
      property targets, etc.)
    - Elided members
    - Some trait values have special completions, like `examples` has
      completions for the target operation's input/output parameters
- Definition
    - Trait values
    - Elided members
    - Shape ids referenced within trait values
- Hover
    - Trait values
    - Elided members
    - Builtin metadata

There's a lot going on here, but there's a few key pieces of this commit
that all work together to make this work:

At the core of these improvements is the addition of a custom parser for
the IDL that provides the needed syntactic information to implement
these features. See the javadoc on the Syntax class for more details on
how the parser works, and why it was written that way. At a high level
though, the parser produces a flat list of `Syntax.Statement`, and that
list is searched through to find things, such as the statement the
cursor is currently in. It is also used to search 'around' a statement,
like to find the shape a trait is being applied to.

Another key piece of these changes is `NodeCursor` and `NodeSearch`.
There are a few places in the syntax of a smithy file where you may have
a node value whose structure is (or can be) described by a Smithy model.
For example, trait values. `NodeCursor` is basically two things: 1. A
path from the start of a `Node` to a position within that `Node`, 2. An
index into that path. `NodeSearch` is used to search a model along the
path of a `NodeCursor`, from a starting shape. For example, when the
cursor is within a trait value, the `NodeCursor` is that path from the
root of the trait value, to the cursor position, and `NodeSearch` is
used to search in the model, starting at the trait's definition, along
the path of the `NodeCursor`, to find what shape corresponds to the
cursor's location. That shape can then be used e.g. to provide completions.

Finally, there's the `Builtins` class, and the corresponding Smithy
model it uses. I originally had a completely different abstraction for
describing the structure of metadata, different shape types' members,
and even `smithy-build.json`. But it was basically just a 'structured
graph', like a Smithy model. So I decided to just _use_ a Smithy model
itself, since I already had the abstractions for traversing it (like I
had to for trait values). The `Builtins` model contains shapes that
define the structure of certain Smithy constructs. For example, I use it
to model the shape of builtin metadata, like suppressions. I also use it
to model the shape of shapes, that is, what members shapes have, and
what their targets are. Some shapes in this model are considered
'builtins' (in the builtins.smithy files). Builtins are shapes that
require some custom processing, or have some special meaning, like
`AnyNamespace`, which is used for describing a namespace that can be
used in
https://smithy.io/2.0/spec/model-validation.html#suppression-metadata.
The builtin model pretty 'meta', and I don't _love_ it, but it reduces a
significant amount of duplicated logic. For example, if we want to give
documentation for some metadata, it is as easy as adding it to the
builtins model. We can also use it to add support for smithy-build.json
completions, hover, and even validation, later. It would be nice if
these definitions lived elsewhere, so other tooling could consume them,
like the Smithy docs for example, and I have some other ideas on how we
can use it, but they're out of scope here.

Testing for this commit comes mostly from the completions, definitions,
and hover tests, which indirectly test lower-level components like the
parser (there are still some parser tests, though).

* Address feedback

* Refactoring

This commit keeps the functionality added to the language features in
the previous commits, but does some broad refactoring of those changes
to clean up the APIs, get rid of some footguns, and reduce the chance of
some concurrency/parallelism issues.

The main changes are:
- Syntax.Ident/Syntax.Node.Str produced by the new parser now copy the
  actual string value. Previously, they only stored the start/end
  positions, and required you to copy the value out of the Document
  on-demand. This reduced the memory footprint of parsing, but I was
  concerned about the Document being changed at the same time another
  thread is trying to copy a value out of it. Copying eagerly avoids
  this. Plus, we can avoid most of the memory issues by doing partial
  reparsing (more on that later).
- Project now stores an index of files -> shapes defined in that file,
  instead of storing the shapes on the SmithyFile. This index is only
  needed to help determine which shapes need to be removed when
  rebuilding the model, so it doesn't make sense for SmithyFile to know
  about it. This also ties into the next change...
- Multiple changes to SmithyFile. SmithyFile now has a subclass,
  IdlFile, which stores its parse result. With the addition of the
  parser, and the changes to make
  DocumentVersion/DocumentImports/DocumentNamespace be computed from the
  parse result, SmithyFile can't represent both IDL and AST files.
  Arguably, it never really did because AST files don't have
  namespaces/imports. Either way, IdlFile now provides access to the
  parse result, which contains DocumentNamespace/Version/Imports, as
  well as the parsed statements. I also added synchronization to handle
  access to the parse result, since it will be mutated on every change.
  I don't really like how this works, but I'm going to address that in a
  future update (which I will describe below).
- Added StatementView, which wraps a list of parsed statements and a
  specific index in that list, providing methods to look "around" that
  index. This replaces the error-prone and unreadable SyntaxSearch,
  which required you to pass around int indicies everywhere.

Some more minor changes to note:
- Moved diagnostics computation into SmithyDiagnostics. It already
  belonged there probably, but especially with the addition of IdlFile I
  just had to do it.
- Moved document symbols into a 'handler' like definition, etc.
- Added `uri` and `isDetached` properties to ProjectAndFile, for
  convenience.

There are still some rough edges with this code, but I plan on making a
follow up PR to address them, so I this one doesn't become even larger.
Specifically, I want to only parse opened/managed files. This could let
us get rid of the whole ProjectFile thing, or at least not require going
through a project to find a file (it would be stored directly on
ServerState). This also makes the synchronization story much simpler,
improves initialization time, and should make it easier to eventually
load projects async.

* Fix hover on member defs

* Fix replace text range

In some cases, when a completion is meant to replace existing text, the
range it was supposed to replace would leave an extra character at the
end. This was because the range's end position was not exclusive.

* Fix test class name
  • Loading branch information
milesziemer authored Jan 17, 2025
1 parent 5c9c72d commit 1dd15b2
Show file tree
Hide file tree
Showing 59 changed files with 8,284 additions and 2,868 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,4 @@ bin
.settings

.java-version
*.smithy
!/src/test/resources/**/*.smithy
.ammonite
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ publishing {
}
}

checkstyle {
toolVersion = "10.12.4"
}

dependencies {
implementation "org.eclipse.lsp4j:org.eclipse.lsp4j:0.23.1"
Expand All @@ -153,6 +156,8 @@ dependencies {
testImplementation "org.hamcrest:hamcrest:2.2"

testRuntimeOnly "org.junit.platform:junit-platform-launcher"

checkstyle "com.puppycrawl.tools:checkstyle:${checkstyle.toolVersion}"
}

tasks.withType(Javadoc).all {
Expand Down
1 change: 0 additions & 1 deletion config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@
<!-- See http://checkstyle.sf.net/config_design.html -->
<module name="FinalClass"/>
<module name="HideUtilityClassConstructor"/>
<module name="InterfaceIsType"/>
<module name="OneTopLevelClass"/>

<!-- Miscellaneous other checks. -->
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/software/amazon/smithy/lsp/ServerState.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ ProjectAndFile findProjectAndFile(String uri) {
String path = LspAdapter.toPath(uri);
ProjectFile projectFile = detachedProject.getProjectFile(path);
if (projectFile != null) {
return new ProjectAndFile(detachedProject, projectFile);
return new ProjectAndFile(uri, detachedProject, projectFile, true);
}
}

Expand Down Expand Up @@ -133,17 +133,16 @@ private ProjectAndFile findAttachedAndRemoveDetached(String uri) {
ProjectFile projectFile = project.getProjectFile(path);
if (projectFile != null) {
detachedProjects.remove(uri);
return new ProjectAndFile(project, projectFile);
return new ProjectAndFile(uri, project, projectFile, false);
}
}

return null;
}

Project createDetachedProject(String uri, String text) {
void createDetachedProject(String uri, String text) {
Project project = ProjectLoader.loadDetached(uri, text);
detachedProjects.put(uri, project);
return project;
}

List<Exception> tryInitProject(Path root) {
Expand Down
191 changes: 34 additions & 157 deletions src/main/java/software/amazon/smithy/lsp/SmithyLanguageServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.eclipse.lsp4j.CompletionParams;
import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.eclipse.lsp4j.DidChangeConfigurationParams;
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
import org.eclipse.lsp4j.DidChangeWatchedFilesParams;
Expand Down Expand Up @@ -74,7 +73,6 @@
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.SetTraceParams;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.SymbolKind;
import org.eclipse.lsp4j.TextDocumentChangeRegistrationOptions;
import org.eclipse.lsp4j.TextDocumentContentChangeEvent;
import org.eclipse.lsp4j.TextDocumentIdentifier;
Expand All @@ -100,26 +98,25 @@
import software.amazon.smithy.lsp.codeactions.SmithyCodeActions;
import software.amazon.smithy.lsp.diagnostics.SmithyDiagnostics;
import software.amazon.smithy.lsp.document.Document;
import software.amazon.smithy.lsp.document.DocumentParser;
import software.amazon.smithy.lsp.document.DocumentShape;
import software.amazon.smithy.lsp.ext.OpenProject;
import software.amazon.smithy.lsp.ext.SelectorParams;
import software.amazon.smithy.lsp.ext.ServerStatus;
import software.amazon.smithy.lsp.ext.SmithyProtocolExtensions;
import software.amazon.smithy.lsp.handler.CompletionHandler;
import software.amazon.smithy.lsp.handler.DefinitionHandler;
import software.amazon.smithy.lsp.handler.HoverHandler;
import software.amazon.smithy.lsp.language.CompletionHandler;
import software.amazon.smithy.lsp.language.DefinitionHandler;
import software.amazon.smithy.lsp.language.DocumentSymbolHandler;
import software.amazon.smithy.lsp.language.HoverHandler;
import software.amazon.smithy.lsp.project.BuildFile;
import software.amazon.smithy.lsp.project.IdlFile;
import software.amazon.smithy.lsp.project.Project;
import software.amazon.smithy.lsp.project.ProjectAndFile;
import software.amazon.smithy.lsp.project.SmithyFile;
import software.amazon.smithy.lsp.protocol.LspAdapter;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.lsp.syntax.Syntax;
import software.amazon.smithy.model.loader.IdlTokenizer;
import software.amazon.smithy.model.selector.Selector;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.validation.Severity;
import software.amazon.smithy.model.validation.ValidationEvent;
import software.amazon.smithy.syntax.Formatter;
import software.amazon.smithy.syntax.TokenTree;
import software.amazon.smithy.utils.IoUtils;
Expand Down Expand Up @@ -163,6 +160,10 @@ ServerState getState() {
return state;
}

Severity getMinimumSeverity() {
return minimumSeverity;
}

@Override
public void connect(LanguageClient client) {
LOGGER.finest("Connect");
Expand Down Expand Up @@ -517,10 +518,11 @@ public void didChange(DidChangeTextDocumentParams params) {
}

// Don't reload or update the project on build file changes, only on save
if (projectAndFile.file() instanceof BuildFile) {
if (!(projectAndFile.file() instanceof SmithyFile smithyFile)) {
return;
}

smithyFile.reparse();
if (!onlyReloadOnSave) {
Project project = projectAndFile.project();

Expand All @@ -529,7 +531,7 @@ public void didChange(DidChangeTextDocumentParams params) {
// Report any parse/shape/trait loading errors
CompletableFuture<Void> future = CompletableFuture
.runAsync(() -> project.updateModelWithoutValidating(uri))
.thenComposeAsync(unused -> sendFileDiagnostics(uri));
.thenComposeAsync(unused -> sendFileDiagnostics(projectAndFile));
state.lifecycleManager().putTask(uri, future);
}
}
Expand All @@ -549,9 +551,10 @@ public void didOpen(DidOpenTextDocumentParams params) {
projectAndFile.file().document().applyEdit(null, text);
} else {
state.createDetachedProject(uri, text);
projectAndFile = state.findProjectAndFile(uri); // Note: This will always be present
}

state.lifecycleManager().putTask(uri, sendFileDiagnostics(uri));
state.lifecycleManager().putTask(uri, sendFileDiagnostics(projectAndFile));
}

@Override
Expand Down Expand Up @@ -597,7 +600,7 @@ public void didSave(DidSaveTextDocumentParams params) {
} else {
CompletableFuture<Void> future = CompletableFuture
.runAsync(() -> project.updateAndValidateModel(uri))
.thenCompose(unused -> sendFileDiagnostics(uri));
.thenCompose(unused -> sendFileDiagnostics(projectAndFile));
state.lifecycleManager().putTask(uri, future);
}
}
Expand All @@ -613,15 +616,13 @@ public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completio
return completedFuture(Either.forLeft(Collections.emptyList()));
}

if (!(projectAndFile.file() instanceof SmithyFile smithyFile)) {
if (!(projectAndFile.file() instanceof IdlFile smithyFile)) {
return completedFuture(Either.forLeft(List.of()));
}

Project project = projectAndFile.project();
return CompletableFutures.computeAsync((cc) -> {
CompletionHandler handler = new CompletionHandler(project, smithyFile);
return Either.forLeft(handler.handle(params, cc));
});
var handler = new CompletionHandler(project, smithyFile);
return CompletableFutures.computeAsync((cc) -> Either.forLeft(handler.handle(params, cc)));
}

@Override
Expand All @@ -643,54 +644,13 @@ public CompletableFuture<CompletionItem> resolveCompletionItem(CompletionItem un
return completedFuture(Collections.emptyList());
}

if (!(projectAndFile.file() instanceof SmithyFile smithyFile)) {
if (!(projectAndFile.file() instanceof IdlFile idlFile)) {
return completedFuture(List.of());
}

return CompletableFutures.computeAsync((cc) -> {
Collection<DocumentShape> documentShapes = smithyFile.documentShapes();
if (documentShapes.isEmpty()) {
return Collections.emptyList();
}

if (cc.isCanceled()) {
return Collections.emptyList();
}

List<Either<SymbolInformation, DocumentSymbol>> documentSymbols = new ArrayList<>(documentShapes.size());
for (DocumentShape documentShape : documentShapes) {
SymbolKind symbolKind;
switch (documentShape.kind()) {
case Inline:
// No shape name in the document text, so no symbol
continue;
case DefinedMember:
case Elided:
symbolKind = SymbolKind.Property;
break;
case DefinedShape:
case Targeted:
default:
symbolKind = SymbolKind.Class;
break;
}

// Check before copying shapeName, which is actually a reference to the underlying document, and may
// be changed.
cc.checkCanceled();

String symbolName = documentShape.shapeName().toString();
if (symbolName.isEmpty()) {
LOGGER.warning("[DocumentSymbols] Empty shape name for " + documentShape);
continue;
}
Range symbolRange = documentShape.range();
DocumentSymbol symbol = new DocumentSymbol(symbolName, symbolKind, symbolRange, symbolRange);
documentSymbols.add(Either.forRight(symbol));
}

return documentSymbols;
});
List<Syntax.Statement> statements = idlFile.getParse().statements();
var handler = new DocumentSymbolHandler(idlFile.document(), statements);
return CompletableFuture.supplyAsync(handler::handle);
}

@Override
Expand All @@ -705,13 +665,13 @@ public CompletableFuture<CompletionItem> resolveCompletionItem(CompletionItem un
return completedFuture(null);
}

if (!(projectAndFile.file() instanceof SmithyFile smithyFile)) {
if (!(projectAndFile.file() instanceof IdlFile smithyFile)) {
return completedFuture(null);
}

Project project = projectAndFile.project();
List<Location> locations = new DefinitionHandler(project, smithyFile).handle(params);
return completedFuture(Either.forLeft(locations));
var handler = new DefinitionHandler(project, smithyFile);
return CompletableFuture.supplyAsync(() -> Either.forLeft(handler.handle(params)));
}

@Override
Expand All @@ -725,15 +685,15 @@ public CompletableFuture<Hover> hover(HoverParams params) {
return completedFuture(null);
}

if (!(projectAndFile.file() instanceof SmithyFile smithyFile)) {
if (!(projectAndFile.file() instanceof IdlFile smithyFile)) {
return completedFuture(null);
}

Project project = projectAndFile.project();

// TODO: Abstract away passing minimum severity
Hover hover = new HoverHandler(project, smithyFile).handle(params, minimumSeverity);
return completedFuture(hover);
var handler = new HoverHandler(project, smithyFile, minimumSeverity);
return CompletableFuture.supplyAsync(() -> handler.handle(params));
}

@Override
Expand Down Expand Up @@ -772,99 +732,16 @@ public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormatting

private void sendFileDiagnosticsForManagedDocuments() {
for (String managedDocumentUri : state.managedUris()) {
state.lifecycleManager().putOrComposeTask(managedDocumentUri, sendFileDiagnostics(managedDocumentUri));
ProjectAndFile projectAndFile = state.findProjectAndFile(managedDocumentUri);
state.lifecycleManager().putOrComposeTask(managedDocumentUri, sendFileDiagnostics(projectAndFile));
}
}

private CompletableFuture<Void> sendFileDiagnostics(String uri) {
private CompletableFuture<Void> sendFileDiagnostics(ProjectAndFile projectAndFile) {
return CompletableFuture.runAsync(() -> {
List<Diagnostic> diagnostics = getFileDiagnostics(uri);
PublishDiagnosticsParams publishDiagnosticsParams = new PublishDiagnosticsParams(uri, diagnostics);
List<Diagnostic> diagnostics = SmithyDiagnostics.getFileDiagnostics(projectAndFile, minimumSeverity);
var publishDiagnosticsParams = new PublishDiagnosticsParams(projectAndFile.uri(), diagnostics);
client.publishDiagnostics(publishDiagnosticsParams);
});
}

List<Diagnostic> getFileDiagnostics(String uri) {
if (LspAdapter.isJarFile(uri) || LspAdapter.isSmithyJarFile(uri)) {
// Don't send diagnostics to jar files since they can't be edited
// and diagnostics could be misleading.
return Collections.emptyList();
}

ProjectAndFile projectAndFile = state.findProjectAndFile(uri);
if (projectAndFile == null) {
client.unknownFileError(uri, "diagnostics");
return List.of();
}

if (!(projectAndFile.file() instanceof SmithyFile smithyFile)) {
return List.of();
}

Project project = projectAndFile.project();
String path = LspAdapter.toPath(uri);

List<Diagnostic> diagnostics = project.modelResult().getValidationEvents().stream()
.filter(validationEvent -> validationEvent.getSeverity().compareTo(minimumSeverity) >= 0)
.filter(validationEvent -> validationEvent.getSourceLocation().getFilename().equals(path))
.map(validationEvent -> toDiagnostic(validationEvent, smithyFile))
.collect(Collectors.toCollection(ArrayList::new));

Diagnostic versionDiagnostic = SmithyDiagnostics.versionDiagnostic(smithyFile);
if (versionDiagnostic != null) {
diagnostics.add(versionDiagnostic);
}

if (state.isDetached(uri)) {
diagnostics.add(SmithyDiagnostics.detachedDiagnostic(smithyFile));
}

return diagnostics;
}

private static Diagnostic toDiagnostic(ValidationEvent validationEvent, SmithyFile smithyFile) {
DiagnosticSeverity severity = toDiagnosticSeverity(validationEvent.getSeverity());
SourceLocation sourceLocation = validationEvent.getSourceLocation();
Range range = determineRange(validationEvent, sourceLocation, smithyFile);
String message = validationEvent.getId() + ": " + validationEvent.getMessage();
return new Diagnostic(range, message, severity, "Smithy");
}

private static Range determineRange(ValidationEvent validationEvent,
SourceLocation sourceLocation,
SmithyFile smithyFile) {
final Range defaultRange = LspAdapter.lineOffset(LspAdapter.toPosition(sourceLocation));

if (smithyFile == null) {
return defaultRange;
}

DocumentParser parser = DocumentParser.forDocument(smithyFile.document());

// Case where we have shapes present
if (validationEvent.getShapeId().isPresent()) {
// Event is (probably) on a member target
if (validationEvent.containsId("Target")) {
DocumentShape documentShape = smithyFile.documentShapesByStartPosition()
.get(LspAdapter.toPosition(sourceLocation));
if (documentShape != null && documentShape.hasMemberTarget()) {
return documentShape.targetReference().range();
}
} else {
// Check if the event location is on a trait application
return Objects.requireNonNullElse(parser.traitIdRange(sourceLocation), defaultRange);
}
}

return Objects.requireNonNullElse(parser.findContiguousRange(sourceLocation), defaultRange);
}

private static DiagnosticSeverity toDiagnosticSeverity(Severity severity) {
return switch (severity) {
case ERROR, DANGER -> DiagnosticSeverity.Error;
case WARNING -> DiagnosticSeverity.Warning;
case NOTE -> DiagnosticSeverity.Information;
default -> DiagnosticSeverity.Hint;
};
}
}
Loading

0 comments on commit 1dd15b2

Please sign in to comment.