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 / +50 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.graphics.GC;
14
16
15
import org.eclipse.core.runtime.Assert;
17
import org.eclipse.core.runtime.Assert;
16
18
Lines 216-222 Link Here
216
	};
218
	};
217
219
218
	private final Implementation COLUMN_IMPLEMENTATION= new Implementation() {
220
	private final Implementation COLUMN_IMPLEMENTATION= new Implementation() {
219
		TextEdit replace(ISelection selection, String replacement) throws BadLocationException {
221
		private TextEdit replace(ISelection selection, String replacement, boolean delete) throws BadLocationException {
220
			try {
222
			try {
221
				MultiTextEdit root;
223
				MultiTextEdit root;
222
				IBlockTextSelection cts= (IBlockTextSelection)selection;
224
				IBlockTextSelection cts= (IBlockTextSelection)selection;
Lines 244-250 Link Here
244
							lastDelim= index[0] + delimiters[index[1]].length();
246
							lastDelim= index[0] + delimiters[index[1]].length();
245
						}
247
						}
246
					}
248
					}
247
					TextEdit replace= createReplaceEdit(line, visualStartColumn, visualEndColumn, string);
249
					TextEdit replace= createReplaceEdit(line, visualStartColumn, visualEndColumn, string, delete);
248
					root.addChild(replace);
250
					root.addChild(replace);
249
				}
251
				}
250
				while (lastDelim != -1) {
252
				while (lastDelim != -1) {
Lines 261-267 Link Here
261
					endLine++;
263
					endLine++;
262
					TextEdit edit;
264
					TextEdit edit;
263
					if (endLine < fDocument.getNumberOfLines()) {
265
					if (endLine < fDocument.getNumberOfLines()) {
264
						edit= createReplaceEdit(endLine, visualStartColumn, visualEndColumn, string);
266
						edit= createReplaceEdit(endLine, visualStartColumn, visualEndColumn, string, delete);
265
					} else {
267
					} else {
266
						// insertion reaches beyond the last line
268
						// insertion reaches beyond the last line
267
						int insertLocation= root.getExclusiveEnd();
269
						int insertLocation= root.getExclusiveEnd();
Lines 279-284 Link Here
279
				Assert.isTrue(false);
281
				Assert.isTrue(false);
280
				return null;
282
				return null;
281
			}
283
			}
284
		}
285
286
		TextEdit replace(ISelection selection, String replacement) throws BadLocationException {
287
			return replace(selection, replacement, false);
282
		}
288
		}
283
289
284
		String getText(ISelection selection) throws BadLocationException {
290
		String getText(ISelection selection) throws BadLocationException {
Lines 321-327 Link Here
321
				IBlockTextSelection cts= (IBlockTextSelection)selection;
327
				IBlockTextSelection cts= (IBlockTextSelection)selection;
322
				selection= new BlockTextSelection(fDocument, cts.getStartLine(), cts.getStartColumn(), cts.getEndLine(), cts.getEndColumn() + 1, fTabWidth);
328
				selection= new BlockTextSelection(fDocument, cts.getStartLine(), cts.getStartColumn(), cts.getEndLine(), cts.getEndColumn() + 1, fTabWidth);
323
			}
329
			}
324
			return replace(selection, ""); //$NON-NLS-1$
330
			return replace(selection, "", true); //$NON-NLS-1$
325
		}
331
		}
326
332
327
		TextEdit backspace(ISelection selection) throws BadLocationException {
333
		TextEdit backspace(ISelection selection) throws BadLocationException {
Lines 392-398 Link Here
392
			return ts.getEndLine() - ts.getStartLine() + 1;
398
			return ts.getEndLine() - ts.getStartLine() + 1;
393
		}
399
		}
394
400
395
		private TextEdit createReplaceEdit(int line, int visualStartColumn, int visualEndColumn, String replacement) throws BadLocationException {
401
		private TextEdit createReplaceEdit(int line, int visualStartColumn, int visualEndColumn, String replacement, boolean delete) throws BadLocationException {
396
			IRegion info= fDocument.getLineInformation(line);
402
			IRegion info= fDocument.getLineInformation(line);
397
			int lineLength= info.getLength();
403
			int lineLength= info.getLength();
398
			String content= fDocument.get(info.getOffset(), lineLength);
404
			String content= fDocument.get(info.getOffset(), lineLength);
Lines 400-410 Link Here
400
			int endColumn= -1;
406
			int endColumn= -1;
401
			int visual= 0;
407
			int visual= 0;
402
			for (int offset= 0; offset < lineLength; offset++) {
408
			for (int offset= 0; offset < lineLength; offset++) {
403
				if (startColumn == -1 && visual >= visualStartColumn)
409
				if (startColumn == -1) {
404
					startColumn= offset;
410
					if (visual == visualStartColumn)
405
				if (visual >= visualEndColumn) {
411
						if (!delete && isWider(content.charAt(offset), visual) && replacement.length() == 0)
406
					endColumn= offset;
412
							startColumn= offset - 1;
407
					break;
413
						else
414
							startColumn= offset;
415
					else if (visual > visualStartColumn) {
416
						if (isWider(content.charAt(offset - 1), visual))
417
							startColumn= offset - 1;
418
						else
419
							startColumn= offset;
420
					}
421
				}
422
				if (startColumn != -1) {
423
					if (visual == visualEndColumn) {
424
						endColumn= offset;
425
						break;
426
					} else if (visual > visualEndColumn) {
427
						if (!delete && isWider(content.charAt(offset - 1), visual))
428
							endColumn= offset - 1;
429
						else
430
							endColumn= offset;
431
						break;
432
					}
408
				}
433
				}
409
				visual+= visualSizeIncrement(content.charAt(offset), visual);
434
				visual+= visualSizeIncrement(content.charAt(offset), visual);
410
			}
435
			}
Lines 476-481 Link Here
476
			return lineLength + Math.max(0, visualColumn - visual);
501
			return lineLength + Math.max(0, visualColumn - visual);
477
		}
502
		}
478
503
504
		private boolean isWider(char character, int visual) {
505
			return visualSizeIncrement(character, visual) > 1;
506
		}
507
479
		/**
508
		/**
480
		 * Returns the increment in visual length represented by <code>character</code> given the
509
		 * 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>
510
		 * current visual length. The visual length is <code>1</code> unless <code>character</code>
Lines 487-492 Link Here
487
		 *         <code>[0,fTabWidth]</code>
516
		 *         <code>[0,fTabWidth]</code>
488
		 */
517
		 */
489
		private int visualSizeIncrement(char character, int visual) {
518
		private int visualSizeIncrement(char character, int visual) {
519
			if (fGC != null && character > 255)
520
				return (int)Math.ceil(fGC.stringExtent(new String(Character.toString(character))).x / fSingleCharWidth);
490
			if (character != '\t')
521
			if (character != '\t')
491
				return 1;
522
				return 1;
492
			if (fTabWidth <= 0)
523
			if (fTabWidth <= 0)
Lines 503-508 Link Here
503
534
504
	private ISelectionProvider fSelectionProvider;
535
	private ISelectionProvider fSelectionProvider;
505
536
537
	private GC fGC;
538
539
	float fSingleCharWidth;
540
541
506
	/**
542
	/**
507
	 * Creates a new processor on the given viewer.
543
	 * Creates a new processor on the given viewer.
508
	 * 
544
	 * 
Lines 510-515 Link Here
510
	 */
546
	 */
511
	public SelectionProcessor(ITextViewer viewer) {
547
	public SelectionProcessor(ITextViewer viewer) {
512
		this(viewer.getDocument(), viewer.getTextWidget().getTabs());
548
		this(viewer.getDocument(), viewer.getTextWidget().getTabs());
549
		fGC= new GC(viewer.getTextWidget());
550
		fSingleCharWidth= fGC.stringExtent(" ").x; //$NON-NLS-1$
551
513
		if (viewer instanceof ITextViewerExtension) {
552
		if (viewer instanceof ITextViewerExtension) {
514
			ITextViewerExtension ext= (ITextViewerExtension)viewer;
553
			ITextViewerExtension ext= (ITextViewerExtension)viewer;
515
			fRewriteTarget= ext.getRewriteTarget();
554
			fRewriteTarget= ext.getRewriteTarget();

Return to bug 386472