Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #10741

    ArthurZ
    Participant

    I am thinking why not allowing commenting a line of code where the cursor currently is on?

    Right now the Comment/Uncomment functionality becomes enabled only when line(s) are selected and this is small but un-needed limitation IMO.

    #10746

    Yutaka Emura
    Keymaster

    Hello,

    Since these commands are in the “Convert Selection” sub menu of the Edit menu, I wanted to make them work same as other commands in the same sub menu. If I allowed these commands to work when unselected, I would have to allow all other commands to work when unselected.

    Thanks for your inputs!

    #10751

    ArthurZ
    Participant

    I see, but in my case I placed the Comment-Uncomment pair onto the toolbar.

    Therefore IMHO, this limitation should not be in this case.

    PS: This useful command (I use very often) is buried too deep in the menus, I suggest to elevate it.

    #10753

    Yutaka Emura
    Keymaster

    Hello,

    How about a macro like this:

    [JavaScript]

    if( document.selection.IsEmpty ){
    editor.ExecuteCommandByID(4154);
    }
    editor.ExecuteCommandByID(4371);

    This macro will comment out the selection if the selections exists, or comment the current line if the selection does not exist.

    Thanks!

    #10754

    ArthurZ
    Participant

    Probably you meant it will comment in either case. At least the code does this. And I think it would be nice to enhance the functionality so if the comment is already there – uncomment – thus toggling the comments on/off.

    #10760

    Yutaka Emura
    Keymaster

    You are right; the above code was to comment in all cases.

    To toggle the comment, it will a little more complicated. To uncomment in all cases, here is the code:

    if( document.selection.IsEmpty ){
    editor.ExecuteCommandByID(4154);
    }
    editor.ExecuteCommandByID(4372);

    Thanks!

    #10763

    ArthurZ
    Participant

    Not quite there, but the following variation is almost what I need, how to un-select a line?

    if( document.selection.IsEmpty )
    {
    // highlight select and comment

    editor.ExecuteCommandByID(4154); editor.ExecuteCommandByID(4371);
    }
    else
    {
    // uncomment
    editor.ExecuteCommandByID(4372);
    }
    #10764

    Meir
    Participant

    Hi!

    IMHO this macro just UN-comments an already commented-out text. It didn’t comment out any un-commented one, or am I missing something?

    #10766

    ArthurZ
    Participant

    Hello Meir,

    It highlights and comments out a line of code (even if it was commented out already). You need to just position the cursor within the line.

    While the cursor is still on the same (just commented) line it un-comments it.

    #10767

    Meir
    Participant

    Dear Arthur,

    I agree with you, but when one or more lines are SELECTED, nothing happens! No commenting-out!

    Is this last behavior planned? If so, what should I add to have it comment out a bunch of selected lines?

    #10768

    ArthurZ
    Participant

    Hello Meir,

    Yes, the macro is designed to work on a single line of unselected text.
    This is because in the rest of the situations we have the right-click -> Convert Selection > Comment/Uncomment functionality or you can pull these two buttons onto the toolbar.
    Agin, the latter just does not work for a single un-highlighted line of code – hence the macro.

    I must add, this creates some uncomfort for me – split, non-unified functionality :hammer: .

    Hope Yutaka is listening: I suggest a change to allow commenting code regardless of whether a line is highlighted/selected.

    #10769

    Meir
    Participant

    Thanks Arthur,

    OK…

    Hmmm! IMHO, the most desirable functionality is a single, toggle command which can be assigned a keyboard shortcut, working on the current line if none is selected or on all selected lines is. If any of these are already commented out, uncomment it!

    Toggle, because obviously if it is already commented out, why would you want to comment it out again? Also in the opposite direction.

    Yes I am sure Yutaka is listening. He always does, and that is not the least reason that I am using EE 🙂

    #10770

    ArthurZ
    Participant

    My both thumbs are up for the proposed!

    By the way your rephrase Meir: “Toggle” is the word and the way to go.

    #10772

    JohnQSmith
    Participant

    Hmmm! IMHO, the most desirable functionality is a single, toggle command which can be assigned a keyboard shortcut, working on the current line if none is selected or on all selected lines is. If any of these are already commented out, uncomment it!

    Toggle, because obviously if it is already commented out, why would you want to comment it out again?

    Because the code I’m editing already has comments that I want to keep. For example,

    ... 
    for(int i=0;i<10;i++) {
    // the following line prints incremented variable
    printf("\%d",i);
    }
    ...

    Using your idea, if I wanted to comment out that loop, I’d end up with

    ...
    //for(int i=0;i<10;i++) {
    the following line prints incremented variable
    // printf("\%d",i);
    //}
    ...

    which would be wrong. 🙁

    If I comment out that block, it should add additional comments on previously commented lines like

    ...
    //for(int i=0;i<10;i++) {
    //// the following line prints incremented variable
    // printf("\%d",i);
    //}
    ...

    that way when I later uncomment it, it will be back the way it was when I started.

    #19416

    QiaoJiao
    Participant

    Sweet and simple (actually, totally based on Emura code with my brute force comment check).
    I assigned it to ctrl+q

    if( document.selection.IsEmpty ) {
    	editor.ExecuteCommandByID(4154);  // select
    
    	// if line has comment sign
    	if ( document.selection.Text.substring(0,2) == '//' ) { // change to (0,1) and '#' if you use #
    		editor.ExecuteCommandByID(4372); // uncomment
    	} else {
    		editor.ExecuteCommandByID(4371); // comment
    	}
    
    	document.selection.Collapse();  // unselect
    }
Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic.

Share with friends... Tweet about this on TwitterShare on FacebookShare on Google+Email this to someone