在 VC6 IDE 中一鍵格式化目前開啟的 C/C++ 原始檔,依環境設定(Tab/空白)自動縮排、大括號對齊。
CodeFormat.dsmFormatByCodeSub FormatByCode()
Dim doc
For Each doc In Application.Documents
doc.Activate
doc.Selection.SelectAll
Dim endLine, curLine
endLine = doc.Selection.BottomLine
doc.Selection.StartOfLine
curLine = doc.Selection.CurrentLine
' 移除每行前導空白
While curLine <= endLine
doc.Selection.SelectLine
doc.Selection = LTrim(doc.Selection)
curLine = doc.Selection.CurrentLine
Wend
' 重置縮排並套用 Smart Format
doc.Selection.SelectAll
For i = 1 To 6: doc.Selection.Unindent: Next
doc.Selection.SmartFormat
doc.Save
doc.Close dsSaveChangesPrompt
Next
End Sub
if (x) { y(); }),內部縮排可能遺失純 IDE 內建功能組合,零外部依賴,絕不破壞編譯通過性。歡迎高手改進(支援目錄遞迴、修正大括號同行問題)。