-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change makes the server register for document lifecycle events like didChange for build files. Previous behavior was to register for file system change events for build files, but eventually we want to give diagnostics, completions, etc. for build files as you type, so we need to track all changes. This change keeps existing behavior from the user's perspective, as we still only reload projects on save, so it serves mostly as a refactor. I also extracted the state managed by SmithyLanguageServer to its own class, ServerState, and flattened ProjectManager into that class, so we can manage the state of the server in a more centralized location.
- Loading branch information
1 parent
24a93bb
commit 0402ac4
Showing
20 changed files
with
995 additions
and
673 deletions.
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)) { | ||
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.