Skip to content

Commit

Permalink
Refactor isDetached
Browse files Browse the repository at this point in the history
  • Loading branch information
milesziemer committed Nov 5, 2024
1 parent 9f1e92b commit b3becca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
48 changes: 30 additions & 18 deletions src/main/java/software/amazon/smithy/lsp/ServerState.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,14 @@ public Document getManagedDocument(Path path) {
}

ProjectAndFile findProjectAndFile(String uri) {
String path = LspAdapter.toPath(uri);
// TODO: From isDetached
for (Project project : attachedProjects.values()) {
ProjectFile projectFile = project.getProjectFile(path);
if (projectFile != null) {
detachedProjects.remove(uri);

return new ProjectAndFile(project, projectFile);
}
ProjectAndFile attached = findAttachedAndRemoveDetached(uri);
if (attached != null) {
return attached;
}

Project detachedProject = detachedProjects.get(uri);
if (detachedProject != null) {
String path = LspAdapter.toPath(uri);
ProjectFile projectFile = detachedProject.getProjectFile(path);
if (projectFile != null) {
return new ProjectAndFile(detachedProject, projectFile);
Expand All @@ -111,21 +106,38 @@ ProjectAndFile findProjectAndFile(String uri) {
}

boolean isDetached(String uri) {
if (detachedProjects.containsKey(uri)) {
ProjectAndFile attached = findAttachedAndRemoveDetached(uri);
// The file is only truly detached if the above didn't find an attached project
// for the given file
return attached == null;
}

return false;
}

/**
* Searches for the given {@code uri} in attached projects, and if found,
* makes sure any old detached projects for that file are removed.
*
* @param uri The uri of the project and file to find
* @return The attached project and file, or null if not found
*/
private ProjectAndFile findAttachedAndRemoveDetached(String uri) {
String path = LspAdapter.toPath(uri);
// We might be in a state where a file was added to a tracked project,
// but was opened before the project loaded. This would result in it
// being placed in a detachedProjects project. Removing it here is basically
// like removing it lazily, although it does feel a little hacky.
String path = LspAdapter.toPath(uri);
if (detachedProjects.containsKey(uri)) {
for (Project project : attachedProjects.values()) {
if (project.smithyFiles().containsKey(path)) {
detachedProjects.remove(uri);
return false;
}
for (Project project : attachedProjects.values()) {
ProjectFile projectFile = project.getProjectFile(path);
if (projectFile != null) {
detachedProjects.remove(uri);
return new ProjectAndFile(project, projectFile);
}
return true;
}
return false;

return null;
}

Project createDetachedProject(String uri, String text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static final class Builder {
String outputDirectory;
final List<ProjectDependency> dependencies = new ArrayList<>();
MavenConfig mavenConfig;
final Map<String, BuildFile> buildFiles = new HashMap<>();
private final Map<String, BuildFile> buildFiles = new HashMap<>();

private Builder() {
}
Expand Down

0 comments on commit b3becca

Please sign in to comment.