Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 312381

Summary: [ui] Smart Insert Close Element functionality (automatic insertion of ending tags) does not work as expected when StructuredTextViewer is embedded in a dialog for editing XML code
Product: [WebTools] WTP Source Editing Reporter: Ankit Goel <ankitgoel1987>
Component: wst.xmlAssignee: Nick Sandonato <nsand.dev>
Status: RESOLVED FIXED QA Contact: Nitin Dahyabhai <thatnitind>
Severity: major    
Priority: P3 CC: ankitgoel1987, david_williams, nsand.dev
Version: unspecified   
Target Milestone: 3.5 M3   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Code for creating the required dialog - Might help in reproducing the problem
none
patch none

Description Ankit Goel CLA 2010-05-11 04:42:41 EDT
Build Identifier: I20090611-1540

I tried embedding org.eclipse.wst.sse.ui.internal.StructuredTextViewer in a dialog for editing XML code. But in this case the Smart Insert Close Element functionality (automatic insertion of ending tags) does not work as expected.

Here goes the problem:

CASE 1: If my dialog is launched with a WTP XML editor open in the background than the ending tags are inserted automatically.

CASE 2: If the dialog is launched alone (with no WTP XML editor in background) ending tags would not be inserted.

I did little debugging on this and realized that org.eclipse.wst.xml.ui.internal.autoedit.StructuredAutoEditStrategyXML class causes this problem. Whenever a key is pressed the method StructuredAutoEditStrategyXML >> customizeDocumentCommand(IDocument, DocumentCommand) is called. This is responsible for inserting the ending tags etc. But before doing the functionality it checks which editor is active and exits without doing anything if appropriate editor is not present.

Hence following happens:

Case 1: (Described above) It finds the WTP XML editor in the background and hence automatically inserts ending tags in the dialog I have created.

Case 2: (Described above) Does not find a WTP XML editor in the background and hence it does not insert the ending tags.

Why the smart insert close element functionality needs to be dependent on a active text editor ? Seems something is wrong in there!!!

Reproducible: Always

Steps to Reproduce:
1. Create a Dialog of your own (by extending TrayDialog class) for editing XML code. This dialog should contain a StructuredTextViewer - need to override the method protected Control createDialogArea(Composite parent). (Will try and attach the java code as a seperate file that does this job)

	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new RowLayout(SWT.VERTICAL));
		IDocument document = getDocument();
		createXMLSourceViewer(composite, document);
		return composite;
	}

	private void createXMLSourceViewer(Composite composite, IDocument document) {
		RowData rowData = new RowData(800, 300);
		StructuredTextViewer structuredViewer = new StructuredTextViewer(composite, null, null,
				true, SWT.NONE);
		structuredViewer.getControl().setLayoutData(rowData);
		StructuredTextViewerConfigurationXML sourceViewerConfiguration = new StructuredTextViewerConfigurationXML();
		structuredViewer.configure(sourceViewerConfiguration);
		structuredViewer.setDocument(document);
	}

	private IDocument getDocument() {
		IDocument document = null;
		IStructuredModel scratchModel = null;
		try {
			scratchModel = StructuredModelManager.getModelManager()
					.getModelForEdit(ContentTypeIdForXML.ContentTypeID_XML,
							new FileInputStream(new File("C:/trialtext.xml")),
							null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		document = scratchModel.getStructuredDocument();
		return document;
	}

Make sure a file C:/trialtext.xml exists.

2. Do relevant coding for launching this dialog from the main menu (or any other point convenient to you).
3. Open a XML file in WTP XML editor.
4. Launch the created dialog (the WTP XML editor opened in Step 3 remains in the background).
5. Type the following:
 <abc>
You will observe that the closing tag </abc> gets inserted automatically.
6. Now close this dialog and the WTP XML editor as well.
7. Launch the dialog again (no other editor should be opened in the background).
8. Type the following:
 <abc>
You will observe that nothing happens - the closing tag </abc> is not inserted automatically.

To get hold of the problem put a break point at the following method:
org.eclipse.wst.xml.ui.internal.autoedit.StructuredAutoEditStrategyXML >> customizeDocumentCommand(IDocument, DocumentCommand)
Every time you press a key on the dialog the debugger stops here.
In step 5 it finds a WTP XML editor and hence inserts the ending tags.
In step 8 no editor is found. It exist without inserting the ending tags.
Comment 1 Ankit Goel CLA 2010-05-11 04:52:14 EDT
Created attachment 167875 [details]
Code for creating the required dialog - Might help in reproducing the problem

This attachment is just a sample code that will help in reproducing the problem. The attached class creates a dialog in which XML code can be edited. Make sure there exists a file 'C:/trialtext.xml' before launching this code or make necessary changes in the attached code.
Comment 2 Nick Sandonato CLA 2010-05-11 09:39:14 EDT
The reason it checks for an editor is because we want to make sure Smart Insert Mode (Ctrl+Shift+Insert) is enabled in the editor. Maybe the condition needs to be updated so that *iff* it's in an editor, check smart insert mode; otherwise, the auto edit strategy will be operational.
Comment 3 Ankit Goel CLA 2010-05-13 06:18:12 EDT
(In reply to comment #2)
> The reason it checks for an editor is because we want to make sure Smart Insert
> Mode (Ctrl+Shift+Insert) is enabled in the editor. Maybe the condition needs to
> be updated so that *iff* it's in an editor, check smart insert mode; otherwise,
> the auto edit strategy will be operational.

By when do we expect this one to be fixed?
Comment 4 Nick Sandonato CLA 2010-05-17 11:30:49 EDT
We're late into the development of Helios at this point. I think we could target this for the first service release of Helios.

In the meantime, would it be possible for you to add your own StructuredAutoEditStrategy that mimics StructuredAutoEditStrategyXML, but is more flexible with how it's being used (doesn't do the check that we make)? You could contribute this through a custom implementation of StructuredTextViewerConfigurationXML overriding getAutoEditStrategies().
Comment 5 Nick Sandonato CLA 2010-06-21 14:38:09 EDT
Created attachment 172354 [details]
patch

Patch making it so that if there is no active editor, we will default to supporting the edit strategy (jsp/html/css/xml).
Comment 6 Nick Sandonato CLA 2010-06-21 14:47:18 EDT
Retargeting since 3.2.2 is aligning with Helios SR1.
Comment 7 Nitin Dahyabhai CLA 2010-08-25 12:06:03 EDT
Not sure this handles the variant of case 1 where the active editor *isn't* an XML Editor.  Will probably need a circuitous way of passing in the correct editor as an optional typeless context object, similar to how validate edit is handled in the workspace with a UI context, so these sorts of situations can be resolved definitively.
Comment 8 Nick Sandonato CLA 2011-04-13 15:44:46 EDT
(In reply to comment #7)
> Not sure this handles the variant of case 1 where the active editor *isn't* an
> XML Editor.  Will probably need a circuitous way of passing in the correct
> editor as an optional typeless context object, similar to how validate edit is
> handled in the workspace with a UI context, so these sorts of situations can be
> resolved definitively.

Is your concern that if, for example, the Java editor is visible and for whatever reason Smart Insert is turned off for it that it will affect the outcome of this operation? If so, you're right, I'm only accounting for the case where there is no active editor.