rioj7 / remove-comments

Visual Studio Code extension to remove comments from files.
11 stars 4 forks source link

Remove Comments will remove all comments from the current selection(s) or the whole document.

This is a rewrite of the extension Remove Comments by plibither8.

It uses the same command description.
Be sure to uninstall the extension: plibither8.remove-comments

It fixes a number of issues:

If the comment starts and ends on the same line it is a single line comment. If the language does not have an open and matching close comment delimeters you only have single line comments. An example of open and matching close comment delimeters are /* and */.

If any of the supported languages contains a mistake or if you use a language not yet supported please create an issue at the repository.

Features

Note: This extension does not 'uncomment' the comments present in the code, but removes them completely.

For HTML: The <style> (CSS) and <script> (Javascript) sections are searched and the correct comment and string settings are used.

For PHP: You don't have to create selections for the individual <?php ?> blocks, these blocks are searched for in the selections, no HTML will be modified.

For Vue Single Files: The language used in a section (tag delimeted) is determined by the lang attribute in the start tag.

Usage

Javascript has comments that are treated as JSDOC strings: /** ..... */
They are handled by the removal commands as strings. If you also want to remove these you can mark them as comments with the command:
Comments: Mark JSDOC String as comment. For next removal only..
In only the next call of the Remove commands these will also be removed for JavaScript files.

If you have defined the setting remove-comments.keep you can ignore this setting with the command
Comments: Ignore any "remove-comments.keep" setting. For next removal only.
In only the next call of the Remove commands the remove-comments.keep setting is not used.

Comments with a prefix

If you choose to remove comments with a prefix you have to enter the prefix exact, this is inclusive the whitespace.

The command remove-comments.removeAllCommentsWithPrefix can be used in a keybinding. You can pass the prefix in the argument:

{
  "key": "ctrl+i p",
  "command": "remove-comments.removeAllCommentsWithPrefix",
  "args": { "prefix": " DELETE" }
}

Extract Strings to a file

The command Comments: Extract Strings to a file scans the current editor for strings and stores them in a file. The strings are appended to the file and the stored strings also have the delimiters.

Each string is saved on 1 line in the format:

fileBasename::line:character string

::line:character is the position of the string start.

It uses the settings:

If you want to extract the strings for a large collection of files you can use the extension: Command on All Files

Settings

remove-comments.keep

With the setting remove-comments.keep you are able to define which comments to keep by testing the comment text with a Regular Expression. The comment text is all the text of the comment without the comment delimiters. For a block comment (like /* ... */) the lines are separated with a Tab character. The Indent Comment Continuation lines are not part of the comment text, but have the same action (keep/remove) as the first line.

The setting remove-comments.keep is an object with the keys a languageId. You can use any known VSC languageId and the special languageId all. The all key is used for any file.

The key can also be a list of languageId's separated by ',' (comma). This way you can use the same rules for multiple languageId's or remove specific rules in the Workspace/Folder settings if you add an illegal languageId to name the group of rules for the languageId's:

"remove-comments.keep": {
  "#grp1,all": {
    "region": {
      "regex": "^\\s*#(end)?region"
    }
  },
  "#grp2,all": {
    "todo": {
      "regex": "^\\s*TODO",
      "flags": "i"
    }
  }
}

The value for each languageId is an object with the keys a name for the named regular expression to use. In VSC the setting objects are merged. By using a name you can override/remove a particular named regular expression defined at the User or Workspace level.

Each named regular expression is an object with the following properties:

Example

If you use the #region/#endregion feature of VSC to create custom folds they are stored in the file using comments like
// #region name
// #endregion
To keep these lines in any file you can define the setting:

"remove-comments.keep": {
  "all": {
    "region": {
      "regex": "^\\s*#(end)?region"
    }
  }
}

Use a particular languageId if you want to limit to certain files.

The Zig language has special comments to generate documentation.
If you want to keep these comments you can define the setting:

"remove-comments.keep": {
  "zig": {
    "documentation": {
      "regex": "^(/(?!/)|!)"
    }
  }
}

Override or Remove a User or Workspace setting

In VSC the setting objects are merged:

You can remove a setting by using a false value:

You can override a named regular expression in the Workspace/Folder settings by redifining the object keys regex and/or flags:

remove-comments.extractStrings.filePath

A string containing a file system path that is to be used to store the strings extracted by the Comments: Extract Strings to a file command. The new strings are appended to this file.

remove-comments.extractStrings.lineJoin

A string containing the text to replace a new line in a multi line string. Used the Comments: Extract Strings to a file command.

remove-comments.removeBlankLines.before

Remove up to N blank lines before a removed comment. If the line with the comment is removed. default 0.
No lines outside the selection will be removed.

remove-comments.removeBlankLines.after

Remove up to N blank lines after a removed comment. If the line with the comment is removed. default 0.
No lines outside the selection will be removed.

Supported Languages

List of Supported Languages * Ada * AL * C * COBOL / ACUCOBOL / OpenCOBOL / BitlangCOBOL * cfml * Clojure * CoffeeScript * CSS * C++ * C# * Dart * Dockerfile * Elixir * Erlang * F# * Go * GraphQL * Groovy * Haskell * Haxe * HTML * Jade * Java * JavaScript * JavaScript React * JSON with comments * Julia * Kotlin * Laravel Blade * LaTex * Less * Lisp * Lua * Makefile * Objective-C * Objective-C++ * Pascal * Perl * Perl 6, Raku * PHP * PL/SQL * PowerShell * Properties (`*.ini`, `*.conf`) * Pug (Jade) * Python * R * Racket * Ruby * Rust * sass * Scala * Scheme * SCSS * Shaderlab * Shell Script (bash) * Solidity * Spark * SQL * Stylus * Swift * Terraform * TOML * TypeScript * TypeScript React * Visual Basic * Verilog / System Verilog * VHDL * Vue Single File * YAML * Zig

License

Licensed under the MIT License.

Known issues

TODO