Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3be90fa421ea3621d1966d5cc10049b4d926a1ed (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*******************************************************************************
 * 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.ui.texteditor;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

import org.eclipse.swt.SWTError;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextListener;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextEvent;

import org.eclipse.ui.internal.texteditor.TextEditorPlugin;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;


/**
 * A delete line target.
 * @since 2.1
 */
class DeleteLineTarget {

	/**
	 * A clipboard which concatenates subsequent delete line actions.
	 */
	private static class DeleteLineClipboard implements MouseListener, ModifyListener, ISelectionChangedListener, ITextListener, FocusListener {

		/** The text viewer. */
		private final ITextViewer fViewer;
		/*
		 * This is a hack to stop a string of deletions when the user moves
		 * the caret. This kludge is necessary since:
		 * 1) Moving the caret does not fire a selection event
		 * 2) There is no support in StyledText for a CaretListener
		 * 3) The AcceleratorScope and KeybindingService classes are internal
		 *
		 * This kludge works by comparing the offset of the caret to the offset
		 * recorded the last time the action was run. If they differ, we do not
		 * continue the session.
		 *
		 * @see #saveState
		 * @see #checkState
		 */
		/** The last known offset of the caret */
		private int fIndex= -1;
		/** The clip board. */
		private Clipboard fClipboard;
		/** A string buffer. */
		private final StringBuffer fBuffer= new StringBuffer();
		/** The delete flag indicates if a deletion is in progress. */
		private boolean fDeleting;

		/**
		 * Creates the clipboard.
		 *
		 * @param viewer the text viewer
		 */
		public DeleteLineClipboard(ITextViewer viewer) {
			Assert.isNotNull(viewer);
			fViewer= viewer;
		}

		/**
		 * Returns the text viewer.
		 *
		 * @return the text viewer
		 */
		public ITextViewer getViewer() {
			return fViewer;
		}

		/**
		 * Saves the current state, to be compared later using
		 * <code>checkState</code>.
		 */
		private void saveState() {
			fIndex= fViewer.getTextWidget().getCaretOffset();
		}

		/**
		 * Checks that the state has not changed since it was saved.
		 *
		 * @return returns <code>true</code> if the current state is the same as
		 * when it was last saved.
		 */
		private boolean hasSameState() {
			return fIndex == fViewer.getTextWidget().getCaretOffset();
		}

		/**
		 * Checks the state of the clipboard.
		 */
		public void checkState() {

			if (fClipboard == null) {
				StyledText text= fViewer.getTextWidget();
				if (text == null)
					return;

				fViewer.getSelectionProvider().addSelectionChangedListener(this);
				text.addFocusListener(this);
				text.addMouseListener(this);
				text.addModifyListener(this);

				fClipboard= new Clipboard(text.getDisplay());
				fBuffer.setLength(0);

			} else if (!hasSameState()) {
				fBuffer.setLength(0);
			}
		}

		/**
		 * Appends the given string to this clipboard.
		 *
		 * @param deltaString the string to append
		 */
		public void append(String deltaString) {
			fBuffer.append(deltaString);
			String string= fBuffer.toString();
			Transfer[] dataTypes= new Transfer[] { TextTransfer.getInstance() };
			Object[] data= new Object[] { string };
			fClipboard.setContents(data, dataTypes);
		}

		/**
		 * Uninstalls this action.
		 */
		private void uninstall() {

			if (fClipboard == null)
				return;

			StyledText text= fViewer.getTextWidget();
			if (text == null)
				return;

			fViewer.getSelectionProvider().removeSelectionChangedListener(this);
			text.removeFocusListener(this);
			text.removeMouseListener(this);
			text.removeModifyListener(this);

			fClipboard.dispose();
			fClipboard= null;
		}

		/**
		 * Mark whether a deletion is in progress.
		 *
		 * @param deleting <code>true</code> if a deletion is in progress
		 */
		public void setDeleting(boolean deleting) {
			fDeleting= deleting;
		}

		/*
		 * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(MouseEvent)
		 */
		public void mouseDoubleClick(MouseEvent e) {
			uninstall();
		}

		/*
		 * @see org.eclipse.swt.events.MouseListener#mouseDown(MouseEvent)
		 */
		public void mouseDown(MouseEvent e) {
			uninstall();
		}

		/*
		 * @see org.eclipse.swt.events.MouseListener#mouseUp(MouseEvent)
		 */
		public void mouseUp(MouseEvent e) {
			uninstall();
		}

		/*
		 * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
		 */
		public void selectionChanged(SelectionChangedEvent event) {
			uninstall();
		}

		/*
		 * @see org.eclipse.swt.events.FocusListener#focusGained(FocusEvent)
		 */
		public void focusGained(FocusEvent e) {
			uninstall();
		}

		/*
		 * @see org.eclipse.swt.events.FocusListener#focusLost(FocusEvent)
		 */
		public void focusLost(FocusEvent e) {
			uninstall();
		}

		/*
		 * @see org.eclipse.jface.text.ITextListener#textChanged(TextEvent)
		 */
		public void textChanged(TextEvent event) {
			uninstall();
		}

		/*
		 * @see org.eclipse.swt.events.ModifyListener#modifyText(ModifyEvent)
		 */
		public void modifyText(ModifyEvent e) {
			if (!fDeleting)
				uninstall();
		}
	}

	/**
	 * The clipboard manager.
	 */
	private final DeleteLineClipboard fClipboard;

	/**
	 * Creates a new target.
	 *
	 * @param viewer the viewer that the new target operates on
	 */
	public DeleteLineTarget(ITextViewer viewer) {
		fClipboard= new DeleteLineClipboard(viewer);
	}

	/**
	 * Returns the document's delete region specified by position and type.
	 *
	 * @param document	the document
	 * @param offset the offset
	 * @param length the length
	 * @param type the line deletion type, must be one of
	 * 	<code>WHOLE_LINE</code>, <code>TO_BEGINNING</code> or <code>TO_END</code>
	 * @return the document's delete region
	 * @throws BadLocationException
	 */
	private static IRegion getDeleteRegion(IDocument document, int offset, int length, int type) throws BadLocationException {

		int line= document.getLineOfOffset(offset);
		int resultOffset= 0;
		int resultLength= 0;

		switch  (type) {
		case DeleteLineAction.WHOLE:
			resultOffset= document.getLineOffset(line);
			int endOffset= offset + length;
			IRegion endLineInfo= document.getLineInformationOfOffset(endOffset);
			int endLine= document.getLineOfOffset(endLineInfo.getOffset()); 
			if (endLineInfo.getOffset() == endOffset && endLine > 0 && length > 0)
				endLine= endLine - 1;
			resultLength= document.getLineOffset(endLine) + document.getLineLength(endLine) - resultOffset;
			break;

		case DeleteLineAction.TO_BEGINNING:
			resultOffset= document.getLineOffset(line);
			resultLength= offset - resultOffset;
			break;

		case DeleteLineAction.TO_END:
			resultOffset= offset;

			IRegion lineRegion= document.getLineInformation(line);
			int end= lineRegion.getOffset() + lineRegion.getLength();

			if (offset == end) {
				String lineDelimiter= document.getLineDelimiter(line);
				resultLength= lineDelimiter == null ? 0 : lineDelimiter.length();

			} else {
				resultLength= end - resultOffset;
			}
			break;

		default:
			throw new IllegalArgumentException();
		}

		return new Region(resultOffset, resultLength);
	}

	/**
	 * Deletes the specified fraction of the line of the given offset.
	 *
	 * @param document the document
	 * @param offset the offset
	 * @param length the length
	 * @param type the line deletion type, must be one of
	 * 	<code>WHOLE_LINE</code>, <code>TO_BEGINNING</code> or <code>TO_END</code>
	 * @param copyToClipboard <code>true</code> if the deleted line should be copied to the clipboard
	 * @throws BadLocationException if position is not valid in the given document
	 */
	public void deleteLine(IDocument document, int offset, int length, int type, boolean copyToClipboard) throws BadLocationException {

		IRegion deleteRegion= getDeleteRegion(document, offset, length, type);
		int deleteOffset= deleteRegion.getOffset();
		int deleteLength= deleteRegion.getLength();

		if (deleteLength == 0)
			return;

		if (copyToClipboard) {

			fClipboard.checkState();
			try {
				fClipboard.append(document.get(deleteOffset, deleteLength));
			} catch (SWTError e) {
				if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD)
					throw e;
				// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=59459
				// don't delete if copy to clipboard fails, rather log & abort

				// log
				Status status= new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, e.code, EditorMessages.Editor_error_clipboard_copy_failed_message, e);
				TextEditorPlugin.getDefault().getLog().log(status);

				fClipboard.uninstall();
				return; // don't delete
			}

			fClipboard.setDeleting(true);
			document.replace(deleteOffset, deleteLength, null);
			fClipboard.setDeleting(false);

			fClipboard.saveState();

		} else {
			document.replace(deleteOffset, deleteLength, null);
		}
	}
}

Back to the top