Is there a shortcut to make a block comment in Xcode?

前端 未结 16 1174
后悔当初
后悔当初 2020-11-30 18:57

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, t

相关标签:
16条回答
  • 2020-11-30 19:26

    You can assign this yourself very easily, here goes a step by step explaination.

    1.) In you xCode .m file type the following, it does not matter where you type as long as it's an empty area.

    /*
    */
    

    2.)Highlight that two lines of code then drag and drop onto 'code snippet library panel' area (it's at the bottom part of Utilities panel). A light blue plus sign will show up if you do it right.

    enter image description here

    3.) After you let go of your mouse button, a new window will pop up and will ask you to add name, short cut etc; as shown. As you can see I added my shortcut to //. So every time I want a block comment I will type //. Hope this helps

    enter image description here

    0 讨论(0)
  • 2020-11-30 19:26

    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:

    0 讨论(0)
  • 2020-11-30 19:28

    In xcode 11.1 swift 5.0

    select the code you would like to add block comment then press + + /

    0 讨论(0)
  • 2020-11-30 19:32

    There is a symbol before help menu on xcode which has Edit user script. On Un/Comment Selection under comments section change my $cCmt = "//"; to my $cCmt = "#"; or whatever your IDE works with. Then by selecting lines and command + / (It's my xcode default) you can comment and uncomment the selected lines.

    0 讨论(0)
  • 2020-11-30 19:33

    @Nikola Milicevic

    Here is the screenshot of the indentation issue. This is very minor, but it is strange that it seems to work so well, in your example visual.

    I am also adding a screenshot of my Automator set-up...

    Thanks

    Update:

    If I change the script slightly to:

    And then select full lines in XCode, I get the desired outcome:

    0 讨论(0)
  • 2020-11-30 19:36

    UPDATE:

    Since I was lazy, and didn't fully implement my solution, I searched around and found BlockComment for Xcode, a recently released plugin (June 2017). Don't bother with my solution, this plugin works beautifully, and I highly recommend it.

    ORIGINAL ANSWER:

    None of the above worked for me on Xcode 7 and 8, so I:

    1. Created Automator service using AppleScript
    2. Make sure "Output replaces selected text" is checked
    3. Enter the following code:

      on run {input, parameters}
      return "/*\n" & (input as string) & "*/"
      end run
      

    Now you can access that service through Xcode - Services menu, or by right clicking on the selected block of code you wish to comment, or giving it a shortcut under System Preferences.

    0 讨论(0)
提交回复
热议问题