2020-10-01 05:37:46 +02:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Collections.Immutable;
|
|
|
|
|
|
using System.Globalization;
|
2020-11-02 18:33:43 +01:00
|
|
|
|
using System.IO.Abstractions;
|
2020-10-01 05:37:46 +02:00
|
|
|
|
using System.Linq;
|
2020-10-24 00:05:07 +02:00
|
|
|
|
using ManagedCommon;
|
2020-10-01 05:37:46 +02:00
|
|
|
|
using Microsoft.Plugin.Folder.Sources.Result;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.Plugin.Folder.Sources
|
|
|
|
|
|
{
|
|
|
|
|
|
public class QueryInternalDirectory : IQueryInternalDirectory
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly FolderSettings _settings;
|
|
|
|
|
|
private readonly IQueryFileSystemInfo _queryFileSystemInfo;
|
2020-11-02 18:33:43 +01:00
|
|
|
|
private readonly IDirectory _directory;
|
2020-10-01 05:37:46 +02:00
|
|
|
|
|
|
|
|
|
|
private static readonly HashSet<char> SpecialSearchChars = new HashSet<char>
|
|
|
|
|
|
{
|
|
|
|
|
|
'?', '*', '>',
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
private static string _warningIconPath;
|
|
|
|
|
|
|
2020-11-02 18:33:43 +01:00
|
|
|
|
public QueryInternalDirectory(FolderSettings folderSettings, IQueryFileSystemInfo queryFileSystemInfo, IDirectory directory)
|
2020-10-01 05:37:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
_settings = folderSettings;
|
|
|
|
|
|
_queryFileSystemInfo = queryFileSystemInfo;
|
2020-11-02 18:33:43 +01:00
|
|
|
|
_directory = directory;
|
2020-10-01 05:37:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static bool HasSpecialChars(string search)
|
|
|
|
|
|
{
|
|
|
|
|
|
return search.Any(c => SpecialSearchChars.Contains(c));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-07 18:25:29 +02:00
|
|
|
|
public static bool RecursiveSearch(string query)
|
2020-10-01 05:37:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
// give the ability to search all folder when it contains a >
|
2020-10-07 18:25:29 +02:00
|
|
|
|
return query.Any(c => c.Equals('>'));
|
2020-10-01 05:37:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private (string search, string incompleteName) Process(string search)
|
|
|
|
|
|
{
|
|
|
|
|
|
string incompleteName = string.Empty;
|
2020-11-02 18:33:43 +01:00
|
|
|
|
if (HasSpecialChars(search) || !_directory.Exists($@"{search}\"))
|
2020-10-01 05:37:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
// if folder doesn't exist, we want to take the last part and use it afterwards to help the user
|
|
|
|
|
|
// find the right folder.
|
2022-06-02 11:44:52 +02:00
|
|
|
|
int index = search.LastIndexOfAny(new char[] { '\\', '/' });
|
2020-10-01 05:37:46 +02:00
|
|
|
|
|
|
|
|
|
|
// No slashes found, so probably not a folder
|
|
|
|
|
|
if (index <= 0 || index >= search.Length - 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
return default;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Remove everything after the last \ and add *
|
2020-10-30 16:43:09 -07:00
|
|
|
|
// Using InvariantCulture since this is internal
|
2020-10-01 05:37:46 +02:00
|
|
|
|
incompleteName = search.Substring(index + 1)
|
|
|
|
|
|
.ToLower(CultureInfo.InvariantCulture) + "*";
|
|
|
|
|
|
search = search.Substring(0, index + 1);
|
2020-11-02 18:33:43 +01:00
|
|
|
|
if (!_directory.Exists(search))
|
2020-10-01 05:37:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
return default;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// folder exist, add \ at the end of doesn't exist
|
2020-10-30 16:43:09 -07:00
|
|
|
|
// Using Ordinal since this is internal and is used for a symbol
|
|
|
|
|
|
if (!search.EndsWith(@"\", StringComparison.Ordinal))
|
2020-10-01 05:37:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
search += @"\";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (search, incompleteName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-14 16:45:40 +01:00
|
|
|
|
public IEnumerable<IItemResult> Query(string search)
|
2020-10-01 05:37:46 +02:00
|
|
|
|
{
|
2022-03-14 16:45:40 +01:00
|
|
|
|
if (search == null)
|
2020-10-01 05:37:46 +02:00
|
|
|
|
{
|
2022-03-14 16:45:40 +01:00
|
|
|
|
throw new ArgumentNullException(nameof(search));
|
2020-10-01 05:37:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-14 16:45:40 +01:00
|
|
|
|
var processed = Process(search);
|
2020-10-01 05:37:46 +02:00
|
|
|
|
|
|
|
|
|
|
if (processed == default)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-14 16:45:40 +01:00
|
|
|
|
var (querySearch, incompleteName) = processed;
|
2020-10-07 18:25:29 +02:00
|
|
|
|
var isRecursive = RecursiveSearch(incompleteName);
|
2020-10-01 05:37:46 +02:00
|
|
|
|
|
2020-10-07 18:25:29 +02:00
|
|
|
|
if (isRecursive)
|
2020-10-01 05:37:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
// match everything before and after search term using supported wildcard '*', ie. *searchterm*
|
|
|
|
|
|
if (string.IsNullOrEmpty(incompleteName))
|
|
|
|
|
|
{
|
|
|
|
|
|
incompleteName = "*";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2022-03-14 16:45:40 +01:00
|
|
|
|
incompleteName = string.Concat("*", incompleteName.AsSpan(1));
|
2020-10-01 05:37:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-14 16:45:40 +01:00
|
|
|
|
yield return new CreateOpenCurrentFolderResult(querySearch);
|
2020-10-01 05:37:46 +02:00
|
|
|
|
|
|
|
|
|
|
// Note: Take 1000 is so that you don't search the whole system before you discard
|
2022-03-14 16:45:40 +01:00
|
|
|
|
var lookup = _queryFileSystemInfo.MatchFileSystemInfo(querySearch, incompleteName, isRecursive)
|
2020-10-01 05:37:46 +02:00
|
|
|
|
.Take(1000)
|
|
|
|
|
|
.ToLookup(r => r.Type);
|
|
|
|
|
|
|
|
|
|
|
|
var folderList = lookup[DisplayType.Directory].ToImmutableArray();
|
|
|
|
|
|
var fileList = lookup[DisplayType.File].ToImmutableArray();
|
|
|
|
|
|
|
2022-03-14 16:45:40 +01:00
|
|
|
|
var fileSystemResult = GenerateFolderResults(querySearch, folderList)
|
|
|
|
|
|
.Concat<IItemResult>(GenerateFileResults(querySearch, fileList))
|
2020-10-01 05:37:46 +02:00
|
|
|
|
.ToImmutableArray();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var result in fileSystemResult)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Show warning message if result has been truncated
|
|
|
|
|
|
if (folderList.Length > _settings.MaxFolderResults || fileList.Length > _settings.MaxFileResults)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return GenerateTruncatedItemResult(folderList.Length + fileList.Length, fileSystemResult.Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerable<FileItemResult> GenerateFileResults(string search, IEnumerable<DisplayFileInfo> fileList)
|
|
|
|
|
|
{
|
|
|
|
|
|
return fileList
|
|
|
|
|
|
.Select(fileSystemInfo => new FileItemResult()
|
|
|
|
|
|
{
|
|
|
|
|
|
FilePath = fileSystemInfo.FullName,
|
|
|
|
|
|
Search = search,
|
|
|
|
|
|
})
|
|
|
|
|
|
.OrderBy(x => x.Title)
|
|
|
|
|
|
.Take(_settings.MaxFileResults);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerable<FolderItemResult> GenerateFolderResults(string search, IEnumerable<DisplayFileInfo> folderList)
|
|
|
|
|
|
{
|
|
|
|
|
|
return folderList
|
|
|
|
|
|
.Select(fileSystemInfo => new FolderItemResult(fileSystemInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
Search = search,
|
|
|
|
|
|
})
|
|
|
|
|
|
.OrderBy(x => x.Title)
|
|
|
|
|
|
.Take(_settings.MaxFolderResults);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static TruncatedItemResult GenerateTruncatedItemResult(int preTruncationCount, int postTruncationCount)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new TruncatedItemResult()
|
|
|
|
|
|
{
|
|
|
|
|
|
PreTruncationCount = preTruncationCount,
|
|
|
|
|
|
PostTruncationCount = postTruncationCount,
|
|
|
|
|
|
WarningIconPath = _warningIconPath,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void SetWarningIcon(Theme theme)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (theme == Theme.Light || theme == Theme.HighContrastWhite)
|
|
|
|
|
|
{
|
|
|
|
|
|
_warningIconPath = "Images/Warning.light.png";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_warningIconPath = "Images/Warning.dark.png";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|