Skip to content

Commit b3a41bf

Browse files
authored
Infer project root correctly from within target folder (#949)
* Infer project root correctly from within target folder. * "Longest" -> "Deepest" path * Fix warning.
1 parent ab93196 commit b3a41bf

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/model/PathConfigs.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,23 @@ private static boolean hasParentSection(URIResolverRegistry reg, ISourceLocation
243243
}
244244
}
245245

246+
/**
247+
* Infers the root of the project that `member` is in.
248+
*/
246249
private static ISourceLocation inferProjectRoot(ISourceLocation member) {
250+
ISourceLocation lastRoot = member;
251+
ISourceLocation root;
252+
do {
253+
root = lastRoot;
254+
lastRoot = inferDeepestProjectRoot(URIUtil.getParentLocation(root));
255+
} while (!lastRoot.equals(URIUtil.getParentLocation(root)));
256+
return root;
257+
}
258+
259+
/**
260+
* Infers the longest project root-like path that `member` is in. Might return a sub-directory of `target/`.
261+
*/
262+
private static ISourceLocation inferDeepestProjectRoot(ISourceLocation member) {
247263
ISourceLocation current = member;
248264
URIResolverRegistry reg = URIResolverRegistry.getInstance();
249265
if (!reg.isDirectory(current)) {
@@ -254,13 +270,13 @@ private static ISourceLocation inferProjectRoot(ISourceLocation member) {
254270
if (reg.exists(URIUtil.getChildLocation(current, "META-INF/RASCAL.MF"))) {
255271
return current;
256272
}
257-
258-
if (URIUtil.getParentLocation(current).equals(current)) {
273+
var parent = URIUtil.getParentLocation(current);
274+
if (parent.equals(current)) {
259275
// we went all the way up to the root
260276
return reg.isDirectory(member) ? member : URIUtil.getParentLocation(member);
261277
}
262278

263-
current = URIUtil.getParentLocation(current);
279+
current = parent;
264280
}
265281

266282
return current;

rascal-lsp/src/main/rascal/lsp/lang/rascal/lsp/IDECheckerWrapper.rsc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,21 @@ loc targetToProject(loc l) {
207207
}
208208

209209
@memo
210+
@synopsis{Infers the root of the project that `member` is in.}
210211
loc inferProjectRoot(loc member) {
212+
parentRoot = member;
213+
root = parentRoot;
214+
215+
do {
216+
root = parentRoot;
217+
parentRoot = inferDeepestProjectRoot(root.parent);
218+
} while (root.parent? && parentRoot != root.parent);
219+
return root;
220+
}
221+
222+
@synopsis{Infers the longest project root-like path that `member` is in.}
223+
@pitfalls{Might return a sub-directory of `target/`.}
224+
loc inferDeepestProjectRoot(loc member) {
211225
current = targetToProject(member);
212226
if (!isDirectory(current)) {
213227
current = current.parent;

0 commit comments

Comments
 (0)