Skip to content

Commit

Permalink
Synchronize on build file changes
Browse files Browse the repository at this point in the history
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
milesziemer committed Oct 3, 2024
1 parent 24a93bb commit 019c186
Show file tree
Hide file tree
Showing 20 changed files with 984 additions and 672 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,16 @@
package software.amazon.smithy.lsp;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;

/**
* Tracks asynchronous lifecycle tasks and client-managed documents.
* Allows cancelling of an ongoing task if a new task needs to be started.
* Tracks asynchronous lifecycle tasks, allowing for cancellation of an ongoing
* task if a new task needs to be started.
*/
final class DocumentLifecycleManager {
private static final Logger LOGGER = Logger.getLogger(DocumentLifecycleManager.class.getName());
private final Map<String, CompletableFuture<Void>> tasks = new HashMap<>();
private final Set<String> managedDocumentUris = new HashSet<>();

Set<String> managedDocuments() {
return managedDocumentUris;
}

boolean isManaged(String uri) {
return managedDocuments().contains(uri);
}

CompletableFuture<Void> getTask(String uri) {
return tasks.get(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* future.
*/
final class FileWatcherRegistrations {
private static final Integer SMITHY_WATCH_FILE_KIND = WatchKind.Delete | WatchKind.Create;
private static final Integer WATCH_FILE_KIND = WatchKind.Delete | WatchKind.Create;
private static final String WATCH_BUILD_FILES_ID = "WatchSmithyBuildFiles";
private static final String WATCH_SMITHY_FILES_ID = "WatchSmithyFiles";
private static final String WATCH_FILES_METHOD = "workspace/didChangeWatchedFiles";
Expand All @@ -57,7 +57,7 @@ private FileWatcherRegistrations() {
static List<Registration> getSmithyFileWatcherRegistrations(Collection<Project> projects) {
List<FileSystemWatcher> smithyFileWatchers = projects.stream()
.flatMap(project -> FilePatterns.getSmithyFileWatchPatterns(project).stream())
.map(pattern -> new FileSystemWatcher(Either.forLeft(pattern), SMITHY_WATCH_FILE_KIND))
.map(pattern -> new FileSystemWatcher(Either.forLeft(pattern), WATCH_FILE_KIND))
.toList();

return Collections.singletonList(new Registration(
Expand All @@ -75,15 +75,15 @@ static List<Unregistration> getSmithyFileWatcherUnregistrations() {

/**
* Creates registrations to tell the client to watch for any build file
* changes, creations, or deletions, across all workspaces.
* creations or deletions, across all workspaces.
*
* @param workspaceRoots The roots of the workspaces to get registrations for
* @return The registrations to watch for build file changes across all workspaces
*/
static List<Registration> getBuildFileWatcherRegistrations(Collection<Path> workspaceRoots) {
List<FileSystemWatcher> watchers = workspaceRoots.stream()
.map(FilePatterns::getWorkspaceBuildFilesWatchPattern)
.map(pattern -> new FileSystemWatcher(Either.forLeft(pattern)))
.map(pattern -> new FileSystemWatcher(Either.forLeft(pattern), WATCH_FILE_KIND))
.toList();

return Collections.singletonList(new Registration(
Expand All @@ -92,6 +92,9 @@ static List<Registration> getBuildFileWatcherRegistrations(Collection<Path> work
new DidChangeWatchedFilesRegistrationOptions(watchers)));
}

/**
* @return The unregistrations to stop watching for build file changes
*/
static List<Unregistration> getBuildFileWatcherUnregistrations() {
return BUILD_FILE_WATCHER_UNREGISTRATIONS;
}
Expand Down
236 changes: 236 additions & 0 deletions src/main/java/software/amazon/smithy/lsp/ServerState.java
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());
}
}
}
}
}
Loading

0 comments on commit 019c186

Please sign in to comment.