-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathFunctionCompletion.java
More file actions
322 lines (271 loc) · 7.62 KB
/
FunctionCompletion.java
File metadata and controls
322 lines (271 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* 12/22/2008
*
* FunctionCompletion.java - A completion representing a function.
*
* This library is distributed under a modified BSD license. See the included
* RSyntaxTextArea.License.txt file for details.
*/
package org.fife.ui.autocomplete;
import java.util.ArrayList;
import java.util.List;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.Position;
/**
* A completion choice representing a function.
*
* @author Robert Futrell
* @version 1.0
*/
public class FunctionCompletion extends VariableCompletion
implements ParameterizedCompletion {
/**
* Parameters to the function.
*/
private List<Parameter> params;
/**
* A description of the return value of this function.
*/
private String returnValDesc;
/**
* Constructor.
*
* @param provider The parent provider.
* @param name The name of this function.
* @param returnType The return type of this function.
*/
public FunctionCompletion(CompletionProvider provider, String name,
String returnType) {
super(provider, name, returnType);
}
@Override
protected void addDefinitionString(StringBuilder sb) {
sb.append("<html><b>");
sb.append(getDefinitionString());
sb.append("</b>");
}
/**
* Adds HTML describing the parameters to this function to a buffer.
*
* @param sb The buffer to append to.
*/
protected void addParameters(StringBuilder sb) {
// TODO: Localize me
int paramCount = getParamCount();
if (paramCount>0) {
sb.append("<b>Parameters:</b><br>");
sb.append("<center><table width='90%'><tr><td>");
for (int i=0; i<paramCount; i++) {
Parameter param = getParam(i);
sb.append("<b>");
sb.append(param.getName()!=null ? param.getName() :
param.getType());
sb.append("</b> ");
String desc = param.getDescription();
if (desc!=null) {
sb.append(desc);
}
sb.append("<br>");
}
sb.append("</td></tr></table></center><br><br>");
}
if (returnValDesc!=null) {
sb.append("<b>Returns:</b><br><center><table width='90%'><tr><td>");
sb.append(returnValDesc);
sb.append("</td></tr></table></center><br><br>");
}
}
/**
* Returns the "definition string" for this function completion. For
* example, for the C "<code>printf</code>" function, this would return
* "<code>int printf(const char *, ...)</code>".
*
* @return The definition string.
*/
@Override
public String getDefinitionString() {
StringBuilder sb = new StringBuilder();
// Add the return type if applicable (C macros like NULL have no type).
String type = getType();
if (type!=null) {
sb.append(type).append(' ');
}
// Add the item being described's name.
sb.append(getName());
// Add parameters for functions.
CompletionProvider provider = getProvider();
char start = provider.getParameterListStart();
if (start!=0) {
sb.append(start);
}
for (int i=0; i<getParamCount(); i++) {
Parameter param = getParam(i);
type = param.getType();
String name = param.getName();
if (type!=null) {
sb.append(type);
if (name!=null) {
sb.append(' ');
}
}
if (name!=null) {
sb.append(name);
}
if (i<params.size()-1) {
sb.append(provider.getParameterListSeparator());
}
}
char end = provider.getParameterListEnd();
if (end!=0) {
sb.append(end);
}
return sb.toString();
}
public ParameterizedCompletionInsertionInfo getInsertionInfo(
JTextComponent tc, boolean replaceTabsWithSpaces) {
ParameterizedCompletionInsertionInfo info =
new ParameterizedCompletionInsertionInfo();
StringBuilder sb = new StringBuilder();
char paramListStart = getProvider().getParameterListStart();
if (paramListStart!='\0') {
sb.append(paramListStart);
}
int dot = tc.getCaretPosition() + sb.length();
int paramCount = getParamCount();
// Get the range in which the caret can move before we hide
// this tool tip.
int minPos = dot;
Position maxPos = null;
try {
maxPos = tc.getDocument().createPosition(dot-sb.length()+1);
} catch (BadLocationException ble) {
ble.printStackTrace(); // Never happens
}
info.setCaretRange(minPos, maxPos);
int firstParamLen = 0;
// Create the text to insert (keep it one completion for
// performance and simplicity of undo/redo).
int start = dot;
for (int i=0; i<paramCount; i++) {
Parameter param = getParam(i);
String paramText = getParamText(param);
if (i==0) {
firstParamLen = paramText.length();
}
sb.append(paramText);
int end = start + paramText.length();
info.addReplacementLocation(start, end);
// Patch for param. list separators with length > 2 -
// thanks to Matthew Adereth!
String sep = getProvider().getParameterListSeparator();
if (i<paramCount-1 && sep!=null) {
sb.append(sep);
start = end + sep.length();
}
}
sb.append(getProvider().getParameterListEnd());
int endOffs = dot + sb.length();
endOffs -= 1;//getProvider().getParameterListStart().length();
info.addReplacementLocation(endOffs, endOffs); // offset after function
info.setDefaultEndOffs(endOffs);
int selectionEnd = paramCount>0 ? (dot+firstParamLen) : dot;
info.setInitialSelection(dot, selectionEnd);
info.setTextToInsert(sb.toString());
return info;
}
/**
* {@inheritDoc}
*/
public Parameter getParam(int index) {
return params.get(index);
}
/**
* Returns the number of parameters to this function.
*
* @return The number of parameters to this function.
* @see #getParam(int)
*/
public int getParamCount() {
return params==null ? 0 : params.size();
}
/**
* {@inheritDoc}
*/
public boolean getShowParameterToolTip() {
return true;
}
/**
* Returns the text to insert for a parameter.
*
* @param param The parameter.
* @return The text.
*/
private String getParamText(ParameterizedCompletion.Parameter param) {
String text = param.getName();
if (text==null) {
text = param.getType();
if (text==null) { // Shouldn't ever happen
text = "arg";
}
}
return text;
}
/**
* Returns the description of the return value of this function.
*
* @return The description, or <code>null</code> if there is none.
* @see #setReturnValueDescription(String)
*/
public String getReturnValueDescription() {
return returnValDesc;
}
/**
* {@inheritDoc}
*/
@Override
public String getSummary() {
StringBuilder sb = new StringBuilder();
addDefinitionString(sb);
if (!possiblyAddDescription(sb)) {
sb.append("<br><br><br>");
}
addParameters(sb);
possiblyAddDefinedIn(sb);
return sb.toString();
}
/**
* {@inheritDoc}
*/
@Override
public String getToolTipText() {
String text = getSummary();
if (text==null) {
text = getDefinitionString();
}
return text;
}
/**
* Sets the parameters to this function.
*
* @param params The parameters. This should be a list of
* {@link ParameterizedCompletion.Parameter}s.
* @see #getParam(int)
* @see #getParamCount()
*/
public void setParams(List<Parameter> params) {
if (params!=null) {
// Deep copy so parsing can re-use its array.
this.params = new ArrayList<Parameter>(params);
}
}
/**
* Sets the description of the return value of this function.
*
* @param desc The description.
* @see #getReturnValueDescription()
*/
public void setReturnValueDescription(String desc) {
this.returnValDesc = desc;
}
}