Is there a shortcut to make a block comment in Xco

2019-01-10 04:32发布

I'm writing ANSI-compatible C code, and hence I can't use the line (//) comment. I'm using Xcode. In Sublime Text and Eclipse, and I think most other IDEs, there are separate keyboard shortcuts for line comments and block comments (/**/). However, I don't see that in Xcode - in fact, I don't even see a menu option to add a block comment. Is it simply not supported in Xcode? That would certainly seem to be a lame decision if so.

14条回答
时光不老,我们不散
2楼-- · 2019-01-10 04:38

UPDATE: Xcode 8 Update

Now with xcode 8 you can do:

+ + /

Note: Below method will not work in xcode version => 8

Very simple steps to add Block Comment functionality to any editor of mac OS X

  1. Open Automator
  2. Choose Services
  3. Search Run Shell Script and double click it

Add the below applescript in textarea

awk 'BEGIN{print "/*"}{print $0}END{print "*/"}'

apple script for block comment

  1. Save script as Block Comment

Add a keyboard shortcut

Open System Preference > Keyboard > Shortcuts, add new shortcut by clicking + and right the same name i.e. Block Comment as you given to applescript in the 4th step. Add your Keyboard Shortcut and click Add button.

New keyboard shortcut

Now you should be able to use block comment in Xcode or any other editor, select some text, use your shortcut key to block comment any line of code or right click, the context menu, and the name you gave to this script should show near the bottom.

查看更多
欢心
3楼-- · 2019-01-10 04:38

If you're looking a way to convert autogenerated comment from Add Documentation action (available under cmd-shift-/) you might find it useful too:

function run(input, parameters) {
  var lines = input[0].split('\n');
  var line1 = lines[0];
  var prefixRe = /^( *)\/\/\/?(.*)/gm;
  var prefix = prefixRe.test(line1) ? line1.replace(prefixRe, "$1") : ""

  var result = prefix + "/*\n";  
  lines.forEach(function(line) {
    result += prefix + line.replace(prefixRe, "$2") + '\n';
  });
  result += '\n' + prefix + ' */';
  return result;
}

Rest the same as in @Charles Robertson answer:

Automator

Services

查看更多
混吃等死
4楼-- · 2019-01-10 04:40

I modified the code of Nikola Milicevic a little bit so it also remove comment block if code is already commented:

on run {input, parameters}
    repeat with anInput in input
        if "/*" is in anInput then
            set input to replaceText("/*", "", input as string)
            set input to replaceText("*/", "", input as string)

            return input
            exit repeat
        end if
    end repeat
    return "/*" & (input as string) & "*/"
end run

on replaceText(find, replace, textString)
    set prevTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to find
    set textString to text items of textString
    set AppleScript's text item delimiters to replace
    set textString to "" & textString
    set AppleScript's text item delimiters to prevTIDs
    return textString
end replaceText

Hope this will help someone.enter image description here

查看更多
再贱就再见
5楼-- · 2019-01-10 04:42

Now with xCode 8 you can do:

+ + /

to auto-generate a doc comment.

Source: https://twitter.com/felix_schwarz/status/774166330161233920

查看更多
甜甜的少女心
6楼-- · 2019-01-10 04:45

Try command + /. It works for me.

So, you just highlight the block of code you want to comment out and press those two keys.

查看更多
我想做一个坏孩纸
7楼-- · 2019-01-10 04:48

Cmd + Shift + 7 will comment the selected lines.

查看更多
登录 后发表回答