Home > Enterprise >  How to comment all marked variable length chunks in VSCode?
How to comment all marked variable length chunks in VSCode?

Time:11-05

In VSCode 1.60.2, I'm working on a distributed system and using logs to help with debugging. When debugging, multiple processes each create their own log. I have nicely formatted outputs scattered through my code, designated as follows:

// non-debugging code
foo();
for (...)
    write(...);

////// DEBUGGING CHUNK
write(...);
for (...) write(...);
write(...);
////// END DEBUGGING CHUNK

// non-debugging code
bar();
for (...)
    if (...)
        write(...);

////// DEBUGGING CHUNK
...
////// END DEBUGGING CHUNK

// non-debugging code
baz();

The processes interact, and execution speed affects some of their outputs. As such, there are certain cases when I want to minimize overheard by running the code without the writes. The above pseudocode may then look like:

// non-debugging code
foo();
for (...)
    write(...);

// ////// DEBUGGING CHUNK
// write(...);
// for (...) write(...);
// write(...);
// ////// END DEBUGGING CHUNK

// non-debugging code
bar();
for (...)
    if (...)
        write(...);

// ////// DEBUGGING CHUNK
// ...
// ////// END DEBUGGING CHUNK

// non-debugging code
baz();

I've been manually going through my files to comment/uncomment chunks one at a time (highlight chunk, Ctrl /), but this takes a while. Since my chunks have a standard format (start and end format; not necessarily size), I hope there may be a way to comment and uncomment all of them at once.

In an attempt to find an easy solution, I've tried looking through extensions, but turned up mostly color highlighting and comment template insertion.

I've also considered using the built-in replace feature, but am not sure how this would help. I see two issues: (1) some statements inside and outside the debugging chunks match, so can't do a simple replacement, and (2) I need to be able to undo spacing changes that could come from regex matching replacements (only the "what to replace" field takes regex; doesn't seem like I can replace with a matched group).

CodePudding user response:

Assuming // is a line comment, you can do this with a couple of extensions. You will need a macro extension like toggling comments on blocks


There may be improvements to the regex. And perhaps in the near future the need for the Find and Transform extension will be unnecessary as there is merged work on including some of its functionality into vscode.

CodePudding user response:

With the extensions Select By and multi-command you can make the following multiCommand in your settings.json

  • go to top of the file
  • select the first occurrence of ////// DEBUGGING CHUNK
  • select all other occurrences
  • mark start of the selections
  • move cursors to end of matching ////// END DEBUGGING CHUNK
  • create selections from marked positions to current positions
  • toggle line comments
  • remove multi cursors (go to top)
    {
      "command": "multiCommand.toggleDebugComment",
      "sequence": [
        "cursorTop",
        { "command": "selectby.regex", "args": { "forward": "(?=(////// DEBUGGING CHUNK))", "forwardInclude": false, "forwardNext": "{{1}}", "forwardNextInclude": true } },
        "editor.action.selectHighlights",
        "selectby.mark",
        { "command": "moveby.regex", "args": { "regex": "////// END DEBUGGING CHUNK", "properties": ["next", "end"] } },
        "selectby.mark",
        "editor.action.commentLine",
        "cursorTop"
      ]
    }
  • Related