-
Notifications
You must be signed in to change notification settings - Fork 14
Fix project root computation #1041
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
Open
toinehartman
wants to merge
18
commits into
main
Choose a base branch
from
fix/968-project-roots
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0aba9b1
Add tests for various project root and path config scenarios.
toinehartman 79d2a2a
Fix path config computation for various edge cases.
toinehartman 55594e5
Re-use project root computation in Rascal.
toinehartman f3c985c
Rename roots class and functions for less verbosity.
toinehartman 4195c18
Simplify while -> if
toinehartman 4505478
Rewrite ternary -> if.
toinehartman cbb77d7
Docs, performance and rewrites (h/t @DavyLandman).
toinehartman 9d2f250
Use absolute project dir in tests.
toinehartman b94cb1c
Always compare paths with separator at the end.
toinehartman 840ad2c
Fix nested project root logic.
toinehartman 49b9df7
Improve and clean up tests.
toinehartman abc744a
Consider Java project roots as well.
toinehartman 436ca05
Check project and module existence in tests by default.
toinehartman b17ffb0
Merge remote-tracking branch 'origin/main' into fix/968-project-roots
toinehartman 254a7b3
Only consider Java project roots.
toinehartman beccba9
Merge remote-tracking branch 'origin/main' into fix/968-project-roots
toinehartman 520ddd0
Do no compute roots for non-source projects.
toinehartman 4a357dd
Split tests over classes.
toinehartman 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 hidden or 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
78 changes: 78 additions & 0 deletions
78
rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/model/Projects.java
This file contains hidden or 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,78 @@ | ||
| /* | ||
| * Copyright (c) 2018-2025, NWO-I CWI and Swat.engineering | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are met: | ||
| * | ||
| * 1. Redistributions of source code must retain the above copyright notice, | ||
| * this list of conditions and the following disclaimer. | ||
| * | ||
| * 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| * this list of conditions and the following disclaimer in the documentation | ||
| * and/or other materials provided with the distribution. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| * POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package org.rascalmpl.vscode.lsp.rascal.model; | ||
|
|
||
| import org.rascalmpl.interpreter.utils.RascalManifest; | ||
| import org.rascalmpl.uri.URIUtil; | ||
|
|
||
| import io.usethesource.vallang.ISourceLocation; | ||
|
|
||
| /** | ||
| * Tools for projects, like path computations. Non-static functions so they can be used in Rascal via `@javaClass` as well. | ||
| */ | ||
| public class Projects { | ||
|
|
||
| /** | ||
| * Infers the shallowest possible root of the project that `origin` is in. | ||
| */ | ||
| public ISourceLocation inferRoot(ISourceLocation origin) { | ||
| origin = origin.top(); | ||
| var innerRoot = inferDeepestRoot(origin); | ||
| var outerRoot = inferDeepestRoot(URIUtil.getParentLocation(innerRoot)); | ||
|
|
||
| if (!innerRoot.equals(outerRoot) && isSameProject(innerRoot, outerRoot)) { | ||
|
toinehartman marked this conversation as resolved.
|
||
| // The roots are not equal, but refer to the same project: the inner root is somewhere inside the target folder. | ||
| // In that case, we need the outer root | ||
| return outerRoot; | ||
| } | ||
|
|
||
| // (innerRoot.equals(outerRoot) || !isSameProject(innerRoot, outerRoot)) | ||
| // Inner is a nested project within outer; we want the root of the nested project. | ||
| return innerRoot; | ||
| } | ||
|
|
||
| private boolean isSameProject(ISourceLocation root1, ISourceLocation root2) { | ||
| var mf = new RascalManifest(); | ||
| return mf.hasManifest(root1) && mf.getProjectName(root1).equals(mf.getProjectName(root2)); | ||
| } | ||
|
|
||
| /** | ||
| * Infers the longest project root-like path that `member` is in. Might return a sub-directory of `target/`. | ||
| */ | ||
| private ISourceLocation inferDeepestRoot(ISourceLocation origin) { | ||
| var root = origin; | ||
|
toinehartman marked this conversation as resolved.
|
||
| while (!new RascalManifest().hasManifest(root)) { | ||
| if (root.getPath().equals(URIUtil.URI_PATH_SEPARATOR)) { | ||
| // File system root; cannot recurse further | ||
| break; | ||
| } | ||
| root = URIUtil.getParentLocation(root); | ||
| } | ||
| return root; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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
73 changes: 73 additions & 0 deletions
73
rascal-lsp/src/test/java/org/rascalmpl/vscode/lsp/rascal/model/PathConfigsTest.java
This file contains hidden or 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,73 @@ | ||
| /* | ||
| * Copyright (c) 2018-2025, NWO-I CWI and Swat.engineering | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are met: | ||
| * | ||
| * 1. Redistributions of source code must retain the above copyright notice, | ||
| * this list of conditions and the following disclaimer. | ||
| * | ||
| * 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| * this list of conditions and the following disclaimer in the documentation | ||
| * and/or other materials provided with the distribution. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| * POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package org.rascalmpl.vscode.lsp.rascal.model; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.Executors; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.mockito.Mock; | ||
| import org.rascalmpl.uri.URIResolverRegistry; | ||
| import org.rascalmpl.uri.URIUtil; | ||
|
|
||
| import io.usethesource.vallang.ISourceLocation; | ||
|
|
||
| public class PathConfigsTest { | ||
|
|
||
| private static final URIResolverRegistry reg = URIResolverRegistry.getInstance(); | ||
| private static ISourceLocation absoluteProjectDir; | ||
| @Mock PathConfigDiagnostics diagnostics; | ||
|
DavyLandman marked this conversation as resolved.
|
||
| private PathConfigs configs; | ||
|
|
||
| @BeforeClass | ||
| public static void initTests() throws IOException { | ||
| absoluteProjectDir = reg.logicalToPhysical(URIUtil.rootLocation("cwd")); | ||
| } | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| configs = new PathConfigs(Executors.newCachedThreadPool(), diagnostics); | ||
| } | ||
|
|
||
| private static void assertEquals(String message, ISourceLocation expected, ISourceLocation actual) { | ||
| Assert.assertEquals(message, URIUtil.getChildLocation(expected, ""), URIUtil.getChildLocation(actual, "")); | ||
| } | ||
|
|
||
| @Test | ||
| public void pathConfigForLsp() { | ||
| var pcfg = configs.lookupConfig(absoluteProjectDir); | ||
| assertEquals("Path config root should equal project URI", absoluteProjectDir, pcfg.getProjectRoot()); | ||
| } | ||
|
|
||
| @Test | ||
| public void pathConfigForLspModule() { | ||
| var pcfg = configs.lookupConfig(URIUtil.getChildLocation(absoluteProjectDir, "src/main/rascal/library/util/LanguageServer.rsc")); | ||
| assertEquals("Path config root should equal project URI", absoluteProjectDir, pcfg.getProjectRoot()); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.