Auto Close XML Tags & Insert Comments

Syntax highlighting, code folding, brace matching, code indenting, and function list

Auto Close XML Tags & Insert Comments

Postby sobencha » Wed Apr 25, 2007 7:03 pm

I'm trying out UltraEdit 13.00a as a possible replacement to using Eclipse as an editor for XML/XSL/PHP and am so far pretty much sold, but I have a couple of issues that I've not been able to find clear answers to thusfar in the forums...

First thing that I am hoping UltraEdit can do is to automatically create the closing XML tag for me when I type the opening one as is done with the XMLBuddy Eclipse plugin (or close a brace/parenthesis in PHP/Java as well). I assume this is possible, I'm just missing how to actually get it to work.

Second thing that I am hoping is possible is the ability to highlight a chunk of code/text/etc. and surround with comments. is this possible?

Any help would be appreciated!

Cheers
User avatar
sobencha
Newbie
 
Posts: 1
Joined: Tue Apr 24, 2007 11:00 pm

Re: Auto Close XML Tags & Insert Comments

Postby Mofi » Thu Apr 26, 2007 6:20 am

sobencha wrote:First thing that I am hoping UltraEdit can do is to automatically create the closing XML tag for me when I type the opening one as is done with the XMLBuddy Eclipse plugin (or close a brace/parenthesis in PHP/Java as well).

Add a new list item for XML files as described at extension based tab settings problem at the Word Wrap/Tab Settings configuration dialog, select this item and specify an auto-complete file for XML editing. Of course you first have to create such an auto-complete file. Or you write a macro or a script which is automatically loaded on startup of UE and is executed with a hotkey, for example the key for >. UEStudio has a HTML Close Tag feature, but I don't know if this works also for XML because I do not create/edit XML files.

sobencha wrote:Second thing that I am hoping is possible is the ability to highlight a chunk of code/text/etc. and surround with comments. is this possible?

XML does not have line comments, but you can do that with several methods as described at Question about "Comment Add" for HTML/XML and the other forum topic linked there.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4058
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Auto Close XML Tags & Insert Comments

Postby boblhead » Thu May 28, 2009 6:25 pm

Might be a little late, but maybe this can help someone else. Here's a script function I run for XML files that automatically adds closing tags. I have assigned a hot key of "Shift + ." so that it executes whenever a ">" is entered. Aside from some screen flicker (unavoidable due to how the UltraEdit object model works), it seems to do the trick. Feel free to improve upon it, but please post any improvements for all to enjoy.

Code: Select all
function closeTag()
{
   if (UltraEdit.activeDocument.isExt("xml"))
   {
      // Get the current cursor position
      var originalPos = UltraEdit.activeDocument.currentPos;
      
      // Select all text up to last tag opening
      UltraEdit.activeDocument.startSelect();
      while (UltraEdit.activeDocument.selection.substr(0, 1) != "<")
      {
         // Extend selection to the left
         UltraEdit.activeDocument.key("LEFT ARROW");
         
         // If we encounter a closing tag or the beginning of the file,
         // bail out of this loop
         if (UltraEdit.activeDocument.currentPos == 0
            || UltraEdit.activeDocument.selection.substr(0, 1) == ">")
         {
            break;
         }            
      }
      
      // We should now have the entire tag selected, so we copy it
      UltraEdit.activeDocument.endSelect();
      
      // Get the new position and selected tag name
      var newPos = UltraEdit.activeDocument.currentPos;
      var tag = UltraEdit.activeDocument.selection;
      
      // Move cursor back to old position
      for (var i = newPos; i < originalPos; i++)
      {
         UltraEdit.activeDocument.key("RIGHT ARROW");   
      }
      
      // Make sure its not a DTD or a closing tag
      if (tag.substr(0, 1) != "<" || tag.substr(0, 2) == "<?" || tag.substr(0, 2) == "</")
      {
         UltraEdit.activeDocument.write(">");
         return;
      }
      
      // Get just the tag name
      var tag = tag.substr(1);
      if (tag.indexOf(" ") >= 0)
      {
         tag = tag.substr(0, tag.indexOf(" "));
      }
      else if (tag.indexOf(">") >= 0)
      {
         tag = tag.substr(0, tag.indexOf(">"));
      }
      
      // Insert closing tag
      UltraEdit.activeDocument.write("></" + tag + ">");
      
      // Now move cursor back between open and close tags
      for (var i = UltraEdit.activeDocument.currentPos; i > originalPos + 1; i--)
      {
         UltraEdit.activeDocument.key("LEFT ARROW");   
      }
      return;
   }
   else
   {
      // If not xml, just process the keystroke
      UltraEdit.activeDocument.write(">");
   }
}
closeTag();
boblhead
Newbie
 
Posts: 1
Joined: Thu May 28, 2009 6:19 pm


Return to Syntax Highlighting