Skip to content

Commit

Permalink
Upgrade completions, definition, hover
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
milesziemer committed Sep 24, 2024
1 parent b425716 commit b1c78fd
Show file tree
Hide file tree
Showing 49 changed files with 7,583 additions and 1,385 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.lsp.handler;
package software.amazon.smithy.lsp;

import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -32,7 +32,7 @@
* everything, since these events should be rarer. But we can optimize it in the
* future.
*/
public final class FileWatcherRegistrationHandler {
public final class FileWatcherRegistrations {
private static final Integer SMITHY_WATCH_FILE_KIND = WatchKind.Delete | WatchKind.Create;
private static final String WATCH_BUILD_FILES_ID = "WatchSmithyBuildFiles";
private static final String WATCH_SMITHY_FILES_ID = "WatchSmithyFiles";
Expand All @@ -41,7 +41,7 @@ public final class FileWatcherRegistrationHandler {
WATCH_SMITHY_FILES_ID,
WATCH_FILES_METHOD));

private FileWatcherRegistrationHandler() {
private FileWatcherRegistrations() {
}

/**
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/software/amazon/smithy/lsp/SmithyLanguageServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@
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.FileWatcherRegistrationHandler;
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.HoverHandler;
import software.amazon.smithy.lsp.project.Project;
import software.amazon.smithy.lsp.project.ProjectChanges;
import software.amazon.smithy.lsp.project.ProjectLoader;
Expand Down Expand Up @@ -306,19 +305,19 @@ private void resolveDetachedProjects(Project oldProject, Project updatedProject)
}

private CompletableFuture<Void> registerSmithyFileWatchers() {
List<Registration> registrations = FileWatcherRegistrationHandler.getSmithyFileWatcherRegistrations(
List<Registration> registrations = FileWatcherRegistrations.getSmithyFileWatcherRegistrations(
projects.attachedProjects().values());
return client.registerCapability(new RegistrationParams(registrations));
}

private CompletableFuture<Void> unregisterSmithyFileWatchers() {
List<Unregistration> unregistrations = FileWatcherRegistrationHandler.getSmithyFileWatcherUnregistrations();
List<Unregistration> unregistrations = FileWatcherRegistrations.getSmithyFileWatcherUnregistrations();
return client.unregisterCapability(new UnregistrationParams(unregistrations));
}

@Override
public void initialized(InitializedParams params) {
List<Registration> registrations = FileWatcherRegistrationHandler.getBuildFileWatcherRegistrations(
List<Registration> registrations = FileWatcherRegistrations.getBuildFileWatcherRegistrations(
projects.attachedProjects().values());
client.registerCapability(new RegistrationParams(registrations));
registerSmithyFileWatchers();
Expand Down Expand Up @@ -497,11 +496,12 @@ public void didChange(DidChangeTextDocumentParams params) {

lifecycleManager.cancelTask(uri);

Document document = projects.getDocument(uri);
if (document == null) {
SmithyFile smithyFile = projects.getSmithyFile(uri);
if (smithyFile == null) {
client.unknownFileError(uri, "change");
return;
}
Document document = smithyFile.document();

for (TextDocumentContentChangeEvent contentChangeEvent : params.getContentChanges()) {
if (contentChangeEvent.getRange() != null) {
Expand All @@ -510,6 +510,8 @@ public void didChange(DidChangeTextDocumentParams params) {
document.applyEdit(document.fullRange(), contentChangeEvent.getText());
}
}
// document.bumpVersion(params.getTextDocument().getVersion());
smithyFile.reparse();

if (!onlyReloadOnSave) {
Project project = projects.getProject(uri);
Expand Down Expand Up @@ -697,14 +699,14 @@ public CompletableFuture<Hover> hover(HoverParams params) {
String uri = params.getTextDocument().getUri();
if (!projects.isTracked(uri)) {
client.unknownFileError(uri, "hover");
return completedFuture(HoverHandler.emptyContents());
return completedFuture(HoverHandler.EMPTY);
}

Project project = projects.getProject(uri);
SmithyFile smithyFile = project.getSmithyFile(uri);

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

Expand Down
67 changes: 53 additions & 14 deletions src/main/java/software/amazon/smithy/lsp/document/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public final class Document {
private final StringBuilder buffer;
private int[] lineIndices;

private Document(StringBuilder buffer, int[] lineIndices) {
private Document(StringBuilder buffer, int[] lineIndices, int changeVersion) {
this.buffer = buffer;
this.lineIndices = lineIndices;
}
Expand All @@ -36,14 +36,14 @@ private Document(StringBuilder buffer, int[] lineIndices) {
public static Document of(String string) {
StringBuilder buffer = new StringBuilder(string);
int[] lineIndicies = computeLineIndicies(buffer);
return new Document(buffer, lineIndicies);
return new Document(buffer, lineIndicies, 0);
}

/**
* @return A copy of this document
*/
public Document copy() {
return new Document(new StringBuilder(copyText()), lineIndices.clone());
return new Document(new StringBuilder(copyText()), lineIndices.clone(), 0);
}

/**
Expand Down Expand Up @@ -97,20 +97,31 @@ public int indexOfLine(int line) {
* doesn't exist
*/
public int lineOfIndex(int idx) {
// TODO: Use binary search or similar
if (idx >= length() || idx < 0) {
return -1;
}

for (int line = 0; line <= lastLine() - 1; line++) {
int currentLineIdx = indexOfLine(line);
int nextLineIdx = indexOfLine(line + 1);
if (idx >= currentLineIdx && idx < nextLineIdx) {
return line;
int low = 0;
int up = lastLine();

while (low <= up) {
int mid = (low + up) / 2;
int midLineIdx = lineIndices[mid];
int midLineEndIdx = lineEndUnchecked(mid);
if (idx >= midLineIdx && idx <= midLineEndIdx) {
return mid;
} else if (idx < midLineIdx) {
up = mid - 1;
} else {
low = mid + 1;
}
}

return lastLine();
return -1;
}

private int lineEndUnchecked(int line) {
if (line == lastLine()) {
return length() - 1;
} else {
return lineIndices[line + 1] - 1;
}
}

/**
Expand Down Expand Up @@ -167,6 +178,34 @@ public Position positionAtIndex(int index) {
return new Position(line, character);
}

/**
* @param start The start character offset
* @param end The end character offset
* @return The range between the two given offsets
*/
public Range rangeBetween(int start, int end) {
if (end < start || start < 0) {
return null;
}

// The start is inclusive, so it should be within the bounds of the document
Position startPos = positionAtIndex(start);
if (startPos == null) {
return null;
}

Position endPos;
if (end == length()) {
int lastLine = lastLine();
int lastCol = length() - lineIndices[lastLine];
endPos = new Position(lastLine, lastCol);
} else {
endPos = positionAtIndex(end);
}

return new Range(startPos, endPos);
}

/**
* @param line The line to find the end of
* @return The index of the end of the given line, or {@code -1} if the
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/software/amazon/smithy/lsp/document/DocumentId.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@ public enum Type {
public String copyIdValue() {
return idSlice.toString();
}

/**
* @return The value of the id without a leading '$'
*/
public String copyIdValueForElidedMember() {
String idValue = copyIdValue();
if (idValue.startsWith("$")) {
return idValue.substring(1);
}
return idValue;
}
}
Loading

0 comments on commit b1c78fd

Please sign in to comment.