Mike on the MATLAB Desktop

October 4th, 2010

R2010b Comment Wrapping Improvements

In R2010b we made a series of improvements to the “comment wrapping” feature in the MATLAB Editor. In this two-part series, we’ll take a detailed dive into those improvements.

In this first part, we’ll introduce comment wrapping and talk about how it’s now easier to use. In the second part, we’ll cover more advanced maneuvers for the serious comment writer.

Wait, what’s comment wrapping?
In short, comment wrapping is a feature of the MATLAB Editor (accessible via the “Text” > “Wrap Comments” menu item) that allows you to go from ragged, messy comments:

comment-wrapping-before

to neatly formatted ones:

comment wrapping after

The feature also includes automatic comment wrapping, which occurs as you type beyond the end of a line. In R2010b, we’ve made this feature easier and more powerful.

No selection required

Previous to R2010b, if you wanted to reformat a section of comments as you edited, you first had to select the region that needed reformatting and then press Ctrl-J (or Shift-Fan-W on the Mac).

comment-wrapping-selection.png

In R2010b, you can still do it this way. But much more conveniently, you can skip making a selection and just let the Editor infer the current section of comments for you. For example, suppose your caret were at the end of line 5 below:

comment-wrapping-noselection.png

Now you can simply press Ctrl-J to reformat this section of comments, and your caret remains at the same location in the text that it had before. Your caret could in fact be anywhere on lines one through five. This allows you to tidy up what you’ve written without taking you out of the flow of typing.

Note that the Editor wraps only the current “section” of comments. So what defines a section? Hopefully, exactly what you’d think: a series of contiguous, identically-indented comment lines. So a non-comment line, a blank line, or a comment indented differently each represents the end of a section. This allows independent “paragraphs” of comments to remain intact and prevents them from being strung together, even if you do a “Select All” > “Wrap Comments” to reformat your whole file.

No more broken strings

One frustrating behavior of comment wrapping prior to R2010b was that if your comments contained text that shouldn’t be broken, such as a URL in publishing hyperlink markup, comment wrapping would go ahead and break it:

comment-wrapping-brokenstring.png

This was true with automatic comment wrapping as well – if you were typing a long string and reached the right-hand margin, it would break. That’s no good for reading and was particularly pernicious for publishing:

comment-wrapping-published-brokenstring.png

In R2010b, comment wrapping abides by this simple rule: if there’s no whitespace, don’t break! This means that URLs, or any long strings, are preserved:

comment-wrapping-published.png

Wrapping up (groan)
That’s all for now. In the next part, we’ll discuss a few more enhancements to this feature. In the meantime, try it out and let me know what you think!

8 Responses to “R2010b Comment Wrapping Improvements”

  1. jiro replied on :

    I really like this enhancement! I put a lot of comments in my code, including hyperlinks, and having the tool intelligently detect URLs and prevent them from breaking really helps. I didn’t know about the new “justification without selection” feature. I’ll definitely be using that technique more often. Thanks!

  2. Noon Silk replied on :

    I do like the tabbing strategy that the MATLAB editor adopts, but I do think that multi-line comments would be brilliant.

    I don’t have specific thoughts on a syntax for this, but potentially following the footsteps of other languages with a

    /%

    %/

    Approach would be appropriate. I don’t think an implementation like that would break any existing code (as the first statement is invalid, and the second would only act if the first state was reached).

  3. Jared replied on :

    @Noon, do block comments do what you’re looking for?

    %{
    this is a block of
    comments until the
    closing marker
    %}

    Jared

  4. Noon Silk replied on :

    Jared, you are quite correct, that does indeed do what I was after and I feel like a fool for not knowing about it :)

  5. Henry replied on :

    Hey Jared, is there a shortcut for making the block comments? …one that is similar to line comments (ctrl-r and ctrl-t)?

  6. Jared replied on :

    Hi Henry,

    Though we do have customizable keyboard shortcuts, we don’t have an action for wrapping a block of text with block-style comments. I’ll make a note of your request, though.

    Thanks,
    Jared

  7. Brooke replied on :

    Hello!

    Is there a way to turn the comment-wrap off?

    Thanks,
    Brooke

  8. Jared replied on :

    Hi Brook,

    Yes – you can go to File > Preferences > Editor/Debugger > Language, and deselect “Wrap comments automatically while typing.”

    Jared

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks
Mike works on the MATLAB Desktop team.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.