Code Commentor II

Keyboard Maestro 8 “Code Commentor II” Macro

Author: Mike Pasini
Version: 2.1a
Last Update: 29 July 2018

Note: Version 2.1a replaces v2.0a. It uses a trigger variation rather than a unique character sequence to distinguish between multiline and single line commenting. Comments dated prior to July 29 refer to the original version. And those items, too, have been addressed in the new version.

This Keyboard Maestro macro automates adding or removing comment tags from a text selection in AppleScript, C, CoffeeScript, CSS, Haskell, HTML, PHP, Perl, Python, Ruby and JavaScript source files.

In languages that support multiline comments, multiline comment tagging is supported when you use the trigger chord Control-Option-#. Single line comments are used when you use the trigger chord Option-#.

The correct comment syntax is derived automatically from the window name of the currently opened file or, alternately, manually from a pick list (which remembers your last choice) when you hold the trigger key down.

If the comment tag for that type is not in the text selection, it's added. If it is, it's removed.

All you have to do is select a line or multiple lines of text or code in your source file and trigger the macro to toggle the comment.

Code Commentor II.v21a.zip (7.6 KB)

Thanks for sharing.

This is not working for me -- maybe I need to change some setup in the Macro?

It does not do anything to this AppleScript

  local oSound, curVol, oNSSound
  
  set oSound to (current application's NSSound's soundNamed:pSoundNameStr)
  
  ### How do I get CURRENT Sound Volume, so I can restore? ###
  -- I tried oSound's getVolume, but that did NOT work
  
  oSound's setVolume:(2.0)
  oSound's play()

If I insert the ">>" as the first line, then it does add the multiline comment.
But then when I select that, and run the macro again, it does not uncomment.
Instead, it gives me this:

--   (*
--   
    local oSound, curVol, oNSSound
    
    set oSound to (current application's NSSound's soundNamed:pSoundNameStr)
    
    ### How do I get CURRENT Sound Volume, so I can restore? ###
    -- I tried oSound's getVolume, but that did NOT work
    
    oSound's setVolume:(2.0)
    oSound's play()*)

This may be personal preference, but when commenting multiple lines, I always want the comment character to be on a separate line, like this:

(*
  local oSound, curVol, oNSSound
  
  set oSound to (current application's NSSound's soundNamed:pSoundNameStr)
  
  ### How do I get CURRENT Sound Volume, so I can restore? ###
  -- I tried oSound's getVolume, but that did NOT work
  
  oSound's setVolume:(2.0)
  oSound's play()
*)

It makes no change to this JXA block.

//--- GET ARRAY OF VALUES FOR KEY "Name" ---
var recentAppsList   = recentAppObjects.map(function(pObj) {return pObj.Name;});

var numApps      = Math.min(maxNumAppsToReturn, recentAppsList.length);
var appList     = recentAppsList.slice(0,numApps);
  
appList.sort();

I'm guessing it sees the first line as a comment, and just quits.
It should add new comment characters to all lines, including the first.

IMO, you need a better way for the user to select multi-line comment block -- maybe a long key press.

One find thought: Like my comment (in another thread) about Script Editor, if the app is "Script Debugger" then the only choice is AppleScript. It would be nice if your macro recognized that.

Sorry, one more thought: You might consider deleting the past KM clipboards created by this macro, so, from a user's perspective, the current clipboard is the last thing he/she put there.

I'll take a look at this in the morning but here are a few points in the meantime:

  1. There is an issue with the single line AS regexp resolved by changing the last block of code to
	} elsif ($lang =~ /AppleScript|Haskell/) {
		if ($clipb !~ /^--/) {
			$clipb =~ s/${selection}/-- $1/gs;
		} else {
			$clipb =~ s/-- ${selection}/$1/gs;
		}
	}
  1. Multiple line comment tags are placed on their own line, as you expect. Not sure why yours weren't but I look into it. The selection itself plays a key role.

  2. If the selection begins with the multiline comment tag, it (and it's ending tag in the selection) will indeed be removed.

  3. Can't duplicate your JXA issue either but I would not select the line beginning "//". Just doesn't work that way.

  4. ">>" as a key is awkward, agreed. I think what I'll do is try an alternate key chord for multiple line commenting (Control-Option-# for mulitple and Option-# for single, say). The delay is already being used to force a language selection (see the ReadMe for details). The alternate chord would branch off into separate scripts but all in the same macro to keep things tidy.

  5. The revision will use AS in Script Debugger even if the file extension is not available.

  6. Yeah, I should delete the entry in the System Clipboard as a housekeeping measure.

Thanks for the feedback.

New version up now.

It recognizes Script Debugger, clears the System Clipboard and implements a dual trigger method of determining which mode to comment in.

I've revised the regexps continually so a few possibilities may have escaped my testing (although I'm not sure which). I tested all the multiline combinations, and full single line with a single line, multiple lines and partial lines. In BBEdit, Script Editor and Script Debugger.

But let me know if you run into something that escaped me.

Thanks for your help.

1 Like

Thanks for the quick response.
I'll test it when I get a few moments.

Glad to help where I can.
Thanks again for sharing, and all of your contributions here.

On note on the new version:

The trigger is described as Control-Option-# and Option-# but you don't hold the Shift key down to get that #. So the trigger is actually 3, not #. Which may matter to you if you want to change the trigger. And if you do, the important thing is to make sure you have both a Control-Option and Option version of your preferred key (and subsequently check for them in the code that follows).

(An earlier version of this comment mentioned an issue with Script Debugger. My mistake, I was inadvertently running an older version of Code Commentor on the Script Debugger selection. The current one above works as expected in Script Debugger. Thanks to @ccstone for the wake-up call.)