-
Notifications
You must be signed in to change notification settings - Fork 20
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
Track build file changes #168
Merged
milesziemer
merged 2 commits into
smithy-lang:main
from
milesziemer:track-build-file-changes
Nov 5, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
236 changes: 236 additions & 0 deletions
236
src/main/java/software/amazon/smithy/lsp/ServerState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.lsp; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.logging.Logger; | ||
import org.eclipse.lsp4j.WorkspaceFolder; | ||
import software.amazon.smithy.lsp.document.Document; | ||
import software.amazon.smithy.lsp.project.Project; | ||
import software.amazon.smithy.lsp.project.ProjectAndFile; | ||
import software.amazon.smithy.lsp.project.ProjectFile; | ||
import software.amazon.smithy.lsp.project.ProjectLoader; | ||
import software.amazon.smithy.lsp.protocol.LspAdapter; | ||
import software.amazon.smithy.lsp.util.Result; | ||
|
||
/** | ||
* Keeps track of the state of the server. | ||
* | ||
* @param detachedProjects Map of smithy file **uri** to detached project | ||
* for that file | ||
* @param attachedProjects Map of directory **path** to attached project roots | ||
* @param workspacePaths Paths to roots of each workspace open in the client | ||
* @param managedUris Uris of each file managed by the server/client, i.e. | ||
* files which may be updated by didChange | ||
* @param lifecycleManager Container for ongoing tasks | ||
*/ | ||
public record ServerState( | ||
Map<String, Project> detachedProjects, | ||
Map<String, Project> attachedProjects, | ||
Set<Path> workspacePaths, | ||
Set<String> managedUris, | ||
DocumentLifecycleManager lifecycleManager | ||
) { | ||
private static final Logger LOGGER = Logger.getLogger(ServerState.class.getName()); | ||
|
||
/** | ||
* Create a new, empty server state. | ||
*/ | ||
public ServerState() { | ||
this( | ||
new HashMap<>(), | ||
new HashMap<>(), | ||
new HashSet<>(), | ||
new HashSet<>(), | ||
new DocumentLifecycleManager()); | ||
} | ||
|
||
/** | ||
* @param uri Uri of the document to get | ||
* @return The document if found and it is managed, otherwise {@code null} | ||
*/ | ||
public Document getManagedDocument(String uri) { | ||
if (managedUris.contains(uri)) { | ||
ProjectAndFile projectAndFile = findProjectAndFile(uri); | ||
if (projectAndFile != null) { | ||
return projectAndFile.file().document(); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param path The path of the document to get | ||
* @return The document if found and it is managed, otherwise {@code null} | ||
*/ | ||
public Document getManagedDocument(Path path) { | ||
if (managedUris.isEmpty()) { | ||
return null; | ||
} | ||
|
||
String uri = LspAdapter.toUri(path.toString()); | ||
return getManagedDocument(uri); | ||
} | ||
|
||
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); | ||
} | ||
} | ||
|
||
Project detachedProject = detachedProjects.get(uri); | ||
if (detachedProject != null) { | ||
ProjectFile projectFile = detachedProject.getProjectFile(path); | ||
if (projectFile != null) { | ||
return new ProjectAndFile(detachedProject, projectFile); | ||
} | ||
} | ||
|
||
LOGGER.warning(() -> "Tried to unknown file: " + uri); | ||
|
||
return null; | ||
} | ||
|
||
boolean isDetached(String 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)) { | ||
hpmellema marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (Project project : attachedProjects.values()) { | ||
if (project.smithyFiles().containsKey(path)) { | ||
detachedProjects.remove(uri); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
Project createDetachedProject(String uri, String text) { | ||
Project project = ProjectLoader.loadDetached(uri, text); | ||
detachedProjects.put(uri, project); | ||
return project; | ||
} | ||
|
||
List<Exception> tryInitProject(Path root) { | ||
LOGGER.finest("Initializing project at " + root); | ||
lifecycleManager.cancelAllTasks(); | ||
|
||
Result<Project, List<Exception>> loadResult = ProjectLoader.load(root, this); | ||
String projectName = root.toString(); | ||
if (loadResult.isOk()) { | ||
Project updatedProject = loadResult.unwrap(); | ||
|
||
// If the project didn't load any config files, it is now empty and should be removed | ||
if (updatedProject.config().buildFiles().isEmpty()) { | ||
removeProjectAndResolveDetached(projectName); | ||
} else { | ||
resolveDetachedProjects(attachedProjects.get(projectName), updatedProject); | ||
attachedProjects.put(projectName, updatedProject); | ||
} | ||
|
||
LOGGER.finest("Initialized project at " + root); | ||
return List.of(); | ||
} | ||
|
||
LOGGER.severe("Init project failed"); | ||
|
||
// TODO: Maybe we just start with this anyways by default, and then add to it | ||
// if we find a smithy-build.json, etc. | ||
// If we overwrite an existing project with an empty one, we lose track of the state of tracked | ||
// files. Instead, we will just keep the original project before the reload failure. | ||
attachedProjects.computeIfAbsent(projectName, ignored -> Project.empty(root)); | ||
|
||
return loadResult.unwrapErr(); | ||
} | ||
|
||
void loadWorkspace(WorkspaceFolder workspaceFolder) { | ||
Path workspaceRoot = Paths.get(URI.create(workspaceFolder.getUri())); | ||
workspacePaths.add(workspaceRoot); | ||
try { | ||
List<Path> projectRoots = ProjectRootVisitor.findProjectRoots(workspaceRoot); | ||
for (Path root : projectRoots) { | ||
tryInitProject(root); | ||
} | ||
} catch (IOException e) { | ||
LOGGER.severe(e.getMessage()); | ||
} | ||
} | ||
|
||
void removeWorkspace(WorkspaceFolder folder) { | ||
Path workspaceRoot = Paths.get(URI.create(folder.getUri())); | ||
workspacePaths.remove(workspaceRoot); | ||
|
||
// Have to do the removal separately, so we don't modify project.attachedProjects() | ||
// while iterating through it | ||
List<String> projectsToRemove = new ArrayList<>(); | ||
for (var entry : attachedProjects.entrySet()) { | ||
if (entry.getValue().root().startsWith(workspaceRoot)) { | ||
projectsToRemove.add(entry.getKey()); | ||
} | ||
} | ||
|
||
for (String projectName : projectsToRemove) { | ||
removeProjectAndResolveDetached(projectName); | ||
} | ||
} | ||
|
||
private void removeProjectAndResolveDetached(String projectName) { | ||
Project removedProject = attachedProjects.remove(projectName); | ||
if (removedProject != null) { | ||
resolveDetachedProjects(removedProject, Project.empty(removedProject.root())); | ||
} | ||
} | ||
|
||
private void resolveDetachedProjects(Project oldProject, Project updatedProject) { | ||
// This is a project reload, so we need to resolve any added/removed files | ||
// that need to be moved to or from detachedProjects projects. | ||
if (oldProject != null) { | ||
Set<String> currentProjectSmithyPaths = oldProject.smithyFiles().keySet(); | ||
Set<String> updatedProjectSmithyPaths = updatedProject.smithyFiles().keySet(); | ||
|
||
Set<String> addedPaths = new HashSet<>(updatedProjectSmithyPaths); | ||
addedPaths.removeAll(currentProjectSmithyPaths); | ||
for (String addedPath : addedPaths) { | ||
String addedUri = LspAdapter.toUri(addedPath); | ||
if (isDetached(addedUri)) { | ||
detachedProjects.remove(addedUri); | ||
} | ||
} | ||
|
||
Set<String> removedPaths = new HashSet<>(currentProjectSmithyPaths); | ||
removedPaths.removeAll(updatedProjectSmithyPaths); | ||
for (String removedPath : removedPaths) { | ||
String removedUri = LspAdapter.toUri(removedPath); | ||
// Only move to a detachedProjects project if the file is managed | ||
if (managedUris.contains(removedUri)) { | ||
Document removedDocument = oldProject.getDocument(removedUri); | ||
// The copy here is technically unnecessary, if we make ModelAssembler support borrowed strings | ||
createDetachedProject(removedUri, removedDocument.copyText()); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im a little confused why we remove it from detached projects here? I would have thought that would get checked correctly in the isDetached below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea it does, but I'm not calling that method here. I think I meant to end up removing
isDetached
, but it ended up being too useful. I did some refactoring and pulled the removal logic into the newfindAttachedAndRemoveDetached