7
7
import javax .swing .text .JTextComponent ;
8
8
9
9
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 ;
10
13
import org .fife .ui .autocomplete .TemplateCompletion ;
11
14
12
15
import com .fasterxml .jackson .databind .ObjectMapper ;
13
16
14
17
import processing .app .Editor ;
15
18
import processing .app .EditorTab ;
16
19
17
- public class ClangCompletionProvider extends BaseCCompletionProvider {
20
+ public class ClangCompletionProvider extends DefaultCompletionProvider {
18
21
19
22
private Editor editor ;
20
23
21
24
public ClangCompletionProvider (Editor e ) {
22
25
super ();
23
26
editor = e ;
27
+ setParameterizedCompletionParams ('(' , ", " , ')' );
24
28
}
25
29
26
30
@ Override
@@ -32,6 +36,8 @@ public List<Completion> getCompletionByInputText(String inputText) {
32
36
@ Override
33
37
protected List <Completion > getCompletionsImpl (JTextComponent textarea ) {
34
38
39
+ List <Completion > res = new ArrayList <>();
40
+
35
41
// Retrieve current line and column
36
42
EditorTab tab = editor .getCurrentTab ();
37
43
int line , col ;
@@ -44,45 +50,71 @@ protected List<Completion> getCompletionsImpl(JTextComponent textarea) {
44
50
} catch (BadLocationException e1 ) {
45
51
// Should never happen...
46
52
e1 .printStackTrace ();
47
- return completions ;
53
+ return res ;
48
54
}
49
55
50
56
try {
51
57
// Run codecompletion engine
52
58
String out = editor .getSketchController ()
53
59
.codeComplete (tab .getSketchFile (), line , col );
54
60
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
-
59
61
// Parse engine output and build code completions
60
62
ObjectMapper mapper = new ObjectMapper ();
61
63
ArduinoCompletionsList allCc ;
62
64
allCc = mapper .readValue (out , ArduinoCompletionsList .class );
63
65
for (ArduinoCompletion cc : allCc ) {
64
- if (cc .type .equals ("macro " )) {
66
+ if (cc .type .equals ("Macro " )) {
65
67
// for now skip macro
66
68
continue ;
67
69
}
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 ;
70
89
String template = "" ;
71
90
for (CompletionChunk chunk : cc .completion .chunks ) {
72
91
if (chunk .t != null ) {
73
- template += "t" ;
92
+ template += chunk . t ;
74
93
}
75
94
if (chunk .res != null ) {
76
- returnType = chunk .res ;
95
+ returnType = " - " + chunk .res ;
77
96
}
78
97
if (chunk .typedtext != null ) {
98
+ template += chunk .typedtext ;
79
99
typedText = chunk .typedtext ;
80
100
}
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
+ }
81
108
}
109
+ template += "${cursor}" ;
110
+ System .out .println ("TEMPLATE: " + template );
111
+ res .add (new TemplateCompletion (this , typedText , typedText + returnType ,
112
+ template ));
82
113
}
114
+ return res ;
83
115
} catch (Exception e ) {
84
116
e .printStackTrace ();
117
+ return res ;
85
118
}
86
- return completions ;
87
119
}
88
120
}
0 commit comments