Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

创建空白的新项目 #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions EncodingNormalior.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.26228.9
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EncodingNormalior", "EncodingNormalior\EncodingNormalior.csproj", "{B4D74A9C-F593-4DD9-9CE6-FF12259E6337}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EncodingNormalior", "EncodingNormalior\EncodingNormalior.csproj", "{B4D74A9C-F593-4DD9-9CE6-FF12259E6337}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EncodingNormalizerVsx", "EncodingNormalizerVsx\EncodingNormalizerVsx.csproj", "{5C4AABAC-646E-4CA0-822A-0A3D18A296C2}"
EndProject
Expand All @@ -13,32 +13,64 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EncodingUtf8AndGBKDifferent
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EncodingNormalior.WPF", "EncodingNormalior.WPF\EncodingNormalior.WPF.csproj", "{DB0F1F5D-E82F-42A7-963F-4F072CAEBFAE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EncodingNormalizerVsix", "EncodingNormalizerVsix\EncodingNormalizerVsix.csproj", "{24B5AD6C-70C2-424D-B960-CD7513FA7695}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B4D74A9C-F593-4DD9-9CE6-FF12259E6337}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B4D74A9C-F593-4DD9-9CE6-FF12259E6337}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4D74A9C-F593-4DD9-9CE6-FF12259E6337}.Debug|x86.ActiveCfg = Debug|Any CPU
{B4D74A9C-F593-4DD9-9CE6-FF12259E6337}.Debug|x86.Build.0 = Debug|Any CPU
{B4D74A9C-F593-4DD9-9CE6-FF12259E6337}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4D74A9C-F593-4DD9-9CE6-FF12259E6337}.Release|Any CPU.Build.0 = Release|Any CPU
{B4D74A9C-F593-4DD9-9CE6-FF12259E6337}.Release|x86.ActiveCfg = Release|Any CPU
{B4D74A9C-F593-4DD9-9CE6-FF12259E6337}.Release|x86.Build.0 = Release|Any CPU
{5C4AABAC-646E-4CA0-822A-0A3D18A296C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C4AABAC-646E-4CA0-822A-0A3D18A296C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C4AABAC-646E-4CA0-822A-0A3D18A296C2}.Debug|x86.ActiveCfg = Debug|Any CPU
{5C4AABAC-646E-4CA0-822A-0A3D18A296C2}.Debug|x86.Build.0 = Debug|Any CPU
{5C4AABAC-646E-4CA0-822A-0A3D18A296C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C4AABAC-646E-4CA0-822A-0A3D18A296C2}.Release|Any CPU.Build.0 = Release|Any CPU
{5C4AABAC-646E-4CA0-822A-0A3D18A296C2}.Release|x86.ActiveCfg = Release|Any CPU
{5C4AABAC-646E-4CA0-822A-0A3D18A296C2}.Release|x86.Build.0 = Release|Any CPU
{388DB615-88E4-4306-B941-DC3C81B06194}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{388DB615-88E4-4306-B941-DC3C81B06194}.Debug|Any CPU.Build.0 = Debug|Any CPU
{388DB615-88E4-4306-B941-DC3C81B06194}.Debug|x86.ActiveCfg = Debug|Any CPU
{388DB615-88E4-4306-B941-DC3C81B06194}.Debug|x86.Build.0 = Debug|Any CPU
{388DB615-88E4-4306-B941-DC3C81B06194}.Release|Any CPU.ActiveCfg = Release|Any CPU
{388DB615-88E4-4306-B941-DC3C81B06194}.Release|Any CPU.Build.0 = Release|Any CPU
{388DB615-88E4-4306-B941-DC3C81B06194}.Release|x86.ActiveCfg = Release|Any CPU
{388DB615-88E4-4306-B941-DC3C81B06194}.Release|x86.Build.0 = Release|Any CPU
{19184DC4-D745-451E-A3DA-9A827730E16A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{19184DC4-D745-451E-A3DA-9A827730E16A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{19184DC4-D745-451E-A3DA-9A827730E16A}.Debug|x86.ActiveCfg = Debug|Any CPU
{19184DC4-D745-451E-A3DA-9A827730E16A}.Debug|x86.Build.0 = Debug|Any CPU
{19184DC4-D745-451E-A3DA-9A827730E16A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{19184DC4-D745-451E-A3DA-9A827730E16A}.Release|Any CPU.Build.0 = Release|Any CPU
{19184DC4-D745-451E-A3DA-9A827730E16A}.Release|x86.ActiveCfg = Release|Any CPU
{19184DC4-D745-451E-A3DA-9A827730E16A}.Release|x86.Build.0 = Release|Any CPU
{DB0F1F5D-E82F-42A7-963F-4F072CAEBFAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DB0F1F5D-E82F-42A7-963F-4F072CAEBFAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DB0F1F5D-E82F-42A7-963F-4F072CAEBFAE}.Debug|x86.ActiveCfg = Debug|Any CPU
{DB0F1F5D-E82F-42A7-963F-4F072CAEBFAE}.Debug|x86.Build.0 = Debug|Any CPU
{DB0F1F5D-E82F-42A7-963F-4F072CAEBFAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DB0F1F5D-E82F-42A7-963F-4F072CAEBFAE}.Release|Any CPU.Build.0 = Release|Any CPU
{DB0F1F5D-E82F-42A7-963F-4F072CAEBFAE}.Release|x86.ActiveCfg = Release|Any CPU
{DB0F1F5D-E82F-42A7-963F-4F072CAEBFAE}.Release|x86.Build.0 = Release|Any CPU
{24B5AD6C-70C2-424D-B960-CD7513FA7695}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{24B5AD6C-70C2-424D-B960-CD7513FA7695}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24B5AD6C-70C2-424D-B960-CD7513FA7695}.Debug|x86.ActiveCfg = Debug|x86
{24B5AD6C-70C2-424D-B960-CD7513FA7695}.Debug|x86.Build.0 = Debug|x86
{24B5AD6C-70C2-424D-B960-CD7513FA7695}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24B5AD6C-70C2-424D-B960-CD7513FA7695}.Release|Any CPU.Build.0 = Release|Any CPU
{24B5AD6C-70C2-424D-B960-CD7513FA7695}.Release|x86.ActiveCfg = Release|x86
{24B5AD6C-70C2-424D-B960-CD7513FA7695}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
308 changes: 308 additions & 0 deletions EncodingNormalizerVsix/EncodingNormalizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
//------------------------------------------------------------------------------
// <copyright file="EncodingNormalizer.cs" company="Company">
// Copyright (c) Company. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

using EncodingNormalizerVsx.View;

using EnvDTE;

using EnvDTE80;

using Microsoft.VisualStudio.Shell;

using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

using Window = System.Windows.Window;

namespace EncodingNormalizerVsx
{
/// <summary>
/// Command handler
/// </summary>
internal sealed class EncodingNormalizer
{
/// <summary>
/// Command ID.
/// </summary>
public const int CommandId = 0x0100;

/// <summary>
/// Command menu group (command set GUID).
/// </summary>
public static readonly Guid CommandSet = new Guid("0640f5ce-e6bc-43ba-b45e-497d70819a20");

/// <summary>
/// VS Package that provides this command, not null.
/// </summary>
private AsyncPackage AsyncPackage { get; }

private Window _conformWindow;
private Window _definitionWindow;

/// <summary>
/// Initializes a new instance of the <see cref="EncodingNormalizer" /> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
/// </summary>
/// <param name="package">Owner package, not null.</param>
private EncodingNormalizer(AsyncPackage package)
{
if (package == null)
{
throw new ArgumentNullException("package");
}

AsyncPackage = package;

OleMenuCommandService commandService = ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
{
CommandID menuCommandID = new CommandID(CommandSet, CommandId);
MenuCommand menuItem = new MenuCommand(EncodingNormalizerCallback, menuCommandID);
commandService.AddCommand(menuItem);

CommandID convertCurrentFileSaveEncodingCommand = new CommandID(CommandSet, 0x0103);
MenuCommand convertCurrentEncodingMenuCommand = new MenuCommand(ConvertCurrentFileEncoding, convertCurrentFileSaveEncodingCommand);
commandService.AddCommand(convertCurrentEncodingMenuCommand);

menuCommandID = new CommandID(CommandSet, 0x0101);
menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
commandService.AddCommand(menuItem);

//menuCommandID = new CommandID(CommandSet, 0x0102);
//menuItem = new MenuCommand(SaveEncoding, menuCommandID);
//commandService.AddCommand(menuItem);
}
}

#pragma warning disable VSTHRD100 // 避免使用 Async Void 方法
private async void ConvertCurrentFileEncoding(object sender, EventArgs e)
#pragma warning restore VSTHRD100 // 避免使用 Async Void 方法
{
await AsyncPackage.JoinableTaskFactory.SwitchToMainThreadAsync();
// 修改用户打开的文件的编码
DTE dte = (DTE)ServiceProvider.GetService(typeof(DTE));
Document document = dte.ActiveDocument;
string str = document.FullName;

new ConvertFileEncodingPage(new FileInfo(str)).Show();
}

/// <summary>
/// Gets the instance of the command.
/// </summary>
public static EncodingNormalizer Instance { get; private set; }

/// <summary>
/// Gets the service provider from the owner package.
/// </summary>
private IServiceProvider ServiceProvider => AsyncPackage;

#pragma warning disable VSTHRD100 // 避免使用 Async Void 方法
private async void EncodingNormalizerCallback(object sender, EventArgs e)
#pragma warning restore VSTHRD100 // 避免使用 Async Void 方法
{
await AsyncPackage.JoinableTaskFactory.SwitchToMainThreadAsync();

DTE dte = (DTE)ServiceProvider.GetService(typeof(DTE));
string file = dte.Solution.FullName;
List<string> project = new List<string>();

if (dte.Solution.Projects.Count > 0)
{
//try
//{
int noLoadProjectCount = await TryParseProjectAsync(dte, project);
//}
// catch (NotImplementedException)
//{
// MessageBox.Show("The project not loaded.", "项目还没有加载完成");
// return;
//}
if (noLoadProjectCount > 0)
{
if (project.Count == 0)
{
MessageBox.Show("All project not loaded.", "全部项目都没有加载完成");
return;
}
MessageBox.Show("存在" + noLoadProjectCount + "个工程没有加载");
}
else
{
if (project.Count == 0)
{
MessageBox.Show("Cant find any project.", "没有发现工程");
return;
}
}
}
else
{
MessageBox.Show("Cant find the solution.", "少年,听说你没有打开工程");
return;
}

ConformWindow(file, project);
}

private async Task<int> TryParseProjectAsync(DTE dte, List<string> project)
{
await AsyncPackage.JoinableTaskFactory.SwitchToMainThreadAsync();

int noLoadProjectCount = 0;

foreach (object temp in dte.Solution.Projects)
{
try
{
if (temp is Project tempProject)
{
if ((tempProject).Kind == ProjectKinds.vsProjectKindSolutionFolder)
{
foreach(var subProject in await GetSolutionFolderProjectsAsync(tempProject))
{
var directory = await ParseProjectFolderAsync(subProject);
if (!string.IsNullOrEmpty(directory))
{
project.Add(directory);
}
}
}
else
{
var directory = await ParseProjectFolderAsync(tempProject);

project.Add(directory);
}
}
}
catch (NotImplementedException)
{
noLoadProjectCount++;
}
}
return noLoadProjectCount;
}

private async Task<string> ParseProjectFolderAsync(Project project)
{
await AsyncPackage.JoinableTaskFactory.SwitchToMainThreadAsync();
string file = project.FullName;
if (!string.IsNullOrEmpty(file))
{
return new FileInfo(file).Directory?.FullName;
}
return "";
}

private async Task<List<Project>> GetSolutionFolderProjectsAsync(Project solutionFolder)
{
await AsyncPackage.JoinableTaskFactory.SwitchToMainThreadAsync();

List<Project> project = new List<Project>();
for (int i = 1; i <= solutionFolder.ProjectItems.Count; i++)
{
Project subProject = solutionFolder.ProjectItems.Item(i).SubProject;
if (subProject == null)
{
continue;
}

// If this is another solution folder, do a recursive call, otherwise add
if (subProject.Kind == ProjectKinds.vsProjectKindSolutionFolder)
{
project.AddRange(await GetSolutionFolderProjectsAsync(subProject));
}
else
{
project.Add(subProject);
}
}
return project;
}

private void ConformWindow(string file, List<string> project)
{
if (_conformWindow != null)
{
_conformWindow.Focus();
_conformWindow.Show();
return;
}

string folder = "";
if (!string.IsNullOrEmpty(file))
{
folder = new FileInfo(file).Directory?.FullName;
}
Window window = new Window()
{
Width = 500,
Height = 500
};
ConformPage conformPage = new ConformPage();
window.Content = conformPage;
window.Title = "编码规范工具";
conformPage.Closing += (_s, _e) =>
{
window.Close();
_conformWindow = null;
};
window.Closed += (_s, _e) => { _conformWindow = null; };
conformPage.SolutionFolder = folder;
conformPage.Project = project;
window.Show();
conformPage.InspectFolderEncoding();
_conformWindow = window;
}

/// <summary>
/// Initializes the singleton instance of the command.
/// </summary>
/// <param name="package">Owner package, not null.</param>
public static void Initialize(AsyncPackage package)
{
Instance = new EncodingNormalizer(package);
}


/// <summary>
/// This function is the callback used to execute the command when the menu item is clicked.
/// See the constructor to see how the menu item is associated with this function using
/// OleMenuCommandService service and MenuCommand class.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event args.</param>
private void MenuItemCallback(object sender, EventArgs e)
{
if (_definitionWindow != null)
{
_definitionWindow.Focus();
_definitionWindow.Show();
return;
}
Window window = new Window();
DefinitionPage definitionPage = new DefinitionPage();
definitionPage.Closing += (_s, _e) =>
{
window.Close();
_definitionWindow = null;
};
window.Closed += (_s, _e) => { _definitionWindow = null; };
window.Title = "编码规范工具设置";
window.Content = definitionPage;
window.Show();

_definitionWindow = window;
}
}
}
Loading