2011-08-10 146 views
7

我將Safari中的Objective-C示例剪切並粘貼到Xcode中,但格式會變得混亂。我知道我可以爲{和\ r進行多次查找和替換,但是在某處是否有一個按鈕修復? Xcode選項/插件。 TextEdit插件等?格式化Objective-C代碼

答:我創建了一個AppleScript中的TextWrangler

tell application "TextWrangler" 
activate 
replace "- (" using "\\r- (" searching in text 1 of text document "untitled text" options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false} 
select find window 
select text 1 of find window 
replace ";" using ";\\r" searching in text 1 of text document "untitled text" options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false} 
select find window 
replace "{" using "{\\r" searching in text 1 of text document "untitled text" options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false} 
select find window 
replace "//" using "\\r//" searching in text 1 of text document "untitled text" options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false} 
end tell 

回答

3

Xcode 4中的重新縮進應該解決您可能遇到的大多數格式問題。但是,如果您對結果仍然不滿意,則必須去第三方。

我曾經經常更改我的代碼格式。我會去更新一些舊的代碼,只是發現它是使用不贊成的,現在不熟悉的格式編寫的。

我一直在使用Uncrustify來使舊文件適應新的格式。它有很多選擇,但仍然缺乏。但是,大多數情況下它應該已經足夠了。

+1

我用的TextWrangler它有選項來記錄一個蘋果,然後找到/替換後添加\ r};和 - ( –

4

Control + I會做一定程度你選擇的任何文本格式上的。

+0

不適用於非常糟糕的代碼,只是試過 –

+0

Control + I或Editor> Structure>重新縮進只會做到這一點:它會重新縮進您的選擇。即使這樣,它的表現也很平庸。 – parvus

0

配置xcode的首選項,使得tab總是縮進。然後選擇任何選擇 - 所有 - 然後選項卡爲您提供格式。這足夠好,做任何涉及非自動格式化的事情都是完全浪費時間。