C# 一次格式化 Visual Studio 项目中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/931406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Formatting - at once - all the files in a Visual Studio project
提问by Dan
I am interested in formatting all the files in a Visual Studio (ver. 2005) project all at once.
我对一次性格式化 Visual Studio(2005 版)项目中的所有文件很感兴趣。
Currently, there is a way to format a single document by doing something like Edit->Advanced->Format Document. However, I don't see a single command to format all the files of a project all at once.
目前,有一种方法可以通过执行诸如Edit->Advanced->Format Document 之类的操作来格式化单个文档。但是,我没有看到一个命令可以一次性格式化项目的所有文件。
Any idea how to do that?
知道怎么做吗?
采纳答案by Dan Fuller
Tim Abell wrote a macro to do this on his blog:
Tim Abell 在他的博客上写了一个宏来做到这一点:
Here's a handy macro script for visual studio I knocked together today. It runs "edit, format document" on every document of the listed file types.
You have to keep an eye on it as it's interactive and does sometimes pop up a message and wait for an answer.
You can get the vb file at https://github.com/timabell/vs-formatter-macroMore info at https://github.com/timabell/vs-formatter-macro/wiki
这是我今天拼凑的 Visual Studio 的一个方便的宏脚本。它对列出的文件类型的每个文档运行“编辑、格式化文档”。
您必须密切关注它,因为它是交互式的,并且有时会弹出消息并等待答复。
您可以在https://github.com/timabell/vs-formatter-macro获取 vb 文件 更多信息请访问https://github.com/timabell/vs-formatter-macro/wiki
The original code is available at the blog post. Note that this is older than the version available on github above.
原始代码可在博客文章中找到。请注意,这比上面 github 上可用的版本旧。
回答by ANeves
Note, that the following solution does not work by itself starting with Visual Studio 2015. You need to apply the answer by Marcus Mangelsdorf as well. Then, this script works in Visual Studio 2015 and 2017.
请注意,从 Visual Studio 2015 开始,以下解决方案本身不起作用。您还需要应用 Marcus Mangelsdorf 的答案。然后,此脚本适用于 Visual Studio 2015 和 2017。
Phil Haack outlined a good procedure - adding a reusable script to indent the project.
Phil Haack 概述了一个很好的过程——添加一个可重用的脚本来缩进项目。
Open your NuGet profile for edition
打开您的 NuGet 配置文件进行版本
- Open the Package Manager;
- Type
$profile
to see the location of your NuGet profile; - Type
mkdir –force (split-path $profile)
to create the profile's folder if it does not exist; - Edit the profile with the command
notepad $profile
.
- 打开包管理器;
- 键入
$profile
以查看您的 NuGet 配置文件的位置; mkdir –force (split-path $profile)
如果配置文件不存在,请键入以创建配置文件的文件夹;- 使用命令编辑配置文件
notepad $profile
。
Add the reusable method to the NuGet profile
将可重用方法添加到 NuGet 配置文件
Phil used davidfowl's Format-Document
method which he found at https://gist.github.com/davidfowl/984358:
Phil 使用了Format-Document
他在https://gist.github.com/davidfowl/984358 上找到的davidfowl方法:
# Function to format all documents based on https://gist.github.com/984353
function Format-Document {
param(
[parameter(ValueFromPipelineByPropertyName = $true)]
[string[]]$ProjectName
)
Process {
$ProjectName | %{
Recurse-Project -ProjectName $_ -Action { param($item)
if($item.Type -eq 'Folder' -or !$item.Language) {
return
}
$window = $item.ProjectItem.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}')
if ($window) {
Write-Host "Processing `"$($item.ProjectItem.Name)`""
[System.Threading.Thread]::Sleep(100)
$window.Activate()
$Item.ProjectItem.Document.DTE.ExecuteCommand('Edit.FormatDocument')
$Item.ProjectItem.Document.DTE.ExecuteCommand('Edit.RemoveAndSort')
$window.Close(1)
}
}
}
}
}
function Recurse-Project {
param(
[parameter(ValueFromPipelineByPropertyName = $true)]
[string[]]$ProjectName,
[parameter(Mandatory = $true)]$Action
)
Process {
# Convert project item guid into friendly name
function Get-Type($kind) {
switch($kind) {
'{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}' { 'File' }
'{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}' { 'Folder' }
default { $kind }
}
}
# Convert language guid to friendly name
function Get-Language($item) {
if(!$item.FileCodeModel) {
return $null
}
$kind = $item.FileCodeModel.Language
switch($kind) {
'{B5E9BD34-6D3E-4B5D-925E-8A43B79820B4}' { 'C#' }
'{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}' { 'VB' }
default { $kind }
}
}
# Walk over all project items running the action on each
function Recurse-ProjectItems($projectItems, $action) {
$projectItems | %{
$obj = New-Object PSObject -Property @{
ProjectItem = $_
Type = Get-Type $_.Kind
Language = Get-Language $_
}
& $action $obj
if($_.ProjectItems) {
Recurse-ProjectItems $_.ProjectItems $action
}
}
}
if($ProjectName) {
$p = Get-Project $ProjectName
}
else {
$p = Get-Project
}
$p | %{ Recurse-ProjectItems $_.ProjectItems $Action }
}
}
# Statement completion for project names
Register-TabExpansion 'Recurse-Project' @{
ProjectName = { Get-Project -All | Select -ExpandProperty Name }
}
Reopen Visual Studio to use the command
重新打开 Visual Studio 以使用该命令
When you reopen Visual Studio, the command is available.
重新打开 Visual Studio 时,该命令可用。
Simply run it from the NuGet Package Manager Console: Format-Document
This will re-format all files of the selected project.
To apply to the whole solution, use the command Get-Project -All | Format-Document
, which lists the projects and then for each of them calls the reformatting command.
只需从 NuGet 包管理器控制台运行它:Format-Document
这将重新格式化所选项目的所有文件。
要应用于整个解决方案,请使用命令Get-Project -All | Format-Document
,它列出项目,然后为每个项目调用重新格式化命令。
As the author put it:
正如作者所说:
With this in place, you can now indulge your OCD and run the Format-Document command to clean up your entire solution. I just ran it against <Project> and now can become the whitespace Nazi I've always wanted to be.
有了这个,你现在可以尽情享受你的强迫症并运行 Format-Document 命令来清理你的整个解决方案。我只是针对 <Project> 运行它,现在可以成为我一直想成为的空白纳粹。
10/10, would run again.
10/10,将再次运行。
回答by Marcus Mangelsdorf
Additional step needed for Visual Studio 2015
Visual Studio 2015 所需的额外步骤
Phil Haack's solution posted by ANevesis perfect, but for some reason $item.FileCodeModel.Language
always returns null in Visual Studio 2015making the Format-Document
skip all files and effectively do nothing.
ANeves 发布的 Phil Haack 的解决方案是完美的,但由于某种原因,$item.FileCodeModel.Language
在Visual Studio 2015 中总是返回 null,从而Format-Document
跳过所有文件并有效地什么都不做。
To (hackily) work around this limitation you can replace the Get-Language
function:
要(hackily)解决此限制,您可以替换该Get-Language
功能:
# Convert language guid to friendly name
function Get-Language($item) {
if(!$item.FileCodeModel) {
return $null
}
$kind = $item.FileCodeModel.Language
switch($kind) {
'{B5E9BD34-6D3E-4B5D-925E-8A43B79820B4}' { 'C#' }
'{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}' { 'VB' }
default { $kind }
}
}
with the following variant that uses the file's extension instead of the Language GUID:
具有以下使用文件扩展名而不是语言 GUID 的变体:
# Convert file extension to friendly language name
function Get-Language($item) {
if(!$item.FileCodeModel) {
return $null
}
$filename = $item.Name
$ext = $filename.substring($filename.lastindexof('.'),
($filename.length - $filename.lastindexof('.')))
switch($ext) {
'.cs' { 'C#' }
'.vb' { 'VB' }
# If you want to prevent re-formatting files that are not VB or C# source files
# (e.g. XML files in your project etc.), replace the following line with
# "default { $null }" (thanks to HHenn for this suggestion!)
default { $ext }
}
}
回答by Mikhail
The Format All Filesextension worked for me. Nothing to do, just install and click!
该格式所有文件的扩展为我工作。什么都不用做,只需安装并点击!
回答by Chayim Friedman
If anyone is still interested in this question, Visual Studio 2019 brought this functionality through a feature called Code Cleanup!
如果有人仍然对这个问题感兴趣,Visual Studio 2019 通过一个名为Code Cleanup的功能带来了这个功能!
Just run Code Cleanup for Solution!
只需运行代码清理解决方案!
And you can also create multiple clean profiles and define which actions happen in each one.
您还可以创建多个干净的配置文件并定义每个配置文件中发生的操作。