hide/unhide all comments in xcode - xcode

Is there a way to hide/unhide all comments in xcode source files?
This would be very helpful when working with code that has way too many comments added.

You can use fold/unfold to hide/show comments using ⌃⇧⌘↑ and ⌃⇧⌘↓. This is also accessible through View --> Code Folding --> Fold/Unfold Comment Blocks.

Try: Ctrl + Command + Shift + Left
Or Ctrl + Command + Left

Surrounding any code with
/*
....code here....
*/
makes that comment into a section that you can fold/unfold.
This works even for blocks of code already commented out with // in long blocks.
Hope this helps someone even though this question is 4 years old!

Expanding a bit on Mish's answer because XCode is very subtle with its UI
As #Mish pointed out, surrounding any code with /* ... */ makes that code fold/unfold-able just like your { ... } sections.
I haven't had any luck with any of the keyboard shortcuts listed on this answer page yet, but if you hover over the left hand side of the /*...*/ section you'll see the UI change in response to that like so:
that when clicked will collapse the comments. I for one intend on trying to do all comments with the /.../ format from now on.
PS: Note that /*...*/ comments can be inline with code on either side and still be collapsable.
EDIT: I'm finding that collapsing comments inside of some {...} brackets is not working because XCode is collapsing the entire {} section instead...

A very easy and practical way to hide all // comments is to change the comments' color to black (or other background editor color you are using, normally dark color) in Xcode preferences.
When you want to see them again, just change color to primitive one, such as green

I'm made a summary , it works for me. It's late but a good flow helps writing good code. for swift 3-4 only
In case where you want to write pseudo code first before actual code, you can write everything into a comment block
/*
first, use http request
in the callback, do
a.
b.
c.
*/
then because you might re-write but the logics are mostly the same, so you might not want to remove the comments, that's fine, but make sure they don't block your view, so you define a short-cut that conveniently fold/unfold all the comment block into one line. (That's the best you can do.); Then when you fold them, it looks like this: /*...
to Define shortcut, go to Xcode => Preference => Key Bindings => fold comment block
In my case I use alt⇧ctrl{ , alt⇧ctrl}
then define it anyway you want, but be careful of conflicts.
It really pays to have pseudo code comments folded

Select the code and comments IN BETWEEN the /* and */ .
Do NOT select the /* and */ . then select Code Folding from Editor menu. It works for me. Swift 5, Xcode 12.4

Related

Can I place something like a comment or annotation next to collapsed code in Visual Studio?

If you collapse a block of commented-out code in Visual Studio, it's pretty normal. You get the little box containing [ // ... ] to indicate that there is a collapsed comment, and you can mouse over it to see a little pop-up of what's inside. Instead of having to mouse-over each time, is there a way to just add a single-line comment above or below the [ // ... ] block without Visual Studio trying to include it in the collapsed section?
For example, when trying to do the following:
//this is a brief description of the collapsed code below
[ // ... ]
Visual Studio then forces the upper comment into the collapsed section below, rendering it useless unless you mouse-over it. So that saves some time potentially, but doesn't eliminate the need to mouse-over; sometimes, it's just easier to read one line to remember what something does, and all the better if you don't have to jump through an extra hoop to read that one line.
You could just do something like this by skipping a line:
//this is a brief description of the collapsed code below
[ // ... ]
And it works the way that I'd want it to -- without collapsing everything together -- but that extra line space in between just looks weird, and having to do that potentially multiple times in a program just makes the whole thing longer. Any advice?
If you want a comment to serve as reminder or summary text for another block of collapsed comment code, say in the event that you want to hold onto some code before determining whether or not to edit it or delete it, it can be done in one of at least two ways in Visual Studio. First, the obvious difference between comment types:
There is a difference in the way that the Visual Studio IDE handles the two comment types. Single-line comments may stand alone, or could be grouped together with other single-line comments to form a collapsible block, but the block cannot be separated into groups of single-line comments without skipping lines in-between. Multi-line comments can always be collapsed, but their indicators /* */ are paired, similar to how the { } symbols are paired in code.
That said, comment types can be combined in at least the following ways to form reminders/summaries.
First:
which collapses like so:
and second:
which collapses like so:
Note again that multi-line comments can always be collapsed, so it might be easy in longer bits of code to lose track of line locations if they are collapsed accidentally.

Is there a quick way to fold and unfold comments in xcode 7?

Is there a quick way to fold and unfold a block of comments in xcode 7?
While I'm trying out new code I like to keep different versions of a "solution" until I decide which one is my favorite and delete the rejected material.
The ability to Fold & Unfold code blocks is set to OFF within Xcode 7.
Steps to change this:
Select Xcode (top left)
Choose Preferences
Navigate to Text Editing (center)
Tick the option Code folding ribbon
Once you've done this, you'll be able to collapse the comments, along with blocks of code.
I hope this helps!
.
Added Picture Reference
Code-folding block comments does not work in Xcode 7 - 7.2.1 in Swift files.
You can, however, hack it by using an empty closure to surround the comments. This will also work with multiple line comments too.
_ = { /* Comment
myCommented.code
*/ }
Definitely a hack, but could save you lots of scrolling around.

Xcode comments autocompletion (appledoc style)

I find documentating my interfaces particularly painful because I need to type everything myself. So I figured that I must not be the only one like this and started to look for a way to reduce my pain but didn't find much.
My question is, is there a shortcut or a way to have Xcode autocomplete my comments? For example, I'd very much like if I type /** + [tab] that it autocompletes to:
/**
* [cursor here]
*
* (maybe some pre-populate a list of arguments for the method below)
*/
- (void)test:(NSString *)testString another:(NSString *)another;
Thanks!
Use Xcode's snippets feature. You can create a snippet with whatever text you want, and add items in the text to be filled in. To create a snippet, just select some text in an Xcode editor and drag it into the snippet library. You can set the completion shortcut, so something like m-comment could be your standard method comment:
Typing the first bit of m-comment then displays the entire shortcut (and any other completions), and accepting the completion adds a comment with the parameterized parts waiting to be filled in. As usual, you can tab from one to the next:
You indicate that a given part of the text is a field to be filled in by enclosing it in <# and #>, like: <#method name#>.
Of course, one of the strengths of Objective-C is that the method name generally tells you what the parameters are. Repeating that in a comment seems like unnecessary extra work. Xcode 3 had better support for scripts included scripts that would automatically generate HeaderDoc comments with parameters. They weren't widely used, though, probably because those kinds of comments weren't really useful.

Big comments with Sublime Text 2

I would like to make comments like this in Sublime Text 2:
/********************
* This is a comment *
********************/
Is there an easy way to make those automatically?
Also, where can I find good documentation about such stuff. I love Sublime, but I feel it's poorly documented!
You could create a snippet for doing this.
Go to Tools -> New Snippet and a new file is opened. Paste this in it:
<snippet>
<content>
<![CDATA[
/********************
* $0 *
********************/
]]>
</content>
<tabTrigger>bigcom</tabTrigger>
</snippet>
Save this in your Packages\User-Folder (which should be set automatically when saving).
Now you can just type bigcom (as defined in the <tabTrigger> - element) and hit tab. The comment will appear and the cursor is set at the position, where $0 is set in the snippet.
Additionaly, you could add a scope - element inside the <snippet>-block, so this snippet will only work in a specific syntax scope, for example:
<scope>source.python</scope>
Unfurtonately, I don't know how you could add the *-character on both sides of the line you are writing in automatically, when you jump into a new line, so I don't know if this fits your needs. You would have to add those manually. Still I hope this helps in some way.
Edit:
Found something for this in another question on stackoverflow. Have a look at this answer. When doing this, at least the * character on the beginning of the new line is added. I'll have a look if I can get it to add the character at the end of the line too.
When it comes to the documentation, I agree, there's not really a lot out there. Of course there is the official documentation: Sublime Doc and of course the forum: Sublime Forum (which is a good resource to some point, not like the poorly filled Doc). On the other hand I always recommend reading the post on net.tutsplus, which is a nice starting point.
I pretty much stumbled over the most interesting parts that come with the standard installation while browsing trough the Global Settings and Key Bindings-Files, which you can open over the Preferences - Menu
Warning, self plug.
The DocBlockr plugin can automatically 'decorate' a comment for you. Right now it only works on inline comments, but it gets the job done. The shortcut key is Ctrl+Enter
// foo bar baz
Becomes
/////////////////
// foo bar baz //
/////////////////
And it works on consecutive comments too:
// foo
// bar baz quux
Becomes
//////////////////
// foo //
// bar baz quux //
//////////////////
You could also try using the DocBlockr plugin
Use this handy sublime plugin https://packagecontrol.io/packages/Comment-Snippets

Can I add my own code snippets to xcode 4?

It'd be really handy to have collection KVC accessor snippets in xcode, as they're a pain to do by hand. Has anyone who's been using 4 for a while worked out how to add new ones?
(...we're allowed to talk about it now, right?)
Yes, it's quite easy - you just highlight text you want to use as a snippet and drag it into the snippets area (dragging selected text can be difficult, I find it works better if you drag from the left edge of the editor). It brings up a dialog box asking you to name it.
Also, you can have custom parameters in snippets - if you put the text <#paramName#> anywhere in the code you are dragging, when you use the snippet it will come up as a replaceable parameter that you can tab between just like in the official Snippets.
Also make sure to set a completion prefix, which makes it shorter to activate the snippet as you are typing. The nice thing about using Snippets over other solutions like global system text expanders, is that snippets can also be limited to being valid in a certain area of code - so for example a snippet that filled out a string formatting line can be marked as valid only within a block, not outside a method. That way the completion prefix only hits in areas where it's valid to use.
You can edit a snippet by clicking once on it, a box will come up with the snippet text and some other snippet settings you can edit.
A tip to help you drag the code every time, select text then click + hold (do not move mouse) until mouse pointer changes from "I" to pointer. You'll then be able to drag the selected text to the Code Snippet Library.
Drag the highlighted code to the Snippet library. (It can be stubborn and not want to drag. Holding the mouse button down for a moment before dragging seems to help.)
You can use Snippets to manage code snippets in Xcode. Check out this demo http://www.youtube.com/watch?v=il4kE4diy0k
Simply select the code, the press ALT key and drag it into the code snippets library and rename it there, Make sure to press the ALT key, other than this you won'n be able to drag it.

Resources