Ideas on writing a macro/script to toggle line comments on selected lines

Ideas on writing a macro/script to toggle line comments on selected lines

19
Basic UserBasic User
19

    Feb 04, 2016#1

    I'm in the process of trying to put a macro or script together that will allow line comments to toggled (selected lines will have comment markers removed or added depending on whether there are any present or not). I was hoping to automate this as much as possible so that, irrespective of the language used for the current file, the appropriate comment marker(s) could be applied. However, for this to be fully automatic there needs to be either, a way to determine the Line Comment value form the current wordfile (preferable) or, if that isn't possible, determine the current file type and use the correct comment marker(s) for it.

    So, does anyone know if it's possible to determine the Line Comment for the active Wordfile?

    Many thanks, PG.
    UltraEdit version and OS info:
    UltraEdit 15.20.0.1017;Windows 7

    6,680583
    Grand MasterGrand Master
    6,680583

      Feb 04, 2016#2

      I'm not sure why a Comment Toggle feature is really necessary as there is Comment Add and Comment Remove which both can be executed with customizable keys making them very fast to use on a selection. The usage of a macro or script to toggle line comment presence at beginning of the selected lines would have only the benefit to need to remember only one instead of two hotkeys.

      UltraEdit supports for HTML related files (HTM HTML SHTML HTT HTA HTX CFM JSP PHP PHTML ASP and others) even multi-language syntax highlighting where the context of caret position determines which syntax highlighting and therefore which wordfile is active. However, let us assume macro/script should be applied only on files highlighted completely with only one syntax highlighting language.

      It is not possible with a macro or script to access data of applied syntax highlighting language. Also the configuration settings like tab width and indent value and all other configuration settings can't be accessed from with a macro or script.

      It is possible to write macro or script code which depends on file extension. For UltraEdit macros there is the command IfExtIs "..." (case-insensitive). In UltraEdit scripts there is UltraEdit.activeDocument.isExt("...") (also case-insensitive) and the possibility to get file extension directly from UltraEdit.activeDocument.path (path and file name with extension of active file if not being a new, unsaved file).

      But let us think about typical structure in source code files of programming languages and scripts. Most lines of a source code files start either with a word or with a whitespace character. So checking with a script how many lines of a selection start with a non word and a non whitespace character in comparison to total number of selected lines would very often give a good estimation if line comment strings should be removed or added on selected lines. But this would not work for batch files with rem as line comment string.

      Another scripting solution would be to first use comment remove command and check if the selected text before and after execution of this command changed at all and how many lines changed in comparison to total number of selected lines. If none or only a small number of selected lines changed, comment remove was obviously the wrong command as none or just a few lines started with a line comment. In this case the selection need to be restored (to restore the few lines already having a line comment) and run next comment add to insert line comment string at beginning of each selected line.

      The main issue I see with a Comment Toggle feature is in my point of view the rule to determine what to do if some lines of selected block start already with a line comment string and others do not.

      Should the decision being taken depending on the beginning of first selected line?

      Or are the beginnings of all selected lines evaluated and the majority of commented and uncommented lines is used to decide to add or remove comments on all lines of selection? And what to do if number of commented and uncommented lines is equal?

      Or should those selected lines with a line comment at beginning be uncommented and the others be commented by the Comment Toggle feature?

      What to do with languages supporting multiple line comments?

      Independent on which rules are used for adding / removing comments on current selection, there will be always use cases where the script toggling the line comments would modify the selection in a manner not wanted by the user.

      Therefore writing a macro (not really good for this task) or a script (better for this task) to toggle comments makes in my opinion only sense for certain languages and when wanted toggling behavior is clearly defined by those who later use this custom enhancement.
      Best regards from an UC/UE/UES for Windows user from Austria

      19
      Basic UserBasic User
      19

        Feb 05, 2016#3

        Hi Mofi

        Thank you for your reply and your views as well.

        It is, perhaps, a trivial requirment and you're right, 'Comment Add/Rmove' can almost do the same thing. The reason i wanted to come up with a 'Comment toggle' is that i regularly have to run code (currently SQL, but i didn't really want to restrict it to that) where i'm testing various combinations of conditions.

        In this situation it would be to do something like,

        Before:
        AND customer_code = :cust_code
        AND balance < 0
        -- AND customer_name = :cust_name
        -- AND balance > 100

        After:
        -- AND customer_code = :cust_code
        -- AND balance < 0
        AND customer_name = :cust_name
        AND balance > 100

        The change from before to after would be accomplished by selecting the four lines and running the macro/script (ideally using a key combination). The macro/script would remove the comments from the lines with them and add comments to the lines without. As it's not possible to determine the Line Comment value from the active wordfile, i will follow your suggestion and determine the comment marker (an evolving situation!) from the file type.

        Thanks again for your time and input.

        PG
        UltraEdit version and OS info:
        UltraEdit 15.20.0.1017;Windows 7

        14
        Basic UserBasic User
        14

          Dec 24, 2017#4

          😎Hello from Paris oh great Google Reknowned Master Mofi😎
          Here's a macro to comment out some part of your code in c# (and others...)

          Code: Select all

          InsertMode
          ColumnModeOff
          HexOff
          Clipboard 9
          IfSel
          Cut 
          "/**/"
          Key LEFT ARROW
          Key LEFT ARROW
          Paste 
          Else
          "/**/"
          Key LEFT ARROW
          Key LEFT ARROW
          EndIf
          Clipboard 0