Skip to content

Commit 6391aac

Browse files
committed
Add interface for getting managed files
Project loading can now depend on a single interface that provides access to managed files, rather than ServerState itself.
1 parent e3f7749 commit 6391aac

File tree

6 files changed

+48
-87
lines changed

6 files changed

+48
-87
lines changed

src/main/java/software/amazon/smithy/lsp/FileCache.java

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.lsp;
7+
8+
import software.amazon.smithy.lsp.document.Document;
9+
10+
public interface ManagedFiles {
11+
/**
12+
* @param uri Uri of the document to get
13+
* @return The document if found and it is managed, otherwise {@code null}
14+
*/
15+
Document getManagedDocument(String uri);
16+
}

src/main/java/software/amazon/smithy/lsp/ServerState.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
/**
3232
* Keeps track of the state of the server.
3333
*/
34-
public final class ServerState {
34+
public final class ServerState implements ManagedFiles {
3535
private static final Logger LOGGER = Logger.getLogger(ServerState.class.getName());
3636

3737
private final Map<String, Project> projects;
@@ -73,10 +73,7 @@ public Project findProjectByRoot(String root) {
7373
return projects.get(root);
7474
}
7575

76-
/**
77-
* @param uri Uri of the document to get
78-
* @return The document if found and it is managed, otherwise {@code null}
79-
*/
76+
@Override
8077
public Document getManagedDocument(String uri) {
8178
if (managedUris.contains(uri)) {
8279
ProjectAndFile projectAndFile = findProjectAndFile(uri);
@@ -88,19 +85,6 @@ public Document getManagedDocument(String uri) {
8885
return null;
8986
}
9087

91-
/**
92-
* @param path The path of the document to get
93-
* @return The document if found and it is managed, otherwise {@code null}
94-
*/
95-
public Document getManagedDocument(Path path) {
96-
if (managedUris.isEmpty()) {
97-
return null;
98-
}
99-
100-
String uri = LspAdapter.toUri(path.toString());
101-
return getManagedDocument(uri);
102-
}
103-
10488
ProjectAndFile findProjectAndFile(String uri) {
10589
String path = LspAdapter.toPath(uri);
10690

src/main/java/software/amazon/smithy/lsp/project/ProjectConfigLoader.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
import java.util.Map;
1616
import java.util.logging.Logger;
1717
import software.amazon.smithy.build.model.SmithyBuildConfig;
18-
import software.amazon.smithy.lsp.ServerState;
18+
import software.amazon.smithy.lsp.ManagedFiles;
1919
import software.amazon.smithy.lsp.document.Document;
20+
import software.amazon.smithy.lsp.protocol.LspAdapter;
2021
import software.amazon.smithy.lsp.util.Result;
2122
import software.amazon.smithy.model.node.Node;
2223
import software.amazon.smithy.model.node.NodeMapper;
@@ -87,11 +88,7 @@ public final class ProjectConfigLoader {
8788
private ProjectConfigLoader() {
8889
}
8990

90-
static Result<ProjectConfig, List<Exception>> loadFromRoot(Path workspaceRoot) {
91-
return loadFromRoot(workspaceRoot, new ServerState());
92-
}
93-
94-
static Result<ProjectConfig, List<Exception>> loadFromRoot(Path workspaceRoot, ServerState state) {
91+
static Result<ProjectConfig, List<Exception>> loadFromRoot(Path workspaceRoot, ManagedFiles managedFiles) {
9592
SmithyBuildConfig.Builder builder = SmithyBuildConfig.builder();
9693
List<Exception> exceptions = new ArrayList<>();
9794
Map<String, BuildFile> buildFiles = new HashMap<>();
@@ -100,7 +97,7 @@ static Result<ProjectConfig, List<Exception>> loadFromRoot(Path workspaceRoot, S
10097
if (Files.isRegularFile(smithyBuildPath)) {
10198
LOGGER.info("Loading smithy-build.json from " + smithyBuildPath);
10299
Result<SmithyBuildConfig, Exception> result = Result.ofFallible(() -> {
103-
BuildFile buildFile = addBuildFile(buildFiles, smithyBuildPath, state);
100+
BuildFile buildFile = addBuildFile(buildFiles, smithyBuildPath, managedFiles);
104101
return SmithyBuildConfig.fromNode(
105102
Node.parseJsonWithComments(buildFile.document().copyText(), buildFile.path()));
106103
});
@@ -116,7 +113,7 @@ static Result<ProjectConfig, List<Exception>> loadFromRoot(Path workspaceRoot, S
116113
Path extPath = workspaceRoot.resolve(ext);
117114
if (Files.isRegularFile(extPath)) {
118115
Result<SmithyBuildExtensions, Exception> result = Result.ofFallible(() -> {
119-
BuildFile buildFile = addBuildFile(buildFiles, extPath, state);
116+
BuildFile buildFile = addBuildFile(buildFiles, extPath, managedFiles);
120117
return loadSmithyBuildExtensions(buildFile);
121118
});
122119
result.get().ifPresent(extensionsBuilder::merge);
@@ -129,7 +126,7 @@ static Result<ProjectConfig, List<Exception>> loadFromRoot(Path workspaceRoot, S
129126
if (Files.isRegularFile(smithyProjectPath)) {
130127
LOGGER.info("Loading .smithy-project.json from " + smithyProjectPath);
131128
Result<ProjectConfig.Builder, Exception> result = Result.ofFallible(() -> {
132-
BuildFile buildFile = addBuildFile(buildFiles, smithyProjectPath, state);
129+
BuildFile buildFile = addBuildFile(buildFiles, smithyProjectPath, managedFiles);
133130
return ProjectConfig.Builder.load(buildFile);
134131
});
135132
if (result.isOk()) {
@@ -154,14 +151,16 @@ static Result<ProjectConfig, List<Exception>> loadFromRoot(Path workspaceRoot, S
154151
return Result.ok(finalConfigBuilder.build());
155152
}
156153

157-
private static BuildFile addBuildFile(Map<String, BuildFile> buildFiles, Path path, ServerState state) {
158-
Document managed = state.getManagedDocument(path);
154+
private static BuildFile addBuildFile(Map<String, BuildFile> buildFiles, Path path, ManagedFiles managedFiles) {
155+
String pathString = path.toString();
156+
String uri = LspAdapter.toUri(pathString);
157+
Document managed = managedFiles.getManagedDocument(uri);
159158
BuildFile buildFile;
160159
if (managed != null) {
161-
buildFile = new BuildFile(path.toString(), managed);
160+
buildFile = new BuildFile(pathString, managed);
162161
} else {
163162
Document document = Document.of(IoUtils.readUtf8File(path));
164-
buildFile = new BuildFile(path.toString(), document);
163+
buildFile = new BuildFile(pathString, document);
165164
}
166165
buildFiles.put(buildFile.path(), buildFile);
167166
return buildFile;

src/main/java/software/amazon/smithy/lsp/project/ProjectLoader.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.function.Supplier;
2323
import java.util.logging.Logger;
2424
import java.util.stream.Stream;
25-
import software.amazon.smithy.lsp.ServerState;
25+
import software.amazon.smithy.lsp.ManagedFiles;
2626
import software.amazon.smithy.lsp.document.Document;
2727
import software.amazon.smithy.lsp.protocol.LspAdapter;
2828
import software.amazon.smithy.lsp.util.Result;
@@ -47,7 +47,7 @@ private ProjectLoader() {
4747
/**
4848
* Loads a detachedProjects (single-file) {@link Project} with the given file.
4949
*
50-
* <p>Unlike {@link #load(Path, ServerState)}, this method isn't
50+
* <p>Unlike {@link #load(Path, ManagedFiles)}, this method isn't
5151
* fallible since it doesn't do any IO that we would want to recover an
5252
* error from.
5353
*
@@ -106,11 +106,11 @@ public static Project loadDetached(String uri, String text) {
106106
* reason about how the project was structured.
107107
*
108108
* @param root Path of the project root
109-
* @param state Server's current state
109+
* @param managedFiles Files managed by the server
110110
* @return Result of loading the project
111111
*/
112-
public static Result<Project, List<Exception>> load(Path root, ServerState state) {
113-
Result<ProjectConfig, List<Exception>> configResult = ProjectConfigLoader.loadFromRoot(root, state);
112+
public static Result<Project, List<Exception>> load(Path root, ManagedFiles managedFiles) {
113+
Result<ProjectConfig, List<Exception>> configResult = ProjectConfigLoader.loadFromRoot(root, managedFiles);
114114
if (configResult.isErr()) {
115115
return Result.err(configResult.unwrapErr());
116116
}
@@ -129,7 +129,7 @@ public static Result<Project, List<Exception>> load(Path root, ServerState state
129129

130130
LoadModelResult result;
131131
try {
132-
result = doLoad(state::getManagedDocument, dependencies, allSmithyFilePaths);
132+
result = doLoad(managedFiles, dependencies, allSmithyFilePaths);
133133
} catch (Exception e) {
134134
return Result.err(Collections.singletonList(e));
135135
}
@@ -154,10 +154,6 @@ private record LoadModelResult(
154154
) {
155155
}
156156

157-
private interface ManagedFiles {
158-
Document getManagedDocument(String uri);
159-
}
160-
161157
private static LoadModelResult doLoad(
162158
ManagedFiles managedFiles,
163159
List<Path> dependencies,
@@ -178,7 +174,7 @@ private static LoadModelResult doLoad(
178174
ValidatedResult<Model> modelResult = loadModel(managedFiles, allSmithyFilePaths, assembler, smithyFiles);
179175

180176
Project.RebuildIndex rebuildIndex = Project.RebuildIndex.create(modelResult);
181-
addExtraSmithyFiles(managedFiles, rebuildIndex.filesToDefinedShapes().keySet(), smithyFiles);
177+
addDependencySmithyFiles(managedFiles, rebuildIndex.filesToDefinedShapes().keySet(), smithyFiles);
182178

183179
return new LoadModelResult(
184180
assemblerFactory,
@@ -207,7 +203,8 @@ private static ValidatedResult<Model> loadModel(
207203
return assembler.assemble();
208204
}
209205

210-
private static void addExtraSmithyFiles(
206+
// Smithy files in jars were loaded by the model assembler via model discovery, so we need to collect those.
207+
private static void addDependencySmithyFiles(
211208
ManagedFiles managedFiles,
212209
Set<String> loadedSmithyFilePaths,
213210
Map<String, SmithyFile> smithyFiles

src/test/java/software/amazon/smithy/lsp/project/ProjectConfigLoaderTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
import org.junit.jupiter.api.Test;
1919
import software.amazon.smithy.build.model.MavenConfig;
2020
import software.amazon.smithy.build.model.MavenRepository;
21+
import software.amazon.smithy.lsp.ServerState;
2122
import software.amazon.smithy.lsp.util.Result;
2223

2324
public class ProjectConfigLoaderTest {
2425
@Test
2526
public void loadsConfigWithEnvVariable() {
2627
System.setProperty("FOO", "bar");
2728
Path root = toPath(getClass().getResource("env-config"));
28-
Result<ProjectConfig, List<Exception>> result = ProjectConfigLoader.loadFromRoot(root);
29+
Result<ProjectConfig, List<Exception>> result = load(root);
2930

3031
assertThat(result.isOk(), is(true));
3132
ProjectConfig config = result.unwrap();
@@ -41,7 +42,7 @@ public void loadsConfigWithEnvVariable() {
4142
@Test
4243
public void loadsLegacyConfig() {
4344
Path root = toPath(getClass().getResource("legacy-config"));
44-
Result<ProjectConfig, List<Exception>> result = ProjectConfigLoader.loadFromRoot(root);
45+
Result<ProjectConfig, List<Exception>> result = load(root);
4546

4647
assertThat(result.isOk(), is(true));
4748
ProjectConfig config = result.unwrap();
@@ -56,7 +57,7 @@ public void loadsLegacyConfig() {
5657
@Test
5758
public void prefersNonLegacyConfig() {
5859
Path root = toPath(getClass().getResource("legacy-config-with-conflicts"));
59-
Result<ProjectConfig, List<Exception>> result = ProjectConfigLoader.loadFromRoot(root);
60+
Result<ProjectConfig, List<Exception>> result = load(root);
6061

6162
assertThat(result.isOk(), is(true));
6263
ProjectConfig config = result.unwrap();
@@ -71,10 +72,14 @@ public void prefersNonLegacyConfig() {
7172
@Test
7273
public void mergesBuildExts() {
7374
Path root = toPath(getClass().getResource("build-exts"));
74-
Result<ProjectConfig, List<Exception>> result = ProjectConfigLoader.loadFromRoot(root);
75+
Result<ProjectConfig, List<Exception>> result = load(root);
7576

7677
assertThat(result.isOk(), is(true));
7778
ProjectConfig config = result.unwrap();
7879
assertThat(config.imports(), containsInAnyOrder(containsString("main.smithy"), containsString("other.smithy")));
7980
}
81+
82+
private static Result<ProjectConfig, List<Exception>> load(Path root) {
83+
return ProjectConfigLoader.loadFromRoot(root, new ServerState());
84+
}
8085
}

0 commit comments

Comments
 (0)