Skip to content

Commit

Permalink
Performance improvement of file searching (#152)
Browse files Browse the repository at this point in the history
* Improved file searching

* Fixing tests, reverting some changes.

---------

Co-authored-by: Coen van den Munckhof <[email protected]>
  • Loading branch information
Sjoerdsjoerd and coenm authored Sep 16, 2024
1 parent 51fd3f2 commit 126e49b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
20 changes: 12 additions & 8 deletions src/RepoM.ActionMenu.Core/ActionMenu/Context/FileFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ namespace RepoM.ActionMenu.Core.ActionMenu.Context;
using Scriban.Syntax;

/// <summary>
/// Provides file related action menu functions and variables accessable through `file`.
/// Provides file related action menu functions and variables accessible through `file`.
/// </summary>
[ActionMenuContext("file")]
internal partial class FileFunctions : ScribanModuleWithFunctions
{
private static readonly EnumerationOptions _findFilesOptions = new()
{
RecurseSubdirectories = true,
AttributesToSkip = FileAttributes.Hidden | FileAttributes.System | FileAttributes.Device | FileAttributes.Directory,
IgnoreInaccessible = true,
MatchType = MatchType.Simple,
ReturnSpecialDirectories = false,
};

public FileFunctions()
{
RegisterFunctions();
Expand Down Expand Up @@ -46,7 +55,7 @@ public FileFunctions()
[ActionMenuContextMember("find_files")]
public static string[] FindFiles(ActionMenuGenerationContext /*IMenuContext*/ context, SourceSpan span, string rootPath, string searchPattern)
{
return FindFilesInner(context as IMenuContext, span, rootPath, searchPattern);
return FindFilesInner(context, span, rootPath, searchPattern);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -123,11 +132,6 @@ internal static bool DirectoryExistsInner(IMenuContext context, string path)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static IEnumerable<string> GetFileEnumerator(IFileSystem fileSystem, string path, string searchPattern)
{
// prefer EnumerateFileSystemInfos() over EnumerateFiles() to include packaged folders like
// .app or .xcodeproj on macOS
return fileSystem.DirectoryInfo.New(path)
.EnumerateFileSystemInfos(searchPattern, SearchOption.AllDirectories)
.Select(f => f.FullName)
.Where(f => !f.StartsWith('.'));
return fileSystem.Directory.EnumerateFileSystemEntries(path, searchPattern, _findFilesOptions);
}
}
10 changes: 1 addition & 9 deletions src/RepoM.App/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,7 @@ private async Task<bool> LstRepositoriesContextMenuOpeningAsync(ContextMenu ctxM
items.Add(new Separator());
}
}
else if (action is DeferredSubActionsUserInterfaceRepositoryAction deferredAction)
{
Control? controlItem = CreateMenuItemNewStyleAsync(action, vm);
if (controlItem != null)
{
items.Add(controlItem);
}
}
else if (action is UserInterfaceRepositoryAction uiAction)
else if (action is DeferredSubActionsUserInterfaceRepositoryAction or UserInterfaceRepositoryAction)
{
Control? controlItem = CreateMenuItemNewStyleAsync(action, vm);
if (controlItem != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,18 @@ public FileFunctionsTests()
public void FindFiles_ShouldReturnEmpty_WhenNoFilesFound()
{
// arrange
var path = "my-path";
var search = "my-search";
var files = Array.Empty<IFileSystemInfo>();
IDirectoryInfo di = A.Fake<IDirectoryInfo>();
A.CallTo(() => _fileSystem.DirectoryInfo.New(path)).Returns(di);
A.CallTo(() => di.EnumerateFileSystemInfos(search, SearchOption.AllDirectories))
.Returns(files);
const string PATH = "my-path";
const string SEARCH = "my-search";
A.CallTo(() => _fileSystem.Directory.EnumerateFileSystemEntries(PATH, SEARCH, A<EnumerationOptions>._))
.Returns(Array.Empty<string>());

// act
IEnumerable result = Sut.FindFilesInner(_context, _span, path, search);
IEnumerable result = Sut.FindFilesInner(_context, _span, PATH, SEARCH);

// assert
result.Should().BeEquivalentTo(Array.Empty<string>());
A.CallTo(() => _fileSystem.DirectoryInfo.New(path)).MustHaveHappenedOnceExactly();
A.CallTo(() => di.EnumerateFileSystemInfos(search, SearchOption.AllDirectories)).MustHaveHappenedOnceExactly();
A.CallTo(() => _fileSystem.Directory.EnumerateFileSystemEntries(PATH, SEARCH, A<EnumerationOptions>._))
.MustHaveHappenedOnceExactly();
}

[Theory]
Expand Down

0 comments on commit 126e49b

Please sign in to comment.