-
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 commit makes the language server work with Smithy projects in subdirectories of the folder (or folders, in the case of e.g. vscode workspace folders) open in the editor. I previously added support for multiple _workspace folders_ (an LSP concept), but I assumed only one _project_ (Smithy LS concept) per workspace folder. So this commit fixes that mixing, allowing many projects per workspace folder. Now, the language server will search through subdirectories of all workspace folders (by default, one workspace folder is open in the client) to find projects. Changes to build files, i.e. smithy-build.json, .smithy-project.json, are now tracked at the workspace level, so you can add a new project to an existing workspace. I also did _some_ cleanup of the project/workspace synchronization code, and moved some things around. A note on some tests: I'm using a `Files.createTempDirectory`, and adding the `TestWorkspace` as a subdir. In a follow-up commit, I will be changing `TestWorkspace` to be something like `TestProject`, which is more accurate. I didn't include it here to avoid a bunch of noise.
- Loading branch information
1 parent
b425716
commit 70858bb
Showing
20 changed files
with
809 additions
and
272 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
src/main/java/software/amazon/smithy/lsp/FilePatterns.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,135 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.lsp; | ||
|
||
import java.io.File; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.Path; | ||
import java.nio.file.PathMatcher; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import software.amazon.smithy.lsp.project.Project; | ||
import software.amazon.smithy.lsp.project.ProjectConfigLoader; | ||
|
||
/** | ||
* Utility methods for computing glob patterns that match against Smithy files | ||
* or build files in Projects and workspaces. | ||
*/ | ||
final class FilePatterns { | ||
private static final int BUILD_FILE_COUNT = 2 + ProjectConfigLoader.SMITHY_BUILD_EXTS.length; | ||
private static final Logger LOGGER = Logger.getLogger(FilePatterns.class.getName()); | ||
|
||
private FilePatterns() { | ||
} | ||
|
||
/** | ||
* @param project The project to get watch patterns for | ||
* @return A list of glob patterns used to watch Smithy files in the given project | ||
*/ | ||
static List<String> getSmithyFileWatchPatterns(Project project) { | ||
return Stream.concat(project.sources().stream(), project.imports().stream()) | ||
.map(path -> getSmithyFilePattern(path, true)) | ||
.toList(); | ||
} | ||
|
||
/** | ||
* @param project The project to get a path matcher for | ||
* @return A path matcher that can check if Smithy files belong to the given project | ||
*/ | ||
static PathMatcher getSmithyFilesPathMatcher(Project project) { | ||
String pattern = Stream.concat(project.sources().stream(), project.imports().stream()) | ||
.map(path -> getSmithyFilePattern(path, false)) | ||
.collect(Collectors.joining(",")); | ||
return toPathMatcher("{" + pattern + "}"); | ||
} | ||
|
||
/** | ||
* @param root The root to get the watch pattern for | ||
* @return A glob pattern used to watch build files in the given workspace | ||
*/ | ||
static String getWorkspaceBuildFilesWatchPattern(Path root) { | ||
return getBuildFilesPattern(root, true); | ||
} | ||
|
||
/** | ||
* @param root The root to get a path matcher for | ||
* @return A path matcher that can check if a file is a build file within the given workspace | ||
*/ | ||
static PathMatcher getWorkspaceBuildFilesPathMatcher(Path root) { | ||
String pattern = getWorkspaceBuildFilesWatchPattern(root); | ||
return toPathMatcher(pattern); | ||
} | ||
|
||
/** | ||
* @param project The project to get a path matcher for | ||
* @return A path matcher that can check if a file is a build file belonging to the given project | ||
*/ | ||
static PathMatcher getProjectBuildFilesPathMatcher(Project project) { | ||
String pattern = getBuildFilesPattern(project.root(), false); | ||
return toPathMatcher(pattern); | ||
} | ||
|
||
private static PathMatcher toPathMatcher(String globPattern) { | ||
return FileSystems.getDefault().getPathMatcher("glob:" + globPattern); | ||
} | ||
|
||
// Patterns for the workspace need to match on all build files in all subdirectories, | ||
// whereas patterns for projects only look at the top level (because project locations | ||
// are defined by the presence of these build files). | ||
private static String getBuildFilesPattern(Path root, boolean isWorkspacePattern) { | ||
List<String> patterns = new ArrayList<>(BUILD_FILE_COUNT); | ||
patterns.add(ProjectConfigLoader.SMITHY_BUILD); | ||
patterns.add(ProjectConfigLoader.SMITHY_PROJECT); | ||
for (String buildExt : ProjectConfigLoader.SMITHY_BUILD_EXTS) { | ||
Path extPath = Path.of(buildExt); // buildExt may have file separators | ||
patterns.add(escapeBackslashes(extPath.toString())); | ||
} | ||
|
||
String rootString = escapeBackslashes(root.toString()); | ||
if (!rootString.endsWith(File.separator)) { | ||
rootString += File.separator; | ||
} | ||
|
||
if (isWorkspacePattern) { | ||
rootString += "**" + File.separator; | ||
} | ||
|
||
String pattern = rootString + "{" + String.join(",", patterns) + "}"; | ||
LOGGER.info("Pattern: " + pattern); | ||
return pattern; | ||
} | ||
|
||
// When computing the pattern used for telling the client which files to watch, we want | ||
// to only watch .smithy/.json files. We don't need it in the PathMatcher pattern because | ||
// we only need to match files, not listen for specific changes (and it is impossible anyway | ||
// because we can't have a nested pattern). | ||
private static String getSmithyFilePattern(Path path, boolean isWatcherPattern) { | ||
String glob = path.toString(); | ||
if (glob.endsWith(".smithy") || glob.endsWith(".json")) { | ||
return escapeBackslashes(glob); | ||
} | ||
|
||
if (!glob.endsWith(File.separator)) { | ||
glob += File.separator; | ||
} | ||
glob += "**"; | ||
|
||
if (isWatcherPattern) { | ||
glob += ".{smithy,json}"; | ||
} | ||
|
||
return escapeBackslashes(glob); | ||
} | ||
|
||
// In glob patterns, '\' is an escape character, so it needs to escaped | ||
// itself to work as a separator (i.e. for windows) | ||
private static String escapeBackslashes(String pattern) { | ||
return pattern.replace("\\", "\\\\"); | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
src/main/java/software/amazon/smithy/lsp/ProjectRootVisitor.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,52 @@ | ||
/* | ||
* 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.nio.file.FileSystems; | ||
import java.nio.file.FileVisitResult; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.PathMatcher; | ||
import java.nio.file.SimpleFileVisitor; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import software.amazon.smithy.lsp.project.ProjectConfigLoader; | ||
|
||
/** | ||
* Finds Project roots based on the location of smithy-build.json and .smithy-project.json. | ||
*/ | ||
final class ProjectRootVisitor extends SimpleFileVisitor<Path> { | ||
private static final PathMatcher PROJECT_ROOT_MATCHER = FileSystems.getDefault().getPathMatcher( | ||
"glob:{" + ProjectConfigLoader.SMITHY_BUILD + "," + ProjectConfigLoader.SMITHY_PROJECT + "}"); | ||
|
||
private final List<Path> roots = new ArrayList<>(); | ||
|
||
/** | ||
* Walks through the file tree starting at {@code workspaceRoot}, collecting | ||
* paths of Project roots. | ||
* | ||
* @param workspaceRoot Root of the workspace to find projects in | ||
* @return A list of project roots | ||
* @throws IOException If an I/O error is thrown while walking files | ||
*/ | ||
static List<Path> findProjectRoots(Path workspaceRoot) throws IOException { | ||
ProjectRootVisitor visitor = new ProjectRootVisitor(); | ||
Files.walkFileTree(workspaceRoot, visitor); | ||
return visitor.roots; | ||
} | ||
|
||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { | ||
Path name = file.getFileName(); | ||
if (name != null && PROJECT_ROOT_MATCHER.matches(name)) { | ||
roots.add(file.getParent()); | ||
return FileVisitResult.SKIP_SIBLINGS; | ||
} | ||
return FileVisitResult.CONTINUE; | ||
} | ||
} |
Oops, something went wrong.