Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store model text in memory #121

Conversation

milesziemer
Copy link
Contributor

This is primarily a refactor of SmithyProject to store raw model text in memory. Previously, we only stored file objects for the model, and gave those to the model assembler. There are two issues with this approach:

  1. While a file is being editted, the source of truth for the file contents should be what the client sends back to the server in the didChange event, not whats actually in the file. To get around this, we've been using a temporary file that stores the contents of the file being editted, and giving that to the model assembler. When publishing diagnostics however, we needed to re-map the file location from the temporary file to the actual file on disk.
  2. The language server needs literal text to provide some of its features, meaning we need to read in files frequently.

These changes update SmithyProject to store a map of URI -> String for each of the model files in the loaded model by walking the shapes and collecting all unique file locations. SmithyTDS is updated to use that when it needs to have literal text of the model for formatting, version diagnostics, and other features.

Various other updates were also made:

  • Added a list of errors to SmithyProject that store any errors that occur while loading the project. Previously, any "load" methods would return Either<Exception, SmithyProject>, which isn't very ergonomic to use. These errors represent failures in the project loading, not validation errors from loading the model.
  • SmithyProject's loading methods are reworked to provide 2 static methods: one for loading the project in a directory, one for loading from specific smithy-build config, and 2 instance methods: one for regular reloading, one for reloading with specific file changes. Previously, we had to manage the loading of the temporary file (or its removal). This also moves all the loading logic into SmithyProject.
  • String-literal uris are replaced with URI class since LSP guarantees uris will be valid (see https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#uri).
  • SmithyInterface has new method to load from text-literal sources.
  • Completions updated to handle invalid model result, so callers don't have to. This allows removing getCompletions from SmithyProject.
  • Various access modifiers adjusted to be more strict, tightening the API to make it easier for us to change implementations.

The following test updates were made:

  • Added multiple test cases for different project loading & reloading situations.
  • Some tests needed to use URI class instead of uri strings.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@milesziemer milesziemer requested a review from a team as a code owner September 1, 2023 14:31
@milesziemer milesziemer force-pushed the refactor-smithy-project branch 23 times, most recently from 5cfa250 to afba16c Compare September 1, 2023 19:41
@milesziemer milesziemer marked this pull request as draft September 5, 2023 18:11
@milesziemer milesziemer marked this pull request as ready for review September 6, 2023 21:12
Comment on lines 176 to 177
if (!existingSourceFiles.contains(new File(changedUri))) {
existingSourceFiles.add(new File(changedUri));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: introduce variable rather than constructing the file twice

return new HashMap<>();
}

public ValidatedResult<Model> getModel() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public methods should have docs

This is primarily a refactor of SmithyProject to store raw model
text in memory. Previously, we only stored file objects for the
model, and gave those to the model assembler. There are two issues
with this approach:
1. While a file is being editted, the source of truth for the file
contents should be what the client sends back to the server in the
`didChange` event, not whats actually in the file. To get around
this, we've been using a temporary file that stores the contents
of the file being editted, and giving that to the model assembler.
When publishing diagnostics however, we needed to re-map the file
location from the temporary file to the actual file on disk.
2. The language server needs literal text to provide some of its
features, meaning we need to read in files frequently.

These changes update SmithyProject to store a map of URI -> String
for each of the model files in the loaded model by walking the
shapes and collecting all unique file locations. SmithyTDS is
updated to use that when it needs to have literal text of the
model for formatting, version diagnostics, and other features.

Various other updates were also made:
- Added a list of errors to SmithyProject that store any errors
that occur while loading the project. Previously, any "load"
methods would return `Either<Exception, SmithyProject>`, which
isn't very ergonomic to use. These errors represent failures
in the project loading, not validation errors from loading
the model.
- SmithyProject's loading methods are reworked to provide
2 static methods: one for loading the project in a directory,
one for loading from specific smithy-build config, and 2
instance methods: one for regular reloading, one for reloading
with specific file changes. Previously, we had to manage the
loading of the temporary file (or its removal). This also
moves all the loading logic into SmithyProject.
- String-literal uris are replaced with URI class since LSP
guarantees uris will be valid (see https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#uri).
- SmithyInterface has new method to load from text-literal
sources.
- Completions updated to handle invalid model result, so callers
don't have to. This allows removing `getCompletions` from
SmithyProject.
- Various access modifiers adjusted to be more strict, tightening
the API to make it easier for us to change implementations.

The following test updates were made:
- Added multiple test cases for different project loading & reloading
situations.
- Some tests needed to use URI class instead of uri strings.
@milesziemer
Copy link
Contributor Author

Done in #146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants