Dan*_*Dan 52 c# formatting visual-studio-2005 visual-studio
我有兴趣一次性格式化Visual Studio(版本2005)项目中的所有文件.
目前,有一种方法可以通过执行Edit-> Advanced-> Format Document等操作来格式化单个文档.但是,我没有看到一个命令来同时格式化项目的所有文件.
知道怎么做吗?
Dan*_*ler 26
蒂姆阿贝尔在他的博客上写了一个宏来做到这一点:
这是一个方便的宏观脚本,我今天在一起的视觉工作室.它在列出的文件类型的每个文档上运行"编辑,格式化文档".
您必须密切关注它,因为它是交互式的,有时会弹出消息并等待答案.
您可以访问http://github.com/timabell/vs-formatter-macro获取vb文件. 更多信息请访问http://wiki.github.com/timabell/vs-formatter-macro
原始代码可在博客文章中找到.请注意,这比上面的github上提供的版本旧.
ANe*_*ves 16
请注意,从Visual Studio 2015开始,以下解决方案本身不起作用.您还需要应用Marcus Mangelsdorf的答案.然后,此脚本适用于Visual Studio 2015和2017.
Phil Haack概述了一个很好的程序 - 添加一个可重复使用的脚本来缩进项目.
$profile以查看NuGet个人资料的位置;mkdir –force (split-path $profile)以创建配置文件的文件夹(如果它不存在);notepad $profile.Phil使用davidfowl的Format-Document方法,他在https://gist.github.com/davidfowl/984358找到了这个方法:
# 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 }
}
Run Code Online (Sandbox Code Playgroud)
重新打开Visual Studio时,该命令可用.
只需从NuGet包管理器控制台运行它:Format-Document这将重新格式化所选项目的所有文件.
要应用于整个解决方案,请使用命令Get-Project -All | Format-Document列出项目,然后为每个项目调用重新格式化命令.
正如作者所说:
有了这个,您现在可以放纵您的OCD并运行Format-Document命令来清理整个解决方案.我只是针对<Project>运行它,现在可以成为我一直想成为的空白纳粹.
10/10,会再次运行.
Mar*_*orf 13
由ANeves发布的Phil Haack的解决方案是完美的,但由于某种原因,$item.FileCodeModel.Language在Visual Studio 2015中总是返回null,使得Format-Document跳过所有文件并且实际上什么都不做.
为了(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 }
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下使用文件扩展名而不是语言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 }
}
}
Run Code Online (Sandbox Code Playgroud)
Ram*_* A. 12
有一种使用dotnetCLI 格式化 Visual Studio 项目中所有文件的新方法:
dotnet tool install -g dotnet-formatProjectFile.csproj使用以下命令行替换项目文件的路径:dotnet format ProjectFile.csproj如果有人仍然对这个问题感兴趣,Visual Studio 2019 通过一个名为Code Cleanup的功能带来了这个功能!
只需运行代码清理解决方案!
您还可以创建多个干净的配置文件并定义每个配置文件中发生的操作。
| 归档时间: |
|
| 查看次数: |
30013 次 |
| 最近记录: |