Skip to content

Commit

Permalink
Add documentation to new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
milesziemer committed Jan 17, 2025
1 parent 6391aac commit dffb02c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
7 changes: 7 additions & 0 deletions src/main/java/software/amazon/smithy/lsp/ManagedFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

import software.amazon.smithy.lsp.document.Document;

/**
* Provides access to {@link Document}s managed by the server.
*
* <p>A document is _managed_ if its state is controlled by the lifecycle methods
* didOpen, didClose, didChange, didSave. In other words, reading from disk _may_
* not provide the accurate file content.
*/
public interface ManagedFiles {
/**
* @param uri Uri of the document to get
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/software/amazon/smithy/lsp/ServerState.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,16 @@ public ServerState() {
this.lifecycleManager = new DocumentLifecycleManager();
}

/**
* @return All projects tracked by the server.
*/
public Collection<Project> getAllProjects() {
return projects.values();
}

/**
* @return All files managed by the server, including their projects.
*/
public Collection<ProjectAndFile> getAllManaged() {
List<ProjectAndFile> allManaged = new ArrayList<>(managedUris.size());
for (String uri : managedUris) {
Expand All @@ -61,18 +67,13 @@ public Collection<ProjectAndFile> getAllManaged() {
return allManaged;
}

/**
* @return All workspace paths tracked by the server.
*/
public Set<Path> workspacePaths() {
return workspacePaths;
}

public DocumentLifecycleManager lifecycleManager() {
return lifecycleManager;
}

public Project findProjectByRoot(String root) {
return projects.get(root);
}

@Override
public Document getManagedDocument(String uri) {
if (managedUris.contains(uri)) {
Expand All @@ -85,6 +86,14 @@ public Document getManagedDocument(String uri) {
return null;
}

DocumentLifecycleManager lifecycleManager() {
return lifecycleManager;
}

Project findProjectByRoot(String root) {
return projects.get(root);
}

ProjectAndFile findProjectAndFile(String uri) {
String path = LspAdapter.toPath(uri);

Expand Down
26 changes: 22 additions & 4 deletions src/main/java/software/amazon/smithy/lsp/project/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,23 @@ public final class Project {
this.rebuildIndex = rebuildIndex;
}

/**
* The type of project, which depends on how it was loaded.
*/
public enum Type {
/**
* A project loaded using some build configuration files, i.e. smithy-build.json.
*/
NORMAL,

/**
* A project loaded from a single source file, without any build configuration files.
*/
DETACHED,

/**
* A project loaded with no source or build configuration files.
*/
EMPTY;
}

Expand Down Expand Up @@ -510,18 +524,22 @@ RebuildIndex recompute(ValidatedResult<Model> modelResult) {
for (Node element : traitNode.expectArrayNode()) {
String elementSourceFilename = element.getSourceLocation().getFilename();
if (!elementSourceFilename.equals(shapeSourceFilename)) {
newIndex.filesToDependentFiles.computeIfAbsent(elementSourceFilename, (f) -> new HashSet<>())
newIndex.filesToDependentFiles
.computeIfAbsent(elementSourceFilename, (f) -> new HashSet<>())
.add(shapeSourceFilename);
newIndex.shapeIdsToDependenciesFiles.computeIfAbsent(shape.getId(), (i) -> new HashSet<>())
newIndex.shapeIdsToDependenciesFiles
.computeIfAbsent(shape.getId(), (i) -> new HashSet<>())
.add(elementSourceFilename);
}
}
} else {
String traitSourceFilename = traitApplication.getSourceLocation().getFilename();
if (!traitSourceFilename.equals(shapeSourceFilename)) {
newIndex.shapesToAppliedTraitsInOtherFiles.computeIfAbsent(shape.getId(), (i) -> new ArrayList<>())
newIndex.shapesToAppliedTraitsInOtherFiles
.computeIfAbsent(shape.getId(), (i) -> new ArrayList<>())
.add(traitApplication);
newIndex.filesToTraitsTheyApply.computeIfAbsent(traitSourceFilename, (f) -> new HashMap<>())
newIndex.filesToTraitsTheyApply
.computeIfAbsent(traitSourceFilename, (f) -> new HashMap<>())
.computeIfAbsent(shape.getId(), (i) -> new ArrayList<>())
.add(traitApplication);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public static Project loadDetached(String uri, String text) {
LoadModelResult result;
try {
result = doLoad(managedFiles, dependencies, allSmithyFilePaths);
} catch (Exception e) {
// TODO: Clean up this comment
// Note: This can't happen because we have no dependencies to turn into URLs
} catch (IOException e) {
// Note: This can't happen because we aren't doing any fallible IO,
// as only the prelude will be read from disk
throw new RuntimeException(e);
}

Expand Down Expand Up @@ -158,7 +158,7 @@ private static LoadModelResult doLoad(
ManagedFiles managedFiles,
List<Path> dependencies,
List<Path> allSmithyFilePaths
) throws Exception {
) throws IOException {
// The model assembler factory is used to get assemblers that already have the correct
// dependencies resolved for future loads
Supplier<ModelAssembler> assemblerFactory = createModelAssemblerFactory(dependencies);
Expand Down Expand Up @@ -278,7 +278,6 @@ private static URLClassLoader createDependenciesClassLoader(List<Path> dependenc
return new URLClassLoader(urls);
}

// TODO: Can there be duplicate paths in this list? If there are, we may end up reading from disk multiple times
// sources and imports can contain directories or files, relative or absolute
private static List<Path> collectAllSmithyPaths(Path root, List<String> sources, List<String> imports) {
List<Path> paths = new ArrayList<>();
Expand Down

0 comments on commit dffb02c

Please sign in to comment.