Block Code Commenting Script

Block Code Commenting Script

2

    Mar 01, 2009#1

    I've written a script for commenting out multiple lines of code at a time. It detects what language you are using from the file extension, and then adds a comment symbol at the start of each selected line. If the selected lines are already commented out then they are uncommented, so it lets you toggle comments on/off as needed. Languages supported include Java, JavaScript, PHP, C-Sharp, Perl, Ruby, UNIX Shell, and VB. It can also comment out sections of an XML/HTML document.

    It is a bit long to post here, but you can download it from http://codebox.org.uk/pages/ultraedit-s ... k-comments

    119
    Power UserPower User
    119

      Mar 03, 2009#2

      Does this do something that the built-in Comment Add and Comment Remove commands don't?

      2

        Mar 03, 2009#3

        Well I never knew about that feature, just found it now. Thanks for the heads-up. Luckily, the answer is yes! The script copes better with commented lines that don't have the comment character/s at the very start of the line. It will remove comments even if the comment is preceded by whitespace. It also works for blocks of HTML/XML which I don't think the built-in feature does. Also, since it's open-source and (I hope) fairly clearly written you can add in your own commenting code to process things differently if you want. If none of those advantages are important to you then I say just use the native feature, since its probably more robust than mine.

        Rob

        25
        Basic UserBasic User
        25

          Mar 11, 2009#4

          Thanks for the script! Great to see contributions for UE community benefit!

          1
          NewbieNewbie
          1

            Jul 24, 2017#5

            maryv wrote: Thanks for the script! Great to see the Provillus contributions for UE community benefit!
            Hey Rob, I tried the script but I couldn't get it to work. Is it working for anyone else?

            6,602548
            Grand MasterGrand Master
            6,602548

              Jul 25, 2017#6

              The script works still fine. I saved the script code into a new ANSI file saved as CommentAddRemove.js and added this file to Script list.

              Then I opened a *.html file, selected a block not containing an HTML block comment and executed the script with a double click on it in Script list. The selected block was embedded after script execution within <!-- and -->. I reselected the commented block and executed the script once again and <!-- and --> were removed by the script.

              Next I opened a *.js file, selected a block containing already some lines with // after preceding spaces and executed the script. The line comment starting // were removed on all lines in selection. I reselected the block, executed the script once again and all lines started with // as expected.

              The HTML and the JavaScript file were both ANSI encoded files.

              However, the built-in commands Comment Add and Comment Remove for adding/removing line comments working in the meantime also when there are preceding whitespaces and Comment Selection and Uncomment Selection for adding/removing a block comment do the same as the script based on line/block comments defined in syntax highlighting language used for active file respectively active section in file (HTML with other languages like CSS, PHP, JavaScript, etc. embedded).
              Best regards from an UC/UE/UES for Windows user from Austria

              8
              NewbieNewbie
              8

                Dec 05, 2017#7

                Are the built-in commands supposed to work for all file types which have a syntax highlighting file?

                I have an (old?) UNIX Shell Scripts wordfile file lying around, also downloaded the (newer?) Bourne & Korn Shell - Comment selection/Uncomment selection works for neither.

                The script here however works fine with shell scripts.

                6,602548
                Grand MasterGrand Master
                6,602548

                  Dec 05, 2017#8

                  The wordfile shell.uew containing language definition UNIX Shell Scripts for files with extension CSH KSH SH is installed with UltraEdit for Windows v23.20 or any later version. It was updated last time for UE v24.00. So this wordfile is definitely not old.

                  The user-contributed wordfile bkshell.uew containing language definition Bourne & Korn Shell for files with extension KSH SH is also quite new according to file time in GitHub repository.

                  An intensive investigation would be required to find out which one is better, most likely the best would be a combination of both. However, it is not good to have two or more *.uew files for same file extension. UltraEdit indicates admittedly in status bar which syntax language definition is used for the active file at active position of the caret in file. The user can click on the language indication in status bar and select a different language on not using basic status bar. But in general it just confuses the user on having multiple language wordfiles installed for same type of file.

                  The commands Comment Add and Comment Remove work on one or more selected lines for any syntax highlighted file having Line Comment = defined in the wordfile. bkshell.uew and shell.uew contain both Line Comment = # and therefore those two commands can be used to add/remove a line comment, i.e. add/remove # at beginning of all selected lines or just the current line if there is no selection at all. shell.uew contains additionally Line Comment Preceding Chars = [~!-~] for not interpreting any # anywhere in a line as begin of a line comment. This line comment highlighting enhancement was discussed on topic BASH Code Folding.

                  The commands Comment Selection and Uncomment Selection work on one or more selected lines for any syntax highlighted file having Block Comment On = and Block Comment Off = defined in the wordfile. This is not the case for Unix shell script highlighting. Those two commands can't be used for shell scripts as block comments are not available in shell script syntax language.
                  Best regards from an UC/UE/UES for Windows user from Austria