Enabling StyleCop for folder plugin (#5844)

This commit is contained in:
Clint Rutkas
2020-08-10 13:28:22 -07:00
committed by GitHub
parent 013ffe1626
commit 7bfd0823db
9 changed files with 614 additions and 563 deletions

View File

@@ -1,16 +1,17 @@
// 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Threading.Tasks;
using System.Windows;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Image;
using Wox.Plugin.SharedCommands;
using Wox.Plugin;
using System.Reflection; using System.Reflection;
using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
namespace Microsoft.Plugin.Folder namespace Microsoft.Plugin.Folder
{ {
@@ -57,7 +58,7 @@ namespace Microsoft.Plugin.Folder
_context.API.ShowMsg(message); _context.API.ShowMsg(message);
return false; return false;
} }
} },
}); });
contextMenus.Add(new ContextMenuResult contextMenus.Add(new ContextMenuResult
@@ -89,7 +90,7 @@ namespace Microsoft.Plugin.Folder
Log.Exception($"|Microsoft.Plugin.Folder.ContextMenuLoader.LoadContextMenus| Failed to open {record.FullPath} in console, {e.Message}", e); Log.Exception($"|Microsoft.Plugin.Folder.ContextMenuLoader.LoadContextMenus| Failed to open {record.FullPath} in console, {e.Message}", e);
return false; return false;
} }
} },
}); });
} }
@@ -106,7 +107,7 @@ namespace Microsoft.Plugin.Folder
Glyph = "\xE838", Glyph = "\xE838",
FontFamily = "Segoe MDL2 Assets", FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.E, AcceleratorKey = Key.E,
AcceleratorModifiers = (ModifierKeys.Control | ModifierKeys.Shift), AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ => Action = _ =>
{ {
try try
@@ -122,7 +123,7 @@ namespace Microsoft.Plugin.Folder
} }
return true; return true;
} },
}; };
} }
@@ -132,16 +133,10 @@ namespace Microsoft.Plugin.Folder
} }
} }
public class SearchResult
{
public string FullPath { get; set; }
public ResultType Type { get; set; }
}
public enum ResultType public enum ResultType
{ {
Volume, Volume,
Folder, Folder,
File File,
} }
} }

View File

@@ -1,4 +1,8 @@
using System; // 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.Linq; using System.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;

View File

@@ -11,8 +11,8 @@
<RowDefinition Height="50"/> <RowDefinition Height="50"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<ListView x:Name="lbxFolders" Grid.Row="0" AllowDrop="True" <ListView x:Name="lbxFolders" Grid.Row="0" AllowDrop="True"
Drop="lbxFolders_Drop" Drop="LbxFolders_Drop"
DragEnter="lbxFolders_DragEnter"> DragEnter="LbxFolders_DragEnter">
<ListView.View> <ListView.View>
<GridView> <GridView>
<GridViewColumn Header="{DynamicResource wox_plugin_folder_folder_path}" Width="180"> <GridViewColumn Header="{DynamicResource wox_plugin_folder_folder_path}" Width="180">
@@ -26,9 +26,9 @@
</ListView.View> </ListView.View>
</ListView> </ListView>
<StackPanel Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal"> <StackPanel Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal">
<Button x:Name="btnDelete" Click="btnDelete_Click" Width="100" Margin="10" Content="{DynamicResource wox_plugin_folder_delete}"/> <Button x:Name="btnDelete" Click="BtnDelete_Click" Width="100" Margin="10" Content="{DynamicResource wox_plugin_folder_delete}"/>
<Button x:Name="btnEdit" Click="btnEdit_Click" Width="100" Margin="10" Content="{DynamicResource wox_plugin_folder_edit}"/> <Button x:Name="btnEdit" Click="BtnEdit_Click" Width="100" Margin="10" Content="{DynamicResource wox_plugin_folder_edit}"/>
<Button x:Name="btnAdd" Click="btnAdd_Click" Width="100" Margin="10" Content="{DynamicResource wox_plugin_folder_add}"/> <Button x:Name="btnAdd" Click="BtnAdd_Click" Width="100" Margin="10" Content="{DynamicResource wox_plugin_folder_add}"/>
</StackPanel> </StackPanel>
</Grid> </Grid>
</UserControl> </UserControl>

View File

@@ -1,5 +1,8 @@
using System; // Copyright (c) Microsoft Corporation
using System.Collections.Generic; // 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.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@@ -13,26 +16,24 @@ using MessageBox = System.Windows.MessageBox;
namespace Microsoft.Plugin.Folder namespace Microsoft.Plugin.Folder
{ {
public partial class FileSystemSettings public partial class FileSystemSettings
{ {
private IPublicAPI woxAPI; private IPublicAPI _woxAPI;
private FolderSettings _settings; private FolderSettings _settings;
public FileSystemSettings(IPublicAPI woxAPI, FolderSettings settings) public FileSystemSettings(IPublicAPI woxAPI, FolderSettings settings)
{ {
this.woxAPI = woxAPI; _woxAPI = woxAPI;
InitializeComponent(); InitializeComponent();
_settings = settings ?? throw new ArgumentNullException(paramName:nameof(settings)); _settings = settings ?? throw new ArgumentNullException(paramName: nameof(settings));
lbxFolders.ItemsSource = _settings.FolderLinks; lbxFolders.ItemsSource = _settings.FolderLinks;
} }
private void btnDelete_Click(object sender, RoutedEventArgs e) private void BtnDelete_Click(object sender, RoutedEventArgs e)
{ {
var selectedFolder = lbxFolders.SelectedItem as FolderLink; if (lbxFolders.SelectedItem is FolderLink selectedFolder)
if (selectedFolder != null)
{ {
string msg = string.Format(CultureInfo.InvariantCulture, woxAPI.GetTranslation("wox_plugin_folder_delete_folder_link"), selectedFolder.Path); string msg = string.Format(CultureInfo.InvariantCulture, _woxAPI.GetTranslation("wox_plugin_folder_delete_folder_link"), selectedFolder.Path);
if (MessageBox.Show(msg, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.Yes) if (MessageBox.Show(msg, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{ {
@@ -42,15 +43,14 @@ namespace Microsoft.Plugin.Folder
} }
else else
{ {
string warning = woxAPI.GetTranslation("wox_plugin_folder_select_folder_link_warning"); string warning = _woxAPI.GetTranslation("wox_plugin_folder_select_folder_link_warning");
MessageBox.Show(warning); MessageBox.Show(warning);
} }
} }
private void btnEdit_Click(object sender, RoutedEventArgs e) private void BtnEdit_Click(object sender, RoutedEventArgs e)
{ {
var selectedFolder = lbxFolders.SelectedItem as FolderLink; if (lbxFolders.SelectedItem is FolderLink selectedFolder)
if (selectedFolder != null)
{ {
using (var folderBrowserDialog = new FolderBrowserDialog()) using (var folderBrowserDialog = new FolderBrowserDialog())
{ {
@@ -66,12 +66,12 @@ namespace Microsoft.Plugin.Folder
} }
else else
{ {
string warning = woxAPI.GetTranslation("wox_plugin_folder_select_folder_link_warning"); string warning = _woxAPI.GetTranslation("wox_plugin_folder_select_folder_link_warning");
MessageBox.Show(warning); MessageBox.Show(warning);
} }
} }
private void btnAdd_Click(object sender, RoutedEventArgs e) private void BtnAdd_Click(object sender, RoutedEventArgs e)
{ {
using (var folderBrowserDialog = new FolderBrowserDialog()) using (var folderBrowserDialog = new FolderBrowserDialog())
{ {
@@ -79,7 +79,7 @@ namespace Microsoft.Plugin.Folder
{ {
var newFolder = new FolderLink var newFolder = new FolderLink
{ {
Path = folderBrowserDialog.SelectedPath Path = folderBrowserDialog.SelectedPath,
}; };
_settings.FolderLinks.Add(newFolder); _settings.FolderLinks.Add(newFolder);
@@ -89,7 +89,7 @@ namespace Microsoft.Plugin.Folder
} }
} }
private void lbxFolders_Drop(object sender, DragEventArgs e) private void LbxFolders_Drop(object sender, DragEventArgs e)
{ {
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
@@ -101,7 +101,7 @@ namespace Microsoft.Plugin.Folder
{ {
var newFolder = new FolderLink var newFolder = new FolderLink
{ {
Path = s Path = s,
}; };
_settings.FolderLinks.Add(newFolder); _settings.FolderLinks.Add(newFolder);
@@ -112,7 +112,7 @@ namespace Microsoft.Plugin.Folder
} }
} }
private void lbxFolders_DragEnter(object sender, DragEventArgs e) private void LbxFolders_DragEnter(object sender, DragEventArgs e)
{ {
if (e.Data.GetDataPresent(DataFormats.FileDrop)) if (e.Data.GetDataPresent(DataFormats.FileDrop))
{ {

View File

@@ -0,0 +1,15 @@
// 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.Collections.Generic;
using Newtonsoft.Json;
namespace Microsoft.Plugin.Folder
{
public class FolderSettings
{
[JsonProperty]
public List<FolderLink> FolderLinks { get; } = new List<FolderLink>();
}
}

View File

@@ -1,4 +1,7 @@
using Microsoft.PowerToys.Settings.UI.Lib; // 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@@ -8,6 +11,7 @@ using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using Microsoft.PowerToys.Settings.UI.Lib;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Infrastructure.Storage; using Wox.Infrastructure.Storage;
using Wox.Plugin; using Wox.Plugin;
@@ -22,11 +26,13 @@ namespace Microsoft.Plugin.Folder
public const string CopyImagePath = "Images\\copy.dark.png"; public const string CopyImagePath = "Images\\copy.dark.png";
private const string _fileExplorerProgramName = "explorer"; private const string _fileExplorerProgramName = "explorer";
private static List<string> _driverNames;
private PluginInitContext _context;
private readonly FolderSettings _settings; private readonly FolderSettings _settings;
private readonly PluginJsonStorage<FolderSettings> _storage; private readonly PluginJsonStorage<FolderSettings> _storage;
private static List<string> _driverNames;
private PluginInitContext _context;
private IContextMenu _contextMenuLoader; private IContextMenu _contextMenuLoader;
public Main() public Main()
@@ -55,7 +61,7 @@ namespace Microsoft.Plugin.Folder
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Do not want to change the behavior of the application, but want to enforce static analysis")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Do not want to change the behavior of the application, but want to enforce static analysis")]
public List<Result> Query(Query query) public List<Result> Query(Query query)
{ {
if(query == null) if (query == null)
{ {
throw new ArgumentNullException(paramName: nameof(query)); throw new ArgumentNullException(paramName: nameof(query));
} }
@@ -64,7 +70,9 @@ namespace Microsoft.Plugin.Folder
string search = query.Search.ToLower(CultureInfo.InvariantCulture); string search = query.Search.ToLower(CultureInfo.InvariantCulture);
if (!IsDriveOrSharedFolder(search)) if (!IsDriveOrSharedFolder(search))
{
return results; return results;
}
results.AddRange(QueryInternal_Directory_Exists(query)); results.AddRange(QueryInternal_Directory_Exists(query));
@@ -111,14 +119,14 @@ namespace Microsoft.Plugin.Folder
{ {
Process.Start(_fileExplorerProgramName, path); Process.Start(_fileExplorerProgramName, path);
return true; return true;
} },
}; };
} }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Do not want to change the behavior of the application, but want to enforce static analysis")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Do not want to change the behavior of the application, but want to enforce static analysis")]
private List<Result> GetUserFolderResults(Query query) private List<Result> GetUserFolderResults(Query query)
{ {
if(query == null) if (query == null)
{ {
throw new ArgumentNullException(paramName: nameof(query)); throw new ArgumentNullException(paramName: nameof(query));
} }
@@ -147,7 +155,7 @@ namespace Microsoft.Plugin.Folder
private static readonly char[] _specialSearchChars = new char[] private static readonly char[] _specialSearchChars = new char[]
{ {
'?', '*', '>' '?', '*', '>',
}; };
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Do not want to change the behavior of the application, but want to enforce static analysis")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Do not want to change the behavior of the application, but want to enforce static analysis")]
@@ -156,7 +164,7 @@ namespace Microsoft.Plugin.Folder
var search = query.Search; var search = query.Search;
var results = new List<Result>(); var results = new List<Result>();
var hasSpecial = search.IndexOfAny(_specialSearchChars) >= 0; var hasSpecial = search.IndexOfAny(_specialSearchChars) >= 0;
string incompleteName = ""; string incompleteName = string.Empty;
if (hasSpecial || !Directory.Exists(search + "\\")) if (hasSpecial || !Directory.Exists(search + "\\"))
{ {
// if folder doesn't exist, we want to take the last part and use it afterwards to help the user // if folder doesn't exist, we want to take the last part and use it afterwards to help the user
@@ -185,7 +193,7 @@ namespace Microsoft.Plugin.Folder
} }
} }
results.Add(CreateOpenCurrentFolderResult( search)); results.Add(CreateOpenCurrentFolderResult(search));
var searchOption = SearchOption.TopDirectoryOnly; var searchOption = SearchOption.TopDirectoryOnly;
incompleteName += "*"; incompleteName += "*";
@@ -210,7 +218,10 @@ namespace Microsoft.Plugin.Folder
foreach (var fileSystemInfo in fileSystemInfos) foreach (var fileSystemInfo in fileSystemInfos)
{ {
if ((fileSystemInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue; if ((fileSystemInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
continue;
}
if (fileSystemInfo is DirectoryInfo) if (fileSystemInfo is DirectoryInfo)
{ {
@@ -262,7 +273,7 @@ namespace Microsoft.Plugin.Folder
return true; return true;
}, },
ContextData = new SearchResult { Type = ResultType.File, FullPath = filePath } ContextData = new SearchResult { Type = ResultType.File, FullPath = filePath },
}; };
return result; return result;
} }
@@ -273,6 +284,7 @@ namespace Microsoft.Plugin.Folder
var folderName = search.TrimEnd('\\').Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.None).Last(); var folderName = search.TrimEnd('\\').Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.None).Last();
var sanitizedPath = Regex.Replace(search, @"[\/\\]+", "\\"); var sanitizedPath = Regex.Replace(search, @"[\/\\]+", "\\");
// A network path must start with \\ // A network path must start with \\
if (sanitizedPath.StartsWith("\\", StringComparison.InvariantCulture)) if (sanitizedPath.StartsWith("\\", StringComparison.InvariantCulture))
{ {
@@ -290,7 +302,7 @@ namespace Microsoft.Plugin.Folder
{ {
Process.Start(_fileExplorerProgramName, sanitizedPath); Process.Start(_fileExplorerProgramName, sanitizedPath);
return true; return true;
} },
}; };
} }
@@ -311,7 +323,6 @@ namespace Microsoft.Plugin.Folder
public void UpdateSettings(PowerLauncherSettings settings) public void UpdateSettings(PowerLauncherSettings settings)
{ {
} }
} }
} }

View File

@@ -135,5 +135,19 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\..\codeAnalysis\GlobalSuppressions.cs">
<Link>GlobalSuppressions.cs</Link>
</Compile>
<AdditionalFiles Include="..\..\..\..\codeAnalysis\StyleCop.json">
<Link>StyleCop.json</Link>
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers">
<Version>1.1.118</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,24 @@
// 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.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
using Wox.Infrastructure;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
namespace Microsoft.Plugin.Folder
{
public class SearchResult
{
public string FullPath { get; set; }
public ResultType Type { get; set; }
}
}

View File

@@ -1,12 +0,0 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Wox.Infrastructure.Storage;
namespace Microsoft.Plugin.Folder
{
public class FolderSettings
{
[JsonProperty]
public List<FolderLink> FolderLinks { get;} = new List<FolderLink>();
}
}