mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-26 20:54:22 +01:00
Use FunctionCompletion when dealing with function autocompletions
This commit is contained in:
parent
f949c79f63
commit
ce22446f60
@ -7,6 +7,9 @@ import javax.swing.text.BadLocationException;
|
|||||||
import javax.swing.text.JTextComponent;
|
import javax.swing.text.JTextComponent;
|
||||||
|
|
||||||
import org.fife.ui.autocomplete.Completion;
|
import org.fife.ui.autocomplete.Completion;
|
||||||
|
import org.fife.ui.autocomplete.DefaultCompletionProvider;
|
||||||
|
import org.fife.ui.autocomplete.FunctionCompletion;
|
||||||
|
import org.fife.ui.autocomplete.ParameterizedCompletion.Parameter;
|
||||||
import org.fife.ui.autocomplete.TemplateCompletion;
|
import org.fife.ui.autocomplete.TemplateCompletion;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
@ -14,13 +17,14 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import processing.app.Editor;
|
import processing.app.Editor;
|
||||||
import processing.app.EditorTab;
|
import processing.app.EditorTab;
|
||||||
|
|
||||||
public class ClangCompletionProvider extends BaseCCompletionProvider {
|
public class ClangCompletionProvider extends DefaultCompletionProvider {
|
||||||
|
|
||||||
private Editor editor;
|
private Editor editor;
|
||||||
|
|
||||||
public ClangCompletionProvider(Editor e) {
|
public ClangCompletionProvider(Editor e) {
|
||||||
super();
|
super();
|
||||||
editor = e;
|
editor = e;
|
||||||
|
setParameterizedCompletionParams('(', ", ", ')');
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -32,6 +36,8 @@ public class ClangCompletionProvider extends BaseCCompletionProvider {
|
|||||||
@Override
|
@Override
|
||||||
protected List<Completion> getCompletionsImpl(JTextComponent textarea) {
|
protected List<Completion> getCompletionsImpl(JTextComponent textarea) {
|
||||||
|
|
||||||
|
List<Completion> res = new ArrayList<>();
|
||||||
|
|
||||||
// Retrieve current line and column
|
// Retrieve current line and column
|
||||||
EditorTab tab = editor.getCurrentTab();
|
EditorTab tab = editor.getCurrentTab();
|
||||||
int line, col;
|
int line, col;
|
||||||
@ -44,7 +50,7 @@ public class ClangCompletionProvider extends BaseCCompletionProvider {
|
|||||||
} catch (BadLocationException e1) {
|
} catch (BadLocationException e1) {
|
||||||
// Should never happen...
|
// Should never happen...
|
||||||
e1.printStackTrace();
|
e1.printStackTrace();
|
||||||
return completions;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -52,37 +58,63 @@ public class ClangCompletionProvider extends BaseCCompletionProvider {
|
|||||||
String out = editor.getSketchController()
|
String out = editor.getSketchController()
|
||||||
.codeComplete(tab.getSketchFile(), line, col);
|
.codeComplete(tab.getSketchFile(), line, col);
|
||||||
|
|
||||||
List<Completion> res = new ArrayList<>();
|
|
||||||
res.add(new TemplateCompletion(this, "for", "interate over array",
|
|
||||||
"for (int ${i} = 0; ${i} < ${array}.length; ${i}++) {\n ${cursor}\n}"));
|
|
||||||
|
|
||||||
// Parse engine output and build code completions
|
// Parse engine output and build code completions
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
ArduinoCompletionsList allCc;
|
ArduinoCompletionsList allCc;
|
||||||
allCc = mapper.readValue(out, ArduinoCompletionsList.class);
|
allCc = mapper.readValue(out, ArduinoCompletionsList.class);
|
||||||
for (ArduinoCompletion cc : allCc) {
|
for (ArduinoCompletion cc : allCc) {
|
||||||
if (cc.type.equals("macro")) {
|
if (cc.type.equals("Macro")) {
|
||||||
// for now skip macro
|
// for now skip macro
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
String returnType;
|
|
||||||
String typedText;
|
if (cc.type.equals("Function")) {
|
||||||
|
List<Parameter> params = new ArrayList<>();
|
||||||
|
for (CompletionChunk chunk : cc.completion.chunks) {
|
||||||
|
if (chunk.placeholder != null) {
|
||||||
|
params.add(new Parameter("type", chunk.placeholder));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FunctionCompletion compl = new FunctionCompletion(this,
|
||||||
|
cc.getCompletion().getTypedText(),
|
||||||
|
cc.getCompletion().getResultType());
|
||||||
|
compl.setParams(params);
|
||||||
|
res.add(compl);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String returnType = "";
|
||||||
|
String typedText = null;
|
||||||
String template = "";
|
String template = "";
|
||||||
for (CompletionChunk chunk : cc.completion.chunks) {
|
for (CompletionChunk chunk : cc.completion.chunks) {
|
||||||
if (chunk.t != null) {
|
if (chunk.t != null) {
|
||||||
template += "t";
|
template += chunk.t;
|
||||||
}
|
}
|
||||||
if (chunk.res != null) {
|
if (chunk.res != null) {
|
||||||
returnType = chunk.res;
|
returnType = " - " + chunk.res;
|
||||||
}
|
}
|
||||||
if (chunk.typedtext != null) {
|
if (chunk.typedtext != null) {
|
||||||
|
template += chunk.typedtext;
|
||||||
typedText = chunk.typedtext;
|
typedText = chunk.typedtext;
|
||||||
}
|
}
|
||||||
|
if (chunk.placeholder != null) {
|
||||||
|
String[] spl = chunk.placeholder.split(" ");
|
||||||
|
template += "${" + spl[spl.length - 1] + "}";
|
||||||
|
}
|
||||||
|
if (chunk.info != null) {
|
||||||
|
System.out.println("INFO: "+chunk.info);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
template += "${cursor}";
|
||||||
|
System.out.println("TEMPLATE: " + template);
|
||||||
|
res.add(new TemplateCompletion(this, typedText, typedText + returnType,
|
||||||
|
template));
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
return completions;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user