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 386472 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jface.text/src/org/eclipse/jface/internal/text/SelectionProcessor.java (-11 / +56 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2009, 2010 Avaloq Evolution AG and others.
2
 * Copyright (c) 2009, 2012 Avaloq Evolution AG and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 11-16 Link Here
11
package org.eclipse.jface.internal.text;
11
package org.eclipse.jface.internal.text;
12
12
13
import java.util.Arrays;
13
import java.util.Arrays;
14
15
import org.eclipse.swt.custom.StyledText;
16
import org.eclipse.swt.graphics.GC;
14
17
15
import org.eclipse.core.runtime.Assert;
18
import org.eclipse.core.runtime.Assert;
16
19
Lines 216-222 Link Here
216
	};
219
	};
217
220
218
	private final Implementation COLUMN_IMPLEMENTATION= new Implementation() {
221
	private final Implementation COLUMN_IMPLEMENTATION= new Implementation() {
219
		TextEdit replace(ISelection selection, String replacement) throws BadLocationException {
222
		private TextEdit replace(ISelection selection, String replacement, boolean delete) throws BadLocationException {
220
			try {
223
			try {
221
				MultiTextEdit root;
224
				MultiTextEdit root;
222
				IBlockTextSelection cts= (IBlockTextSelection)selection;
225
				IBlockTextSelection cts= (IBlockTextSelection)selection;
Lines 244-250 Link Here
244
							lastDelim= index[0] + delimiters[index[1]].length();
247
							lastDelim= index[0] + delimiters[index[1]].length();
245
						}
248
						}
246
					}
249
					}
247
					TextEdit replace= createReplaceEdit(line, visualStartColumn, visualEndColumn, string);
250
					TextEdit replace= createReplaceEdit(line, visualStartColumn, visualEndColumn, string, delete);
248
					root.addChild(replace);
251
					root.addChild(replace);
249
				}
252
				}
250
				while (lastDelim != -1) {
253
				while (lastDelim != -1) {
Lines 261-267 Link Here
261
					endLine++;
264
					endLine++;
262
					TextEdit edit;
265
					TextEdit edit;
263
					if (endLine < fDocument.getNumberOfLines()) {
266
					if (endLine < fDocument.getNumberOfLines()) {
264
						edit= createReplaceEdit(endLine, visualStartColumn, visualEndColumn, string);
267
						edit= createReplaceEdit(endLine, visualStartColumn, visualEndColumn, string, delete);
265
					} else {
268
					} else {
266
						// insertion reaches beyond the last line
269
						// insertion reaches beyond the last line
267
						int insertLocation= root.getExclusiveEnd();
270
						int insertLocation= root.getExclusiveEnd();
Lines 279-284 Link Here
279
				Assert.isTrue(false);
282
				Assert.isTrue(false);
280
				return null;
283
				return null;
281
			}
284
			}
285
		}
286
287
		TextEdit replace(ISelection selection, String replacement) throws BadLocationException {
288
			return replace(selection, replacement, false);
282
		}
289
		}
283
290
284
		String getText(ISelection selection) throws BadLocationException {
291
		String getText(ISelection selection) throws BadLocationException {
Lines 321-327 Link Here
321
				IBlockTextSelection cts= (IBlockTextSelection)selection;
328
				IBlockTextSelection cts= (IBlockTextSelection)selection;
322
				selection= new BlockTextSelection(fDocument, cts.getStartLine(), cts.getStartColumn(), cts.getEndLine(), cts.getEndColumn() + 1, fTabWidth);
329
				selection= new BlockTextSelection(fDocument, cts.getStartLine(), cts.getStartColumn(), cts.getEndLine(), cts.getEndColumn() + 1, fTabWidth);
323
			}
330
			}
324
			return replace(selection, ""); //$NON-NLS-1$
331
			return replace(selection, "", true); //$NON-NLS-1$
325
		}
332
		}
326
333
327
		TextEdit backspace(ISelection selection) throws BadLocationException {
334
		TextEdit backspace(ISelection selection) throws BadLocationException {
Lines 392-398 Link Here
392
			return ts.getEndLine() - ts.getStartLine() + 1;
399
			return ts.getEndLine() - ts.getStartLine() + 1;
393
		}
400
		}
394
401
395
		private TextEdit createReplaceEdit(int line, int visualStartColumn, int visualEndColumn, String replacement) throws BadLocationException {
402
		private TextEdit createReplaceEdit(int line, int visualStartColumn, int visualEndColumn, String replacement, boolean delete) throws BadLocationException {
396
			IRegion info= fDocument.getLineInformation(line);
403
			IRegion info= fDocument.getLineInformation(line);
397
			int lineLength= info.getLength();
404
			int lineLength= info.getLength();
398
			String content= fDocument.get(info.getOffset(), lineLength);
405
			String content= fDocument.get(info.getOffset(), lineLength);
Lines 400-410 Link Here
400
			int endColumn= -1;
407
			int endColumn= -1;
401
			int visual= 0;
408
			int visual= 0;
402
			for (int offset= 0; offset < lineLength; offset++) {
409
			for (int offset= 0; offset < lineLength; offset++) {
403
				if (startColumn == -1 && visual >= visualStartColumn)
410
				if (startColumn == -1) {
404
					startColumn= offset;
411
					if (visual == visualStartColumn)
405
				if (visual >= visualEndColumn) {
412
						if (!delete && isWider(content.charAt(offset), visual) && replacement.length() == 0)
406
					endColumn= offset;
413
							startColumn= offset - 1;
407
					break;
414
						else
415
							startColumn= offset;
416
					else if (visual > visualStartColumn) {
417
						if (isWider(content.charAt(offset - 1), visual))
418
							startColumn= offset - 1;
419
						else
420
							startColumn= offset;
421
					}
422
				}
423
				if (startColumn != -1) {
424
					if (visual == visualEndColumn) {
425
						endColumn= offset;
426
						break;
427
					} else if (visual > visualEndColumn) {
428
						if (!delete && isWider(content.charAt(offset - 1), visual))
429
							endColumn= offset - 1;
430
						else
431
							endColumn= offset;
432
						break;
433
					}
408
				}
434
				}
409
				visual+= visualSizeIncrement(content.charAt(offset), visual);
435
				visual+= visualSizeIncrement(content.charAt(offset), visual);
410
			}
436
			}
Lines 476-481 Link Here
476
			return lineLength + Math.max(0, visualColumn - visual);
502
			return lineLength + Math.max(0, visualColumn - visual);
477
		}
503
		}
478
504
505
		private boolean isWider(char character, int visual) {
506
			return visualSizeIncrement(character, visual) > 1;
507
		}
508
479
		/**
509
		/**
480
		 * Returns the increment in visual length represented by <code>character</code> given the
510
		 * Returns the increment in visual length represented by <code>character</code> given the
481
		 * current visual length. The visual length is <code>1</code> unless <code>character</code>
511
		 * current visual length. The visual length is <code>1</code> unless <code>character</code>
Lines 487-492 Link Here
487
		 *         <code>[0,fTabWidth]</code>
517
		 *         <code>[0,fTabWidth]</code>
488
		 */
518
		 */
489
		private int visualSizeIncrement(char character, int visual) {
519
		private int visualSizeIncrement(char character, int visual) {
520
			if (character > 255 && fStyledText != null) {
521
				GC gc= null;
522
				try {
523
					gc= new GC(fStyledText);
524
					int charWidth= gc.stringExtent(new String(Character.toString(character))).x;
525
					int singleCharWidth= gc.stringExtent(" ").x; //$NON-NLS-1$
526
					return (int)Math.ceil(charWidth / singleCharWidth);
527
				} finally {
528
					if (gc != null)
529
						gc.dispose();
530
				}
531
			}
490
			if (character != '\t')
532
			if (character != '\t')
491
				return 1;
533
				return 1;
492
			if (fTabWidth <= 0)
534
			if (fTabWidth <= 0)
Lines 503-508 Link Here
503
545
504
	private ISelectionProvider fSelectionProvider;
546
	private ISelectionProvider fSelectionProvider;
505
547
548
	private StyledText fStyledText;
549
506
	/**
550
	/**
507
	 * Creates a new processor on the given viewer.
551
	 * Creates a new processor on the given viewer.
508
	 * 
552
	 * 
Lines 515-520 Link Here
515
			fRewriteTarget= ext.getRewriteTarget();
559
			fRewriteTarget= ext.getRewriteTarget();
516
		}
560
		}
517
		fSelectionProvider= viewer.getSelectionProvider();
561
		fSelectionProvider= viewer.getSelectionProvider();
562
		fStyledText= viewer.getTextWidget();
518
	}
563
	}
519
564
520
	/**
565
	/**

Return to bug 386472