Auto Close XML Tags & Insert Comments

Auto Close XML Tags & Insert Comments

1
NewbieNewbie
1

    Apr 26, 2007#1

    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

    6,686585
    Grand MasterGrand Master
    6,686585

      Re: Auto Close XML Tags & Insert Comments

      Apr 26, 2007#2

      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.

      Edit: A feature for automatic closing of XML/HTML tags which can be enabled or disabled at Advanced - Configuration - Editor - XML/HTML is available since UltraEdit for Windows v18.00 and UEStudio v12.00.
      Best regards from an UC/UE/UES for Windows user from Austria

      1
      NewbieNewbie
      1

        Re: Auto Close XML Tags & Insert Comments

        May 28, 2009#3

        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();