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