Sublime Forum

[Explained] Toggle Comment on a folded code block only comments displayed lines

#1

Using ST3 (3103), and not sure if this is in ST itself or the Scala package.

I was debugging a test suite and wanted to comment other tests in the suite to make sure they didn’t interfere while using shared mutable state (DB). I had those tests folded in the editor, selected the start and end of a test block, pressed Cmd+/, and it appeared to work correctly.

On unfolding the block, I could see that only the starting and ending lines of the block (those that were displayed while it was folded) were commented. The rest of the block was still active.

I would’ve expected the whole block to be commented over a folded region using the Toggle Comment command.

0 Likes

#2

Can you provide more exact reproduction steps? I just tested this in a Python file (which has no block comments) and it worked fine.

0 Likes

#3

I believe this is in the Scala support package. Toggle Comment uses line comments.

Any way, the file I was working in uses ScalaTest, so the code looks similar to

1. "My suite" - {
2.   "should do something" in {
3.     val x = 1 + 1
4.     val y = x * 3
5.   }
6. }

Folding 2-5, selecting that folded region and Cmd+/ will comment only lines 2 and 5. Lines 3 and 4 will be uncommented.

With a fresh brain I just checked, and it seems to work fine in a normal Scala source file.

0 Likes

#4

I can see that there are cases where it can be a bit confusing as to what might get commented while code is folded. The following is default ST3 behavior, no packages etc. This applies both to line comments and block comments.

Take the following example Python file:

def test():
    """test function"""
    if True:
        pass

do the following:

  1. fold the function
  2. click to the left of the fold arrow to select the line - notice the selection includes the folded ...
  3. press ctrl+/
  4. unfold the function
  5. see that only the function definition line got commented

revert the comment and do the following:

  1. fold the function
  2. press ctrl+a to select the entire file
  3. press ctrl+/
  4. unfold the function
  5. see that the entire file got commented

Conclusion

to comment the entire function, you have to select not only the line that is folded, but a tiny bit more - select the line, and then press shift+->. It doesn’t look like the selection moved at all, but check the status bar. It goes from:

2 lines, 12 characters selected

to

4 lines, 61 characters selected

(pressing shift+-> again at this point will select the new line after the folded region, and you will get 5 lines, 62 characters selected in the status bar)

hope that helps someone :slightly_smiling:

1 Like