Objective-C代码格式化

7

我在Safari中复制Objective-C示例并粘贴到Xcode中时,格式会出现问题。 我知道我可以使用多个查找和替换来处理{和\r,但是否有一个按钮解决方案? Xcode选项/插件,TextEdit插件等等?

回答:我在TextWrangler中创建了一个AppleScript。

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个回答

4

Control + I 组合键可以对所选文本进行一定程度的格式化。


Control + I 或者编辑器 > 结构 > 重新缩进只会重新缩进你的选择。而且即使如此,它的性能也很一般。 - parvus

3
重新缩进Xcode 4应该可以解决大多数格式问题。但是,如果您仍然对结果不满意,您需要使用第三方工具。我曾经经常更改我的代码格式。我会去更新一些旧代码,结果发现它们是使用已弃用、现在不熟悉的格式编写的。我一直在使用Uncrustify将旧文件适应新格式。它有很多选项,但还是有所欠缺。但是,它应该足够满足大多数情况。

1
我使用了TextWrangler,它有记录AppleScript的选项,然后进行查找/替换以在};和-后添加\r。 - brian.clear

0

配置Xcode的首选项,使其始终缩进制表符。然后使用选择所有然后制表符所给出的格式进行操作。它足够好,而且进行任何需要手动格式化的操作都是完全浪费时间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接