ファイルを保存するときに各行の末尾の空白文字を自動的に削除するようにVisual Studio 2008を構成することは可能ですか?組み込みオプションはないようですが、これを行うための拡張機能はありますか?
CodeMaidは非常に人気のあるVisual Studio拡張機能であり、他の便利なクリーンアップとともにこれを自動的に行います。
保存時にファイルをクリーンアップするように設定しましたが、これがデフォルトだと思います。
正規表現を使用した検索/置換
「検索と置換」ダイアログで、「検索オプション」を展開し、「使用」を選択し、「正規表現」を選択します
検索対象: ":Zs#$
"
置換: ""
すべて置換をクリックします
他のエディター(a normal Regular Expression parser) ":Zs#$
" だろう "\s*$
"。
これを行うために、保存後に実行するマクロを作成できます。
マクロのEnvironmentEventsモジュールに次を追加します。
Private saved As Boolean = False
Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) _
Handles DocumentEvents.DocumentSaved
If Not saved Then
Try
DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, _
"\t", _
vsFindOptions.vsFindOptionsRegularExpression, _
" ", _
vsFindTarget.vsFindTargetCurrentDocument, , , _
vsFindResultsLocation.vsFindResultsNone)
' Remove all the trailing whitespaces.
DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, _
":Zs+$", _
vsFindOptions.vsFindOptionsRegularExpression, _
String.Empty, _
vsFindTarget.vsFindTargetCurrentDocument, , , _
vsFindResultsLocation.vsFindResultsNone)
saved = True
document.Save()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Trim White Space exception")
End Try
Else
saved = False
End If
End Sub
私はこれをしばらく問題なく使用しています。マクロは作成しませんでしたが、簡単なGoogle検索で見つけることができるace_guidelines.vsmacrosのマクロを変更しました。
保存する前に、自動フォーマットのショートカットを使用できる場合があります CTRL+K+D。
これは、次の3つのアクションで簡単に実行できます。
Ctrl + A (すべてのテキストを選択)
編集->詳細->水平空白を削除
編集->詳細->フォーマット選択
数秒待ってから完了します。
それは Ctrl + Z「何かがうまくいかなかった場合に備えて。
すでに与えられたすべての答えから要素を取り、ここで私が終わったコードです。 (主にC++コードを記述しますが、必要に応じてさまざまなファイル拡張子を簡単に確認できます。)
貢献してくれたみんなに感謝します!
Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) _
Handles DocumentEvents.DocumentSaved
Dim fileName As String
Dim result As vsFindResult
Try
fileName = document.Name.ToLower()
If fileName.EndsWith(".cs") _
Or fileName.EndsWith(".cpp") _
Or fileName.EndsWith(".c") _
Or fileName.EndsWith(".h") Then
' Remove trailing whitespace
result = DTE.Find.FindReplace( _
vsFindAction.vsFindActionReplaceAll, _
"{:b}+$", _
vsFindOptions.vsFindOptionsRegularExpression, _
String.Empty, _
vsFindTarget.vsFindTargetFiles, _
document.FullName, _
"", _
vsFindResultsLocation.vsFindResultsNone)
If result = vsFindResult.vsFindResultReplaced Then
' Triggers DocumentEvents_DocumentSaved event again
document.Save()
End If
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Trim White Space exception")
End Try
End Sub
私は [〜#〜] vwd [〜#〜] 2010を使用していますが、残念ながらマクロはサポートされていません。だから私はちょうどにコピー/貼り付けを行います Notepad ++ 左上のメニューEdit
> Blank Operations
> Trim Trailing Space
他にも利用可能な関連操作があります。次に、Visual Studioにコピーして貼り付けます。
Notepad ++の代わりに NetBeans を使用することもできます。Notepad++の[ソース]メニューの下に[末尾のスペースを削除する]があります。
これが1人のプロジェクトでない限り、実行しないでください。ローカルファイルとソースコードリポジトリを比較するのは簡単なことです。空白をクリアすると、変更する必要のない行が変更されます。私は完全に理解しています。空白をすべて統一するのが大好きですが、これはよりクリーンなコラボレーションのためにcollaborationめるべきものです。
Jeff Muirバージョンは、ソースコードファイルをトリミングするだけであれば少し改善されると思います(私の場合はC#ですが、拡張機能を追加するのは簡単です)。また、ドキュメントウィンドウが表示されることを確認するためのチェックを追加しました。そのチェックがない場合、奇妙なエラー(LINQ to SQLファイル '* .dbml'など)が表示されるためです。
Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) Handles DocumentEvents.DocumentSaved
Dim result As vsFindResult
Try
If (document.ActiveWindow Is Nothing) Then
Return
End If
If (document.Name.ToLower().EndsWith(".cs")) Then
document.Activate()
result = DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, ":Zs+$", vsFindOptions.vsFindOptionsRegularExpression, String.Empty, vsFindTarget.vsFindTargetCurrentDocument, , , vsFindResultsLocation.vsFindResultsNone)
If result = vsFindResult.vsFindResultReplaced Then
document.Save()
End If
End If
Catch ex As Exception
MsgBox(ex.Message & Chr(13) & "Document: " & document.FullName, MsgBoxStyle.OkOnly, "Trim White Space exception")
End Try
End Sub
個人的にはTrailing Whitespace VisualizerVisual Studio 2012をサポートするVisual Studio拡張機能が大好きです。
ArtisticStyle (C++)を使用してこれを行い、コードを再フォーマットします。ただし、これを外部ツールとして追加する必要があり、気に入らない場合は自分でトリガーする必要があります。
ただし、コードを手動で実行する代価を払うことができる、よりカスタムな方法(複数行関数パラメーターなど)でコードを再フォーマットできることは素晴らしいことです。ツールは無料です。
Dyausの答えと connect report の正規表現に基づいて、すべてを保存し、タブをスペースに置き換えず、静的変数を必要としないマクロを以下に示します。そのマイナス面は?おそらくFindReplace
への複数の呼び出しが原因で、少し遅いようです。
_Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) _
Handles DocumentEvents.DocumentSaved
Try
' Remove all the trailing whitespaces.
If vsFindResult.vsFindResultReplaced = DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, _
"{:b}+$", _
vsFindOptions.vsFindOptionsRegularExpression, _
String.Empty, _
vsFindTarget.vsFindTargetFiles, _
document.FullName, , _
vsFindResultsLocation.vsFindResultsNone) Then
document.Save()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Trim White Space exception")
End Try
End Sub
_
Visual Studio 2012アドインでこれを使用しようとする他の人にとって、私が使用することになった正規表現は[ \t]+(?=\r?$)
です(必要に応じてバックスラッシュをエスケープすることを忘れないでください)。 _{:b}+$
_の raw変換 がキャリッジリターンと一致しないという問題を修正しようとするいくつかの無駄な試みの後、私はここに到着しました。
リファクタリングでVS2010をクラッシュさせず、非テキストファイルを保存するときにIDEをハングさせないこのバージョンのマクロがあると思います。これを試してください。
Private Sub DocumentEvents_DocumentSaved( _
ByVal document As EnvDTE.Document) _
Handles DocumentEvents.DocumentSaved
' See if we're saving a text file
Dim textDocument As EnvDTE.TextDocument = _
TryCast(document.Object(), EnvDTE.TextDocument)
If textDocument IsNot Nothing Then
' Perform search/replace on the text document directly
' Convert tabs to spaces
Dim convertedTabs = textDocument.ReplacePattern("\t", " ", _
vsFindOptions.vsFindOptionsRegularExpression)
' Remove trailing whitespace from each line
Dim removedTrailingWS = textDocument.ReplacePattern(":Zs+$", "", _
vsFindOptions.vsFindOptionsRegularExpression)
' Re-save the document if either replace was successful
' (NOTE: Should recurse only once; the searches will fail next time)
If convertedTabs Or removedTrailingWS Then
document.Save()
End If
End If
End Sub