1 | package com.framsticks.framclipse.editors.codeCompletion; |
---|
2 | |
---|
3 | import org.eclipse.core.runtime.Assert; |
---|
4 | import org.eclipse.jface.text.BadLocationException; |
---|
5 | import org.eclipse.jface.text.IDocument; |
---|
6 | import org.eclipse.jface.text.contentassist.ICompletionProposal; |
---|
7 | import org.eclipse.jface.text.contentassist.IContextInformation; |
---|
8 | import org.eclipse.swt.graphics.Image; |
---|
9 | import org.eclipse.swt.graphics.Point; |
---|
10 | |
---|
11 | public class OrderableCompletionProposal implements ICompletionProposal, |
---|
12 | Comparable<OrderableCompletionProposal> { |
---|
13 | |
---|
14 | /** The string to be displayed in the completion proposal popup. */ |
---|
15 | private String fDisplayString; |
---|
16 | /** The replacement string. */ |
---|
17 | private String fReplacementString; |
---|
18 | /** The replacement offset. */ |
---|
19 | private int fReplacementOffset; |
---|
20 | /** The replacement length. */ |
---|
21 | private int fReplacementLength; |
---|
22 | /** The cursor position after this proposal has been applied. */ |
---|
23 | private int fCursorPosition; |
---|
24 | /** The image to be displayed in the completion proposal popup. */ |
---|
25 | private Image fImage; |
---|
26 | /** The context information of this proposal. */ |
---|
27 | private IContextInformation fContextInformation; |
---|
28 | /** The additional info of this proposal. */ |
---|
29 | private String fAdditionalProposalInfo; |
---|
30 | |
---|
31 | private boolean function; |
---|
32 | private String name; |
---|
33 | private String type; |
---|
34 | |
---|
35 | /** |
---|
36 | * Creates a new completion proposal based on the provided information. The |
---|
37 | * replacement string is considered being the display string too. All |
---|
38 | * remaining fields are set to <code>null</code>. |
---|
39 | * |
---|
40 | * @param replacementString |
---|
41 | * the actual string to be inserted into the document |
---|
42 | * @param replacementOffset |
---|
43 | * the offset of the text to be replaced |
---|
44 | * @param replacementLength |
---|
45 | * the length of the text to be replaced |
---|
46 | * @param cursorPosition |
---|
47 | * the position of the cursor following the insert relative to |
---|
48 | * replacementOffset |
---|
49 | */ |
---|
50 | public OrderableCompletionProposal(String replacementString, int replacementOffset, |
---|
51 | int replacementLength, int cursorPosition) { |
---|
52 | this(replacementString, replacementOffset, replacementLength, cursorPosition, null, null, |
---|
53 | null, null); |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Creates a new completion proposal. All fields are initialized based on |
---|
58 | * the provided information. |
---|
59 | * |
---|
60 | * @param replacementString |
---|
61 | * the actual string to be inserted into the document |
---|
62 | * @param replacementOffset |
---|
63 | * the offset of the text to be replaced |
---|
64 | * @param replacementLength |
---|
65 | * the length of the text to be replaced |
---|
66 | * @param cursorPosition |
---|
67 | * the position of the cursor following the insert relative to |
---|
68 | * replacementOffset |
---|
69 | * @param image |
---|
70 | * the image to display for this proposal |
---|
71 | * @param displayString |
---|
72 | * the string to be displayed for the proposal |
---|
73 | * @param contextInformation |
---|
74 | * the context information associated with this proposal |
---|
75 | * @param additionalProposalInfo |
---|
76 | * the additional information associated with this proposal |
---|
77 | */ |
---|
78 | public OrderableCompletionProposal(String replacementString, int replacementOffset, |
---|
79 | int replacementLength, int cursorPosition, Image image, String displayString, |
---|
80 | IContextInformation contextInformation, String additionalProposalInfo) { |
---|
81 | Assert.isNotNull(replacementString); |
---|
82 | Assert.isTrue(replacementOffset >= 0); |
---|
83 | Assert.isTrue(replacementLength >= 0); |
---|
84 | Assert.isTrue(cursorPosition >= 0); |
---|
85 | |
---|
86 | fReplacementString = replacementString; |
---|
87 | fReplacementOffset = replacementOffset; |
---|
88 | fReplacementLength = replacementLength; |
---|
89 | fCursorPosition = cursorPosition; |
---|
90 | fImage = image; |
---|
91 | fDisplayString = displayString; |
---|
92 | fContextInformation = contextInformation; |
---|
93 | fAdditionalProposalInfo = additionalProposalInfo; |
---|
94 | } |
---|
95 | |
---|
96 | /* |
---|
97 | * @see ICompletionProposal#apply(IDocument) |
---|
98 | */ |
---|
99 | public void apply(IDocument document) { |
---|
100 | try { |
---|
101 | document.replace(fReplacementOffset, fReplacementLength, fReplacementString); |
---|
102 | } catch (BadLocationException x) { |
---|
103 | // ignore |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | /* |
---|
108 | * @see ICompletionProposal#getSelection(IDocument) |
---|
109 | */ |
---|
110 | public Point getSelection(IDocument document) { |
---|
111 | return new Point(fReplacementOffset + fCursorPosition, 0); |
---|
112 | } |
---|
113 | |
---|
114 | /* |
---|
115 | * @see ICompletionProposal#getContextInformation() |
---|
116 | */ |
---|
117 | public IContextInformation getContextInformation() { |
---|
118 | return fContextInformation; |
---|
119 | } |
---|
120 | |
---|
121 | /* |
---|
122 | * @see ICompletionProposal#getImage() |
---|
123 | */ |
---|
124 | public Image getImage() { |
---|
125 | return fImage; |
---|
126 | } |
---|
127 | |
---|
128 | /* |
---|
129 | * @see ICompletionProposal#getDisplayString() |
---|
130 | */ |
---|
131 | public String getDisplayString() { |
---|
132 | if (fDisplayString != null) { |
---|
133 | return fDisplayString; |
---|
134 | } |
---|
135 | return fReplacementString; |
---|
136 | } |
---|
137 | |
---|
138 | /* |
---|
139 | * @see ICompletionProposal#getAdditionalProposalInfo() |
---|
140 | */ |
---|
141 | public String getAdditionalProposalInfo() { |
---|
142 | return fAdditionalProposalInfo; |
---|
143 | } |
---|
144 | |
---|
145 | public boolean isFunction() { |
---|
146 | return function; |
---|
147 | } |
---|
148 | |
---|
149 | public void setFunction(boolean function) { |
---|
150 | this.function = function; |
---|
151 | } |
---|
152 | |
---|
153 | public String getName() { |
---|
154 | return name; |
---|
155 | } |
---|
156 | |
---|
157 | public void setName(String name) { |
---|
158 | this.name = name; |
---|
159 | } |
---|
160 | |
---|
161 | public String getType() { |
---|
162 | return type; |
---|
163 | } |
---|
164 | |
---|
165 | public void setType(String type) { |
---|
166 | this.type = type; |
---|
167 | } |
---|
168 | |
---|
169 | public int compareTo(OrderableCompletionProposal o) { |
---|
170 | if (o.function && !function) { |
---|
171 | return -1; |
---|
172 | } |
---|
173 | if (!o.function && function) { |
---|
174 | return 1; |
---|
175 | } |
---|
176 | |
---|
177 | if ((type == null) && (o.type != null)) { |
---|
178 | int typeNameComparison = -1 * o.type.compareTo(name); |
---|
179 | if (typeNameComparison == 0) { |
---|
180 | return -1; |
---|
181 | } |
---|
182 | return typeNameComparison; |
---|
183 | } |
---|
184 | if (type != null) { |
---|
185 | if (o.type == null) { |
---|
186 | int typeNameComparison = type.compareTo(o.name); |
---|
187 | if (typeNameComparison == 0) { |
---|
188 | return 1; |
---|
189 | } |
---|
190 | return typeNameComparison; |
---|
191 | } |
---|
192 | |
---|
193 | int typeComparison = type.compareTo(o.type); |
---|
194 | if (typeComparison != 0) { |
---|
195 | return typeComparison; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | return name.compareTo(o.name); |
---|
200 | } |
---|
201 | } |
---|