-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description - Add `--find-project` option, which allows selecting a project file by traversing up from a certain path. This is useful when working in a particular file and wanting to to a Dafny CLI call that uses the project for that file. ### How has this been tested? - Extended the existing CLI test `projectFile.dfy` <small>By submitting this pull request, I confirm that my contribution is made under the terms of the [MIT license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>
- Loading branch information
1 parent
0860638
commit 2e866ef
Showing
15 changed files
with
141 additions
and
78 deletions.
There are no files selected for viewing
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
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
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
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,48 @@ | ||
#nullable enable | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.Dafny; | ||
|
||
public class ProjectFileOpener { | ||
private readonly IFileSystem fileSystem; | ||
private readonly IToken origin; | ||
|
||
public ProjectFileOpener(IFileSystem fileSystem, IToken origin) { | ||
this.fileSystem = fileSystem; | ||
this.origin = origin; | ||
} | ||
|
||
public async Task<DafnyProject?> TryFindProject(Uri uri) { | ||
return uri.LocalPath.EndsWith(DafnyProject.FileName) | ||
? await DafnyProject.Open(fileSystem, uri, origin) | ||
: await FindProjectFile(uri); | ||
} | ||
|
||
private async Task<DafnyProject?> FindProjectFile(Uri sourceUri) { | ||
DafnyProject? projectFile = null; | ||
|
||
var folder = Path.GetDirectoryName(sourceUri.LocalPath); | ||
while (!string.IsNullOrEmpty(folder) && projectFile == null) { | ||
projectFile = await OpenProjectInFolder(folder); | ||
|
||
if (projectFile != null && projectFile.Uri != sourceUri && | ||
!(projectFile.Errors.HasErrors || projectFile.ContainsSourceFile(sourceUri))) { | ||
projectFile = null; | ||
} | ||
|
||
folder = Path.GetDirectoryName(folder); | ||
} | ||
|
||
return projectFile; | ||
} | ||
|
||
protected virtual Task<DafnyProject?> OpenProjectInFolder(string folderPath) { | ||
var configFileUri = new Uri(Path.Combine(folderPath, DafnyProject.FileName)); | ||
if (!fileSystem.Exists(configFileUri)) { | ||
return Task.FromResult<DafnyProject?>(null); | ||
} | ||
|
||
return DafnyProject.Open(fileSystem, configFileUri, origin)!; | ||
} | ||
} |
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
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
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
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
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
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
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
45 changes: 45 additions & 0 deletions
45
Source/DafnyLanguageServer/Workspace/CachingProjectFileOpener.cs
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,45 @@ | ||
using System; | ||
using System.CommandLine; | ||
using System.Runtime.Caching; | ||
using System.Threading.Tasks; | ||
using DafnyCore; | ||
using Microsoft.Dafny; | ||
|
||
public class CachingProjectFileOpener : ProjectFileOpener { | ||
public const int DefaultProjectFileCacheExpiryTime = 100; | ||
private readonly object nullRepresentative = new(); // Needed because you can't store null in the MemoryCache, but that's a value we want to cache. | ||
private readonly DafnyOptions serverOptions; | ||
private readonly MemoryCache projectFilePerFolderCache = new("projectFiles"); | ||
|
||
|
||
static CachingProjectFileOpener() { | ||
DooFile.RegisterNoChecksNeeded(ProjectFileCacheExpiry, false); | ||
} | ||
|
||
public CachingProjectFileOpener(IFileSystem fileSystem, DafnyOptions serverOptions, IToken origin) : base(fileSystem, origin) { | ||
this.serverOptions = serverOptions; | ||
} | ||
|
||
public static readonly Option<int> ProjectFileCacheExpiry = new("--project-file-cache-expiry", () => DefaultProjectFileCacheExpiryTime, | ||
@"How many milliseconds the server will cache project file contents".TrimStart()) { | ||
IsHidden = true | ||
}; | ||
|
||
protected override async Task<DafnyProject?> OpenProjectInFolder(string folderPath) { | ||
var cacheExpiry = serverOptions.Get(ProjectFileCacheExpiry); | ||
if (cacheExpiry == 0) { | ||
return await base.OpenProjectInFolder(folderPath); | ||
} | ||
|
||
var cachedResult = projectFilePerFolderCache.Get(folderPath); | ||
if (cachedResult != null) { | ||
return cachedResult == nullRepresentative ? null : ((DafnyProject?)cachedResult)?.Clone(); | ||
} | ||
|
||
var result = await base.OpenProjectInFolder(folderPath); | ||
projectFilePerFolderCache.Set(new CacheItem(folderPath, (object?)result ?? nullRepresentative), new CacheItemPolicy { | ||
AbsoluteExpiration = new DateTimeOffset(DateTime.Now.Add(TimeSpan.FromMilliseconds(cacheExpiry))) | ||
}); | ||
return result?.Clone(); | ||
} | ||
} |
Oops, something went wrong.