Mercurial > projects > dwt-addons
annotate dwtx/jface/text/contentassist/CompletionProposalPopup.d @ 159:7926b636c282
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 27 Aug 2008 01:57:58 +0200 |
parents | 25f1f92fa3df |
children | 1a5b8f8129df |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 IBM Corporation 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 * IBM Corporation - initial API and implementation | |
10 * Sean Montgomery, sean_montgomery@comcast.net - https://bugs.eclipse.org/bugs/show_bug.cgi?id=116454 | |
11 * Port to the D programming language: | |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 module dwtx.jface.text.contentassist.CompletionProposalPopup; | |
15 | |
131 | 16 import dwtx.jface.text.contentassist.ContentAssistEvent; // packageimport |
17 import dwtx.jface.text.contentassist.Helper; // packageimport | |
18 import dwtx.jface.text.contentassist.PopupCloser; // packageimport | |
19 import dwtx.jface.text.contentassist.IContentAssistant; // packageimport | |
20 import dwtx.jface.text.contentassist.CompletionProposal; // packageimport | |
21 import dwtx.jface.text.contentassist.ICompletionProposalExtension5; // packageimport | |
22 import dwtx.jface.text.contentassist.IContextInformationValidator; // packageimport | |
23 import dwtx.jface.text.contentassist.IContentAssistListener; // packageimport | |
24 import dwtx.jface.text.contentassist.ICompletionProposalExtension6; // packageimport | |
25 import dwtx.jface.text.contentassist.ICompletionListener; // packageimport | |
26 import dwtx.jface.text.contentassist.ICompletionProposalExtension2; // packageimport | |
27 import dwtx.jface.text.contentassist.IContentAssistantExtension4; // packageimport | |
28 import dwtx.jface.text.contentassist.ContextInformation; // packageimport | |
29 import dwtx.jface.text.contentassist.ICompletionProposalExtension3; // packageimport | |
30 import dwtx.jface.text.contentassist.ContextInformationValidator; // packageimport | |
31 import dwtx.jface.text.contentassist.ICompletionProposal; // packageimport | |
32 import dwtx.jface.text.contentassist.IContentAssistProcessor; // packageimport | |
33 import dwtx.jface.text.contentassist.AdditionalInfoController; // packageimport | |
34 import dwtx.jface.text.contentassist.IContextInformationPresenter; // packageimport | |
35 import dwtx.jface.text.contentassist.ICompletionProposalExtension4; // packageimport | |
36 import dwtx.jface.text.contentassist.ICompletionListenerExtension; // packageimport | |
37 import dwtx.jface.text.contentassist.ContextInformationPopup; // packageimport | |
38 import dwtx.jface.text.contentassist.IContextInformationExtension; // packageimport | |
39 import dwtx.jface.text.contentassist.IContentAssistantExtension2; // packageimport | |
40 import dwtx.jface.text.contentassist.ContentAssistSubjectControlAdapter; // packageimport | |
41 import dwtx.jface.text.contentassist.ICompletionProposalExtension; // packageimport | |
42 import dwtx.jface.text.contentassist.IContextInformation; // packageimport | |
43 import dwtx.jface.text.contentassist.IContentAssistantExtension3; // packageimport | |
44 import dwtx.jface.text.contentassist.ContentAssistant; // packageimport | |
45 import dwtx.jface.text.contentassist.IContentAssistantExtension; // packageimport | |
46 import dwtx.jface.text.contentassist.JFaceTextMessages; // packageimport | |
47 | |
48 | |
129 | 49 import dwt.dwthelper.utils; |
50 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
51 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
52 |
129 | 53 |
54 import dwt.DWT; | |
55 import dwt.custom.BusyIndicator; | |
56 import dwt.custom.StyleRange; | |
57 import dwt.events.ControlEvent; | |
58 import dwt.events.ControlListener; | |
59 import dwt.events.DisposeEvent; | |
60 import dwt.events.DisposeListener; | |
61 import dwt.events.FocusEvent; | |
62 import dwt.events.FocusListener; | |
63 import dwt.events.KeyAdapter; | |
64 import dwt.events.KeyEvent; | |
65 import dwt.events.KeyListener; | |
66 import dwt.events.MouseAdapter; | |
67 import dwt.events.MouseEvent; | |
68 import dwt.events.SelectionEvent; | |
69 import dwt.events.SelectionListener; | |
70 import dwt.events.TraverseEvent; | |
71 import dwt.events.TraverseListener; | |
72 import dwt.events.VerifyEvent; | |
73 import dwt.graphics.Color; | |
74 import dwt.graphics.Font; | |
75 import dwt.graphics.FontData; | |
76 import dwt.graphics.Image; | |
77 import dwt.graphics.Point; | |
78 import dwt.graphics.Rectangle; | |
79 import dwt.layout.GridData; | |
80 import dwt.layout.GridLayout; | |
81 import dwt.widgets.Control; | |
82 import dwt.widgets.Display; | |
83 import dwt.widgets.Event; | |
84 import dwt.widgets.Label; | |
85 import dwt.widgets.Listener; | |
86 import dwt.widgets.Shell; | |
87 import dwt.widgets.Table; | |
88 import dwt.widgets.TableItem; | |
89 import dwtx.core.commands.AbstractHandler; | |
90 import dwtx.core.commands.ExecutionEvent; | |
91 import dwtx.core.commands.ExecutionException; | |
92 import dwtx.core.commands.IHandler; | |
93 import dwtx.core.runtime.Assert; | |
94 import dwtx.jface.bindings.keys.KeySequence; | |
95 import dwtx.jface.bindings.keys.SWTKeySupport; | |
96 import dwtx.jface.contentassist.IContentAssistSubjectControl; | |
97 import dwtx.jface.internal.text.InformationControlReplacer; | |
98 import dwtx.jface.internal.text.TableOwnerDrawSupport; | |
99 import dwtx.jface.preference.JFacePreferences; | |
100 import dwtx.jface.resource.JFaceResources; | |
101 import dwtx.jface.text.AbstractInformationControlManager; | |
102 import dwtx.jface.text.BadLocationException; | |
103 import dwtx.jface.text.DocumentEvent; | |
104 import dwtx.jface.text.IDocument; | |
105 import dwtx.jface.text.IDocumentListener; | |
106 import dwtx.jface.text.IEditingSupport; | |
107 import dwtx.jface.text.IEditingSupportRegistry; | |
108 import dwtx.jface.text.IInformationControl; | |
109 import dwtx.jface.text.IRegion; | |
110 import dwtx.jface.text.IRewriteTarget; | |
111 import dwtx.jface.text.ITextViewer; | |
112 import dwtx.jface.text.ITextViewerExtension; | |
113 import dwtx.jface.text.TextUtilities; | |
158 | 114 import dwtx.jface.text.AbstractInformationControlManager; |
129 | 115 import dwtx.jface.util.Geometry; |
116 import dwtx.jface.viewers.StyledString; | |
117 | |
118 | |
119 /** | |
120 * This class is used to present proposals to the user. If additional | |
121 * information exists for a proposal, then selecting that proposal | |
122 * will result in the information being displayed in a secondary | |
123 * window. | |
124 * | |
125 * @see dwtx.jface.text.contentassist.ICompletionProposal | |
126 * @see dwtx.jface.text.contentassist.AdditionalInfoController | |
127 */ | |
128 class CompletionProposalPopup : IContentAssistListener { | |
129 /** | |
130 * Set to <code>true</code> to use a Table with DWT.VIRTUAL. | |
131 * XXX: This is a workaround for: https://bugs.eclipse.org/bugs/show_bug.cgi?id=90321 | |
132 * More details see also: https://bugs.eclipse.org/bugs/show_bug.cgi?id=98585#c36 | |
133 * @since 3.1 | |
134 */ | |
159 | 135 private static bool USE_VIRTUAL_; |
136 private static bool USE_VIRTUAL_init = false;; | |
137 private static bool USE_VIRTUAL(){ | |
138 if( !USE_VIRTUAL_init ){ | |
139 USE_VIRTUAL_init = true; | |
140 USE_VIRTUAL_ = !"motif".equals(DWT.getPlatform()); //$NON-NLS-1$ | |
141 } | |
142 return USE_VIRTUAL_; | |
143 } | |
129 | 144 |
145 /** | |
146 * Completion proposal selection handler. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
147 * |
129 | 148 * @since 3.4 |
149 */ | |
150 final class ProposalSelectionHandler : AbstractHandler { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
151 |
129 | 152 /** |
153 * Selection operation codes. | |
154 */ | |
159 | 155 static const int SELECT_NEXT= 1; |
156 static const int SELECT_PREVIOUS= 2; | |
129 | 157 |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
158 |
129 | 159 private int fOperationCode; |
160 | |
161 /** | |
162 * Creates a new selection handler. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
163 * |
129 | 164 * @param operationCode the operation code |
165 * @since 3.4 | |
166 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
167 public this(int operationCode) { |
129 | 168 Assert.isLegal(operationCode is SELECT_NEXT || operationCode is SELECT_PREVIOUS); |
169 fOperationCode= operationCode; | |
170 } | |
171 | |
172 /* | |
173 * @see dwtx.core.commands.AbstractHandler#execute(dwtx.core.commands.ExecutionEvent) | |
174 * @since 3.4 | |
175 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
135
diff
changeset
|
176 public Object execute(ExecutionEvent event) { |
129 | 177 int itemCount= fProposalTable.getItemCount(); |
178 int selectionIndex= fProposalTable.getSelectionIndex(); | |
179 switch (fOperationCode) { | |
180 case SELECT_NEXT: | |
181 selectionIndex+= 1; | |
182 if (selectionIndex > itemCount - 1) | |
183 selectionIndex= 0; | |
184 break; | |
185 case SELECT_PREVIOUS: | |
186 selectionIndex-= 1; | |
187 if (selectionIndex < 0) | |
188 selectionIndex= itemCount - 1; | |
189 break; | |
190 } | |
191 selectProposal(selectionIndex, false); | |
192 return null; | |
193 } | |
194 | |
195 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
196 |
129 | 197 |
198 /** | |
199 * The empty proposal displayed if there is nothing else to show. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
200 * |
129 | 201 * @since 3.2 |
202 */ | |
203 private static final class EmptyProposal : ICompletionProposal, ICompletionProposalExtension { | |
204 | |
205 String fDisplayString; | |
206 int fOffset; | |
207 /* | |
208 * @see ICompletionProposal#apply(IDocument) | |
209 */ | |
210 public void apply(IDocument document) { | |
211 } | |
212 | |
213 /* | |
214 * @see ICompletionProposal#getSelection(IDocument) | |
215 */ | |
216 public Point getSelection(IDocument document) { | |
217 return new Point(fOffset, 0); | |
218 } | |
219 | |
220 /* | |
221 * @see ICompletionProposal#getContextInformation() | |
222 */ | |
223 public IContextInformation getContextInformation() { | |
224 return null; | |
225 } | |
226 | |
227 /* | |
228 * @see ICompletionProposal#getImage() | |
229 */ | |
230 public Image getImage() { | |
231 return null; | |
232 } | |
233 | |
234 /* | |
235 * @see ICompletionProposal#getDisplayString() | |
236 */ | |
237 public String getDisplayString() { | |
238 return fDisplayString; | |
239 } | |
240 | |
241 /* | |
242 * @see ICompletionProposal#getAdditionalProposalInfo() | |
243 */ | |
244 public String getAdditionalProposalInfo() { | |
245 return null; | |
246 } | |
247 | |
248 /* | |
249 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension#apply(dwtx.jface.text.IDocument, char, int) | |
250 */ | |
251 public void apply(IDocument document, char trigger, int offset) { | |
252 } | |
253 | |
254 /* | |
255 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension#isValidFor(dwtx.jface.text.IDocument, int) | |
256 */ | |
257 public bool isValidFor(IDocument document, int offset) { | |
258 return false; | |
259 } | |
260 | |
261 /* | |
262 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension#getTriggerCharacters() | |
263 */ | |
264 public char[] getTriggerCharacters() { | |
265 return null; | |
266 } | |
267 | |
268 /* | |
269 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension#getContextInformationPosition() | |
270 */ | |
271 public int getContextInformationPosition() { | |
272 return -1; | |
273 } | |
274 } | |
275 | |
276 private final class ProposalSelectionListener : KeyListener { | |
277 public void keyPressed(KeyEvent e) { | |
278 if (!Helper.okToUse(fProposalShell)) | |
279 return; | |
280 | |
281 if (e.character is 0 && e.keyCode is DWT.CTRL) { | |
282 // http://dev.eclipse.org/bugs/show_bug.cgi?id=34754 | |
283 int index= fProposalTable.getSelectionIndex(); | |
284 if (index >= 0) | |
285 selectProposal(index, true); | |
286 } | |
287 } | |
288 | |
289 public void keyReleased(KeyEvent e) { | |
290 if (!Helper.okToUse(fProposalShell)) | |
291 return; | |
292 | |
293 if (e.character is 0 && e.keyCode is DWT.CTRL) { | |
294 // http://dev.eclipse.org/bugs/show_bug.cgi?id=34754 | |
295 int index= fProposalTable.getSelectionIndex(); | |
296 if (index >= 0) | |
297 selectProposal(index, false); | |
298 } | |
299 } | |
300 } | |
301 | |
302 private final class CommandKeyListener : KeyAdapter { | |
303 private KeySequence fCommandSequence; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
304 |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
305 private this(KeySequence keySequence) { |
129 | 306 fCommandSequence= keySequence; |
307 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
308 |
129 | 309 public void keyPressed(KeyEvent e) { |
310 if (!Helper.okToUse(fProposalShell)) | |
311 return; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
312 |
129 | 313 int accelerator= SWTKeySupport.convertEventToUnmodifiedAccelerator(e); |
314 KeySequence sequence= KeySequence.getInstance(SWTKeySupport.convertAcceleratorToKeyStroke(accelerator)); | |
315 if (sequence.equals(fCommandSequence)) | |
316 if (fContentAssistant.isPrefixCompletionEnabled()) | |
317 incrementalComplete(); | |
318 else | |
319 showProposals(false); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
320 |
129 | 321 } |
322 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
323 |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
324 |
129 | 325 /** The associated text viewer. */ |
326 private ITextViewer fViewer; | |
327 /** The associated content assistant. */ | |
328 private ContentAssistant fContentAssistant; | |
329 /** The used additional info controller. */ | |
330 private AdditionalInfoController fAdditionalInfoController; | |
331 /** The closing strategy for this completion proposal popup. */ | |
159 | 332 private PopupCloser fPopupCloser; |
129 | 333 /** The popup shell. */ |
334 private Shell fProposalShell; | |
335 /** The proposal table. */ | |
336 private Table fProposalTable; | |
337 /** Indicates whether a completion proposal is being inserted. */ | |
338 private bool fInserting= false; | |
339 /** The key listener to control navigation. */ | |
340 private ProposalSelectionListener fKeyListener; | |
341 /** List of document events used for filtering proposals. */ | |
159 | 342 private List fDocumentEvents; |
129 | 343 /** Listener filling the document event queue. */ |
344 private IDocumentListener fDocumentListener; | |
345 /** The filter list of proposals. */ | |
346 private ICompletionProposal[] fFilteredProposals; | |
347 /** The computed list of proposals. */ | |
348 private ICompletionProposal[] fComputedProposals; | |
349 /** The offset for which the proposals have been computed. */ | |
350 private int fInvocationOffset; | |
351 /** The offset for which the computed proposals have been filtered. */ | |
352 private int fFilterOffset; | |
353 /** | |
354 * The most recently selected proposal. | |
355 * @since 3.0 | |
356 */ | |
357 private ICompletionProposal fLastProposal; | |
358 /** | |
359 * The content assist subject control. | |
360 * This replaces <code>fViewer</code> | |
361 * | |
362 * @since 3.0 | |
363 */ | |
364 private IContentAssistSubjectControl fContentAssistSubjectControl; | |
365 /** | |
366 * The content assist subject control adapter. | |
367 * This replaces <code>fViewer</code> | |
368 * | |
369 * @since 3.0 | |
370 */ | |
371 private ContentAssistSubjectControlAdapter fContentAssistSubjectControlAdapter; | |
372 /** | |
373 * Remembers the size for this completion proposal popup. | |
374 * @since 3.0 | |
375 */ | |
376 private Point fSize; | |
377 /** | |
378 * Editor helper that communicates that the completion proposal popup may | |
379 * have focus while the 'logical focus' is still with the editor. | |
380 * @since 3.1 | |
381 */ | |
382 private IEditingSupport fFocusHelper; | |
383 /** | |
384 * Set to true by {@link #computeFilteredProposals(int, DocumentEvent)} if | |
385 * the returned proposals are a subset of {@link #fFilteredProposals}, | |
386 * <code>false</code> if not. | |
387 * @since 3.1 | |
388 */ | |
389 private bool fIsFilteredSubset; | |
390 /** | |
391 * The filter runnable. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
392 * |
129 | 393 * @since 3.1.1 |
394 */ | |
159 | 395 private Runnable fFilterRunnable; |
396 private void fFilterRunnableInit(){ | |
397 fFilterRunnable = dgRunnable( { | |
129 | 398 if (!fIsFilterPending) |
399 return; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
400 |
129 | 401 fIsFilterPending= false; |
402 | |
403 if (!Helper.okToUse(fContentAssistSubjectControlAdapter.getControl())) | |
404 return; | |
405 | |
406 int offset= fContentAssistSubjectControlAdapter.getSelectedRange().x; | |
407 ICompletionProposal[] proposals= null; | |
408 try { | |
409 if (offset > -1) { | |
410 DocumentEvent event= TextUtilities.mergeProcessedDocumentEvents(fDocumentEvents); | |
411 proposals= computeFilteredProposals(offset, event); | |
412 } | |
413 } catch (BadLocationException x) { | |
414 } finally { | |
415 fDocumentEvents.clear(); | |
416 } | |
417 fFilterOffset= offset; | |
418 | |
419 if (proposals !is null && proposals.length > 0) | |
420 setProposals(proposals, fIsFilteredSubset); | |
421 else | |
422 hide(); | |
159 | 423 }); |
424 } | |
425 | |
129 | 426 /** |
427 * <code>true</code> if <code>fFilterRunnable</code> has been | |
428 * posted, <code>false</code> if not. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
429 * |
129 | 430 * @since 3.1.1 |
431 */ | |
432 private bool fIsFilterPending= false; | |
433 /** | |
434 * The info message at the bottom of the popup, or <code>null</code> for no popup (if | |
435 * ContentAssistant does not provide one). | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
436 * |
129 | 437 * @since 3.2 |
438 */ | |
439 private Label fMessageText; | |
440 /** | |
441 * The font used for <code>fMessageText</code> or null; dispose when done. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
442 * |
129 | 443 * @since 3.2 |
444 */ | |
445 private Font fMessageTextFont; | |
446 /** | |
447 * The most recent completion offset (used to determine repeated invocation) | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
448 * |
129 | 449 * @since 3.2 |
450 */ | |
451 private int fLastCompletionOffset; | |
452 /** | |
453 * The (reusable) empty proposal. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
454 * |
129 | 455 * @since 3.2 |
456 */ | |
159 | 457 private EmptyProposal fEmptyProposal; |
129 | 458 /** |
459 * The text for the empty proposal, or <code>null</code> to use the default text. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
460 * |
129 | 461 * @since 3.2 |
462 */ | |
463 private String fEmptyMessage= null; | |
464 /** | |
465 * Tells whether colored labels support is enabled. | |
466 * Only valid while the popup is active. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
467 * |
129 | 468 * @since 3.4 |
469 */ | |
470 private bool fIsColoredLabelsSupportEnabled= false; | |
471 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
472 |
129 | 473 /** |
474 * Creates a new completion proposal popup for the given elements. | |
475 * | |
476 * @param contentAssistant the content assistant feeding this popup | |
477 * @param viewer the viewer on top of which this popup appears | |
478 * @param infoController the information control collaborating with this popup | |
479 * @since 2.0 | |
480 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
481 public this(ContentAssistant contentAssistant, ITextViewer viewer, AdditionalInfoController infoController) { |
159 | 482 // DWT instance init |
483 fDocumentEvents= new ArrayList(); | |
484 fPopupCloser= new PopupCloser(); | |
485 if( fEmptyProposal is null ) fEmptyProposal= new EmptyProposal(); | |
486 fFilterRunnableInit(); | |
487 | |
129 | 488 fContentAssistant= contentAssistant; |
489 fViewer= viewer; | |
490 fAdditionalInfoController= infoController; | |
491 fContentAssistSubjectControlAdapter= new ContentAssistSubjectControlAdapter(fViewer); | |
492 } | |
493 | |
494 /** | |
495 * Creates a new completion proposal popup for the given elements. | |
496 * | |
497 * @param contentAssistant the content assistant feeding this popup | |
498 * @param contentAssistSubjectControl the content assist subject control on top of which this popup appears | |
499 * @param infoController the information control collaborating with this popup | |
500 * @since 3.0 | |
501 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
502 public this(ContentAssistant contentAssistant, IContentAssistSubjectControl contentAssistSubjectControl, AdditionalInfoController infoController) { |
159 | 503 // DWT instance init |
504 fDocumentEvents= new ArrayList(); | |
505 fPopupCloser= new PopupCloser(); | |
506 if( fEmptyProposal is null ) fEmptyProposal= new EmptyProposal(); | |
507 fFilterRunnableInit(); | |
508 | |
129 | 509 fContentAssistant= contentAssistant; |
510 fContentAssistSubjectControl= contentAssistSubjectControl; | |
511 fAdditionalInfoController= infoController; | |
512 fContentAssistSubjectControlAdapter= new ContentAssistSubjectControlAdapter(fContentAssistSubjectControl); | |
513 } | |
514 | |
515 /** | |
516 * Computes and presents completion proposals. The flag indicates whether this call has | |
517 * be made out of an auto activation context. | |
518 * | |
519 * @param autoActivated <code>true</code> if auto activation context | |
520 * @return an error message or <code>null</code> in case of no error | |
521 */ | |
158 | 522 public String showProposals(bool autoActivated) { |
129 | 523 |
524 if (fKeyListener is null) | |
525 fKeyListener= new ProposalSelectionListener(); | |
526 | |
527 final Control control= fContentAssistSubjectControlAdapter.getControl(); | |
528 | |
529 if (!Helper.okToUse(fProposalShell) && control !is null && !control.isDisposed()) { | |
530 // add the listener before computing the proposals so we don't move the caret | |
531 // when the user types fast. | |
532 fContentAssistSubjectControlAdapter.addKeyListener(fKeyListener); | |
533 | |
158 | 534 BusyIndicator.showWhile(control.getDisplay(), dgRunnable((bool autoActivated_) { |
129 | 535 |
158 | 536 fInvocationOffset= fContentAssistSubjectControlAdapter.getSelectedRange().x; |
537 fFilterOffset= fInvocationOffset; | |
538 fLastCompletionOffset= fFilterOffset; | |
539 fComputedProposals= computeProposals(fInvocationOffset); | |
129 | 540 |
158 | 541 int count= (fComputedProposals is null ? 0 : fComputedProposals.length); |
542 if (count is 0 && hideWhenNoProposals(autoActivated_)) | |
543 return; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
544 |
158 | 545 if (count is 1 && !autoActivated_ && canAutoInsert(fComputedProposals[0])) { |
546 insertProposal(fComputedProposals[0], cast(wchar) 0, 0, fInvocationOffset); | |
547 hide(); | |
548 } else { | |
549 createProposalSelector(); | |
550 setProposals(fComputedProposals, false); | |
551 displayProposals(); | |
129 | 552 } |
158 | 553 }, autoActivated )); |
129 | 554 } else { |
555 fLastCompletionOffset= fFilterOffset; | |
556 handleRepeatedInvocation(); | |
557 } | |
558 | |
559 return getErrorMessage(); | |
560 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
561 |
129 | 562 /** |
563 * Hides the popup and returns <code>true</code> if the popup is configured | |
564 * to never display an empty list. Returns <code>false</code> otherwise. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
565 * |
129 | 566 * @param autoActivated whether the invocation was auto-activated |
567 * @return <code>false</code> if an empty list should be displayed, <code>true</code> otherwise | |
568 * @since 3.2 | |
569 */ | |
570 private bool hideWhenNoProposals(bool autoActivated) { | |
571 if (autoActivated || !fContentAssistant.isShowEmptyList()) { | |
572 if (!autoActivated) { | |
573 Control control= fContentAssistSubjectControlAdapter.getControl(); | |
574 if (control !is null && !control.isDisposed()) | |
575 control.getDisplay().beep(); | |
576 } | |
577 hide(); | |
578 return true; | |
579 } | |
580 return false; | |
581 } | |
582 | |
583 /** | |
584 * If content assist is set up to handle cycling, then the proposals are recomputed. Otherwise, | |
585 * nothing happens. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
586 * |
129 | 587 * @since 3.2 |
588 */ | |
589 private void handleRepeatedInvocation() { | |
590 if (fContentAssistant.isRepeatedInvocationMode()) { | |
591 fComputedProposals= computeProposals(fFilterOffset); | |
592 setProposals(fComputedProposals, false); | |
593 } | |
594 } | |
595 | |
596 /** | |
597 * Returns the completion proposal available at the given offset of the | |
598 * viewer's document. Delegates the work to the content assistant. | |
599 * | |
600 * @param offset the offset | |
601 * @return the completion proposals available at this offset | |
602 */ | |
603 private ICompletionProposal[] computeProposals(int offset) { | |
604 if (fContentAssistSubjectControl !is null) | |
605 return fContentAssistant.computeCompletionProposals(fContentAssistSubjectControl, offset); | |
606 return fContentAssistant.computeCompletionProposals(fViewer, offset); | |
607 } | |
608 | |
609 /** | |
610 * Returns the error message. | |
611 * | |
612 * @return the error message | |
613 */ | |
614 private String getErrorMessage() { | |
615 return fContentAssistant.getErrorMessage(); | |
616 } | |
617 | |
618 /** | |
619 * Creates the proposal selector. | |
620 */ | |
621 private void createProposalSelector() { | |
622 if (Helper.okToUse(fProposalShell)) | |
623 return; | |
624 | |
625 Control control= fContentAssistSubjectControlAdapter.getControl(); | |
626 fProposalShell= new Shell(control.getShell(), DWT.ON_TOP | DWT.RESIZE ); | |
627 fProposalShell.setFont(JFaceResources.getDefaultFont()); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
628 if (USE_VIRTUAL) { |
129 | 629 fProposalTable= new Table(fProposalShell, DWT.H_SCROLL | DWT.V_SCROLL | DWT.VIRTUAL); |
630 | |
135 | 631 Listener listener= new class() Listener { |
129 | 632 public void handleEvent(Event event) { |
633 handleSetData(event); | |
634 } | |
635 }; | |
636 fProposalTable.addListener(DWT.SetData, listener); | |
637 } else { | |
638 fProposalTable= new Table(fProposalShell, DWT.H_SCROLL | DWT.V_SCROLL); | |
639 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
640 |
129 | 641 fIsColoredLabelsSupportEnabled= fContentAssistant.isColoredLabelsSupportEnabled(); |
642 if (fIsColoredLabelsSupportEnabled) | |
643 TableOwnerDrawSupport.install(fProposalTable); | |
644 | |
645 fProposalTable.setLocation(0, 0); | |
646 if (fAdditionalInfoController !is null) | |
647 fAdditionalInfoController.setSizeConstraints(50, 10, true, true); | |
648 | |
649 GridLayout layout= new GridLayout(); | |
650 layout.marginWidth= 0; | |
651 layout.marginHeight= 0; | |
652 layout.verticalSpacing= 1; | |
653 fProposalShell.setLayout(layout); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
654 |
129 | 655 if (fContentAssistant.isStatusLineVisible()) { |
656 createMessageText(); | |
657 } | |
658 | |
659 GridData data= new GridData(GridData.FILL_BOTH); | |
660 | |
661 Point size= fContentAssistant.restoreCompletionProposalPopupSize(); | |
662 if (size !is null) { | |
663 fProposalTable.setLayoutData(data); | |
664 fProposalShell.setSize(size); | |
665 } else { | |
666 int height= fProposalTable.getItemHeight() * 10; | |
667 // use golden ratio as default aspect ratio | |
668 final double aspectRatio= (1 + Math.sqrt(5)) / 2; | |
134 | 669 int width= cast(int) (height * aspectRatio); |
129 | 670 Rectangle trim= fProposalTable.computeTrim(0, 0, width, height); |
671 data.heightHint= trim.height; | |
672 data.widthHint= trim.width; | |
673 fProposalTable.setLayoutData(data); | |
674 fProposalShell.pack(); | |
675 } | |
676 fContentAssistant.addToLayout(this, fProposalShell, ContentAssistant.LayoutManager.LAYOUT_PROPOSAL_SELECTOR, fContentAssistant.getSelectionOffset()); | |
677 | |
135 | 678 fProposalShell.addControlListener(new class() ControlListener { |
129 | 679 |
680 public void controlMoved(ControlEvent e) {} | |
681 | |
682 public void controlResized(ControlEvent e) { | |
683 if (fAdditionalInfoController !is null) { | |
684 // reset the cached resize constraints | |
685 fAdditionalInfoController.setSizeConstraints(50, 10, true, false); | |
686 } | |
687 | |
688 fSize= fProposalShell.getSize(); | |
689 } | |
690 }); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
691 |
129 | 692 fProposalShell.setBackground(control.getDisplay().getSystemColor(DWT.COLOR_GRAY)); |
693 | |
694 Color c= getBackgroundColor(control); | |
695 fProposalTable.setBackground(c); | |
696 | |
697 c= getForegroundColor(control); | |
698 fProposalTable.setForeground(c); | |
699 | |
135 | 700 fProposalTable.addSelectionListener(new class() SelectionListener { |
129 | 701 |
702 public void widgetSelected(SelectionEvent e) {} | |
703 | |
704 public void widgetDefaultSelected(SelectionEvent e) { | |
705 insertSelectedProposalWithMask(e.stateMask); | |
706 } | |
707 }); | |
708 | |
709 fPopupCloser.install(fContentAssistant, fProposalTable, fAdditionalInfoController); | |
710 | |
135 | 711 fProposalShell.addDisposeListener(new class() DisposeListener { |
129 | 712 public void widgetDisposed(DisposeEvent e) { |
713 unregister(); // but don't dispose the shell, since we're being called from its disposal event! | |
714 } | |
715 }); | |
716 | |
717 fProposalTable.setHeaderVisible(false); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
718 |
129 | 719 addCommandSupport(fProposalTable); |
720 } | |
721 | |
722 /** | |
723 * Returns the minimal required height for the proposal, may return 0 if the popup has not been | |
724 * created yet. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
725 * |
129 | 726 * @return the minimal height |
727 * @since 3.3 | |
728 */ | |
729 int getMinimalHeight() { | |
730 int height= 0; | |
731 if (Helper.okToUse(fProposalTable)) { | |
732 int items= fProposalTable.getItemHeight() * 10; | |
733 Rectangle trim= fProposalTable.computeTrim(0, 0, DWT.DEFAULT, items); | |
734 height= trim.height; | |
735 } | |
736 if (Helper.okToUse(fMessageText)) | |
737 height+= fMessageText.getSize().y + 1; | |
738 return height; | |
739 } | |
740 | |
741 /** | |
742 * Adds command support to the given control. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
743 * |
129 | 744 * @param control the control to watch for focus |
745 * @since 3.2 | |
746 */ | |
158 | 747 private void addCommandSupport(Control control) { |
129 | 748 final KeySequence commandSequence= fContentAssistant.getRepeatedInvocationKeySequence(); |
749 if (commandSequence !is null && !commandSequence.isEmpty() && fContentAssistant.isRepeatedInvocationMode()) { | |
158 | 750 control.addFocusListener(new class(control,commandSequence) FocusListener { |
751 Control control_; | |
752 KeySequence commandSequence_; | |
753 this(Control a, KeySequence b){ | |
754 control_=a; | |
755 commandSequence_=b; | |
756 } | |
129 | 757 private CommandKeyListener fCommandKeyListener; |
758 public void focusGained(FocusEvent e) { | |
158 | 759 if (Helper.okToUse(control_)) { |
129 | 760 if (fCommandKeyListener is null) { |
158 | 761 fCommandKeyListener= new CommandKeyListener(commandSequence_); |
129 | 762 fProposalTable.addKeyListener(fCommandKeyListener); |
763 } | |
764 } | |
765 } | |
766 public void focusLost(FocusEvent e) { | |
767 if (fCommandKeyListener !is null) { | |
158 | 768 control_.removeKeyListener(fCommandKeyListener); |
129 | 769 fCommandKeyListener= null; |
770 } | |
771 } | |
772 }); | |
773 } | |
158 | 774 control.addFocusListener(new class(control) FocusListener { |
775 Control control_; | |
129 | 776 private TraverseListener fTraverseListener; |
158 | 777 this(Control a){ |
778 control_=a; | |
779 } | |
129 | 780 public void focusGained(FocusEvent e) { |
158 | 781 if (Helper.okToUse(control_)) { |
129 | 782 if (fTraverseListener is null) { |
135 | 783 fTraverseListener= new class() TraverseListener { |
129 | 784 public void keyTraversed(TraverseEvent event) { |
785 if (event.detail is DWT.TRAVERSE_TAB_NEXT) { | |
786 IInformationControl iControl= fAdditionalInfoController.getCurrentInformationControl2(); | |
787 if (fAdditionalInfoController.getInternalAccessor().canReplace(iControl)) { | |
788 fAdditionalInfoController.getInternalAccessor().replaceInformationControl(true); | |
789 event.doit= false; | |
790 } | |
791 } | |
792 } | |
793 }; | |
794 fProposalTable.addTraverseListener(fTraverseListener); | |
795 } | |
796 } | |
797 } | |
798 public void focusLost(FocusEvent e) { | |
799 if (fTraverseListener !is null) { | |
158 | 800 control_.removeTraverseListener(fTraverseListener); |
129 | 801 fTraverseListener= null; |
802 } | |
803 } | |
804 }); | |
805 } | |
806 | |
807 /** | |
808 * Returns the background color to use. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
809 * |
129 | 810 * @param control the control to get the display from |
811 * @return the background color | |
812 * @since 3.2 | |
813 */ | |
814 private Color getBackgroundColor(Control control) { | |
815 Color c= fContentAssistant.getProposalSelectorBackground(); | |
816 if (c is null) | |
817 c= JFaceResources.getColorRegistry().get(JFacePreferences.CONTENT_ASSIST_BACKGROUND_COLOR); | |
818 return c; | |
819 } | |
820 | |
821 /** | |
822 * Returns the foreground color to use. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
823 * |
129 | 824 * @param control the control to get the display from |
825 * @return the foreground color | |
826 * @since 3.2 | |
827 */ | |
828 private Color getForegroundColor(Control control) { | |
829 Color c= fContentAssistant.getProposalSelectorForeground(); | |
830 if (c is null) | |
831 c= JFaceResources.getColorRegistry().get(JFacePreferences.CONTENT_ASSIST_FOREGROUND_COLOR); | |
832 return c; | |
833 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
834 |
129 | 835 /** |
836 * Creates the caption line under the proposal table. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
837 * |
129 | 838 * @since 3.2 |
839 */ | |
840 private void createMessageText() { | |
841 if (fMessageText is null) { | |
842 fMessageText= new Label(fProposalShell, DWT.RIGHT); | |
843 GridData textData= new GridData(DWT.FILL, DWT.BOTTOM, true, false); | |
844 fMessageText.setLayoutData(textData); | |
845 fMessageText.setText(fContentAssistant.getStatusMessage() + " "); //$NON-NLS-1$ | |
846 if (fMessageTextFont is null) { | |
847 Font font= fMessageText.getFont(); | |
848 Display display= fProposalShell.getDisplay(); | |
849 FontData[] fontDatas= font.getFontData(); | |
850 for (int i= 0; i < fontDatas.length; i++) | |
851 fontDatas[i].setHeight(fontDatas[i].getHeight() * 9 / 10); | |
852 fMessageTextFont= new Font(display, fontDatas); | |
853 } | |
854 fMessageText.setFont(fMessageTextFont); | |
855 fMessageText.setBackground(getBackgroundColor(fProposalShell)); | |
856 fMessageText.setForeground(getForegroundColor(fProposalShell)); | |
857 | |
858 if (fContentAssistant.isRepeatedInvocationMode()) { | |
859 fMessageText.setCursor(fProposalShell.getDisplay().getSystemCursor(DWT.CURSOR_HAND)); | |
135 | 860 fMessageText.addMouseListener(new class() MouseAdapter { |
129 | 861 public void mouseUp(MouseEvent e) { |
862 fLastCompletionOffset= fFilterOffset; | |
863 fProposalTable.setFocus(); | |
864 handleRepeatedInvocation(); | |
865 } | |
866 | |
867 public void mouseDown(MouseEvent e) { | |
868 } | |
869 }); | |
870 } | |
871 } | |
872 } | |
873 | |
874 /* | |
875 * @since 3.1 | |
876 */ | |
877 private void handleSetData(Event event) { | |
134 | 878 TableItem item= cast(TableItem) event.item; |
129 | 879 int index= fProposalTable.indexOf(item); |
880 | |
881 if (0 <= index && index < fFilteredProposals.length) { | |
882 ICompletionProposal current= fFilteredProposals[index]; | |
883 | |
884 String displayString; | |
885 StyleRange[] styleRanges= null; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
886 if (fIsColoredLabelsSupportEnabled && cast(ICompletionProposalExtension6)current ) { |
134 | 887 StyledString styledString= (cast(ICompletionProposalExtension6)current).getStyledDisplayString(); |
129 | 888 displayString= styledString.getString(); |
889 styleRanges= styledString.getStyleRanges(); | |
890 } else | |
891 displayString= current.getDisplayString(); | |
892 | |
893 item.setText(displayString); | |
894 if (fIsColoredLabelsSupportEnabled) | |
895 TableOwnerDrawSupport.storeStyleRanges(item, 0, styleRanges); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
896 |
129 | 897 item.setImage(current.getImage()); |
898 item.setData(current); | |
899 } else { | |
900 // this should not happen, but does on win32 | |
901 } | |
902 } | |
903 | |
904 /** | |
905 * Returns the proposal selected in the proposal selector. | |
906 * | |
907 * @return the selected proposal | |
908 * @since 2.0 | |
909 */ | |
910 private ICompletionProposal getSelectedProposal() { | |
911 /* Make sure that there is no filter runnable pending. | |
912 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=31427 | |
913 */ | |
914 if (fIsFilterPending) | |
915 fFilterRunnable.run(); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
916 |
129 | 917 // filter runnable may have hidden the proposals |
918 if (!Helper.okToUse(fProposalTable)) | |
919 return null; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
920 |
129 | 921 int i= fProposalTable.getSelectionIndex(); |
922 if (fFilteredProposals is null || i < 0 || i >= fFilteredProposals.length) | |
923 return null; | |
924 return fFilteredProposals[i]; | |
925 } | |
926 | |
927 /** | |
928 * Takes the selected proposal and applies it. | |
929 * | |
930 * @param stateMask the state mask | |
931 * @since 3.2 | |
932 */ | |
933 private void insertSelectedProposalWithMask(int stateMask) { | |
934 ICompletionProposal p= getSelectedProposal(); | |
935 hide(); | |
936 if (p !is null) | |
134 | 937 insertProposal(p, cast(wchar) 0, stateMask, fContentAssistSubjectControlAdapter.getSelectedRange().x); |
129 | 938 } |
939 | |
940 /** | |
941 * Applies the given proposal at the given offset. The given character is the | |
942 * one that triggered the insertion of this proposal. | |
943 * | |
944 * @param p the completion proposal | |
945 * @param trigger the trigger character | |
946 * @param stateMask the state mask | |
947 * @param offset the offset | |
948 * @since 2.1 | |
949 */ | |
158 | 950 private void insertProposal(ICompletionProposal p, char trigger, int stateMask, int offset) { |
129 | 951 |
952 fInserting= true; | |
953 IRewriteTarget target= null; | |
158 | 954 IEditingSupport helper= new class(offset) IEditingSupport { |
955 int offset_; | |
956 this(int a){ | |
957 offset_=a; | |
958 } | |
129 | 959 public bool isOriginator(DocumentEvent event, IRegion focus) { |
158 | 960 return focus.getOffset() <= offset_ && focus.getOffset() + focus.getLength() >= offset_; |
129 | 961 } |
962 | |
963 public bool ownsFocusShell() { | |
964 return false; | |
965 } | |
966 | |
967 }; | |
968 | |
969 try { | |
970 | |
971 IDocument document= fContentAssistSubjectControlAdapter.getDocument(); | |
972 | |
138 | 973 if ( cast(ITextViewerExtension)fViewer ) { |
134 | 974 ITextViewerExtension extension= cast(ITextViewerExtension) fViewer; |
129 | 975 target= extension.getRewriteTarget(); |
976 } | |
977 | |
978 if (target !is null) | |
979 target.beginCompoundChange(); | |
980 | |
138 | 981 if ( cast(IEditingSupportRegistry)fViewer ) { |
134 | 982 IEditingSupportRegistry registry= cast(IEditingSupportRegistry) fViewer; |
129 | 983 registry.register(helper); |
984 } | |
985 | |
986 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
987 if (cast(ICompletionProposalExtension2)p && fViewer !is null) { |
134 | 988 ICompletionProposalExtension2 e= cast(ICompletionProposalExtension2) p; |
129 | 989 e.apply(fViewer, trigger, stateMask, offset); |
138 | 990 } else if ( cast(ICompletionProposalExtension)p ) { |
134 | 991 ICompletionProposalExtension e= cast(ICompletionProposalExtension) p; |
129 | 992 e.apply(document, trigger, offset); |
993 } else { | |
994 p.apply(document); | |
995 } | |
996 | |
997 Point selection= p.getSelection(document); | |
998 if (selection !is null) { | |
999 fContentAssistSubjectControlAdapter.setSelectedRange(selection.x, selection.y); | |
1000 fContentAssistSubjectControlAdapter.revealRange(selection.x, selection.y); | |
1001 } | |
1002 | |
1003 IContextInformation info= p.getContextInformation(); | |
1004 if (info !is null) { | |
1005 | |
1006 int contextInformationOffset; | |
138 | 1007 if ( cast(ICompletionProposalExtension)p ) { |
134 | 1008 ICompletionProposalExtension e= cast(ICompletionProposalExtension) p; |
129 | 1009 contextInformationOffset= e.getContextInformationPosition(); |
1010 } else { | |
1011 if (selection is null) | |
1012 selection= fContentAssistSubjectControlAdapter.getSelectedRange(); | |
1013 contextInformationOffset= selection.x + selection.y; | |
1014 } | |
1015 | |
1016 fContentAssistant.showContextInformation(info, contextInformationOffset); | |
1017 } else | |
1018 fContentAssistant.showContextInformation(null, -1); | |
1019 | |
1020 | |
1021 } finally { | |
1022 if (target !is null) | |
1023 target.endCompoundChange(); | |
1024 | |
138 | 1025 if ( cast(IEditingSupportRegistry)fViewer ) { |
134 | 1026 IEditingSupportRegistry registry= cast(IEditingSupportRegistry) fViewer; |
129 | 1027 registry.unregister(helper); |
1028 } | |
1029 fInserting= false; | |
1030 } | |
1031 } | |
1032 | |
1033 /** | |
1034 * Returns whether this popup has the focus. | |
1035 * | |
1036 * @return <code>true</code> if the popup has the focus | |
1037 */ | |
1038 public bool hasFocus() { | |
1039 if (Helper.okToUse(fProposalShell)) { | |
1040 if ((fProposalShell.isFocusControl() || fProposalTable.isFocusControl())) | |
1041 return true; | |
1042 /* | |
1043 * We have to delegate this query to the additional info controller | |
1044 * as well, since the content assistant is the widget token owner | |
1045 * and its closer does not know that the additional info control can | |
1046 * now also take focus. | |
1047 */ | |
1048 if (fAdditionalInfoController !is null) { | |
1049 IInformationControl informationControl= fAdditionalInfoController.getCurrentInformationControl2(); | |
1050 if (informationControl !is null && informationControl.isFocusControl()) | |
1051 return true; | |
1052 InformationControlReplacer replacer= fAdditionalInfoController.getInternalAccessor().getInformationControlReplacer(); | |
1053 if (replacer !is null) { | |
1054 informationControl= replacer.getCurrentInformationControl2(); | |
1055 if (informationControl !is null && informationControl.isFocusControl()) | |
1056 return true; | |
1057 } | |
1058 } | |
1059 } | |
1060 | |
1061 return false; | |
1062 } | |
1063 | |
1064 /** | |
1065 * Hides this popup. | |
1066 */ | |
1067 public void hide() { | |
1068 | |
1069 unregister(); | |
1070 | |
138 | 1071 if ( cast(IEditingSupportRegistry)fViewer ) { |
134 | 1072 IEditingSupportRegistry registry= cast(IEditingSupportRegistry) fViewer; |
129 | 1073 registry.unregister(fFocusHelper); |
1074 } | |
1075 | |
1076 if (Helper.okToUse(fProposalShell)) { | |
1077 | |
1078 fContentAssistant.removeContentAssistListener(this, ContentAssistant.PROPOSAL_SELECTOR); | |
1079 | |
1080 fPopupCloser.uninstall(); | |
1081 fProposalShell.setVisible(false); | |
1082 fProposalShell.dispose(); | |
1083 fProposalShell= null; | |
1084 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1085 |
129 | 1086 if (fMessageTextFont !is null) { |
1087 fMessageTextFont.dispose(); | |
1088 fMessageTextFont= null; | |
1089 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1090 |
129 | 1091 if (fMessageText !is null) { |
1092 fMessageText= null; | |
1093 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1094 |
129 | 1095 fEmptyMessage= null; |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1096 |
129 | 1097 fLastCompletionOffset= -1; |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1098 |
129 | 1099 fContentAssistant.fireSessionEndEvent(); |
1100 } | |
1101 | |
1102 /** | |
1103 * Unregister this completion proposal popup. | |
1104 * | |
1105 * @since 3.0 | |
1106 */ | |
1107 private void unregister() { | |
1108 if (fDocumentListener !is null) { | |
1109 IDocument document= fContentAssistSubjectControlAdapter.getDocument(); | |
1110 if (document !is null) | |
1111 document.removeDocumentListener(fDocumentListener); | |
1112 fDocumentListener= null; | |
1113 } | |
1114 fDocumentEvents.clear(); | |
1115 | |
1116 if (fKeyListener !is null && fContentAssistSubjectControlAdapter.getControl() !is null && !fContentAssistSubjectControlAdapter.getControl().isDisposed()) { | |
1117 fContentAssistSubjectControlAdapter.removeKeyListener(fKeyListener); | |
1118 fKeyListener= null; | |
1119 } | |
1120 | |
1121 if (fLastProposal !is null) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1122 if (cast(ICompletionProposalExtension2)fLastProposal && fViewer !is null) { |
134 | 1123 ICompletionProposalExtension2 extension= cast(ICompletionProposalExtension2) fLastProposal; |
129 | 1124 extension.unselected(fViewer); |
1125 } | |
1126 fLastProposal= null; | |
1127 } | |
1128 | |
1129 fFilteredProposals= null; | |
1130 fComputedProposals= null; | |
1131 | |
1132 fContentAssistant.possibleCompletionsClosed(); | |
1133 } | |
1134 | |
1135 /** | |
1136 *Returns whether this popup is active. It is active if the proposal selector is visible. | |
1137 * | |
1138 * @return <code>true</code> if this popup is active | |
1139 */ | |
1140 public bool isActive() { | |
1141 return fProposalShell !is null && !fProposalShell.isDisposed(); | |
1142 } | |
1143 | |
1144 /** | |
1145 * Initializes the proposal selector with these given proposals. | |
1146 * @param proposals the proposals | |
1147 * @param isFilteredSubset if <code>true</code>, the proposal table is | |
1148 * not cleared, but the proposals that are not in the passed array | |
1149 * are removed from the displayed set | |
1150 */ | |
1151 private void setProposals(ICompletionProposal[] proposals, bool isFilteredSubset) { | |
1152 ICompletionProposal[] oldProposals= fFilteredProposals; | |
1153 ICompletionProposal oldProposal= getSelectedProposal(); // may trigger filtering and a reentrant call to setProposals() | |
1154 if (oldProposals !is fFilteredProposals) // reentrant call was first - abort | |
1155 return; | |
1156 | |
1157 if (Helper.okToUse(fProposalTable)) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1158 if (cast(ICompletionProposalExtension2)oldProposal && fViewer !is null) |
134 | 1159 (cast(ICompletionProposalExtension2) oldProposal).unselected(fViewer); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1160 |
129 | 1161 if (proposals is null || proposals.length is 0) { |
1162 fEmptyProposal.fOffset= fFilterOffset; | |
1163 fEmptyProposal.fDisplayString= fEmptyMessage !is null ? fEmptyMessage : JFaceTextMessages.getString("CompletionProposalPopup.no_proposals"); //$NON-NLS-1$ | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1164 proposals= [ fEmptyProposal ]; |
129 | 1165 } |
1166 | |
1167 fFilteredProposals= proposals; | |
1168 final int newLen= proposals.length; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1169 if (USE_VIRTUAL) { |
129 | 1170 fProposalTable.setItemCount(newLen); |
1171 fProposalTable.clearAll(); | |
1172 } else { | |
1173 fProposalTable.setRedraw(false); | |
1174 fProposalTable.setItemCount(newLen); | |
1175 TableItem[] items= fProposalTable.getItems(); | |
1176 for (int i= 0; i < items.length; i++) { | |
1177 TableItem item= items[i]; | |
1178 ICompletionProposal proposal= proposals[i]; | |
1179 item.setText(proposal.getDisplayString()); | |
1180 item.setImage(proposal.getImage()); | |
1181 item.setData(proposal); | |
1182 } | |
1183 fProposalTable.setRedraw(true); | |
1184 } | |
1185 | |
1186 Point currentLocation= fProposalShell.getLocation(); | |
1187 Point newLocation= getLocation(); | |
1188 if ((newLocation.x < currentLocation.x && newLocation.y is currentLocation.y) || newLocation.y < currentLocation.y) | |
1189 fProposalShell.setLocation(newLocation); | |
1190 | |
1191 selectProposal(0, false); | |
1192 } | |
1193 } | |
1194 | |
1195 /** | |
1196 * Returns the graphical location at which this popup should be made visible. | |
1197 * | |
1198 * @return the location of this popup | |
1199 */ | |
1200 private Point getLocation() { | |
1201 int caret= fContentAssistSubjectControlAdapter.getCaretOffset(); | |
1202 Rectangle location= fContentAssistant.getLayoutManager().computeBoundsBelowAbove(fProposalShell, fSize is null ? fProposalShell.getSize() : fSize, caret, this); | |
1203 return Geometry.getLocation(location); | |
1204 } | |
1205 | |
1206 /** | |
1207 * Returns the size of this completion proposal popup. | |
1208 * | |
1209 * @return a Point containing the size | |
1210 * @since 3.0 | |
1211 */ | |
1212 Point getSize() { | |
1213 return fSize; | |
1214 } | |
1215 | |
1216 /** | |
1217 * Displays this popup and install the additional info controller, so that additional info | |
1218 * is displayed when a proposal is selected and additional info is available. | |
1219 */ | |
1220 private void displayProposals() { | |
1221 | |
1222 if (!Helper.okToUse(fProposalShell) || !Helper.okToUse(fProposalTable)) | |
1223 return; | |
1224 | |
1225 if (fContentAssistant.addContentAssistListener(this, ContentAssistant.PROPOSAL_SELECTOR)) { | |
1226 | |
1227 ensureDocumentListenerInstalled(); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1228 |
129 | 1229 if (fFocusHelper is null) { |
135 | 1230 fFocusHelper= new class() IEditingSupport { |
129 | 1231 |
1232 public bool isOriginator(DocumentEvent event, IRegion focus) { | |
1233 return false; // this helper just covers the focus change to the proposal shell, no remote editions | |
1234 } | |
1235 | |
1236 public bool ownsFocusShell() { | |
1237 return true; | |
1238 } | |
1239 | |
1240 }; | |
1241 } | |
138 | 1242 if ( cast(IEditingSupportRegistry)fViewer ) { |
134 | 1243 IEditingSupportRegistry registry= cast(IEditingSupportRegistry) fViewer; |
129 | 1244 registry.register(fFocusHelper); |
1245 } | |
1246 | |
1247 | |
1248 /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=52646 | |
1249 * on GTK, setVisible and such may run the event loop | |
1250 * (see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=47511) | |
1251 * Since the user may have already canceled the popup or selected | |
1252 * an entry (ESC or RETURN), we have to double check whether | |
1253 * the table is still okToUse. See comments below | |
1254 */ | |
1255 fProposalShell.setVisible(true); // may run event loop on GTK | |
1256 // transfer focus since no verify key listener can be attached | |
1257 if (!fContentAssistSubjectControlAdapter.supportsVerifyKeyListener() && Helper.okToUse(fProposalShell)) | |
1258 fProposalShell.setFocus(); // may run event loop on GTK ?? | |
1259 | |
1260 if (fAdditionalInfoController !is null && Helper.okToUse(fProposalTable)) { | |
1261 fAdditionalInfoController.install(fProposalTable); | |
1262 fAdditionalInfoController.handleTableSelectionChanged(); | |
1263 } | |
1264 } else | |
1265 hide(); | |
1266 } | |
1267 | |
1268 /** | |
1269 * Installs the document listener if not already done. | |
1270 * | |
1271 * @since 3.2 | |
1272 */ | |
1273 private void ensureDocumentListenerInstalled() { | |
1274 if (fDocumentListener is null) { | |
135 | 1275 fDocumentListener= new class() IDocumentListener { |
129 | 1276 public void documentAboutToBeChanged(DocumentEvent event) { |
1277 if (!fInserting) | |
1278 fDocumentEvents.add(event); | |
1279 } | |
1280 | |
1281 public void documentChanged(DocumentEvent event) { | |
1282 if (!fInserting) | |
1283 filterProposals(); | |
1284 } | |
1285 }; | |
1286 IDocument document= fContentAssistSubjectControlAdapter.getDocument(); | |
1287 if (document !is null) | |
1288 document.addDocumentListener(fDocumentListener); | |
1289 } | |
1290 } | |
1291 | |
1292 /* | |
1293 * @see IContentAssistListener#verifyKey(VerifyEvent) | |
1294 */ | |
1295 public bool verifyKey(VerifyEvent e) { | |
1296 if (!Helper.okToUse(fProposalShell)) | |
1297 return true; | |
1298 | |
1299 char key= e.character; | |
1300 if (key is 0) { | |
1301 int newSelection= fProposalTable.getSelectionIndex(); | |
1302 int visibleRows= (fProposalTable.getSize().y / fProposalTable.getItemHeight()) - 1; | |
1303 int itemCount= fProposalTable.getItemCount(); | |
1304 bool smartToggle= false; | |
1305 switch (e.keyCode) { | |
1306 | |
1307 case DWT.ARROW_LEFT : | |
1308 case DWT.ARROW_RIGHT : | |
1309 filterProposals(); | |
1310 return true; | |
1311 | |
1312 case DWT.ARROW_UP : | |
1313 newSelection -= 1; | |
1314 if (newSelection < 0) | |
1315 newSelection= itemCount - 1; | |
1316 break; | |
1317 | |
1318 case DWT.ARROW_DOWN : | |
1319 newSelection += 1; | |
1320 if (newSelection > itemCount - 1) | |
1321 newSelection= 0; | |
1322 break; | |
1323 | |
1324 case DWT.PAGE_DOWN : | |
1325 newSelection += visibleRows; | |
1326 if (newSelection >= itemCount) | |
1327 newSelection= itemCount - 1; | |
1328 break; | |
1329 | |
1330 case DWT.PAGE_UP : | |
1331 newSelection -= visibleRows; | |
1332 if (newSelection < 0) | |
1333 newSelection= 0; | |
1334 break; | |
1335 | |
1336 case DWT.HOME : | |
1337 newSelection= 0; | |
1338 break; | |
1339 | |
1340 case DWT.END : | |
1341 newSelection= itemCount - 1; | |
1342 break; | |
1343 | |
1344 default : | |
1345 if (e.keyCode !is DWT.CAPS_LOCK && e.keyCode !is DWT.MOD1 && e.keyCode !is DWT.MOD2 && e.keyCode !is DWT.MOD3 && e.keyCode !is DWT.MOD4) | |
1346 hide(); | |
1347 return true; | |
1348 } | |
1349 | |
1350 selectProposal(newSelection, smartToggle); | |
1351 | |
1352 e.doit= false; | |
1353 return false; | |
1354 | |
1355 } | |
1356 | |
1357 // key !is 0 | |
1358 switch (key) { | |
1359 case 0x1B: // Esc | |
1360 e.doit= false; | |
1361 hide(); | |
1362 break; | |
1363 | |
1364 case '\n': // Ctrl-Enter on w2k | |
1365 case '\r': // Enter | |
1366 e.doit= false; | |
1367 insertSelectedProposalWithMask(e.stateMask); | |
1368 break; | |
1369 | |
1370 case '\t': | |
1371 e.doit= false; | |
1372 fProposalShell.setFocus(); | |
1373 return false; | |
1374 | |
1375 default: | |
1376 ICompletionProposal p= getSelectedProposal(); | |
138 | 1377 if ( cast(ICompletionProposalExtension)p ) { |
134 | 1378 ICompletionProposalExtension t= cast(ICompletionProposalExtension) p; |
129 | 1379 char[] triggers= t.getTriggerCharacters(); |
1380 if (contains(triggers, key)) { | |
1381 e.doit= false; | |
1382 hide(); | |
1383 insertProposal(p, key, e.stateMask, fContentAssistSubjectControlAdapter.getSelectedRange().x); | |
1384 } | |
1385 } | |
1386 } | |
1387 | |
1388 return true; | |
1389 } | |
1390 | |
1391 /** | |
1392 * Selects the entry with the given index in the proposal selector and feeds | |
1393 * the selection to the additional info controller. | |
1394 * | |
1395 * @param index the index in the list | |
1396 * @param smartToggle <code>true</code> if the smart toggle key has been pressed | |
1397 * @since 2.1 | |
1398 */ | |
1399 private void selectProposal(int index, bool smartToggle) { | |
1400 | |
1401 ICompletionProposal oldProposal= getSelectedProposal(); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1402 if (cast(ICompletionProposalExtension2)oldProposal && fViewer !is null) |
134 | 1403 (cast(ICompletionProposalExtension2) oldProposal).unselected(fViewer); |
129 | 1404 |
1405 if (fFilteredProposals is null) { | |
1406 fireSelectionEvent(null, smartToggle); | |
1407 return; | |
1408 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1409 |
129 | 1410 ICompletionProposal proposal= fFilteredProposals[index]; |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1411 if (cast(ICompletionProposalExtension2)proposal && fViewer !is null) |
134 | 1412 (cast(ICompletionProposalExtension2) proposal).selected(fViewer, smartToggle); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1413 |
129 | 1414 fireSelectionEvent(proposal, smartToggle); |
1415 | |
1416 fLastProposal= proposal; | |
1417 | |
1418 fProposalTable.setSelection(index); | |
1419 fProposalTable.showSelection(); | |
1420 if (fAdditionalInfoController !is null) | |
1421 fAdditionalInfoController.handleTableSelectionChanged(); | |
1422 } | |
1423 | |
1424 /** | |
1425 * Fires a selection event, see {@link ICompletionListener}. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1426 * |
129 | 1427 * @param proposal the selected proposal, possibly <code>null</code> |
1428 * @param smartToggle true if the smart toggle is on | |
1429 * @since 3.2 | |
1430 */ | |
1431 private void fireSelectionEvent(ICompletionProposal proposal, bool smartToggle) { | |
1432 fContentAssistant.fireSelectionEvent(proposal, smartToggle); | |
1433 } | |
1434 | |
1435 /** | |
1436 * Returns whether the given character is contained in the given array of | |
1437 * characters. | |
1438 * | |
1439 * @param characters the list of characters | |
1440 * @param c the character to look for in the list | |
1441 * @return <code>true</code> if character belongs to the list | |
1442 * @since 2.0 | |
1443 */ | |
1444 private bool contains(char[] characters, char c) { | |
1445 | |
1446 if (characters is null) | |
1447 return false; | |
1448 | |
1449 for (int i= 0; i < characters.length; i++) { | |
1450 if (c is characters[i]) | |
1451 return true; | |
1452 } | |
1453 | |
1454 return false; | |
1455 } | |
1456 | |
1457 /* | |
1458 * @see IEventConsumer#processEvent(VerifyEvent) | |
1459 */ | |
1460 public void processEvent(VerifyEvent e) { | |
1461 } | |
1462 | |
1463 /** | |
1464 * Filters the displayed proposal based on the given cursor position and the | |
1465 * offset of the original invocation of the content assistant. | |
1466 */ | |
1467 private void filterProposals() { | |
1468 if (!fIsFilterPending) { | |
1469 fIsFilterPending= true; | |
1470 Control control= fContentAssistSubjectControlAdapter.getControl(); | |
1471 control.getDisplay().asyncExec(fFilterRunnable); | |
1472 } | |
1473 } | |
1474 | |
1475 /** | |
1476 * Computes the subset of already computed proposals that are still valid for | |
1477 * the given offset. | |
1478 * | |
1479 * @param offset the offset | |
1480 * @param event the merged document event | |
1481 * @return the set of filtered proposals | |
1482 * @since 3.0 | |
1483 */ | |
1484 private ICompletionProposal[] computeFilteredProposals(int offset, DocumentEvent event) { | |
1485 | |
1486 if (offset is fInvocationOffset && event is null) { | |
1487 fIsFilteredSubset= false; | |
1488 return fComputedProposals; | |
1489 } | |
1490 | |
1491 if (offset < fInvocationOffset) { | |
1492 fIsFilteredSubset= false; | |
1493 fInvocationOffset= offset; | |
1494 fContentAssistant.fireSessionRestartEvent(); | |
1495 fComputedProposals= computeProposals(fInvocationOffset); | |
1496 return fComputedProposals; | |
1497 } | |
1498 | |
1499 ICompletionProposal[] proposals; | |
1500 if (offset < fFilterOffset) { | |
1501 proposals= fComputedProposals; | |
1502 fIsFilteredSubset= false; | |
1503 } else { | |
1504 proposals= fFilteredProposals; | |
1505 fIsFilteredSubset= true; | |
1506 } | |
1507 | |
1508 if (proposals is null) { | |
1509 fIsFilteredSubset= false; | |
1510 return null; | |
1511 } | |
1512 | |
1513 IDocument document= fContentAssistSubjectControlAdapter.getDocument(); | |
1514 int length= proposals.length; | |
1515 List filtered= new ArrayList(length); | |
1516 for (int i= 0; i < length; i++) { | |
1517 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1518 if (cast(ICompletionProposalExtension2)proposals[i] ) { |
129 | 1519 |
134 | 1520 ICompletionProposalExtension2 p= cast(ICompletionProposalExtension2) proposals[i]; |
129 | 1521 if (p.validate(document, offset, event)) |
1522 filtered.add(p); | |
1523 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1524 } else if (cast(ICompletionProposalExtension)proposals[i] ) { |
129 | 1525 |
134 | 1526 ICompletionProposalExtension p= cast(ICompletionProposalExtension) proposals[i]; |
129 | 1527 if (p.isValidFor(document, offset)) |
1528 filtered.add(p); | |
1529 | |
1530 } else { | |
1531 // restore original behavior | |
1532 fIsFilteredSubset= false; | |
1533 fInvocationOffset= offset; | |
1534 fContentAssistant.fireSessionRestartEvent(); | |
1535 fComputedProposals= computeProposals(fInvocationOffset); | |
1536 return fComputedProposals; | |
1537 } | |
1538 } | |
1539 | |
158 | 1540 return arraycast!(ICompletionProposal)( filtered.toArray()); |
129 | 1541 } |
1542 | |
1543 /** | |
1544 * Requests the proposal shell to take focus. | |
1545 * | |
1546 * @since 3.0 | |
1547 */ | |
1548 public void setFocus() { | |
1549 if (Helper.okToUse(fProposalShell)) { | |
1550 fProposalShell.setFocus(); | |
1551 } | |
1552 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1553 |
129 | 1554 /** |
1555 * Returns <code>true</code> if <code>proposal</code> should be auto-inserted, | |
1556 * <code>false</code> otherwise. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1557 * |
129 | 1558 * @param proposal the single proposal that might be automatically inserted |
1559 * @return <code>true</code> if <code>proposal</code> can be inserted automatically, | |
1560 * <code>false</code> otherwise | |
1561 * @since 3.1 | |
1562 */ | |
1563 private bool canAutoInsert(ICompletionProposal proposal) { | |
1564 if (fContentAssistant.isAutoInserting()) { | |
138 | 1565 if ( cast(ICompletionProposalExtension4)proposal ) { |
134 | 1566 ICompletionProposalExtension4 ext= cast(ICompletionProposalExtension4) proposal; |
129 | 1567 return ext.isAutoInsertable(); |
1568 } | |
1569 return true; // default behavior before ICompletionProposalExtension4 was introduced | |
1570 } | |
1571 return false; | |
1572 } | |
1573 | |
1574 /** | |
1575 * Completes the common prefix of all proposals directly in the code. If no | |
1576 * common prefix can be found, the proposal popup is shown. | |
1577 * | |
1578 * @return an error message if completion failed. | |
1579 * @since 3.0 | |
1580 */ | |
1581 public String incrementalComplete() { | |
1582 if (Helper.okToUse(fProposalShell) && fFilteredProposals !is null) { | |
1583 if (fLastCompletionOffset is fFilterOffset) { | |
1584 handleRepeatedInvocation(); | |
1585 } else { | |
1586 fLastCompletionOffset= fFilterOffset; | |
1587 completeCommonPrefix(); | |
1588 } | |
1589 } else { | |
1590 final Control control= fContentAssistSubjectControlAdapter.getControl(); | |
1591 | |
1592 if (fKeyListener is null) | |
1593 fKeyListener= new ProposalSelectionListener(); | |
1594 | |
1595 if (!Helper.okToUse(fProposalShell) && !control.isDisposed()) | |
1596 fContentAssistSubjectControlAdapter.addKeyListener(fKeyListener); | |
1597 | |
135 | 1598 BusyIndicator.showWhile(control.getDisplay(), new class() Runnable { |
129 | 1599 public void run() { |
1600 | |
1601 fInvocationOffset= fContentAssistSubjectControlAdapter.getSelectedRange().x; | |
1602 fFilterOffset= fInvocationOffset; | |
1603 fLastCompletionOffset= fFilterOffset; | |
1604 fFilteredProposals= computeProposals(fInvocationOffset); | |
1605 | |
1606 int count= (fFilteredProposals is null ? 0 : fFilteredProposals.length); | |
1607 if (count is 0 && hideWhenNoProposals(false)) | |
1608 return; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1609 |
129 | 1610 if (count is 1 && canAutoInsert(fFilteredProposals[0])) { |
134 | 1611 insertProposal(fFilteredProposals[0], cast(wchar) 0, 0, fInvocationOffset); |
129 | 1612 hide(); |
1613 } else { | |
1614 ensureDocumentListenerInstalled(); | |
1615 if (count > 0 && completeCommonPrefix()) | |
1616 hide(); | |
1617 else { | |
1618 fComputedProposals= fFilteredProposals; | |
1619 createProposalSelector(); | |
1620 setProposals(fComputedProposals, false); | |
1621 displayProposals(); | |
1622 } | |
1623 } | |
1624 } | |
1625 }); | |
1626 } | |
1627 return getErrorMessage(); | |
1628 } | |
1629 | |
1630 /** | |
1631 * Acts upon <code>fFilteredProposals</code>: if there is just one valid | |
1632 * proposal, it is inserted, otherwise, the common prefix of all proposals | |
1633 * is inserted into the document. If there is no common prefix, nothing | |
1634 * happens and <code>false</code> is returned. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1635 * |
129 | 1636 * @return <code>true</code> if a single proposal was inserted and the |
1637 * selector can be closed, <code>false</code> otherwise | |
1638 * @since 3.0 | |
1639 */ | |
1640 private bool completeCommonPrefix() { | |
1641 | |
1642 // 0: insert single proposals | |
1643 if (fFilteredProposals.length is 1) { | |
1644 if (canAutoInsert(fFilteredProposals[0])) { | |
134 | 1645 insertProposal(fFilteredProposals[0], cast(wchar) 0, 0, fFilterOffset); |
129 | 1646 hide(); |
1647 return true; | |
1648 } | |
1649 return false; | |
1650 } | |
1651 | |
1652 // 1: extract pre- and postfix from all remaining proposals | |
1653 IDocument document= fContentAssistSubjectControlAdapter.getDocument(); | |
1654 | |
1655 // contains the common postfix in the case that there are any proposals matching our LHS | |
1656 StringBuffer rightCasePostfix= null; | |
1657 List rightCase= new ArrayList(); | |
1658 | |
1659 bool isWrongCaseMatch= false; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1660 |
129 | 1661 // the prefix of all case insensitive matches. This differs from the document |
1662 // contents and will be replaced. | |
1663 CharSequence wrongCasePrefix= null; | |
1664 int wrongCasePrefixStart= 0; | |
1665 // contains the common postfix of all case-insensitive matches | |
1666 StringBuffer wrongCasePostfix= null; | |
1667 List wrongCase= new ArrayList(); | |
1668 | |
1669 for (int i= 0; i < fFilteredProposals.length; i++) { | |
1670 ICompletionProposal proposal= fFilteredProposals[i]; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1671 |
138 | 1672 if (!( cast(ICompletionProposalExtension3)proposal )) |
129 | 1673 return false; |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1674 |
134 | 1675 int start= (cast(ICompletionProposalExtension3)proposal).getPrefixCompletionStart(fContentAssistSubjectControlAdapter.getDocument(), fFilterOffset); |
1676 CharSequence insertion= (cast(ICompletionProposalExtension3)proposal).getPrefixCompletionText(fContentAssistSubjectControlAdapter.getDocument(), fFilterOffset); | |
129 | 1677 if (insertion is null) |
1678 insertion= proposal.getDisplayString(); | |
1679 try { | |
1680 int prefixLength= fFilterOffset - start; | |
1681 int relativeCompletionOffset= Math.min(insertion.length(), prefixLength); | |
1682 String prefix= document.get(start, prefixLength); | |
1683 if (!isWrongCaseMatch && insertion.toString().startsWith(prefix)) { | |
1684 isWrongCaseMatch= false; | |
1685 rightCase.add(proposal); | |
1686 CharSequence newPostfix= insertion.subSequence(relativeCompletionOffset, insertion.length()); | |
1687 if (rightCasePostfix is null) | |
1688 rightCasePostfix= new StringBuffer(newPostfix.toString()); | |
1689 else | |
1690 truncatePostfix(rightCasePostfix, newPostfix); | |
1691 } else if (i is 0 || isWrongCaseMatch) { | |
1692 CharSequence newPrefix= insertion.subSequence(0, relativeCompletionOffset); | |
1693 if (isPrefixCompatible(wrongCasePrefix, wrongCasePrefixStart, newPrefix, start, document)) { | |
1694 isWrongCaseMatch= true; | |
1695 wrongCasePrefix= newPrefix; | |
1696 wrongCasePrefixStart= start; | |
1697 CharSequence newPostfix= insertion.subSequence(relativeCompletionOffset, insertion.length()); | |
1698 if (wrongCasePostfix is null) | |
1699 wrongCasePostfix= new StringBuffer(newPostfix.toString()); | |
1700 else | |
1701 truncatePostfix(wrongCasePostfix, newPostfix); | |
1702 wrongCase.add(proposal); | |
1703 } else { | |
1704 return false; | |
1705 } | |
1706 } else | |
1707 return false; | |
1708 } catch (BadLocationException e2) { | |
1709 // bail out silently | |
1710 return false; | |
1711 } | |
1712 | |
1713 if (rightCasePostfix !is null && rightCasePostfix.length() is 0 && rightCase.size() > 1) | |
1714 return false; | |
1715 } | |
1716 | |
1717 // 2: replace single proposals | |
1718 | |
1719 if (rightCase.size() is 1) { | |
134 | 1720 ICompletionProposal proposal= cast(ICompletionProposal) rightCase.get(0); |
129 | 1721 if (canAutoInsert(proposal) && rightCasePostfix.length() > 0) { |
134 | 1722 insertProposal(proposal, cast(wchar) 0, 0, fInvocationOffset); |
129 | 1723 hide(); |
1724 return true; | |
1725 } | |
1726 return false; | |
1727 } else if (isWrongCaseMatch && wrongCase.size() is 1) { | |
134 | 1728 ICompletionProposal proposal= cast(ICompletionProposal) wrongCase.get(0); |
129 | 1729 if (canAutoInsert(proposal)) { |
134 | 1730 insertProposal(proposal, cast(wchar) 0, 0, fInvocationOffset); |
129 | 1731 hide(); |
1732 return true; | |
1733 } | |
1734 return false; | |
1735 } | |
1736 | |
1737 // 3: replace post- / prefixes | |
1738 | |
1739 CharSequence prefix; | |
1740 if (isWrongCaseMatch) | |
1741 prefix= wrongCasePrefix; | |
1742 else | |
1743 prefix= ""; //$NON-NLS-1$ | |
1744 | |
1745 CharSequence postfix; | |
1746 if (isWrongCaseMatch) | |
1747 postfix= wrongCasePostfix; | |
1748 else | |
1749 postfix= rightCasePostfix; | |
1750 | |
1751 if (prefix is null || postfix is null) | |
1752 return false; | |
1753 | |
1754 try { | |
1755 // 4: check if parts of the postfix are already in the document | |
1756 int to= Math.min(document.getLength(), fFilterOffset + postfix.length()); | |
1757 StringBuffer inDocument= new StringBuffer(document.get(fFilterOffset, to - fFilterOffset)); | |
1758 truncatePostfix(inDocument, postfix); | |
1759 | |
1760 // 5: replace and reveal | |
1761 document.replace(fFilterOffset - prefix.length(), prefix.length() + inDocument.length(), prefix.toString() + postfix.toString()); | |
1762 | |
1763 fContentAssistSubjectControlAdapter.setSelectedRange(fFilterOffset + postfix.length(), 0); | |
1764 fContentAssistSubjectControlAdapter.revealRange(fFilterOffset + postfix.length(), 0); | |
1765 fFilterOffset+= postfix.length(); | |
1766 fLastCompletionOffset= fFilterOffset; | |
1767 | |
1768 return false; | |
1769 } catch (BadLocationException e) { | |
1770 // ignore and return false | |
1771 return false; | |
1772 } | |
1773 } | |
1774 | |
1775 /* | |
1776 * @since 3.1 | |
1777 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
135
diff
changeset
|
1778 private bool isPrefixCompatible(CharSequence oneSequence, int oneOffset, CharSequence twoSequence, int twoOffset, IDocument document) { |
129 | 1779 if (oneSequence is null || twoSequence is null) |
1780 return true; | |
1781 | |
1782 int min= Math.min(oneOffset, twoOffset); | |
1783 int oneEnd= oneOffset + oneSequence.length(); | |
1784 int twoEnd= twoOffset + twoSequence.length(); | |
1785 | |
1786 String one= document.get(oneOffset, min - oneOffset) + oneSequence + document.get(oneEnd, Math.min(fFilterOffset, fFilterOffset - oneEnd)); | |
1787 String two= document.get(twoOffset, min - twoOffset) + twoSequence + document.get(twoEnd, Math.min(fFilterOffset, fFilterOffset - twoEnd)); | |
1788 | |
1789 return one.equals(two); | |
1790 } | |
1791 | |
1792 /** | |
1793 * Truncates <code>buffer</code> to the common prefix of <code>buffer</code> | |
1794 * and <code>sequence</code>. | |
1795 * | |
1796 * @param buffer the common postfix to truncate | |
1797 * @param sequence the characters to truncate with | |
1798 */ | |
1799 private void truncatePostfix(StringBuffer buffer, CharSequence sequence) { | |
1800 // find common prefix | |
1801 int min= Math.min(buffer.length(), sequence.length()); | |
1802 for (int c= 0; c < min; c++) { | |
1803 if (sequence.charAt(c) !is buffer.charAt(c)) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1804 buffer.delete_(c, buffer.length()); |
129 | 1805 return; |
1806 } | |
1807 } | |
1808 | |
1809 // all equal up to minimum | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1810 buffer.delete_(min, buffer.length()); |
129 | 1811 } |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1812 |
129 | 1813 /** |
1814 * Sets the message for the repetition affordance text at the bottom of the proposal. Only has | |
1815 * an effect if {@link ContentAssistant#isRepeatedInvocationMode()} returns <code>true</code>. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1816 * |
129 | 1817 * @param message the new caption |
1818 * @since 3.2 | |
1819 */ | |
1820 void setMessage(String message) { | |
1821 Assert.isNotNull(message); | |
1822 if (isActive() && fMessageText !is null) | |
1823 fMessageText.setText(message + " "); //$NON-NLS-1$ | |
1824 } | |
1825 | |
1826 /** | |
1827 * Sets the text to be displayed if no proposals are available. Only has an effect if | |
1828 * {@link ContentAssistant#isShowEmptyList()} returns <code>true</code>. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1829 * |
129 | 1830 * @param message the empty message |
1831 * @since 3.2 | |
1832 */ | |
1833 void setEmptyMessage(String message) { | |
1834 Assert.isNotNull(message); | |
1835 fEmptyMessage= message; | |
1836 } | |
1837 | |
1838 /** | |
1839 * Enables or disables showing of the caption line. See also {@link #setMessage(String)}. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1840 * |
129 | 1841 * @param show |
1842 * @since 3.2 | |
1843 */ | |
1844 public void setStatusLineVisible(bool show) { | |
1845 if (!isActive() || show is (fMessageText !is null)) | |
1846 return; // nothing to do | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1847 |
129 | 1848 if (show) { |
1849 createMessageText(); | |
1850 } else { | |
1851 fMessageText.dispose(); | |
1852 fMessageText= null; | |
1853 } | |
1854 fProposalShell.layout(); | |
1855 } | |
1856 | |
1857 /** | |
1858 * Informs the popup that it is being placed above the caret line instead of below. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1859 * |
129 | 1860 * @param above <code>true</code> if the location of the popup is above the caret line, <code>false</code> if it is below |
1861 * @since 3.3 | |
1862 */ | |
1863 void switchedPositionToAbove(bool above) { | |
1864 if (fAdditionalInfoController !is null) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1865 fAdditionalInfoController.setFallbackAnchors([ |
129 | 1866 AbstractInformationControlManager.ANCHOR_RIGHT, |
1867 AbstractInformationControlManager.ANCHOR_LEFT, | |
1868 above ? AbstractInformationControlManager.ANCHOR_TOP : AbstractInformationControlManager.ANCHOR_BOTTOM | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1869 ]); |
129 | 1870 } |
1871 } | |
1872 | |
1873 /** | |
1874 * Returns a new proposal selection handler. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1875 * |
129 | 1876 * @param operationCode the operation code |
1877 * @return the handler | |
1878 * @since 3.4 | |
1879 */ | |
1880 IHandler createProposalSelectionHandler(int operationCode) { | |
1881 return new ProposalSelectionHandler(operationCode); | |
1882 } | |
1883 | |
1884 } |