Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7eb2c2ce20cd5653d4a457004ca6031a194ea7e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.internal.ui.actions;

import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import org.eclipse.jface.text.Assert;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPartitioningException;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.DefaultPositionUpdater;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.jface.text.IPositionUpdater;
import org.eclipse.jface.text.IRewriteTarget;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorExtension2;
import org.eclipse.ui.texteditor.TextEditorAction;

/**
 * Common block comment code.
 * 
 * @since 3.0
 */
public abstract class BlockCommentAction extends TextEditorAction {
	/**
	 * Creates a new instance.
	 * @param bundle
	 * @param prefix
	 * @param editor
	 */
	public BlockCommentAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
		super(bundle, prefix, editor);
	}

	/**
	 * An edit is a kind of <code>DocumentEvent</code>, in this case an edit instruction, that is 
	 * affiliated with a <code>Position</code> on a document. The offset of the document event is 
	 * not stored statically, but taken from the affiliated <code>Position</code>, which gets 
	 * updated when other edits occur.
	 */
	static class Edit extends DocumentEvent {
		
		/**
		 * Factory for edits which manages the creation, installation and destruction of 
		 * position categories, position updaters etc. on a certain document. Once a factory has
		 * been obtained, <code>Edit</code> objects can be obtained from it which will be linked to
		 * the document by positions of one position category.
		 * <p>Clients are required to call <code>release</code> once the <code>Edit</code>s are not
		 * used any more, so the positions can be discarded.</p>
		 */
		public static class EditFactory {
	
			/** The position category basename for this edits. */
			private static final String CATEGORY= "__positionalEditPositionCategory"; //$NON-NLS-1$
			
			/** The count of factories. */
			private static int fgCount= 0;
		
			/** This factory's category. */
			private final String fCategory;
			private IDocument fDocument;
			private IPositionUpdater fUpdater;
			
			/**
			 * Creates a new <code>EditFactory</code> with an unambiguous position category name.
			 * @param document the document that is being edited.
			 */
			public EditFactory(IDocument document) {
				fCategory= CATEGORY + fgCount++;
				fDocument= document;
			}
			
			/**
			 * Creates a new edition on the document of this factory.
			 * 
			 * @param offset the offset of the edition at the point when is created.
			 * @param length the length of the edition (not updated via the position update mechanism)
			 * @param text the text to be replaced on the document
			 * @return an <code>Edit</code> reflecting the edition on the document
			 */
			public Edit createEdit(int offset, int length, String text) throws BadLocationException {
				
				if (!fDocument.containsPositionCategory(fCategory)) {
					fDocument.addPositionCategory(fCategory);
					fUpdater= new DefaultPositionUpdater(fCategory);
					fDocument.addPositionUpdater(fUpdater);
				}
				
				Position position= new Position(offset);
				try {
					fDocument.addPosition(fCategory, position);
				} catch (BadPositionCategoryException e) {
					Assert.isTrue(false);
				}
				return new Edit(fDocument, length, text, position);
			}
			
			/**
			 * Releases the position category on the document and uninstalls the position updater. 
			 * <code>Edit</code>s managed by this factory are not updated after this call.
			 */
			public void release() {
				if (fDocument != null && fDocument.containsPositionCategory(fCategory)) {
					fDocument.removePositionUpdater(fUpdater);
					try {
						fDocument.removePositionCategory(fCategory);
					} catch (BadPositionCategoryException e) {
						Assert.isTrue(false);
					}
					fDocument= null;
					fUpdater= null;
				}
			}
		}
		
		/** The position in the document where this edit be executed. */
		private Position fPosition;
		
		/**
		 * Creates a new edition on <code>document</code>, taking its offset from <code>position</code>.
		 * 
		 * @param document the document being edited
		 * @param length the length of the edition
		 * @param text the replacement text of the edition
		 * @param position the position keeping the edition's offset
		 */
		protected Edit(IDocument document, int length, String text, Position position) {
			super(document, 0, length, text);
			fPosition= position;
		}
		
		/*
		 * @see org.eclipse.jface.text.DocumentEvent#getOffset()
		 */
		public int getOffset() {
			return fPosition.getOffset();
		}
		
		/**
		 * Executes the edition on document. The offset is taken from the position.
		 * 
		 * @throws BadLocationException if the execution of the document fails.
		 */
		public void perform() throws BadLocationException {
			getDocument().replace(getOffset(), getLength(), getText());
		}
		
	}

	public void run() {
		if (!isEnabled())
			return;
			
		ITextEditor editor= getTextEditor();
		if (editor == null || !ensureEditable(editor))
			return;
			
		ITextSelection selection= getCurrentSelection();
		if (!isValidSelection(selection))
			return;
		
		if (!validateEditorInputState())
			return;
		
		IDocumentProvider docProvider= editor.getDocumentProvider();
		IEditorInput input= editor.getEditorInput();
		if (docProvider == null || input == null)
			return;
			
		IDocument document= docProvider.getDocument(input);
		if (document == null)
			return;
			
		IDocumentExtension3 docExtension;
		if (document instanceof IDocumentExtension3)
			docExtension= (IDocumentExtension3) document;
		else
			return;
		
		IRewriteTarget target= (IRewriteTarget)editor.getAdapter(IRewriteTarget.class);
		if (target != null) {
			target.beginCompoundChange();
		}
		
		Edit.EditFactory factory= new Edit.EditFactory(document);
		
		try {
			runInternal(selection, docExtension, factory);
	
		} catch (BadLocationException e) {
			// can happen on concurrent modification, deletion etc. of the document 
			// -> don't complain, just bail out
		} catch (BadPartitioningException e) {
			// should not happen
			Assert.isTrue(false, "bad partitioning");  //$NON-NLS-1$
		} finally {
			factory.release();
			
			if (target != null) {
				target.endCompoundChange();
			}
		}
	}

	/**
	 * Calls <code>perform</code> on all <code>Edit</code>s in <code>edits</code>.
	 * 
	 * @param edits a list of <code>Edit</code>s
	 * @throws BadLocationException if an <code>Edit</code> threw such an exception.
	 */
	protected void executeEdits(List edits) throws BadLocationException {
		for (Iterator it= edits.iterator(); it.hasNext();) {
			Edit edit= (Edit) it.next();
			edit.perform();
		}
	}

	/**
	 * Ensures that the editor is modifyable. If the editor is an instance of
	 * <code>ITextEditorExtension2</code>, its <code>validateEditorInputState</code> method 
	 * is called, otherwise, the result of <code>isEditable</code> is returned.
	 * 
	 * @param editor the editor to be checked
	 * @return <code>true</code> if the editor is editable, <code>false</code> otherwise
	 */
	protected boolean ensureEditable(ITextEditor editor) {
		Assert.isNotNull(editor);
	
		if (editor instanceof ITextEditorExtension2) {
			ITextEditorExtension2 ext= (ITextEditorExtension2) editor;
			return ext.validateEditorInputState();
		}
		
		return editor.isEditable();
	}

	/*
	 * @see org.eclipse.ui.texteditor.IUpdate#update()
	 */
	public void update() {
		super.update();
		
		if (isEnabled()) {
			if (!canModifyEditor() || !isValidSelection(getCurrentSelection()))
				setEnabled(false);
		}
	}

	/**
	 * Returns the editor's selection, or <code>null</code> if no selection can be obtained or the 
	 * editor is <code>null</code>.
	 * 
	 * @return the selection of the action's editor, or <code>null</code>
	 */
	protected ITextSelection getCurrentSelection() {
		ITextEditor editor= getTextEditor();
		if (editor != null) {
			ISelectionProvider provider= editor.getSelectionProvider();
			if (provider != null) {
				ISelection selection= provider.getSelection();
				if (selection instanceof ITextSelection) 
					return (ITextSelection) selection;
			}
		}
		return null;
	}

	/**
	 * Runs the real command once all the editor, document, and selection checks have succeeded.
	 * 
	 * @param selection the current selection we are being called for
	 * @param docExtension the document extension where we get the partitioning from
	 * @param factory the edit factory we can use to create <code>Edit</code>s 
	 * @throws BadLocationException if an edition fails
	 * @throws BadPartitioningException if a partitioning call fails
	 */
	protected abstract void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadLocationException, BadPartitioningException;

	/**
	 * Checks whether <code>selection</code> is valid.
	 * 
	 * @param selection the selection to check
	 * @return <code>true</code> if the selection is valid, <code>false</code> otherwise
	 */
	protected abstract boolean isValidSelection(ITextSelection selection);

	/**
	 * Returns the text to be inserted at the selection start.
	 * 
	 * @return the text to be inserted at the selection start
	 */
	protected String getCommentStart() {
		// for now: no space story
		return "/*"; //$NON-NLS-1$
	}

	/**
	 * Returns the text to be inserted at the selection end.
	 * 
	 * @return the text to be inserted at the selection end
	 */
	protected String getCommentEnd() {
		// for now: no space story
		return "*/"; //$NON-NLS-1$
	}


}

Back to the top