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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13
|
|
14 module dwtx.jface.text.templates.PositionBasedCompletionProposal;
|
|
15
|
|
16 import dwt.dwthelper.utils;
|
|
17
|
|
18
|
|
19
|
|
20
|
|
21
|
|
22 import dwt.graphics.Image;
|
|
23 import dwt.graphics.Point;
|
|
24 import dwtx.core.runtime.Assert;
|
|
25 import dwtx.jface.text.BadLocationException;
|
|
26 import dwtx.jface.text.DocumentEvent;
|
|
27 import dwtx.jface.text.IDocument;
|
|
28 import dwtx.jface.text.ITextViewer;
|
|
29 import dwtx.jface.text.Position;
|
|
30 import dwtx.jface.text.contentassist.ICompletionProposal;
|
|
31 import dwtx.jface.text.contentassist.ICompletionProposalExtension2;
|
|
32 import dwtx.jface.text.contentassist.IContextInformation;
|
|
33
|
|
34
|
|
35 /**
|
|
36 * A position based completion proposal.
|
|
37 *
|
|
38 * @since 3.0
|
|
39 */
|
|
40 final class PositionBasedCompletionProposal : ICompletionProposal, ICompletionProposalExtension2 {
|
|
41
|
|
42 /** The string to be displayed in the completion proposal popup */
|
|
43 private String fDisplayString;
|
|
44 /** The replacement string */
|
|
45 private String fReplacementString;
|
|
46 /** The replacement position. */
|
|
47 private Position fReplacementPosition;
|
|
48 /** The cursor position after this proposal has been applied */
|
|
49 private int fCursorPosition;
|
|
50 /** The image to be displayed in the completion proposal popup */
|
|
51 private Image fImage;
|
|
52 /** The context information of this proposal */
|
|
53 private IContextInformation fContextInformation;
|
|
54 /** The additional info of this proposal */
|
|
55 private String fAdditionalProposalInfo;
|
|
56
|
|
57 /**
|
|
58 * Creates a new completion proposal based on the provided information. The replacement string is
|
|
59 * considered being the display string too. All remaining fields are set to <code>null</code>.
|
|
60 *
|
|
61 * @param replacementString the actual string to be inserted into the document
|
|
62 * @param replacementPosition the position of the text to be replaced
|
|
63 * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
|
|
64 */
|
|
65 public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition) {
|
|
66 this(replacementString, replacementPosition, cursorPosition, null, null, null, null);
|
|
67 }
|
|
68
|
|
69 /**
|
|
70 * Creates a new completion proposal. All fields are initialized based on the provided information.
|
|
71 *
|
|
72 * @param replacementString the actual string to be inserted into the document
|
|
73 * @param replacementPosition the position of the text to be replaced
|
|
74 * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
|
|
75 * @param image the image to display for this proposal
|
|
76 * @param displayString the string to be displayed for the proposal
|
|
77 * @param contextInformation the context information associated with this proposal
|
|
78 * @param additionalProposalInfo the additional information associated with this proposal
|
|
79 */
|
|
80 public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition, Image image, String displayString, IContextInformation contextInformation, String additionalProposalInfo) {
|
|
81 Assert.isNotNull(replacementString);
|
|
82 Assert.isTrue(replacementPosition !is null);
|
|
83
|
|
84 fReplacementString= replacementString;
|
|
85 fReplacementPosition= replacementPosition;
|
|
86 fCursorPosition= cursorPosition;
|
|
87 fImage= image;
|
|
88 fDisplayString= displayString;
|
|
89 fContextInformation= contextInformation;
|
|
90 fAdditionalProposalInfo= additionalProposalInfo;
|
|
91 }
|
|
92
|
|
93 /*
|
|
94 * @see ICompletionProposal#apply(IDocument)
|
|
95 */
|
|
96 public void apply(IDocument document) {
|
|
97 try {
|
|
98 document.replace(fReplacementPosition.getOffset(), fReplacementPosition.getLength(), fReplacementString);
|
|
99 } catch (BadLocationException x) {
|
|
100 // ignore
|
|
101 }
|
|
102 }
|
|
103
|
|
104 /*
|
|
105 * @see ICompletionProposal#getSelection(IDocument)
|
|
106 */
|
|
107 public Point getSelection(IDocument document) {
|
|
108 return new Point(fReplacementPosition.getOffset() + fCursorPosition, 0);
|
|
109 }
|
|
110
|
|
111 /*
|
|
112 * @see ICompletionProposal#getContextInformation()
|
|
113 */
|
|
114 public IContextInformation getContextInformation() {
|
|
115 return fContextInformation;
|
|
116 }
|
|
117
|
|
118 /*
|
|
119 * @see ICompletionProposal#getImage()
|
|
120 */
|
|
121 public Image getImage() {
|
|
122 return fImage;
|
|
123 }
|
|
124
|
|
125 /*
|
|
126 * @see dwtx.jface.text.contentassist.ICompletionProposal#getDisplayString()
|
|
127 */
|
|
128 public String getDisplayString() {
|
|
129 if (fDisplayString !is null)
|
|
130 return fDisplayString;
|
|
131 return fReplacementString;
|
|
132 }
|
|
133
|
|
134 /*
|
|
135 * @see ICompletionProposal#getAdditionalProposalInfo()
|
|
136 */
|
|
137 public String getAdditionalProposalInfo() {
|
|
138 return fAdditionalProposalInfo;
|
|
139 }
|
|
140
|
|
141 /*
|
|
142 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension2#apply(dwtx.jface.text.ITextViewer, char, int, int)
|
|
143 */
|
|
144 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
|
|
145 apply(viewer.getDocument());
|
|
146 }
|
|
147
|
|
148 /*
|
|
149 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension2#selected(dwtx.jface.text.ITextViewer, bool)
|
|
150 */
|
|
151 public void selected(ITextViewer viewer, bool smartToggle) {
|
|
152 }
|
|
153
|
|
154 /*
|
|
155 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension2#unselected(dwtx.jface.text.ITextViewer)
|
|
156 */
|
|
157 public void unselected(ITextViewer viewer) {
|
|
158 }
|
|
159
|
|
160 /*
|
|
161 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension2#validate(dwtx.jface.text.IDocument, int, dwtx.jface.text.DocumentEvent)
|
|
162 */
|
|
163 public bool validate(IDocument document, int offset, DocumentEvent event) {
|
|
164 try {
|
|
165 String content= document.get(fReplacementPosition.getOffset(), offset - fReplacementPosition.getOffset());
|
|
166 if (fReplacementString.startsWith(content))
|
|
167 return true;
|
|
168 } catch (BadLocationException e) {
|
|
169 // ignore concurrently modified document
|
|
170 }
|
|
171 return false;
|
|
172 }
|
|
173
|
|
174 }
|