Reformatting a block of line comments

Reformatting a block of line comments

1
NewbieNewbie
1

    Jan 15, 2011#1

    Suppose I have a comment like this:

    //This is a comment and I want to test reflow in a comment. Will
    //UltraEdit do what I want?

    Later I want to insert a sentence:

    //This is a comment and I want to test reflow in a comment. Now I insert
    //a sentence. Will
    //UltraEdit do what I want?

    Reformat Paragraph gives me this:

    //This is a comment and I want to test reflow in a comment. Now I insert
    //a sentence. Will //UltraEdit do what I want?

    Is there some way to make it do this instead:

    //This is a comment and I want to test reflow in a comment. Now I insert
    //a sentence. Will UltraEdit do what I want?

    6,603548
    Grand MasterGrand Master
    6,603548

      Jan 15, 2011#2

      That can be done, but not with a single command. If there are no preceding spaces you need to select all line comments, use Edit - Comment Remove, then use command Format - Reformat Paragraph, reselect the block, and finally add the line comments with Edit - Comment Add.

      If you need this often, it could be worth to do this reformatting with a script. The script has to
      • load the currently selected block into a string variable,
      • extract the preceding spaces/tabs, the line comment characters // and if present the following space(s) from first line and remember this string,
      • remove the preceding spaces/tabs, line comment characters and following spaces from all other lines,
      • merge the lines to a single line with smart inserting a space where needed merging two lines,
      • split the long lines into lines of maximal X characters per line based on last space found before the line length limit, or if no space found exactly on line length limit, of course with taking the before extracted preceding spaces/tabs and line comment characters into account,
      • insert again the preceding spaces/tabs and line comment characters,
      • and write finally back to the file the reformatted block over the still selected block.
      That is not really difficult to code, but to make it useful for many programmers, many different conditions must be taken into account, like last line selected without selecting also the line termination, or one user prefers // comment and the other prefers //comment, the preceding whitespaces must be kept as is with spaces, or tabs, or even mixed. Tabs are a problem for line length limit because of different tab stop widths used by the programmers. So a general script for this task would need to analyze in file directly to find out the tab stop value (in optimal case that only 1 tab stop value is used). However, it is possible to code such a script for general usage and surely many programmers would be happy if somebody takes this challenge and code it. If you code that script just for you, you can surely make it more simple because you know the format you want to see.

      Why I have not already coded such a script? Well, I use a block comment when writing a multi-line comment and a line comment usually only for a single or two line comment. Why? Block comments can be folded easily, but a block of single line comments must be manually selected and folded, and reformatting a block of multiple single line comments after modifying it is more difficult than for a block comment.