Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting Objective-C code

I cut and paste Objective-C examples off Safari into Xcode but the formatting gets messed up. I know I can do multiple find and replace for { and \r, but is there a one button fix somewhere? Xcode option / plugin. TextEdit plugin etc?

Answer: I created an AppleScript in 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
like image 310
brian.clear Avatar asked Aug 10 '11 13:08

brian.clear


2 Answers

Control + I will do some degree of formatting on whatever text you have selected.

like image 164
Bryan Irace Avatar answered Oct 25 '22 18:10

Bryan Irace


Re-indenting in Xcode 4 should solve most formatting issues you might have. However, if you're still not happy with the result, you'll have to go third-party.

I used to change my code formatting quite regularly. I'd go to update some old code, only to find that it was written using deprecated, now unfamiliar, formats.

I've been using Uncrustify to adapt old files to new formats. It's got quite a few options, but is still lacking. However, it should suffice for most cases.

like image 21
Ryan Mason-Davies Avatar answered Oct 25 '22 17:10

Ryan Mason-Davies