Skip to content

Commit 0284620

Browse files
committed
Implement autocomplete endpoint
1 parent d45e2c2 commit 0284620

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/arduino.cc/builder/jsonrpc/rpc.go

+70
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,32 @@ type (
3939
}
4040
)
4141

42+
type (
43+
CompleteHandler struct {
44+
watcher *fsnotify.Watcher
45+
ctx *types.Context
46+
}
47+
CompleteParams struct {
48+
HardwareFolders string
49+
ToolsFolders string
50+
BuiltInLibrariesFolders string
51+
OtherLibrariesFolders string
52+
SketchLocation string
53+
FQBN string
54+
ArduinoAPIVersion string
55+
CustomBuildProperties string
56+
Verbose bool
57+
BuildCachePath string
58+
BuildPath string
59+
WarningsLevel string
60+
CodeCompleteAt string
61+
}
62+
CompleteResult struct {
63+
Message string `json:"message"`
64+
Error error
65+
}
66+
)
67+
4268
type (
4369
WatchHandler struct {
4470
watcher *fsnotify.Watcher
@@ -51,6 +77,49 @@ type (
5177
}
5278
)
5379

80+
func (h *CompleteHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (interface{}, *jsonrpc.Error) {
81+
82+
var p CompleteParams
83+
if err := jsonrpc.Unmarshal(params, &p); err != nil {
84+
fmt.Println(err)
85+
return nil, err
86+
}
87+
88+
h.ctx.HardwareFolders = strings.Split(p.HardwareFolders, ",")
89+
h.ctx.ToolsFolders = strings.Split(p.ToolsFolders, ",")
90+
h.ctx.BuiltInLibrariesFolders = strings.Split(p.BuiltInLibrariesFolders, ",")
91+
h.ctx.OtherLibrariesFolders = strings.Split(p.OtherLibrariesFolders, ",")
92+
h.ctx.SketchLocation = p.SketchLocation
93+
h.ctx.CustomBuildProperties = strings.Split(p.CustomBuildProperties, ",")
94+
h.ctx.ArduinoAPIVersion = p.ArduinoAPIVersion
95+
h.ctx.FQBN = p.FQBN
96+
h.ctx.Verbose = false //p.Verbose
97+
h.ctx.BuildCachePath = p.BuildCachePath
98+
h.ctx.BuildPath = p.BuildPath
99+
h.ctx.WarningsLevel = p.WarningsLevel
100+
h.ctx.PrototypesSection = ""
101+
h.ctx.CodeCompleteAt = p.CodeCompleteAt
102+
103+
h.ctx.IncludeFolders = h.ctx.IncludeFolders[0:0]
104+
h.ctx.LibrariesObjectFiles = h.ctx.LibrariesObjectFiles[0:0]
105+
h.ctx.CoreObjectsFiles = h.ctx.CoreObjectsFiles[0:0]
106+
h.ctx.SketchObjectFiles = h.ctx.SketchObjectFiles[0:0]
107+
108+
h.ctx.ImportedLibraries = h.ctx.ImportedLibraries[0:0]
109+
110+
err := builder.RunPreprocess(h.ctx)
111+
if err != nil {
112+
return BuildResult{
113+
Message: h.ctx.GetLogger().Flush(),
114+
Error: err,
115+
}, nil
116+
}
117+
118+
return CompleteResult{
119+
Message: h.ctx.GetLogger().Flush(),
120+
}, nil
121+
}
122+
54123
func (h *BuildHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (interface{}, *jsonrpc.Error) {
55124

56125
var p BuildParams
@@ -134,6 +203,7 @@ func RegisterAndServeJsonRPC(ctx *types.Context) {
134203
watcher := startWatching(ctx)
135204

136205
jsonrpc.RegisterMethod("Main.Build", &BuildHandler{watcher, ctx}, BuildParams{}, BuildResult{})
206+
jsonrpc.RegisterMethod("Main.Complete", &CompleteHandler{watcher, ctx}, CompleteParams{}, CompleteResult{})
137207
jsonrpc.RegisterMethod("Main.AddWatchPath", &WatchHandler{watcher}, WatchParams{}, WatchResult{})
138208

139209
http.HandleFunc("/jrpc", func(w http.ResponseWriter, r *http.Request) {

src/arduino.cc/builder/preprocess_sketch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,6 @@ func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
115115
type OutputCodeCompletions struct{}
116116

117117
func (s *OutputCodeCompletions) Run(ctx *types.Context) error {
118-
fmt.Println(ctx.CodeCompletions)
118+
ctx.GetLogger().Println(constants.LOG_LEVEL_INFO, "%s", ctx.CodeCompletions)
119119
return nil
120120
}

0 commit comments

Comments
 (0)