-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathcompletion.go
219 lines (194 loc) · 6.74 KB
/
completion.go
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
// This file is part of arduino-cli.
//
// Copyright 2023 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package arguments
import (
"context"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/commands/core"
"github.com/arduino/arduino-cli/commands/lib"
"github.com/arduino/arduino-cli/internal/cli/instance"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
)
// GetInstalledBoards is an helper function useful to autocomplete.
// It returns a list of fqbn
// it's taken from cli/board/listall.go
func GetInstalledBoards() []string {
inst := instance.CreateAndInit()
list, _ := board.ListAll(context.Background(), &rpc.BoardListAllRequest{
Instance: inst,
SearchArgs: nil,
IncludeHiddenBoards: false,
})
var res []string
// transform the data structure for the completion
for _, i := range list.Boards {
res = append(res, i.Fqbn+"\t"+i.Name)
}
return res
}
// GetInstalledProtocols is an helper function useful to autocomplete.
// It returns a list of protocols available based on the installed boards
func GetInstalledProtocols() []string {
inst := instance.CreateAndInit()
// FIXME: We must not access PackageManager directly here but use one of the commands.* functions
pme, release := commands.GetPackageManagerExplorer(&rpc.BoardListAllRequest{Instance: inst})
if pme == nil {
return nil // should never happen...
}
defer release()
boards := pme.InstalledBoards()
installedProtocols := make(map[string]struct{})
for _, board := range boards {
for _, protocol := range board.Properties.SubTree("upload.tool").FirstLevelKeys() {
if protocol == "default" {
// default is used as fallback when trying to upload to a board
// using a protocol not defined for it, it's useless showing it
// in autocompletion
continue
}
installedProtocols[protocol] = struct{}{}
}
}
res := make([]string, len(installedProtocols))
i := 0
for k := range installedProtocols {
res[i] = k
i++
}
return res
}
// GetInstalledProgrammers is an helper function useful to autocomplete.
// It returns a list of programmers available based on the installed boards
func GetInstalledProgrammers() []string {
inst := instance.CreateAndInit()
// we need the list of the available fqbn in order to get the list of the programmers
listAllReq := &rpc.BoardListAllRequest{
Instance: inst,
SearchArgs: nil,
IncludeHiddenBoards: false,
}
list, _ := board.ListAll(context.Background(), listAllReq)
// FIXME: We must not access PackageManager directly here but use one of the commands.* functions
pme, release := commands.GetPackageManagerExplorer(listAllReq)
if pme == nil {
return nil // should never happen...
}
defer release()
installedProgrammers := make(map[string]string)
for _, board := range list.Boards {
fqbn, _ := cores.ParseFQBN(board.Fqbn)
_, boardPlatform, _, _, _, _ := pme.ResolveFQBN(fqbn)
for programmerID, programmer := range boardPlatform.Programmers {
installedProgrammers[programmerID] = programmer.Name
}
}
res := make([]string, len(installedProgrammers))
i := 0
for programmerID := range installedProgrammers {
res[i] = programmerID + "\t" + installedProgrammers[programmerID]
i++
}
return res
}
// GetUninstallableCores is an helper function useful to autocomplete.
// It returns a list of cores which can be uninstalled
func GetUninstallableCores() []string {
inst := instance.CreateAndInit()
platforms, _ := core.GetPlatforms(&rpc.PlatformListRequest{
Instance: inst,
UpdatableOnly: false,
All: false,
})
var res []string
// transform the data structure for the completion
for _, i := range platforms {
res = append(res, i.Id+"\t"+i.Name)
}
return res
}
// GetInstallableCores is an helper function useful to autocomplete.
// It returns a list of cores which can be installed/downloaded
func GetInstallableCores() []string {
inst := instance.CreateAndInit()
platforms, _ := core.PlatformSearch(&rpc.PlatformSearchRequest{
Instance: inst,
SearchArgs: "",
AllVersions: false,
})
var res []string
// transform the data structure for the completion
for _, i := range platforms.SearchOutput {
res = append(res, i.Id+"\t"+i.Name)
}
return res
}
// GetInstalledLibraries is an helper function useful to autocomplete.
// It returns a list of libs which are currently installed, including the builtin ones
func GetInstalledLibraries() []string {
return getLibraries(true)
}
// GetUninstallableLibraries is an helper function useful to autocomplete.
// It returns a list of libs which can be uninstalled
func GetUninstallableLibraries() []string {
return getLibraries(false)
}
func getLibraries(all bool) []string {
inst := instance.CreateAndInit()
libs, _ := lib.LibraryList(context.Background(), &rpc.LibraryListRequest{
Instance: inst,
All: all,
Updatable: false,
Name: "",
Fqbn: "",
})
var res []string
// transform the data structure for the completion
for _, i := range libs.InstalledLibraries {
res = append(res, i.Library.Name+"\t"+i.Library.Sentence)
}
return res
}
// GetInstallableLibs is an helper function useful to autocomplete.
// It returns a list of libs which can be installed/downloaded
func GetInstallableLibs() []string {
inst := instance.CreateAndInit()
libs, _ := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
Instance: inst,
Query: "", // if no query is specified all the libs are returned
})
var res []string
// transform the data structure for the completion
for _, i := range libs.Libraries {
res = append(res, i.Name+"\t"+i.Latest.Sentence)
}
return res
}
// GetConnectedBoards is an helper function useful to autocomplete.
// It returns a list of boards which are currently connected
// Obviously it does not suggests network ports because of the timeout
func GetConnectedBoards() []string {
inst := instance.CreateAndInit()
list, _, _ := board.List(&rpc.BoardListRequest{
Instance: inst,
})
var res []string
// transform the data structure for the completion
for _, i := range list {
res = append(res, i.Port.Address)
}
return res
}