Skip to content

Commit 7275f62

Browse files
cmagliefacchinm
authored andcommitted
Use FunctionCompletion when dealing with function autocompletions
1 parent 8214404 commit 7275f62

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

app/src/cc/arduino/autocomplete/ClangCompletionProvider.java

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@
77
import javax.swing.text.JTextComponent;
88

99
import org.fife.ui.autocomplete.Completion;
10+
import org.fife.ui.autocomplete.DefaultCompletionProvider;
11+
import org.fife.ui.autocomplete.FunctionCompletion;
12+
import org.fife.ui.autocomplete.ParameterizedCompletion.Parameter;
1013
import org.fife.ui.autocomplete.TemplateCompletion;
1114

1215
import com.fasterxml.jackson.databind.ObjectMapper;
1316

1417
import processing.app.Editor;
1518
import processing.app.EditorTab;
1619

17-
public class ClangCompletionProvider extends BaseCCompletionProvider {
20+
public class ClangCompletionProvider extends DefaultCompletionProvider {
1821

1922
private Editor editor;
2023

2124
public ClangCompletionProvider(Editor e) {
2225
super();
2326
editor = e;
27+
setParameterizedCompletionParams('(', ", ", ')');
2428
}
2529

2630
@Override
@@ -32,6 +36,8 @@ public List<Completion> getCompletionByInputText(String inputText) {
3236
@Override
3337
protected List<Completion> getCompletionsImpl(JTextComponent textarea) {
3438

39+
List<Completion> res = new ArrayList<>();
40+
3541
// Retrieve current line and column
3642
EditorTab tab = editor.getCurrentTab();
3743
int line, col;
@@ -44,45 +50,71 @@ protected List<Completion> getCompletionsImpl(JTextComponent textarea) {
4450
} catch (BadLocationException e1) {
4551
// Should never happen...
4652
e1.printStackTrace();
47-
return completions;
53+
return res;
4854
}
4955

5056
try {
5157
// Run codecompletion engine
5258
String out = editor.getSketchController()
5359
.codeComplete(tab.getSketchFile(), line, col);
5460

55-
List<Completion> res = new ArrayList<>();
56-
res.add(new TemplateCompletion(this, "for", "interate over array",
57-
"for (int ${i} = 0; ${i} < ${array}.length; ${i}++) {\n ${cursor}\n}"));
58-
5961
// Parse engine output and build code completions
6062
ObjectMapper mapper = new ObjectMapper();
6163
ArduinoCompletionsList allCc;
6264
allCc = mapper.readValue(out, ArduinoCompletionsList.class);
6365
for (ArduinoCompletion cc : allCc) {
64-
if (cc.type.equals("macro")) {
66+
if (cc.type.equals("Macro")) {
6567
// for now skip macro
6668
continue;
6769
}
68-
String returnType;
69-
String typedText;
70+
71+
if (cc.type.equals("Function")) {
72+
List<Parameter> params = new ArrayList<>();
73+
for (CompletionChunk chunk : cc.completion.chunks) {
74+
if (chunk.placeholder != null) {
75+
params.add(new Parameter("type", chunk.placeholder));
76+
}
77+
}
78+
79+
FunctionCompletion compl = new FunctionCompletion(this,
80+
cc.getCompletion().getTypedText(),
81+
cc.getCompletion().getResultType());
82+
compl.setParams(params);
83+
res.add(compl);
84+
continue;
85+
}
86+
87+
String returnType = "";
88+
String typedText = null;
7089
String template = "";
7190
for (CompletionChunk chunk : cc.completion.chunks) {
7291
if (chunk.t != null) {
73-
template += "t";
92+
template += chunk.t;
7493
}
7594
if (chunk.res != null) {
76-
returnType = chunk.res;
95+
returnType = " - " + chunk.res;
7796
}
7897
if (chunk.typedtext != null) {
98+
template += chunk.typedtext;
7999
typedText = chunk.typedtext;
80100
}
101+
if (chunk.placeholder != null) {
102+
String[] spl = chunk.placeholder.split(" ");
103+
template += "${" + spl[spl.length - 1] + "}";
104+
}
105+
if (chunk.info != null) {
106+
System.out.println("INFO: "+chunk.info);
107+
}
81108
}
109+
template += "${cursor}";
110+
System.out.println("TEMPLATE: " + template);
111+
res.add(new TemplateCompletion(this, typedText, typedText + returnType,
112+
template));
82113
}
114+
return res;
83115
} catch (Exception e) {
84116
e.printStackTrace();
117+
return res;
85118
}
86-
return completions;
87119
}
88120
}

0 commit comments

Comments
 (0)