lukashadden wrote:Can I use the same shortcut key as the line comment, but only have the script run if line comments are not supported for the file type?
Yes, but with a limitation. There are the commands
Comment Add to insert line comments and
Comment Remove to remove line comments. This script supports both depending on presence of a block comment. So you have 2 options now:
- You delete the hotkey assigned to command EditCommandAdd in the Key Mapping configuration dialog. Next you add the script to the script list and assign the hotkey to the script which you have just removed from command Comment Add. Last you have to modify the script as described below.
With this configuration the script makes block comment add/remove for files with a language not supporting line comments and only line comment add for files with a language supporting line comments. Removing line comments must be still down with Comment Remove.
- You remove the hotkey assignments from both internal commands. You create a copy of the script with a different name and add both scripts to the list of scripts with the hotkeys assigned before to the internal commands EditCommandAdd and EditCommandRemove. On second script for removing line comments you have to change 1 command as shown below.
With this configuration both scripts make block comment add/remove for files with a language not supporting line comments while one script is for adding line comments and the other for removing line comments.
lukashadden wrote:Can I comment the current line even if nothing is selected?
Yes, this is possible, if the script is slightly changed too.
The script posted above contains at beginning the lines:
- Code: Select all
// Is something selected in active file?
if (UltraEdit.activeDocument.isSel()) {
This command must be replaced by:
- Code: Select all
// If nothing selected in active file, select the current line.
if (!UltraEdit.activeDocument.isSel()) UltraEdit.activeDocument.selectLine();
// Does this file contain according to file extension a language supporting line comments?
if (!UltraEdit.activeDocument.isExt("css") && !UltraEdit.activeDocument.isExt("htm") &&
!UltraEdit.activeDocument.isExt("html") /* and other file extensions */ ) {
// Yes, use the internal command for adding a line comment.
UltraEdit.activeDocument.commentAdd();
// Yes, use the internal command for removing a line comment.
// UltraEdit.activeDocument.commentRemove(); //<- For the other script for removing line comments.
}
else {
// The file contains a language not supporting line comments.
// Insert or remove block comments with keeping indents.
The problem with this approach is that a HTML file contains usually multiple languages. Some do not support line comments like CSS and HTML, others support line comments like Javascript. There is no possibility to get access by script to any syntax highlighting information like language name or comment definitions. Therefore everything must be coded in script.
As you can see the script uses a sequence of a not case sensitive
isExt function with evaluating the negative return value in an AND combination to determine which file extensions contain usually languages which definitely do not support line comments. For those files the script code is used, for all other the internal line comment add/remove commands.
Of course for HTML code the script would not make a good job at the moment as
/* and
*/ are the wrong strings for HTML comments. So the script must be further enhanced for other languages than CSS, C/C++/C#, Javascript, ...
Finally you could enhance the script for making also line comment add/remove by script code instead of using UltraEdit's internal commands. It is up to you how smart you would like to code this script. The main techniques required are already documented now in the script I have posted above.