Skip to main content
summaryrefslogtreecommitdiffstats
blob: dc9d498d001beebbaacf97cad3818ec5111f1819 (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers 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
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.editors;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiImages;
import org.eclipse.mylyn.internal.tasks.ui.actions.CopyTaskDetailsAction;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskActivationHistory;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskElementLabelProvider;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.AbstractTask;
import org.eclipse.mylyn.tasks.core.TaskList;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditorInput;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

/**
 * @author Frank Becker
 * @author Steffen Pingel
 */
public class RepositoryCompletionProcessor implements IContentAssistProcessor {

	private class ProposalComputer {

		public static final String LABEL_SEPARATOR = " -------------------------------------------- ";

		private final Set<AbstractTask> addedTasks = new HashSet<AbstractTask>();

		private boolean addSeparator;

		private final int offset;

		private final String prefix;

		private final List<CompletionProposal> resultList = new ArrayList<CompletionProposal>();

		public ProposalComputer(ITextViewer viewer, int offset) {
			this.offset = offset;
			this.prefix = extractPrefix(viewer, offset).toLowerCase();
		}

		private void addProposal(AbstractTask task, String replacement, boolean includeTaskPrefix) {
			if (addSeparator) {
				if (!addedTasks.isEmpty()) {
					resultList.add(createSeparator());
				}
				addSeparator = false;
			}

			replacement = getReplacement(task, replacement, includeTaskPrefix);
			String displayString = labelProvider.getText(task);
			resultList.add(new CompletionProposal(replacement, offset - prefix.length(), prefix.length(),
					replacement.length(), labelProvider.getImage(task), displayString, null, null));

			addedTasks.add(task);
		}

		public void addSeparator() {
			addSeparator = true;
		}

		public void addTasks(List<AbstractTask> tasks) {
			for (AbstractTask task : tasks) {
				addTask(task);
			}
		}

		public void addTask(AbstractTask task) {
			if (addedTasks.contains(task)) {
				return;
			}

			String taskKey = task.getTaskKey();
			if (prefix.length() == 0) {
				addProposal(task, taskKey, true);
			} else if (taskKey != null && taskKey.startsWith(prefix)) {
				addProposal(task, taskKey, false);
			} else if (containsPrefix(task)) {
				addProposal(task, taskKey, true);
			}
		}

		private String getReplacement(AbstractTask task, String text, boolean includeTaskPrefix) {
			// add an absolute reference to the task if the viewer does not have a repository
			if (taskRepository == null || text == null || !taskRepository.getUrl().equals(task.getRepositoryUrl())) {
				return CopyTaskDetailsAction.getTextForTask(task);
			}

			if (includeTaskPrefix) {
				return getTaskPrefix(task) + text;
			} else {
				return text;
			}
		}

		private boolean containsPrefix(AbstractTask task) {
			String searchTest = getTaskPrefix(task) + " " + labelProvider.getText(task);
			String[] tokens = searchTest.split("\\s");
			for (String token : tokens) {
				if (token.toLowerCase().startsWith(prefix)) {
					return true;
				}
			}
			return false;
		}

		private CompletionProposal createSeparator() {
			return new CompletionProposal("", offset, 0, 0,
					TasksUiImages.getImage(TasksUiImages.CONTENT_ASSIST_SEPARATOR), LABEL_SEPARATOR, null, null);
		}

		/**
		 * Returns the prefix of the currently completed text. Assumes that any character that is not a line break or
		 * white space can be part of a task id.
		 */
		private String extractPrefix(ITextViewer viewer, int offset) {
			int i = offset;
			IDocument document = viewer.getDocument();
			if (i > document.getLength()) {
				return "";
			}

			try {
				while (i > 0) {
					char ch = document.getChar(i - 1);
					if (Character.isWhitespace(ch)) {
						break;
					}
					i--;
				}

				return document.get(i, offset - i);
			} catch (BadLocationException e) {
				return "";
			}
		}

		public void filterTasks(List<AbstractTask> tasks) {
			for (Iterator<AbstractTask> it = tasks.iterator(); it.hasNext();) {
				AbstractTask task = it.next();
				if (!select(task)) {
					it.remove();
				}
			}
		}

		private boolean select(AbstractTask task) {
			return !task.isLocal() //
					&& (taskRepository == null || task.getRepositoryUrl().equals(taskRepository.getUrl()));
		}

		public ICompletionProposal[] getResult() {
			return resultList.toArray(new ICompletionProposal[resultList.size()]);
		}

	}

	private static final int MAX_OPEN_EDITORS = 10;

	private static final int MAX_ACTIVATED_TASKS = 10;

	private final TaskElementLabelProvider labelProvider = new TaskElementLabelProvider(false);

	private final TaskRepository taskRepository;

	public RepositoryCompletionProcessor(TaskRepository taskRepository) {
		this.taskRepository = taskRepository;
	}

	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
		ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
		// adjust offset to end of normalized selection
		if (selection.getOffset() == offset) {
			offset = selection.getOffset() + selection.getLength();
		}

		ProposalComputer proposalComputer = new ProposalComputer(viewer, offset);

		// add tasks from navigation history
//		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
//		if (window != null) {
//			IWorkbenchPage page = window.getActivePage();
//			if (page != null) {
//				INavigationHistory history = page.getNavigationHistory();
//				INavigationLocation[] locations = history.getLocations();
//				if (locations != null) {
//					for (INavigationLocation location : locations) {
//						// location is always null
//					}
//				}
//			}
//		}

		// add open editor
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window != null) {
			IWorkbenchPage page = window.getActivePage();
			if (page != null) {
				IEditorReference[] editorReferences = page.getEditorReferences();
				int count = 0;
				for (int i = editorReferences.length - 1; i >= 0 && count < MAX_OPEN_EDITORS; i--) {
					try {
						if (editorReferences[i].getEditorInput() instanceof TaskEditorInput) {
							TaskEditorInput input = (TaskEditorInput) editorReferences[i].getEditorInput();
							AbstractTask task = input.getTask();
							if (task != null && !task.isLocal()) {
								proposalComputer.addTask(task);
								count++;
							}
						}
					} catch (PartInitException e) {
						// ignore
					}
				}
			}
		}

		// add tasks from activation history
		TaskActivationHistory taskHistory = TasksUiPlugin.getTaskListManager().getTaskActivationHistory();
		List<AbstractTask> tasks = taskHistory.getPreviousTasks(TaskListView.getActiveWorkingSets());
		int count = 0;
		for (int i = tasks.size() - 1; i >= 0 && count < MAX_ACTIVATED_TASKS; i--) {
			AbstractTask task = tasks.get(i);
			if (!task.isLocal()) {
				proposalComputer.addTask(task);
			}
		}

		// add all remaining tasks for repository
		if (taskRepository != null) {
			proposalComputer.addSeparator();

			TaskList taskList = TasksUiPlugin.getTaskListManager().getTaskList();
			tasks = new ArrayList<AbstractTask>(taskList.getAllTasks());
			proposalComputer.filterTasks(tasks);
			Collections.sort(tasks, new Comparator<AbstractTask>() {
				public int compare(AbstractTask o1, AbstractTask o2) {
					return labelProvider.getText(o1).compareTo(labelProvider.getText(o2));
				}
			});
			proposalComputer.addTasks(tasks);
		}

		return proposalComputer.getResult();
	}

	public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
		return null;
	}

	public char[] getCompletionProposalAutoActivationCharacters() {
		return null;
	}

	public char[] getContextInformationAutoActivationCharacters() {
		return null;
	}

	public IContextInformationValidator getContextInformationValidator() {
		return null;
	}

	public String getErrorMessage() {
		return null;
	}

	private String getTaskPrefix(AbstractTask task) {
		AbstractRepositoryConnector connector = TasksUiPlugin.getConnector(task.getConnectorKind());
		String prefix = connector.getTaskIdPrefix();
		// FIXME work around for Trac "#" prefix
		return (prefix.length() > 1) ? prefix + " " : prefix;
	}

}

Back to the top