Xcode 中注释块 /* ... */ 的键盘快捷键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14679558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keyboard shortcut for comment blocks /* ... */ in Xcode
提问by Michael Campsall
I am looking for a keyboard shortcut to comment out a selection of code. I already know about Command+ /to add //
in front of selected lines of code, but I want to be able to create /* ... */
comment blocks this way.
我正在寻找一个键盘快捷键来注释掉一段代码。我已经知道在选定的代码行前面添加Command+ ,但我希望能够以这种方式创建注释块。///
/* ... */
The reason I want a shortcut for /* ... */
is to be able to fold the comment blocks, which as far as I know you cannot do with the //
comments.
我想要一个快捷方式的原因/* ... */
是能够折叠评论块,据我所知,您不能对评论进行折叠//
。
So either I need a way to fold //
type comments or a keyboard shortcut to create /* ... */
comments
所以要么我需要一种折叠//
类型注释的方法,要么需要一个键盘快捷键来创建/* ... */
注释
any ideas?
有任何想法吗?
采纳答案by mbuc91
It isn't as good as I'd like it to be, but this is the best option I've come up with so far. I created a few custom code snippets in Xcode, each of which replaces a shortcut with an appropriately sized comment block.
它没有我希望的那么好,但这是迄今为止我想出的最佳选择。我在 Xcode 中创建了一些自定义代码片段,每个片段都用适当大小的注释块替换了一个快捷方式。
For example, I have it replace the shortcut "com1" with an 80-character-wide comment block (for non-indented lines). That's the following code in the Snippet Library in Xcode:
例如,我将快捷方式“com1”替换为 80 个字符宽的注释块(对于非缩进的行)。这是 Xcode 中的 Snippet Library 中的以下代码:
/*******************************************************************************
<#comment#>
******************************************************************************/
For indented lines, I have shortcuts "com2", "com3", and "com4", which are 76, 72, and 68 characters wide (respectively).
对于缩进的行,我有快捷方式“com2”、“com3”和“com4”,它们的宽度分别为 76、72 和 68 个字符。
======
======
Also, Xcode's documentation isn't that helpful when it comes to code snippets. To create a new code snippet, show the Utilities view (the pane on the right) and select the {}
icon towards the bottom to show the Code Snippet Library. Select some code to add, and drag it over to the Code Snippet Library (by left-clicking and holding for a secondand then dragging - as a Windows user it took me far too long to figure this out). This will create a new entry titled My Code Snippet
at the very bottom of the Code Snippet Library, which you can mouse over and Edit
to your liking.
此外,Xcode 的文档在代码片段方面也没有那么有用。要创建新的代码片段,请显示 Utilities 视图(右侧的窗格)并选择{}
底部的图标以显示代码片段库。选择一些要添加的代码,然后将其拖到代码片段库中(通过左键单击并按住一秒钟然后拖动——作为 Windows 用户,我花了很长时间才弄明白这一点)。这将My Code Snippet
在代码片段库的最底部创建一个新条目,您可以Edit
根据自己的喜好将鼠标悬停在该条目上。
======
======
EDIT: HereI have uploaded a zip file containing five code snippets. Levels 1 through 4 (com1, com2, com3, com4) are 80, 76, 72, and 68 characters wide, respectively. Level 0 is a single line comment block that does not automatically format width (I use it for single comments to maintain formatting).
编辑:在这里我上传了一个包含五个代码片段的 zip 文件。级别 1 到 4(com1、com2、com3、com4)分别为 80、76、72 和 68 个字符宽。级别 0 是单行注释块,不会自动格式化宽度(我将它用于单条注释以保持格式)。
EDIT 2: Also, user code snippets are stored in ~/Library/Developer/Xcode/UserData/CodeSnippets/
.
编辑 2:此外,用户代码片段存储在~/Library/Developer/Xcode/UserData/CodeSnippets/
.
回答by lozflan
i reposted your question on apple dev forums .... i am also looking for the answer. here is a response i got ..... havent tried to do it yet but will soon.
我在苹果开发论坛上转发了你的问题......我也在寻找答案。这是我得到的回应..... 还没有尝试过,但很快就会。
in the WWDC 2012 video Session 402 - Working Efficiently with Xcode ( from around 6 minutes in) there's a description of how to use the Mac OSX Automator to add a service to manipulate selected text. The example shown in the video is to remove duplicates in a selection of text using the shell commands sort and uniq. Using this approach you could shell out to an awk
在 WWDC 2012 视频 Session 402 - Working Efficiently with Xcode(从大约 6 分钟开始)中,描述了如何使用 Mac OSX Automator 添加服务来操作所选文本。视频中显示的示例是使用 shell 命令 sort 和 uniq 删除选定文本中的重复项。使用这种方法,您可以使用 awk
awk 'BEGIN{print "/"}{print $0}END{print "/"}'
awk 'BEGIN{print "/ "}{print $0}END{print "/"}'
which will, for any given selected text, put the comment delimiters before and after. Then add a shortcut to invoke this service.
对于任何给定的选定文本,它将在前后放置注释分隔符。然后添加一个快捷方式来调用这个服务。