View | Details | Raw Unified | Return to bug 236160 | Differences between
and this patch

Collapse All | Expand All

(-)ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java (+4 lines)
Lines 70-74 Link Here
70
		
70
		
71
		// compare tests
71
		// compare tests
72
		addTest(CStructureCreatorTest.suite());
72
		addTest(CStructureCreatorTest.suite());
73
		
74
		// block comment tests
75
		addTest(AddBlockCommentTest.suite());
76
		addTest(RemoveBlockCommentTest.suite());
73
    }
77
    }
74
}
78
}
(-)ui/org/eclipse/cdt/ui/tests/text/AddBlockCommentTest.java (+377 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 Wind River Systems, Inc. and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Anton Leherbauer (Wind River Systems) - initial API and implementation
10
 *     Andrew Gvozdev 
11
 *******************************************************************************/
12
package org.eclipse.cdt.ui.tests.text;
13
14
import junit.framework.TestSuite;
15
16
import org.eclipse.jface.action.IAction;
17
import org.eclipse.jface.text.BadLocationException;
18
import org.eclipse.jface.text.IDocument;
19
20
import org.eclipse.cdt.core.CCorePlugin;
21
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
22
import org.eclipse.cdt.core.model.ICProject;
23
import org.eclipse.cdt.core.testplugin.CProjectHelper;
24
import org.eclipse.cdt.ui.tests.BaseUITestCase;
25
26
import org.eclipse.cdt.internal.core.model.ext.SourceRange;
27
28
import org.eclipse.cdt.internal.ui.editor.CEditor;
29
30
/**
31
 * Tests for the AddBlockCommentAction.
32
 *
33
 * @since 5.0
34
 */
35
public class AddBlockCommentTest extends BaseUITestCase {
36
	private ICProject fCProject;
37
	public static TestSuite suite() {
38
		return suite(AddBlockCommentTest.class, "_");
39
	}
40
41
	private CEditor fEditor;
42
	private IDocument fDocument;
43
	
44
	/*
45
	 * The class describes a position on a line counting in ordinary people way,
46
	 * starting from 1.
47
	 */
48
	static class LinePosition {
49
		private int line;
50
		private int position;
51
		private IDocument fDoc;
52
		
53
		LinePosition(int line, int positionOnLine, IDocument doc) {
54
			this.line = line;
55
			this.position = positionOnLine;
56
			this.fDoc = doc;
57
		}
58
59
		int getOffset() throws BadLocationException {
60
			return fDoc.getLineOffset(line-1) + position-1;
61
		}
62
		
63
	}
64
65
	@Override
66
	protected void setUp() throws Exception {
67
		super.setUp();
68
		
69
		final String PROJECT= "BlockComment";
70
		// Using any existing file just to open CEditor, the content is not used
71
		final String filename= "/BlockComment/src/sample/Before.cpp";
72
		fCProject= EditorTestHelper.createCProject(PROJECT, "resources/formatter");
73
		fCProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.TAB);
74
		fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(filename), true);
75
		fDocument= EditorTestHelper.getSourceViewer(fEditor).getDocument();
76
		// Delete contents
77
		fDocument.set("");
78
	}
79
80
	@Override
81
	protected void tearDown() throws Exception {
82
		EditorTestHelper.closeEditor(fEditor);
83
84
		if (fCProject != null)
85
			CProjectHelper.delete(fCProject);
86
		
87
		super.tearDown();
88
	}
89
90
	/**
91
	 * Run an action to comment block defined by line positions
92
	 * and assert that the result matches the expected result.
93
	 * "Before" and "After" are taken from test comments.
94
	 * 
95
	 * @param startLinePosition
96
	 * @param endLinePosition
97
	 * @throws Exception
98
	 */
99
	protected void assertFormatterResult(
100
			LinePosition startLinePosition,
101
			LinePosition endLinePosition) throws Exception {
102
103
		StringBuffer[] contents= getContentsForTest(2);
104
		String before = contents[0].toString();
105
		String after  = contents[1].toString();
106
		
107
		fDocument.set(before);
108
109
		SourceRange range = new SourceRange( startLinePosition.getOffset(),
110
				endLinePosition.getOffset()-startLinePosition.getOffset() );
111
		fEditor.setSelection(range, true);
112
		
113
		IAction commentAction= fEditor.getAction("AddBlockComment");
114
		assertNotNull("No AddBlockComment action", commentAction);
115
		commentAction.setEnabled(true);
116
		commentAction.run();
117
		
118
		String expected= after;
119
		assertEquals(expected, fDocument.get());
120
	}
121
122
	
123
	/**
124
	 * Run an action to comment block defined by line positions
125
	 * and assert that the result matches the expected result.
126
	 * "Before" and "After" are taken from test comments.
127
	 * 
128
	 * @param startLinePosition
129
	 * @param endLinePosition
130
	 * @param before editor contents before the operation
131
	 * @param after expected editor contents after the operation
132
	 * @throws Exception
133
	 */
134
	protected void assertFormatterResult(
135
			LinePosition startLinePosition,
136
			LinePosition endLinePosition,
137
			String before,
138
			String after) throws Exception {
139
		
140
		fDocument.set(before);
141
		
142
		SourceRange range = new SourceRange( startLinePosition.getOffset(),
143
				endLinePosition.getOffset()-startLinePosition.getOffset() );
144
		fEditor.setSelection(range, true);
145
		
146
		IAction commentAction= fEditor.getAction("AddBlockComment");
147
		assertNotNull("No AddBlockComment action", commentAction);
148
		commentAction.setEnabled(true);
149
		commentAction.run();
150
		
151
		String expected= after;
152
		assertEquals(expected, fDocument.get());
153
	}
154
	
155
	//int i, j, k;
156
	
157
	//int i, /*j,*/ k;
158
	public void testCommentPlain() throws Exception {
159
		LinePosition startSelection = new LinePosition(1,8,fDocument);
160
		LinePosition endSelection   = new LinePosition(1,10,fDocument);
161
		assertFormatterResult(startSelection,endSelection);
162
	}
163
164
	//int i,
165
	//    j,
166
	//    k;
167
	
168
	//int /*i,
169
	//    j,*/
170
	//    k;
171
	public void testCommentPartialLines1() throws Exception {
172
		LinePosition startSelection = new LinePosition(1,5,fDocument);
173
		LinePosition endSelection   = new LinePosition(2,7,fDocument);
174
		assertFormatterResult(startSelection,endSelection);
175
	}
176
	
177
	//int i,
178
	//    j,
179
	//    k;
180
	
181
	//int i,
182
	///*    j,
183
	//    k*/;
184
	public void testCommentPartialLines2() throws Exception {
185
		LinePosition startSelection = new LinePosition(2,1,fDocument);
186
		LinePosition endSelection   = new LinePosition(3,6,fDocument);
187
		assertFormatterResult(startSelection,endSelection);
188
	}
189
	
190
	//int i;
191
	//int j;
192
	//int k;
193
	
194
	//int i;
195
	///*int j;*/
196
	//int k;
197
	public void testCommentExactlyOneLine() throws Exception {
198
		LinePosition startSelection = new LinePosition(2,1,fDocument);
199
		LinePosition endSelection   = new LinePosition(3,1,fDocument);
200
		assertFormatterResult(startSelection,endSelection);
201
	}
202
	
203
	//int i;
204
	//int j;
205
	//int k;
206
	//int l;
207
	
208
	//int i;
209
	///*
210
	//int j;
211
	//int k;
212
	//*/
213
	//int l;
214
	public void testCommentTwoOrMoreLines() throws Exception {
215
		LinePosition startSelection = new LinePosition(2,1,fDocument);
216
		LinePosition endSelection   = new LinePosition(4,1,fDocument);
217
		assertFormatterResult(startSelection,endSelection);
218
	}
219
	
220
	//const int i;
221
	
222
	///*const*/ int i;
223
	public void testCommentFirstCharacterInFile() throws Exception {
224
		LinePosition startSelection = new LinePosition(1,1,fDocument);
225
		LinePosition endSelection   = new LinePosition(1,6,fDocument);
226
		assertFormatterResult(startSelection,endSelection);
227
	}
228
	
229
	//#include <x>
230
	//#include <y>
231
	
232
	///*#include <x>*/
233
	//#include <y>
234
	public void testCommentPreprocessorFirstLine() throws Exception {
235
		LinePosition startSelection = new LinePosition(1,1,fDocument);
236
		LinePosition endSelection   = new LinePosition(1,6,fDocument);
237
		assertFormatterResult(startSelection,endSelection);
238
	}
239
	
240
	//int i; // comment
241
	//int j;
242
	
243
	//int i; /*// comment*/
244
	//int j;
245
	public void testCommentCppComment() throws Exception {
246
		LinePosition startSelection = new LinePosition(1,10,fDocument);
247
		LinePosition endSelection   = new LinePosition(1,12,fDocument);
248
		assertFormatterResult(startSelection,endSelection);
249
	}
250
	
251
	//#include <x>
252
	//#include <y>
253
	//#include <z>
254
255
	//#include <x>
256
	///*#include <y>*/
257
	//#include <z>
258
	public void testCommentSpecialPartition() throws Exception {
259
		LinePosition startSelection = new LinePosition(2,1,fDocument);
260
		LinePosition endSelection   = new LinePosition(3,1,fDocument);
261
		assertFormatterResult(startSelection,endSelection);
262
	}
263
	
264
	//#include <x>
265
	//   #include \
266
	//      <y>
267
	//#include <z>
268
	
269
	//#include <x>
270
	///*
271
	//   #include \
272
	//      <y>
273
	//*/
274
	//#include <z>
275
	public void testCommentSpecialPartitionExtra() throws Exception {
276
		LinePosition startSelection = new LinePosition(2,8,fDocument);
277
		LinePosition endSelection   = new LinePosition(2,10,fDocument);
278
		assertFormatterResult(startSelection,endSelection);
279
	}
280
	
281
	///*comment*/
282
	//int i;
283
	
284
	///*comment*/
285
	//int i;
286
	public void testCommentCommentNoScrewUp() throws Exception {
287
		LinePosition startSelection = new LinePosition(1,1,fDocument);
288
		LinePosition endSelection   = new LinePosition(2,1,fDocument);
289
		assertFormatterResult(startSelection,endSelection);
290
	}
291
	
292
	
293
	//int i;
294
	///*comment*/
295
	//int j;
296
	
297
	///*int i;
298
	//comment*/
299
	//int j;
300
	public void testCommentMergeUp() throws Exception {
301
		LinePosition startSelection = new LinePosition(1,1,fDocument);
302
		LinePosition endSelection   = new LinePosition(2,5,fDocument);
303
		assertFormatterResult(startSelection,endSelection);
304
	}
305
	
306
	//int i;
307
	///*comment*/
308
	//int j;
309
	
310
	//int i;
311
	///*comment
312
	//int j;*/
313
	public void testCommentMergeDown() throws Exception {
314
		LinePosition startSelection = new LinePosition(2,5,fDocument);
315
		LinePosition endSelection   = new LinePosition(3,7,fDocument);
316
		assertFormatterResult(startSelection,endSelection);
317
	}
318
	
319
	///*comment1*/
320
	///*comment2*/
321
	//int i;
322
	
323
	///*comment1
324
	//comment2*/
325
	//int i;
326
	public void testCommentMergeComments() throws Exception {
327
		LinePosition startSelection = new LinePosition(1,5,fDocument);
328
		LinePosition endSelection   = new LinePosition(2,5,fDocument);
329
		assertFormatterResult(startSelection,endSelection);
330
	}
331
	
332
	//
333
	//
334
	
335
	//
336
	///*
337
	//
338
	//*/
339
	public void testCommentEndOfFileNoLoopingPlease() throws Exception {
340
		// Don't care much about formatting but no looping please
341
		LinePosition startSelection = new LinePosition(2,1,fDocument);
342
		LinePosition endSelection   = new LinePosition(3,1,fDocument);
343
		assertFormatterResult(startSelection,endSelection);
344
	}
345
	
346
	public void testCommentLastCharacterNoEOL() throws Exception {
347
		String before = "int i;";
348
		String after = "/*int i;*/";
349
		LinePosition startSelection = new LinePosition(1,1,fDocument);
350
		LinePosition endSelection   = new LinePosition(1,7,fDocument);
351
		assertFormatterResult(startSelection,endSelection,before,after);
352
	}
353
	
354
	public void testCommentLastCppCommentNoEOL() throws Exception {
355
		String before = "//int i;";
356
		String after = "/*//int i;*/";
357
		LinePosition startSelection = new LinePosition(1,3,fDocument);
358
		LinePosition endSelection   = new LinePosition(1,7,fDocument);
359
		assertFormatterResult(startSelection,endSelection,before,after);
360
	}
361
	
362
	public void testMixedEOLs() throws Exception {
363
		String before =
364
			  "int i;\r\n"
365
			+ "int j;\n"
366
			+ "int k;";
367
		String after =
368
			  "/*\r\n"
369
			+ "int i;\r\n"
370
			+ "int j;\n"
371
			+ "*/\n"
372
			+ "int k;";
373
		LinePosition startSelection = new LinePosition(1,1,fDocument);
374
		LinePosition endSelection   = new LinePosition(3,1,fDocument);
375
		assertFormatterResult(startSelection,endSelection,before,after);
376
	}
377
}
(-)ui/org/eclipse/cdt/ui/tests/text/RemoveBlockCommentTest.java (+340 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 Wind River Systems, Inc. and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Anton Leherbauer (Wind River Systems) - initial API and implementation
10
 *     Andrew Gvozdev
11
 *******************************************************************************/
12
package org.eclipse.cdt.ui.tests.text;
13
14
import junit.framework.TestSuite;
15
16
import org.eclipse.jface.action.IAction;
17
import org.eclipse.jface.text.IDocument;
18
19
import org.eclipse.cdt.core.CCorePlugin;
20
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
21
import org.eclipse.cdt.core.model.ICProject;
22
import org.eclipse.cdt.core.testplugin.CProjectHelper;
23
import org.eclipse.cdt.ui.tests.BaseUITestCase;
24
import org.eclipse.cdt.ui.tests.text.AddBlockCommentTest.LinePosition;
25
26
import org.eclipse.cdt.internal.core.model.ext.SourceRange;
27
28
import org.eclipse.cdt.internal.ui.editor.CEditor;
29
30
/**
31
 * Tests for the RemoveBlockCommentAction.
32
 *
33
 * @since 5.0
34
 */
35
public class RemoveBlockCommentTest extends BaseUITestCase {
36
	private ICProject fCProject;
37
	public static TestSuite suite() {
38
		return suite(RemoveBlockCommentTest.class, "_");
39
	}
40
41
	private CEditor fEditor;
42
	private IDocument fDocument;
43
	
44
	@Override
45
	protected void setUp() throws Exception {
46
		super.setUp();
47
		
48
		final String PROJECT= "BlockComment";
49
		// Using any existing file just to open CEditor, the content is not used
50
		final String filename= "/BlockComment/src/sample/Before.cpp";
51
		fCProject= EditorTestHelper.createCProject(PROJECT, "resources/formatter");
52
		fCProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.TAB);
53
		fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(filename), true);
54
55
		fDocument= EditorTestHelper.getSourceViewer(fEditor).getDocument();
56
		// Delete contents
57
		fDocument.set("");
58
	}
59
60
	@Override
61
	protected void tearDown() throws Exception {
62
		EditorTestHelper.closeEditor(fEditor);
63
64
		if (fCProject != null)
65
			CProjectHelper.delete(fCProject);
66
		
67
		super.tearDown();
68
	}
69
70
	/**
71
	 * Run an action to comment block defined by line positions
72
	 * and assert that the result matches the expected result.
73
	 * "Before" and "After" are taken from test comments.
74
	 * 
75
	 * @param startLinePosition
76
	 * @param endLinePosition
77
	 * @throws Exception
78
	 */
79
	protected void assertFormatterResult(
80
			LinePosition startLinePosition,
81
			LinePosition endLinePosition) throws Exception {
82
83
		StringBuffer[] contents= getContentsForTest(2);
84
		String before = contents[0].toString();
85
		String after  = contents[1].toString();
86
		
87
		fDocument.set(before);
88
89
		SourceRange range = new SourceRange( startLinePosition.getOffset(),
90
				endLinePosition.getOffset()-startLinePosition.getOffset() );
91
		fEditor.setSelection(range, true);
92
		
93
		IAction commentAction= fEditor.getAction("RemoveBlockComment");
94
		assertNotNull("No RemoveBlockComment action", commentAction);
95
		commentAction.setEnabled(true);
96
		commentAction.run();
97
		
98
		String expected= after;
99
		assertEquals(expected, fDocument.get());
100
	}
101
	
102
	/**
103
	 * Run an action to comment block defined by line positions
104
	 * and assert that the result matches the expected result.
105
	 * "Before" and "After" are taken from test comments.
106
	 * 
107
	 * @param startLinePosition
108
	 * @param endLinePosition
109
	 * @param before editor contents before the operation
110
	 * @param after expected editor contents after the operation
111
	 * @throws Exception
112
	 */
113
	protected void assertFormatterResult(
114
			LinePosition startLinePosition,
115
			LinePosition endLinePosition,
116
			String before,
117
			String after) throws Exception {
118
		
119
		fDocument.set(before);
120
		
121
		SourceRange range = new SourceRange( startLinePosition.getOffset(),
122
				endLinePosition.getOffset()-startLinePosition.getOffset() );
123
		fEditor.setSelection(range, true);
124
		
125
		IAction commentAction= fEditor.getAction("RemoveBlockComment");
126
		assertNotNull("No RemoveBlockComment action", commentAction);
127
		commentAction.setEnabled(true);
128
		commentAction.run();
129
		
130
		String expected= after;
131
		assertEquals(expected, fDocument.get());
132
	}
133
134
	
135
	//int i, /*j,*/ k;
136
	
137
	//int i, j, k;
138
	public void testUncommentPlain() throws Exception {
139
		LinePosition startSelection = new LinePosition(1,10,fDocument);
140
		LinePosition endSelection   = new LinePosition(1,11,fDocument);
141
		assertFormatterResult(startSelection,endSelection);
142
	}
143
	
144
	//int /*j,*/ k;
145
	
146
	//int j, k;
147
	public void testRightBorderIn() throws Exception {
148
		LinePosition startSelection = new LinePosition(1,10,fDocument);
149
		LinePosition endSelection   = new LinePosition(1,11,fDocument);
150
		assertFormatterResult(startSelection,endSelection);
151
	}
152
	
153
	//int /*j,*/ k;
154
	
155
	//int /*j,*/ k;
156
	public void testRightBorderOut() throws Exception {
157
		LinePosition startSelection = new LinePosition(1,11,fDocument);
158
		LinePosition endSelection   = new LinePosition(1,12,fDocument);
159
		assertFormatterResult(startSelection,endSelection);
160
	}
161
	//123456789-123
162
	
163
	//int /*j,*/ k;
164
	
165
	//int j, k;
166
	public void testLeftBorderIn() throws Exception {
167
		LinePosition startSelection = new LinePosition(1,5,fDocument);
168
		LinePosition endSelection   = new LinePosition(1,6,fDocument);
169
		assertFormatterResult(startSelection,endSelection);
170
	}
171
	
172
	//int /*j,*/ k;
173
	
174
	//int /*j,*/ k;
175
	public void testLeftBorderOut() throws Exception {
176
		LinePosition startSelection = new LinePosition(1,4,fDocument);
177
		LinePosition endSelection   = new LinePosition(1,5,fDocument);
178
		assertFormatterResult(startSelection,endSelection);
179
	}
180
	
181
	//int /*i,
182
	//    j,*/
183
	//    k;
184
	
185
	//int i,
186
	//    j,
187
	//    k;
188
	public void testUncommentPartialLines1() throws Exception {
189
		LinePosition startSelection = new LinePosition(1,7,fDocument);
190
		LinePosition endSelection   = new LinePosition(2,2,fDocument);
191
		assertFormatterResult(startSelection,endSelection);
192
	}
193
	
194
	//int i,
195
	///*    j,
196
	//    k*/;
197
	
198
	//int i,
199
	//    j,
200
	//    k;
201
	public void testUncommentPartialLines2() throws Exception {
202
		LinePosition startSelection = new LinePosition(2,1,fDocument);
203
		LinePosition endSelection   = new LinePosition(3,8,fDocument);
204
		assertFormatterResult(startSelection,endSelection);
205
	}
206
207
	//int i;
208
	///*int j;*/
209
	//int k;
210
	
211
	//int i;
212
	//int j;
213
	//int k;
214
	public void testUncommentExactlyOneLineNoLoopingPlease() throws Exception {
215
		LinePosition startSelection = new LinePosition(2,1,fDocument);
216
		LinePosition endSelection   = new LinePosition(3,1,fDocument);
217
		assertFormatterResult(startSelection,endSelection);
218
	}
219
	
220
	//int i;
221
	///*
222
	//int j;
223
	//int k;
224
	//*/
225
	//int l;
226
	
227
	//int i;
228
	//int j;
229
	//int k;
230
	//int l;
231
	public void testUncommentTwoOrMoreLines() throws Exception {
232
		LinePosition startSelection = new LinePosition(2,1,fDocument);
233
		LinePosition endSelection   = new LinePosition(4,1,fDocument);
234
		assertFormatterResult(startSelection,endSelection);
235
	}
236
	
237
	///*const*/ int i;
238
	
239
	//const int i;
240
	public void testUncommentFirstCharacterInFile() throws Exception {
241
		LinePosition startSelection = new LinePosition(1,1,fDocument);
242
		LinePosition endSelection   = new LinePosition(1,6,fDocument);
243
		assertFormatterResult(startSelection,endSelection);
244
	}
245
	
246
	///*#include <x>*/
247
	//#include <y>
248
	
249
	//#include <x>
250
	//#include <y>
251
	public void testUncommentPreprocessorFirstLine() throws Exception {
252
		LinePosition startSelection = new LinePosition(1,1,fDocument);
253
		LinePosition endSelection   = new LinePosition(1,6,fDocument);
254
		assertFormatterResult(startSelection,endSelection);
255
	}
256
	
257
	//int i; /*// comment*/
258
	//int j;
259
	
260
	//int i; // comment
261
	//int j;
262
	public void testUncommentCppComment() throws Exception {
263
		LinePosition startSelection = new LinePosition(1,10,fDocument);
264
		LinePosition endSelection   = new LinePosition(1,12,fDocument);
265
		assertFormatterResult(startSelection,endSelection);
266
	}
267
268
	//#include <x>
269
	///*#include <y>*/
270
	//#include <z>
271
	
272
	//#include <x>
273
	//#include <y>
274
	//#include <z>
275
	public void testUncommentSpecialPartition() throws Exception {
276
		LinePosition startSelection = new LinePosition(2,1,fDocument);
277
		LinePosition endSelection   = new LinePosition(3,1,fDocument);
278
		assertFormatterResult(startSelection,endSelection);
279
	}
280
	
281
	//#include <x>
282
	///*
283
	//   #include \
284
	//      <y>
285
	//*/
286
	//#include <z>
287
	
288
	//#include <x>
289
	//   #include \
290
	//      <y>
291
	//#include <z>
292
	public void testUncommentSpecialPartitionExtra() throws Exception {
293
		LinePosition startSelection = new LinePosition(3,8,fDocument);
294
		LinePosition endSelection   = new LinePosition(3,10,fDocument);
295
		assertFormatterResult(startSelection,endSelection);
296
	}
297
	
298
	///*int i;*/
299
	///*int j;*/
300
	//int k;
301
	
302
	//int i;
303
	//int j;
304
	//int k;
305
	public void testUncommentConnectedComments() throws Exception {
306
		LinePosition startSelection = new LinePosition(1,5,fDocument);
307
		LinePosition endSelection   = new LinePosition(2,5,fDocument);
308
		assertFormatterResult(startSelection,endSelection);
309
	}
310
	
311
	//
312
	///*
313
	//
314
	//*/
315
	
316
	//
317
	//
318
	public void testUncommentEndOfFile() throws Exception {
319
		LinePosition startSelection = new LinePosition(3,1,fDocument);
320
		LinePosition endSelection   = new LinePosition(5,2,fDocument);
321
		assertFormatterResult(startSelection,endSelection);
322
	}
323
	
324
	public void testMixedEOLs() throws Exception {
325
		String before =
326
			  "/*\r\n"
327
			+ "int i;\r\n"
328
			+ "int j;\n"
329
			+ "*/\n"
330
			+ "int k;";
331
		String after =
332
			  "int i;\r\n"
333
			+ "int j;\n"
334
			+ "int k;";
335
		LinePosition startSelection = new LinePosition(2,1,fDocument);
336
		LinePosition endSelection   = new LinePosition(3,1,fDocument);
337
		assertFormatterResult(startSelection,endSelection,before,after);
338
	}
339
	
340
}
(-)src/org/eclipse/cdt/internal/ui/actions/AddBlockCommentAction.java (-81 / +112 lines)
Lines 16-34 Link Here
16
import java.util.List;
16
import java.util.List;
17
import java.util.ResourceBundle;
17
import java.util.ResourceBundle;
18
18
19
import org.eclipse.core.runtime.Assert;
20
import org.eclipse.jface.text.BadLocationException;
19
import org.eclipse.jface.text.BadLocationException;
21
import org.eclipse.jface.text.BadPartitioningException;
20
import org.eclipse.jface.text.BadPartitioningException;
22
import org.eclipse.jface.text.IDocument;
21
import org.eclipse.jface.text.IDocument;
23
import org.eclipse.jface.text.IDocumentExtension3;
22
import org.eclipse.jface.text.IDocumentExtension3;
23
import org.eclipse.jface.text.IRegion;
24
import org.eclipse.jface.text.ITextSelection;
24
import org.eclipse.jface.text.ITextSelection;
25
import org.eclipse.jface.text.ITypedRegion;
25
import org.eclipse.jface.text.ITypedRegion;
26
import org.eclipse.jface.text.Region;
26
import org.eclipse.ui.texteditor.ITextEditor;
27
import org.eclipse.ui.texteditor.ITextEditor;
27
28
28
import org.eclipse.cdt.ui.text.ICPartitions;
29
import org.eclipse.cdt.ui.text.ICPartitions;
29
30
30
/**
31
/**
31
 * Action that encloses the editor's current selection with Java block comment terminators
32
 * Action that encloses the editor's current selection with Java/C block comment terminators
32
 * (<code>&#47;&#42;</code> and <code>&#42;&#47;</code>).
33
 * (<code>&#47;&#42;</code> and <code>&#42;&#47;</code>).
33
 * 
34
 * 
34
 * @since 3.0
35
 * @since 3.0
Lines 51-154 Link Here
51
	 * @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#runInternal(org.eclipse.jface.text.ITextSelection, org.eclipse.jface.text.IDocumentExtension3, org.eclipse.jdt.internal.ui.actions.BlockCommentAction.Edit.EditFactory)
52
	 * @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#runInternal(org.eclipse.jface.text.ITextSelection, org.eclipse.jface.text.IDocumentExtension3, org.eclipse.jdt.internal.ui.actions.BlockCommentAction.Edit.EditFactory)
52
	 */
53
	 */
53
	@Override
54
	@Override
54
	protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadLocationException, BadPartitioningException {
55
	protected void runInternal(ITextSelection selection,
55
		int selectionOffset= selection.getOffset();
56
			IDocumentExtension3 docExtension, Edit.EditFactory factory)
56
		int selectionEndOffset= selectionOffset + selection.getLength();
57
			throws BadLocationException, BadPartitioningException {
58
		
59
		if ( !(docExtension instanceof IDocument) ) return;
60
		
57
		List<Edit> edits= new LinkedList<Edit>();
61
		List<Edit> edits= new LinkedList<Edit>();
58
		ITypedRegion partition= docExtension.getPartition(ICPartitions.C_PARTITIONING, selectionOffset, false);
59
60
		handleFirstPartition(partition, edits, factory, selectionOffset);
61
62
62
		while (partition.getOffset() + partition.getLength() < selectionEndOffset) {
63
		ITypedRegion firstPartition = docExtension.getPartition(ICPartitions.C_PARTITIONING,
63
			partition= handleInteriorPartition(partition, edits, factory, docExtension);
64
				selection.getOffset(), false);
65
		ITypedRegion lastPartition = docExtension.getPartition(ICPartitions.C_PARTITIONING,
66
				selection.getOffset() + selection.getLength() - 1, false);
67
		
68
		int commentAreaStart = selection.getOffset();
69
		int commentAreaEnd = selection.getOffset()+selection.getLength();
70
		// Include special partitions fully in the comment area
71
		if (isSpecialPartition(firstPartition.getType())) {
72
			commentAreaStart = firstPartition.getOffset();
64
		}
73
		}
74
		if (isSpecialPartition(lastPartition.getType())) {
75
			commentAreaEnd = lastPartition.getOffset() + lastPartition.getLength();
76
		}
77
		Region estimatedCommentArea = new Region(commentAreaStart,commentAreaEnd-commentAreaStart);
78
65
		
79
		
66
		handleLastPartition(partition, edits, factory, selectionEndOffset);
80
		Region commentArea = handleEnclosingPartitions(estimatedCommentArea, lastPartition,
81
				(IDocument)docExtension, factory, edits);
82
83
		handleInteriorPartition(commentArea, firstPartition, docExtension, factory, edits);
67
		
84
		
68
		executeEdits(edits);
85
		executeEdits(edits);
69
	}
86
	}
70
87
71
	/**
88
	/**
72
	 * Handle the first partition of the selected text.
89
	 * Add enclosing comment tags for the whole area to be commented
73
	 * 
90
	 * 
74
	 * @param partition
91
	 * @param commentArea initial comment area which can be adjusted
75
	 * @param edits
92
	 * @param lastPartition last partition
76
	 * @param factory
93
	 * @param doc document
77
	 * @param offset
94
	 * @param factory Edit factory
78
	 */
95
	 * @param edits List of edits to update the document
79
	private void handleFirstPartition(ITypedRegion partition, List<Edit> edits, Edit.EditFactory factory, int offset) throws BadLocationException {
96
	 * @return new possibly adjusted comment area
80
		
97
	 * @throws BadLocationException
81
		int partOffset= partition.getOffset();
98
	 */
82
		String partType= partition.getType();
99
	private Region handleEnclosingPartitions(Region commentArea,
83
		
100
			ITypedRegion lastPartition, IDocument doc, Edit.EditFactory factory,
84
		Assert.isTrue(partOffset <= offset, "illegal partition"); //$NON-NLS-1$
101
			List<Edit> edits) throws BadLocationException {
85
		
102
		
86
		// first partition: mark start of comment
103
		int commentAreaStart = commentArea.getOffset();
87
		if (partType == IDocument.DEFAULT_CONTENT_TYPE) {
104
		int commentAreaEnd = commentArea.getOffset() + commentArea.getLength();
88
			// Java code: right where selection starts
105
		
89
			edits.add(factory.createEdit(offset, 0, getCommentStart()));
106
		String commentStartTag = getCommentStart(); // "/*"
90
		} else if (isSpecialPartition(partType)) {
107
		String commentEndTag   = getCommentEnd();   // "*/"
91
			// special types: include the entire partition
108
		
92
			edits.add(factory.createEdit(partOffset, 0, getCommentStart()));
109
		String startLineEOL = doc.getLineDelimiter(doc.getLineOfOffset(commentAreaStart));
93
		}	// javadoc: no mark, will only start after comment
110
		if (startLineEOL==null) startLineEOL=""; //$NON-NLS-1$
111
		String endLineEOL = doc.getLineDelimiter(doc.getLineOfOffset(commentAreaEnd-1));
112
		if (endLineEOL==null) endLineEOL=""; //$NON-NLS-1$
113
		
114
		boolean isLeftEol = commentAreaStart<startLineEOL.length()
115
			|| doc.get(commentAreaStart-startLineEOL.length(),startLineEOL.length()).equals(startLineEOL);
116
		boolean isRightEol = doc.get(commentAreaEnd-endLineEOL.length(),endLineEOL.length()).equals(endLineEOL);
117
		
118
		if (isLeftEol && isRightEol) {
119
			// Block of full lines found
120
			int areaStartLine = doc.getLineOfOffset(commentAreaStart+startLineEOL.length());
121
			int areaEndLine = doc.getLineOfOffset(commentAreaEnd-endLineEOL.length());
122
			if (areaStartLine!=areaEndLine) {
123
				// If multiple full lines arrange inserting comment tags on their own lines
124
				commentStartTag = getCommentStart()+startLineEOL;
125
				commentEndTag   = getCommentEnd()+endLineEOL;
126
			} else {
127
				// If one full line insert end comment tag on the same line (before the EOL)
128
				commentAreaEnd = commentAreaEnd-endLineEOL.length();
129
			}
130
		} else {
131
			if (lastPartition.getType()==ICPartitions.C_SINGLE_LINE_COMMENT) {
132
				// C++ comments "//" partition ends with EOL, insert end comment tag before it
133
				// on the same line, so we get something like /*// text*/
134
				commentAreaEnd = commentAreaEnd-endLineEOL.length();
135
			}
136
		}
137
		
138
		edits.add(factory.createEdit(commentAreaStart, 0, commentStartTag));
139
		edits.add(factory.createEdit(commentAreaEnd, 0, commentEndTag));
94
		
140
		
141
		return new Region(commentAreaStart,commentAreaEnd-commentAreaStart);
95
	}
142
	}
96
143
97
	/**
144
	/**
98
	 * Handles the end of the given partition and the start of the next partition, which is returned.
145
	 * Make all inside partitions join in one comment, in particular remove
146
	 * all enclosing comment tokens of the inside partitions.
99
	 * 
147
	 * 
100
	 * @param partition
148
	 * @param commentArea comment area region
101
	 * @param edits
149
	 * @param partition first partition
102
	 * @param factory
150
	 * @param docExtension document
103
	 * @param docExtension
151
	 * @param factory EditFactory
152
	 * @param List of edits to update the document
104
	 * @throws BadLocationException
153
	 * @throws BadLocationException
105
	 * @throws BadPartitioningException
154
	 * @throws BadPartitioningException
106
	 * @return the region
107
	 */
155
	 */
108
	private ITypedRegion handleInteriorPartition(ITypedRegion partition, List<Edit> edits, Edit.EditFactory factory, IDocumentExtension3 docExtension) throws BadPartitioningException, BadLocationException {
156
	private void handleInteriorPartition(IRegion commentArea,
109
157
			ITypedRegion partition, IDocumentExtension3 docExtension,
110
		// end of previous partition
158
			Edit.EditFactory factory, List<Edit> edits)
111
		String partType= partition.getType();
159
			throws BadLocationException, BadPartitioningException {
112
		int partEndOffset= partition.getOffset() + partition.getLength();
160
		
113
		int tokenLength= getCommentStart().length();
161
		int commentAreaEnd = commentArea.getOffset() + commentArea.getLength();
114
		
162
		int prevPartitionEnd = -1;
115
		if (partType == ICPartitions.C_MULTI_LINE_COMMENT) {	
163
		int partitionEnd = partition.getOffset()+partition.getLength();
116
			// already in a comment - remove ending mark
164
		
117
			edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$	
165
		final int startCommentTokenLength = getCommentStart().length();
118
		}
166
		final int endCommentTokenLength = getCommentEnd().length();
119
167
		
120
		// advance to next partition
168
		while (partitionEnd<=commentAreaEnd) {
121
		partition= docExtension.getPartition(ICPartitions.C_PARTITIONING, partEndOffset, false);
169
			if (partition.getType() == ICPartitions.C_MULTI_LINE_COMMENT) {	
122
		partType= partition.getType();
170
				// already in a comment - remove start/end tokens
123
171
				edits.add(factory.createEdit(partition.getOffset(), startCommentTokenLength, "")); //$NON-NLS-1$
124
		// start of next partition		
172
				edits.add(factory.createEdit(partitionEnd - endCommentTokenLength, endCommentTokenLength, "")); //$NON-NLS-1$	
125
		if (partType == ICPartitions.C_MULTI_LINE_COMMENT) {
173
			}
126
			// already in a comment - remove startToken
174
			// advance to next partition
127
			edits.add(factory.createEdit(partition.getOffset(), getCommentStart().length(), "")); //$NON-NLS-1$
175
			prevPartitionEnd = partitionEnd;
176
			partition= docExtension.getPartition(ICPartitions.C_PARTITIONING, partitionEnd, false);
177
			partitionEnd = partition.getOffset() + partition.getLength();
178
			
179
			// break the loop if we get stuck and no advance was made
180
			if (partitionEnd<=prevPartitionEnd) break;
128
		}
181
		}
129
		return partition;
130
	}
131
132
	/**
133
	 * Handles the end of the last partition.
134
	 * 
135
	 * @param partition
136
	 * @param edits
137
	 * @param factory
138
	 * @param endOffset
139
	 */
140
	private void handleLastPartition(ITypedRegion partition, List<Edit> edits, Edit.EditFactory factory, int endOffset) throws BadLocationException {
141
142
		String partType= partition.getType();
143
		
144
		if (partType == IDocument.DEFAULT_CONTENT_TYPE) {
145
			// normal java: end comment where selection ends
146
			edits.add(factory.createEdit(endOffset, 0, getCommentEnd()));
147
		} else if (isSpecialPartition(partType)) {
148
			// special types: consume entire partition
149
			edits.add(factory.createEdit(partition.getOffset() + partition.getLength(), 0, getCommentEnd()));
150
		}
151
		
152
	}
182
	}
153
183
154
	/**
184
	/**
Lines 162-167 Link Here
162
		return partType == ICPartitions.C_CHARACTER
192
		return partType == ICPartitions.C_CHARACTER
163
				|| partType == ICPartitions.C_STRING
193
				|| partType == ICPartitions.C_STRING
164
				|| partType == ICPartitions.C_SINGLE_LINE_COMMENT
194
				|| partType == ICPartitions.C_SINGLE_LINE_COMMENT
195
				|| partType == ICPartitions.C_MULTI_LINE_COMMENT
165
				|| partType == ICPartitions.C_PREPROCESSOR;
196
				|| partType == ICPartitions.C_PREPROCESSOR;
166
	}
197
	}
167
198
(-)src/org/eclipse/cdt/internal/ui/actions/RemoveBlockCommentAction.java (-20 / +53 lines)
Lines 17-23 Link Here
17
17
18
import org.eclipse.jface.text.BadLocationException;
18
import org.eclipse.jface.text.BadLocationException;
19
import org.eclipse.jface.text.BadPartitioningException;
19
import org.eclipse.jface.text.BadPartitioningException;
20
import org.eclipse.jface.text.IDocument;
20
import org.eclipse.jface.text.IDocumentExtension3;
21
import org.eclipse.jface.text.IDocumentExtension3;
22
import org.eclipse.jface.text.IRegion;
21
import org.eclipse.jface.text.ITextSelection;
23
import org.eclipse.jface.text.ITextSelection;
22
import org.eclipse.jface.text.ITypedRegion;
24
import org.eclipse.jface.text.ITypedRegion;
23
import org.eclipse.ui.texteditor.ITextEditor;
25
import org.eclipse.ui.texteditor.ITextEditor;
Lines 25-31 Link Here
25
import org.eclipse.cdt.ui.text.ICPartitions;
27
import org.eclipse.cdt.ui.text.ICPartitions;
26
28
27
/**
29
/**
28
 * Action that removes the enclosing comment marks from a Java block comment.
30
 * Action that removes the enclosing comment marks from a Java/C block comment.
29
 * 
31
 * 
30
 * @since 3.0
32
 * @since 3.0
31
 */
33
 */
Lines 49-82 Link Here
49
	 */
51
	 */
50
	@Override
52
	@Override
51
	protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadPartitioningException, BadLocationException {
53
	protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadPartitioningException, BadLocationException {
54
		if ( !(docExtension instanceof IDocument) ) return;
55
52
		List<Edit> edits= new LinkedList<Edit>();
56
		List<Edit> edits= new LinkedList<Edit>();
53
		int tokenLength= getCommentStart().length();
54
		
57
		
55
		int offset= selection.getOffset();
58
		int partitionStart = -1;
56
		int endOffset= offset + selection.getLength();
59
		int partitionEnd   = selection.getOffset();
57
60
58
		ITypedRegion partition= docExtension.getPartition(ICPartitions.C_PARTITIONING, offset, false);
61
		do {
59
		int partOffset= partition.getOffset();
62
			ITypedRegion partition = docExtension.getPartition(ICPartitions.C_PARTITIONING, partitionEnd, false);
60
		int partEndOffset= partOffset + partition.getLength();
63
			if (partition.getOffset() <= partitionStart) {
61
		
64
				// If we did not advance break the loop
62
		while (partEndOffset < endOffset) {
65
				break;
63
			
66
			}
67
			partitionStart = partition.getOffset();
68
			partitionEnd   = partitionStart + partition.getLength();
64
			if (partition.getType() == ICPartitions.C_MULTI_LINE_COMMENT) {
69
			if (partition.getType() == ICPartitions.C_MULTI_LINE_COMMENT) {
65
				edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
70
				uncommentPartition((IDocument)docExtension, factory, edits, partitionStart, partitionEnd);
66
				edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
71
			}
72
		} while (partitionEnd < selection.getOffset()+selection.getLength());
73
74
		executeEdits(edits);
75
	}
76
77
	private void uncommentPartition(IDocument doc, Edit.EditFactory factory,
78
			List<Edit> edits, int partitionStart, int partitionEnd)
79
			throws BadLocationException {
80
		
81
		int startCommentTokenLength = getCommentStart().length();
82
		int endCommentTokenLength = getCommentEnd().length();
83
84
		// Remove whole line (with EOL) if it contains start or end comment tag
85
		// and nothing else
86
		if (partitionStart >= 0) {
87
			IRegion lineRegion = doc.getLineInformationOfOffset(partitionStart);
88
			String lineContent = doc.get(lineRegion.getOffset(), lineRegion.getLength());
89
			// start comment tag '/*'
90
			if (lineContent.equals(getCommentStart())) {
91
				String eol = doc.getLineDelimiter(doc.getLineOfOffset(partitionStart));
92
				if (eol!=null) {
93
					startCommentTokenLength = startCommentTokenLength+eol.length();
94
				}
67
			}
95
			}
68
			
69
			partition= docExtension.getPartition(ICPartitions.C_PARTITIONING, partEndOffset, false);
70
			partOffset= partition.getOffset();
71
			partEndOffset= partOffset + partition.getLength();
72
		}
96
		}
73
97
74
		if (partition.getType() == ICPartitions.C_MULTI_LINE_COMMENT) {
98
		int commentContentEnd = partitionEnd - endCommentTokenLength;
75
			edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
99
		if (partitionEnd < doc.getLength()) {
76
			edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
100
			IRegion lineRegion = doc.getLineInformationOfOffset(partitionEnd);
101
			String lineContent = doc.get(lineRegion.getOffset(), lineRegion.getLength());
102
			// end comment tag '*/'
103
			if (lineContent.equals(getCommentEnd())) {
104
				String eol = doc.getLineDelimiter(doc.getLineOfOffset(partitionEnd));
105
				if (eol!=null) {
106
					endCommentTokenLength = endCommentTokenLength + eol.length();
107
				}
108
			}
77
		}
109
		}
78
110
79
		executeEdits(edits);
111
		edits.add(factory.createEdit(partitionStart, startCommentTokenLength, "")); //$NON-NLS-1$
112
		edits.add(factory.createEdit(commentContentEnd, endCommentTokenLength, "")); //$NON-NLS-1$
80
	}
113
	}
81
114
82
	/*
115
	/*

Return to bug 236160