Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 71271 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/wst/xml/ui/internal/autoedit/StructuredAutoEditStrategyXML.java (-21 / +75 lines)
Lines 12-33 Link Here
12
 *******************************************************************************/
12
 *******************************************************************************/
13
package org.eclipse.wst.xml.ui.internal.autoedit;
13
package org.eclipse.wst.xml.ui.internal.autoedit;
14
14
15
import org.eclipse.core.runtime.Preferences;
15
import org.eclipse.jface.text.BadLocationException;
16
import org.eclipse.jface.text.BadLocationException;
16
import org.eclipse.jface.text.DocumentCommand;
17
import org.eclipse.jface.text.DocumentCommand;
18
import org.eclipse.jface.text.IAutoEditStrategy;
17
import org.eclipse.jface.text.IDocument;
19
import org.eclipse.jface.text.IDocument;
20
import org.eclipse.jface.text.IRegion;
21
import org.eclipse.ui.IEditorPart;
22
import org.eclipse.ui.IWorkbenchPage;
23
import org.eclipse.ui.IWorkbenchWindow;
24
import org.eclipse.ui.PlatformUI;
25
import org.eclipse.ui.texteditor.ITextEditor;
18
import org.eclipse.ui.texteditor.ITextEditorExtension3;
26
import org.eclipse.ui.texteditor.ITextEditorExtension3;
19
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
27
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
20
import org.eclipse.wst.sse.core.internal.provisional.StructuredModelManager;
28
import org.eclipse.wst.sse.core.internal.provisional.StructuredModelManager;
21
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
29
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
22
import org.eclipse.wst.sse.ui.internal.StructuredDocumentCommand;
30
import org.eclipse.wst.sse.ui.internal.StructuredDocumentCommand;
23
import org.eclipse.wst.sse.ui.internal.autoedit.BasicAutoEditStrategy;
31
import org.eclipse.wst.xml.core.internal.XMLCorePlugin;
32
import org.eclipse.wst.xml.core.internal.preferences.XMLCorePreferenceNames;
24
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
33
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
25
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
34
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
26
import org.eclipse.wst.xml.ui.internal.Logger;
35
import org.eclipse.wst.xml.ui.internal.Logger;
27
import org.w3c.dom.Node;
36
import org.w3c.dom.Node;
28
37
29
38
30
public class StructuredAutoEditStrategyXML extends BasicAutoEditStrategy {
39
public class StructuredAutoEditStrategyXML implements IAutoEditStrategy {
31
	public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
40
	public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
32
		StructuredDocumentCommand structuredDocumentCommand = (StructuredDocumentCommand) command;
41
		StructuredDocumentCommand structuredDocumentCommand = (StructuredDocumentCommand) command;
33
		Object textEditor = getActiveTextEditor();
42
		Object textEditor = getActiveTextEditor();
Lines 47-52 Link Here
47
			if (model != null)
56
			if (model != null)
48
				model.releaseFromRead();
57
				model.releaseFromRead();
49
		}
58
		}
59
60
		// spaces for tab character
61
		if (command.text != null && command.text.length() > 0 && command.text.charAt(0) == '\t')
62
			smartInsertForTab(command, document);
50
	}
63
	}
51
64
52
	private boolean isCommentNode(IDOMNode node) {
65
	private boolean isCommentNode(IDOMNode node) {
Lines 57-80 Link Here
57
		return (node != null && node.getNodeType() == Node.DOCUMENT_NODE);
70
		return (node != null && node.getNodeType() == Node.DOCUMENT_NODE);
58
	}
71
	}
59
72
60
	protected boolean isEndTagRequired(IDOMNode node) {
73
	private void smartInsertForComment(StructuredDocumentCommand structuredDocumentCommand, IDocument document, IStructuredModel model) {
61
62
		if (node == null)
63
			return false;
64
		return node.isContainer();
65
	}
66
67
	protected boolean prefixedWith(IDocument document, int offset, String string) {
68
69
		try {
70
			return document.getLength() >= string.length() && document.get(offset - string.length(), string.length()).equals(string);
71
		} catch (BadLocationException e) {
72
			Logger.logException(e);
73
			return false;
74
		}
75
	}
76
77
	protected void smartInsertForComment(StructuredDocumentCommand structuredDocumentCommand, IDocument document, IStructuredModel model) {
78
		try {
74
		try {
79
			if (structuredDocumentCommand.text.equals("-") && document.getLength() >= 3 && document.get(structuredDocumentCommand.offset - 3, 3).equals("<!-")) { //$NON-NLS-1$ //$NON-NLS-2$
75
			if (structuredDocumentCommand.text.equals("-") && document.getLength() >= 3 && document.get(structuredDocumentCommand.offset - 3, 3).equals("<!-")) { //$NON-NLS-1$ //$NON-NLS-2$
80
				structuredDocumentCommand.text += " "; //$NON-NLS-1$
76
				structuredDocumentCommand.text += " "; //$NON-NLS-1$
Lines 87-93 Link Here
87
83
88
	}
84
	}
89
85
90
	protected void smartInsertForEndTag(StructuredDocumentCommand structuredDocumentCommand, IDocument document, IStructuredModel model) {
86
	private void smartInsertForEndTag(StructuredDocumentCommand structuredDocumentCommand, IDocument document, IStructuredModel model) {
91
		try {
87
		try {
92
			if (structuredDocumentCommand.text.equals("/") && document.getLength() >= 1 && document.get(structuredDocumentCommand.offset - 1, 1).equals("<")) { //$NON-NLS-1$ //$NON-NLS-2$
88
			if (structuredDocumentCommand.text.equals("/") && document.getLength() >= 1 && document.get(structuredDocumentCommand.offset - 1, 1).equals("<")) { //$NON-NLS-1$ //$NON-NLS-2$
93
				IDOMNode parentNode = (IDOMNode) ((IDOMNode) model.getIndexedRegion(structuredDocumentCommand.offset - 1)).getParentNode();
89
				IDOMNode parentNode = (IDOMNode) ((IDOMNode) model.getIndexedRegion(structuredDocumentCommand.offset - 1)).getParentNode();
Lines 109-114 Link Here
109
		} catch (BadLocationException e) {
105
		} catch (BadLocationException e) {
110
			Logger.logException(e);
106
			Logger.logException(e);
111
		}
107
		}
108
	}
109
110
	/**
111
	 * Insert spaces for tabs
112
	 * 
113
	 * @param command
114
	 */
115
	private void smartInsertForTab(DocumentCommand command, IDocument document) {
116
		// tab key was pressed. now check preferences to see if need to insert
117
		// spaces instead of tab
118
		Preferences preferences = XMLCorePlugin.getDefault().getPluginPreferences();
119
		if (XMLCorePreferenceNames.SPACE.equals(preferences.getString(XMLCorePreferenceNames.INDENTATION_CHAR))) {
120
			int indentationWidth = preferences.getInt(XMLCorePreferenceNames.INDENTATION_SIZE);
121
122
			StringBuffer indent = new StringBuffer();
123
			if (indentationWidth != 0) {
124
				int indentSize = indentationWidth;
125
				try {
126
					IRegion firstLine = document.getLineInformationOfOffset(command.offset);
127
					int offsetInLine = command.offset - firstLine.getOffset();
128
					int remainder = offsetInLine % indentationWidth;
129
130
					indentSize = indentationWidth - remainder;
131
				} catch (BadLocationException e) {
132
					Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
133
				}
134
135
				for (int i = 0; i < indentSize; i++)
136
					indent.append(' ');
137
			}
112
138
139
			// replace \t characters with spaces
140
			command.text = indent.toString();
141
		}
142
	}
143
	
144
	/**
145
	 * Return the active text editor if possible, otherwise the active editor
146
	 * part.
147
	 * 
148
	 * @return Object
149
	 */
150
	private Object getActiveTextEditor() {
151
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
152
		if (window != null) {
153
			IWorkbenchPage page = window.getActivePage();
154
			if (page != null) {
155
				IEditorPart editor = page.getActiveEditor();
156
				if (editor != null) {
157
					if (editor instanceof ITextEditor)
158
						return editor;
159
					ITextEditor textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
160
					if (textEditor != null)
161
						return textEditor;
162
					return editor;
163
				}
164
			}
165
		}
166
		return null;
113
	}
167
	}
114
}
168
}
(-)src/org/eclipse/wst/xml/ui/internal/provisional/StructuredTextViewerConfigurationXML.java (-22 / +75 lines)
Lines 14-20 Link Here
14
14
15
import java.util.ArrayList;
15
import java.util.ArrayList;
16
import java.util.List;
16
import java.util.List;
17
import java.util.Vector;
17
18
19
import org.eclipse.core.runtime.Preferences;
18
import org.eclipse.jface.preference.IPreferenceStore;
20
import org.eclipse.jface.preference.IPreferenceStore;
19
import org.eclipse.jface.text.IAutoEditStrategy;
21
import org.eclipse.jface.text.IAutoEditStrategy;
20
import org.eclipse.jface.text.IDocument;
22
import org.eclipse.jface.text.IDocument;
Lines 48-53 Link Here
48
import org.eclipse.wst.sse.ui.internal.taginfo.ProblemAnnotationHoverProcessor;
50
import org.eclipse.wst.sse.ui.internal.taginfo.ProblemAnnotationHoverProcessor;
49
import org.eclipse.wst.sse.ui.internal.taginfo.TextHoverManager;
51
import org.eclipse.wst.sse.ui.internal.taginfo.TextHoverManager;
50
import org.eclipse.wst.sse.ui.internal.util.EditorUtility;
52
import org.eclipse.wst.sse.ui.internal.util.EditorUtility;
53
import org.eclipse.wst.xml.core.internal.XMLCorePlugin;
54
import org.eclipse.wst.xml.core.internal.preferences.XMLCorePreferenceNames;
51
import org.eclipse.wst.xml.core.internal.provisional.format.FormatProcessorXML;
55
import org.eclipse.wst.xml.core.internal.provisional.format.FormatProcessorXML;
52
import org.eclipse.wst.xml.core.internal.provisional.text.IXMLPartitions;
56
import org.eclipse.wst.xml.core.internal.provisional.text.IXMLPartitions;
53
import org.eclipse.wst.xml.core.internal.text.rules.StructuredTextPartitionerForXML;
57
import org.eclipse.wst.xml.core.internal.text.rules.StructuredTextPartitionerForXML;
Lines 65-95 Link Here
65
69
66
/**
70
/**
67
 * This class provides
71
 * This class provides
72
 * 
68
 * @since 1.0
73
 * @since 1.0
69
 */
74
 */
70
public class StructuredTextViewerConfigurationXML extends StructuredTextViewerConfiguration {
75
public class StructuredTextViewerConfigurationXML extends StructuredTextViewerConfiguration {
71
    
76
72
	InformationPresenter fInformationPresenter = null;
77
	InformationPresenter fInformationPresenter = null;
73
78
74
	public StructuredTextViewerConfigurationXML() {
79
	public StructuredTextViewerConfigurationXML() {
75
		super();
80
		super();
76
	}
81
	}
77
	
82
78
	public StructuredTextViewerConfigurationXML(IPreferenceStore store) {
83
	public StructuredTextViewerConfigurationXML(IPreferenceStore store) {
79
		super(store);
84
		super(store);
80
	}
85
	}
81
	
86
82
	/* (non-Javadoc)
87
	/*
83
	 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAutoEditStrategies(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
88
	 * (non-Javadoc)
89
	 * 
90
	 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAutoEditStrategies(org.eclipse.jface.text.source.ISourceViewer,
91
	 *      java.lang.String)
84
	 */
92
	 */
85
	public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) {
93
	public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) {
86
		List allStrategies = new ArrayList(0);
94
		List allStrategies = new ArrayList(0);
87
		
95
88
		IAutoEditStrategy[] superStrategies = super.getAutoEditStrategies(sourceViewer, contentType);
96
		IAutoEditStrategy[] superStrategies = super.getAutoEditStrategies(sourceViewer, contentType);
89
		for (int i = 0; i < superStrategies.length; i++) {
97
		for (int i = 0; i < superStrategies.length; i++) {
90
			allStrategies.add(superStrategies[i]);
98
			allStrategies.add(superStrategies[i]);
91
		}
99
		}
92
		
100
93
		if (contentType == IXMLPartitions.XML_DEFAULT) {
101
		if (contentType == IXMLPartitions.XML_DEFAULT) {
94
			allStrategies.add(new StructuredAutoEditStrategyXML());
102
			allStrategies.add(new StructuredAutoEditStrategyXML());
95
		}
103
		}
Lines 183-188 Link Here
183
			fInformationPresenter.setInformationProvider(xmlInformationProvider, IStructuredPartitionTypes.DEFAULT_PARTITION);
191
			fInformationPresenter.setInformationProvider(xmlInformationProvider, IStructuredPartitionTypes.DEFAULT_PARTITION);
184
			fInformationPresenter.setInformationProvider(xmlInformationProvider, IXMLPartitions.XML_DEFAULT);
192
			fInformationPresenter.setInformationProvider(xmlInformationProvider, IXMLPartitions.XML_DEFAULT);
185
			fInformationPresenter.setSizeConstraints(60, 10, true, true);
193
			fInformationPresenter.setSizeConstraints(60, 10, true, true);
194
			fInformationPresenter.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
186
		}
195
		}
187
		return fInformationPresenter;
196
		return fInformationPresenter;
188
	}
197
	}
Lines 197-227 Link Here
197
		}
206
		}
198
207
199
		if (fReconciler == null) {
208
		if (fReconciler == null) {
200
            fReconciler = new StructuredRegionProcessor();
209
			fReconciler = new StructuredRegionProcessor();
201
			fReconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
210
			fReconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
202
		}
211
		}
203
212
204
		boolean reconcilingEnabled = fPreferenceStore.getBoolean(CommonEditorPreferenceNames.EVALUATE_TEMPORARY_PROBLEMS);
213
		boolean reconcilingEnabled = fPreferenceStore.getBoolean(CommonEditorPreferenceNames.EVALUATE_TEMPORARY_PROBLEMS);
205
		
214
206
        if(!reconcilingEnabled)
215
		if (!reconcilingEnabled)
207
            return fReconciler;
216
			return fReconciler;
208
        
217
209
		// the second time through, the strategies are set
218
		// the second time through, the strategies are set
210
		if (fReconciler != null) {
219
		if (fReconciler != null) {
211
220
212
			IDocument doc = ((StructuredTextEditor)editorPart).getDocumentProvider().getDocument(editorPart.getEditorInput());
221
			IDocument doc = ((StructuredTextEditor) editorPart).getDocumentProvider().getDocument(editorPart.getEditorInput());
213
			IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
222
			IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
214
			
223
215
			try {
224
			try {
216
225
217
				if (sModel != null) {
226
				if (sModel != null) {
218
					
227
219
					IReconcilingStrategy markupStrategy = new StructuredTextReconcilingStrategyForMarkup((ITextEditor) editorPart);
228
					IReconcilingStrategy markupStrategy = new StructuredTextReconcilingStrategyForMarkup((ITextEditor) editorPart);
220
                    fReconciler.setReconcilingStrategy(markupStrategy, IXMLPartitions.XML_DEFAULT);
229
					fReconciler.setReconcilingStrategy(markupStrategy, IXMLPartitions.XML_DEFAULT);
221
					fReconciler.setDefaultStrategy(markupStrategy);
230
					fReconciler.setDefaultStrategy(markupStrategy);
222
231
223
					String contentTypeId = sModel.getContentTypeIdentifier();
232
					String contentTypeId = sModel.getContentTypeIdentifier();
224
					if(contentTypeId != null)
233
					if (contentTypeId != null)
225
						fReconciler.setValidatorStrategy(createValidatorStrategy(contentTypeId));
234
						fReconciler.setValidatorStrategy(createValidatorStrategy(contentTypeId));
226
				}
235
				}
227
			} finally {
236
			} finally {
Lines 272-289 Link Here
272
			fInformationPresenter.uninstall();
281
			fInformationPresenter.uninstall();
273
		}
282
		}
274
	}
283
	}
275
	
284
276
	/* (non-Javadoc)
285
	/*
286
	 * (non-Javadoc)
287
	 * 
277
	 * @see org.eclipse.ui.editors.text.TextSourceViewerConfiguration#getHyperlinkDetectors(org.eclipse.jface.text.source.ISourceViewer)
288
	 * @see org.eclipse.ui.editors.text.TextSourceViewerConfiguration#getHyperlinkDetectors(org.eclipse.jface.text.source.ISourceViewer)
278
	 */
289
	 */
279
	public IHyperlinkDetector[] getHyperlinkDetectors(ISourceViewer sourceViewer) {
290
	public IHyperlinkDetector[] getHyperlinkDetectors(ISourceViewer sourceViewer) {
280
		if (sourceViewer == null || !fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINKS_ENABLED))
291
		if (sourceViewer == null || !fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINKS_ENABLED))
281
			return null;
292
			return null;
282
		
293
283
		List allDetectors = new ArrayList(0);
294
		List allDetectors = new ArrayList(0);
284
		allDetectors.add(new XMLHyperlinkDetector());
295
		allDetectors.add(new XMLHyperlinkDetector());
285
		
296
286
		IHyperlinkDetector[] superDetectors =  super.getHyperlinkDetectors(sourceViewer);
297
		IHyperlinkDetector[] superDetectors = super.getHyperlinkDetectors(sourceViewer);
287
		for (int m = 0; m < superDetectors.length; m++) {
298
		for (int m = 0; m < superDetectors.length; m++) {
288
			IHyperlinkDetector detector = superDetectors[m];
299
			IHyperlinkDetector detector = superDetectors[m];
289
			if (!allDetectors.contains(detector)) {
300
			if (!allDetectors.contains(detector)) {
Lines 292-295 Link Here
292
		}
303
		}
293
		return (IHyperlinkDetector[]) allDetectors.toArray(new IHyperlinkDetector[0]);
304
		return (IHyperlinkDetector[]) allDetectors.toArray(new IHyperlinkDetector[0]);
294
	}
305
	}
306
307
	public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
308
		Vector vector = new Vector();
309
310
		// prefix[0] is either '\t' or ' ' x tabWidth, depending on preference
311
		Preferences preferences = XMLCorePlugin.getDefault().getPluginPreferences();
312
		int indentationWidth = preferences.getInt(XMLCorePreferenceNames.INDENTATION_SIZE);
313
		String indentCharPref = preferences.getString(XMLCorePreferenceNames.INDENTATION_CHAR);
314
		boolean useSpaces = XMLCorePreferenceNames.SPACE.equals(indentCharPref);
315
316
		for (int i = 0; i <= indentationWidth; i++) {
317
			StringBuffer prefix = new StringBuffer();
318
			boolean appendTab = false;
319
320
			if (useSpaces) {
321
				for (int j = 0; j + i < indentationWidth; j++)
322
					prefix.append(' ');
323
324
				if (i != 0)
325
					appendTab = true;
326
			} else {
327
				for (int j = 0; j < i; j++)
328
					prefix.append(' ');
329
330
				if (i != indentationWidth)
331
					appendTab = true;
332
			}
333
334
			if (appendTab) {
335
				prefix.append('\t');
336
				vector.add(prefix.toString());
337
				// remove the tab so that indentation - tab is also an indent
338
				// prefix
339
				prefix.deleteCharAt(prefix.length() - 1);
340
			}
341
			vector.add(prefix.toString());
342
		}
343
344
		vector.add(""); //$NON-NLS-1$
345
346
		return (String[]) vector.toArray(new String[vector.size()]);
347
	}
295
}
348
}

Return to bug 71271