Skip to content

Commit

Permalink
LineSorter 3.5
Browse files Browse the repository at this point in the history
- 3 different scenarios for processing empty lines
    1. Remove
    2. Process as ordinary string
    3. Process as mask
- Fixed critical bug with deletion custom sorts on extension update
  • Loading branch information
Kir-Antipov committed Sep 13, 2018
1 parent e575551 commit 2e42e8e
Show file tree
Hide file tree
Showing 28 changed files with 347 additions and 100 deletions.
15 changes: 15 additions & 0 deletions LineSorter.Export/Enums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.ComponentModel;

namespace LineSorter.Export
{
public enum EmptyLineAction
{
DependsOnSettings,
Remove,
AsLine,
AsMask,

[Browsable(false)]
AsGroupMarker
}
}
1 change: 1 addition & 0 deletions LineSorter.Export/IUserSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface IUserSort
{
string Guid { get; }
string Name { get; }
EmptyLineAction EmptyLineAction { get; }
IEnumerable<string> Sort(IEnumerable<string> Source);
}
}
1 change: 1 addition & 0 deletions LineSorter.Export/LineSorter.Export.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Enums.cs" />
<Compile Include="IUserSort.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Row.cs" />
Expand Down
4 changes: 2 additions & 2 deletions LineSorter.Export/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyVersion("3.0.0.0")]
[assembly: AssemblyFileVersion("3.0.0.0")]
[assembly: AssemblyVersion("3.5.0.0")]
[assembly: AssemblyFileVersion("3.5.0.0")]
[assembly: NeutralResourcesLanguage("en", UltimateResourceFallbackLocation.Satellite)]

[assembly: AssemblyTitle("LineSorter.Export")]
Expand Down
2 changes: 1 addition & 1 deletion LineSorter/Commands/CommandAlphSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static CommandAlphSort()

#region Functions
protected override void Execute(OleMenuCommand Button) =>
TextSelection.GetSelection(Package, out bool newLine).Select(x => (Row)x).OrderBy(x => x.Cleared).ThenBy(x => x.Cleared.Length).Select(x => (string)x).ReplaceSelection(newLine);
TextSelection.GetSelection(Package, out int[] poses, out bool newLine).Select(x => (Row)x).OrderBy(x => x.Cleared).ThenBy(x => x.Cleared.Length).Select(x => (string)x).ReplaceSelection(poses, newLine);
#endregion
}
}
2 changes: 1 addition & 1 deletion LineSorter/Commands/CommandAlphSortDesc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static CommandAlphSortDesc()

#region Functions
protected override void Execute(OleMenuCommand Button) =>
TextSelection.GetSelection(Package, out bool newLine).Select(x => (Row)x).OrderByDescending(x => x.Cleared).ThenByDescending(x => x.Cleared.Length).Select(x => (string)x).ReplaceSelection(newLine);
TextSelection.GetSelection(Package, out int[] poses, out bool newLine).Select(x => (Row)x).OrderByDescending(x => x.Cleared).ThenByDescending(x => x.Cleared.Length).Select(x => (string)x).ReplaceSelection(poses, newLine);
#endregion
}
}
6 changes: 1 addition & 5 deletions LineSorter/Commands/CommandAnchor.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using EnvDTE;
using System;
using System;
using System.Linq;
using LineSorter.Export;
using LineSorter.Options;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Threading;
using Microsoft.VisualStudio.Shell.Interop;

namespace LineSorter.Commands
{
Expand Down
2 changes: 1 addition & 1 deletion LineSorter/Commands/CommandLengthSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static CommandLengthSort()

#region Functions
protected override void Execute(OleMenuCommand Button) =>
TextSelection.GetSelection(Package, out bool newLine).Select(x => (Row)x).OrderBy(x => x.Cleared.Length).ThenBy(x => x.Cleared).Select(x => (string)x).ReplaceSelection(newLine);
TextSelection.GetSelection(Package, out int[] poses, out bool newLine).Select(x => (Row)x).OrderBy(x => x.Cleared.Length).ThenBy(x => x.Cleared).Select(x => (string)x).ReplaceSelection(poses, newLine);
#endregion
}
}
2 changes: 1 addition & 1 deletion LineSorter/Commands/CommandLengthSortDesc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static CommandLengthSortDesc()

#region Functions
protected override void Execute(OleMenuCommand Button) =>
TextSelection.GetSelection(Package, out bool newLine).Select(x => (Row)x).OrderByDescending(x => x.Cleared.Length).ThenByDescending(x => x.Cleared).Select(x => (string)x).ReplaceSelection(newLine);
TextSelection.GetSelection(Package, out int[] poses, out bool newLine).Select(x => (Row)x).OrderByDescending(x => x.Cleared.Length).ThenByDescending(x => x.Cleared).Select(x => (string)x).ReplaceSelection(poses, newLine);
#endregion
}
}
2 changes: 1 addition & 1 deletion LineSorter/Commands/CommandRandomSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static CommandRandomSort()

#region Functions
protected override void Execute(OleMenuCommand Button) =>
Shuffle(TextSelection.GetSelection(Package, out bool newLine)).ReplaceSelection(newLine);
Shuffle(TextSelection.GetSelection(Package, out int[] poses, out bool newLine)).ReplaceSelection(poses, newLine);

private static IEnumerable<T> Shuffle<T>(IEnumerable<T> Source)
{
Expand Down
7 changes: 2 additions & 5 deletions LineSorter/Commands/CommandUserSort.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.IO;
using System.Linq;
using LineSorter.Export;
using System.Reflection;
using LineSorter.Helpers;
using LineSorter.Options;
using System.Collections.Generic;
using LineSorter.Commands.UserSort;
using Microsoft.VisualStudio.Shell;
Expand All @@ -27,7 +24,7 @@ static CommandUserSort()
CommandID = 0x0105;
CommandSet = new Guid("e9f69e2b-6313-4c2b-9765-1ddd6439d519");
SavePath = Path.Combine(VSPackage.Path, "UserSort\\");
ExportFile = Path.Combine(SavePath, "LineSorter.Export.dll");
ExportFile = new Uri(typeof(IUserSort).Assembly.CodeBase, UriKind.Absolute).LocalPath;
if (!Directory.Exists(SavePath))
Directory.CreateDirectory(SavePath);
}
Expand All @@ -49,7 +46,7 @@ protected override void Execute(OleMenuCommand Button)

public void AddSort(IUserSort Sort)
{
MenuCommandWrapper wrapper = Factory.Create(() => Sort.Sort(TextSelection.GetSelection(Package, out bool was)).ReplaceSelection(was), Sort.Name);
MenuCommandWrapper wrapper = Factory.Create(() => Sort.Sort(TextSelection.GetSelection(Package, Sort.EmptyLineAction, out int[] poses, out bool was)).ReplaceSelection(Sort.EmptyLineAction, poses, was), Sort.Name);
wrapper.SetParameter("Guid", Sort.Guid);
}
#endregion
Expand Down
27 changes: 26 additions & 1 deletion LineSorter/Commands/MenuCommandWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using EnvDTE;
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using Microsoft.VisualStudio.Shell;
using System.ComponentModel.Design;
using Task = System.Threading.Tasks.Task;
using System.Linq;

namespace LineSorter.Commands
{
Expand Down Expand Up @@ -77,6 +81,27 @@ public void ChangeCmdID(CommandID NewID)
}
public void ChangeCmdID(int NewID) => ChangeCmdID(new CommandID(CommandID.Guid, NewID));

public async Task SetHotkeyAsync(Keys Mod1, Keys Key1, Keys Key2) => await SetBindingsAsync(GetStringBinding(Mod1, Key1, Key2));
public async Task SetHotkeyAsync(Keys Mod1, Keys Key1, Keys Mod2, Keys Key2) => await SetBindingsAsync(GetStringBinding(Mod1, Key1, Mod2, Key2));
public async Task RemoveHotkeyAsync() => await SetBindingsAsync();
private async Task SetBindingsAsync(params string[] Bindings)
{
await VSPackage.Instance.JoinableTaskFactory.SwitchToMainThreadAsync();
DTE dte = (await VSPackage.Instance.GetServiceAsync(typeof(DTE))) as DTE;
Command cmd = dte.Commands.Item(CommandID.Guid.ToString(), CommandID.ID);
cmd.Bindings = Bindings;
}
private string GetStringBinding(params Keys[] Keys)
{
return $"Global::{string.Join("+", Keys.Select(getKey))}";
string getKey(Keys key)
{
if ((int)key > 47 && (int)key < 58)
return ((int)key - 48).ToString();
return key.ToString();
}
}

public override string ToString() => Text;
public override int GetHashCode() => CommandID.GetHashCode();
public override bool Equals(object obj) => obj is MenuCommandWrapper wrapper && wrapper.CommandID == CommandID;
Expand Down
54 changes: 45 additions & 9 deletions LineSorter/Commands/UserSort/FormCreateFunc.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions LineSorter/Commands/UserSort/FormCreateFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,26 @@ static FormCreateFunc()
Manager["labelFunc", ru] = "Тип функции:";
Manager["labelT", en] = "`T` is:";
Manager["labelT", ru] = "`T` является:";
Manager["labelEmpty", en] = "Empty Lines:";
Manager["labelEmpty", ru] = "Пустые линии:";
Manager["labelName", en] = "Name:";
Manager["labelName", ru] = "Название:";
Manager["buttCompile", en] = "Compile";
Manager["buttCompile", ru] = "Компилировать";
Manager["buttCancel", en] = "Cancel";
Manager["buttCancel", ru] = "Отмена";
Manager["comboFunc.Items.0", en] = "Comparer (Func<T, T, int>)";
Manager["comboFunc.Items.0", ru] = "Компаратор (Func<T, T, int>)";
Manager["comboFunc.Items.1", en] = "Full implementation (Func<IEnumerable<T>, IEnumerable<T>>)";
Manager["comboFunc.Items.1", ru] = "Полная реализация (Func<IEnumerable<T>, IEnumerable<T>>)";
Manager["comboLines.Items.0", en] = "Depends on global settings";
Manager["comboLines.Items.0", ru] = "Аналогично глобальным настройкам";
Manager["comboLines.Items.1", en] = "Remove";
Manager["comboLines.Items.1", ru] = "Удалять";
Manager["comboLines.Items.2", en] = "As ordinary strings";
Manager["comboLines.Items.2", ru] = "Как обычные строки";
Manager["comboLines.Items.3", en] = "As mask";
Manager["comboLines.Items.3", ru] = "В качестве маски";
Manager["Error", en] = "Error!";
Manager["Error", ru] = "Ошибка!";
Manager["Error.NoName", en] = "Please specify the function name!";
Expand All @@ -86,9 +100,10 @@ public FormCreateFunc(string SavePath)
comboLang.SelectedIndex = 0;
comboFunc.SelectedIndex = 0;
comboType.SelectedIndex = 0;
comboLines.SelectedIndex = 0;
Measure = Graphics.FromImage(new Bitmap(textMain.Width, textMain.MaximumSize.Height));
foreach (Control x in new Control[] { this, labelLang, labelFunc, labelT, labelName, buttCompile, buttCancel })
x.Text = Manager[x.Name];
Manager.Localize(this);

}
public FormCreateFunc() : this(string.Empty) { }
#endregion
Expand All @@ -105,6 +120,7 @@ private void Compile()
bool isCSharp = comboLang.SelectedIndex == 0;
bool isString = comboType.SelectedIndex == 0;
bool isCompare = comboFunc.SelectedIndex == 0;
string emptyLineAction = ((EmptyLineAction)comboLines.SelectedIndex).ToString();
string userCode = $"{(isCSharp ? textBefore.Text : (isCompare ? $"{textBefore.Text} Implements IComparer(Of {(isString ? "String" : "Row")}).Compare" : (isString ? ($"{textBefore.Text} Implements IUserSort.Sort") : textBefore.Text)))}{Environment.NewLine}{textMain.Text}{Environment.NewLine}{textAfter.Text}";
if (isCSharp)
{
Expand Down Expand Up @@ -161,6 +177,7 @@ public class Wrapper : IUserSort
{{
public string Guid {{ get {{ return ""{guid}""; }} }}
public string Name {{ get {{ return ""{name}""; }} }}
public EmptyLineAction EmptyLineAction {{ get {{ return EmptyLineAction.{emptyLineAction}; }} }}
{userCode}
public override string ToString()
{{
Expand All @@ -183,6 +200,11 @@ Public ReadOnly Property Name As String Implements IUserSort.Name
Return ""{name}""
End Get
End Property
Public ReadOnly Property EmptyLineAction As EmptyLineAction Implements IUserSort.EmptyLineAction
Get
Return EmptyLineAction.{emptyLineAction}
End Get
End Property
{userCode}
Public Overrides Function ToString() As String
Return Name
Expand Down
3 changes: 1 addition & 2 deletions LineSorter/Commands/UserSort/FormTestFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public FormTestFunc(IUserSort Sorter) : this()
using Microsoft.CodeAnalysis.Text;
using Antlr4.Runtime.Misc;";
this.Sorter = Sorter;
foreach (Control x in new Control[] { this, labelTest, labelResult, buttDone })
x.Text = Manager[x.Name];
Manager.Localize(this);
Sort();
}
#endregion
Expand Down
Loading

0 comments on commit 2e42e8e

Please sign in to comment.