-
Notifications
You must be signed in to change notification settings - Fork 0
/
intellisense.go
346 lines (298 loc) · 11.1 KB
/
intellisense.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package intellisense
import (
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/mcu-art/ergomcutool/config"
"github.com/mcu-art/ergomcutool/utils"
)
// CCppPropertiesReplacements are JSON entries for 'c_cpp_properties.json'
// that are generated automatically based on the project configuration.
type CCppPropertiesReplacements struct {
IncludePath []string `json:"includePath"`
Defines []string `json:"defines"`
CompilerPath string `json:"compilerPath"`
}
// LaunchReplacements are JSON entries for 'launch.json'
// that are generated automatically based on the project configuration.
type LaunchReplacements struct {
Executable string `json:"executable"`
ConfigFiles []string `json:"configFiles"`
SvdFile string `json:"svdFile"`
}
// SettingsReplacements are JSON entries for 'settings.json'
// that are generated automatically based on the project configuration.
type SettingsReplacements struct {
IncludePaths []string `json:"C_Cpp_Runner.includePaths"`
CCompilerPath string `json:"C_Cpp_Runner.cCompilerPath"`
CppCompilerPath string `json:"C_Cpp_Runner.cppCompilerPath"`
DebuggerPath string `json:"C_Cpp_Runner.debuggerPath"`
CortexDebugArmToolchainPath string `json:"cortex-debug.armToolchainPath"`
CortexDebugOpenocdPath string `json:"cortex-debug.openocdPath"`
CortexDebugGdbPath string `json:"cortex-debug.gdbPath"`
}
// ConfigurationEntry is an auxiliary type that stores
// Configuration taken from JSON file as a map,
// and its index in the 'configurations' array.
type ConfigurationEntry struct {
// C is a configuration entry
C map[string]any
Index int
}
func readJsonFileToMap(filePath string) (map[string]any, error) {
r := make(map[string]any)
if utils.FileExists(filePath) {
currentFileData, err := os.ReadFile(filePath)
if err != nil {
return r, fmt.Errorf("failed to read %q: %w", filePath, err)
}
err = json.Unmarshal(currentFileData, &r)
if err != nil {
return r, fmt.Errorf("failed to unmarshal json %q: %w", filePath, err)
}
}
return r, nil
}
// findConfigurations takes c_cpp_properties.json as an input
// and returns a slice of configurations that have names
// starting with specified prefix along with their index.
func findConfigurations(
root map[string]any, prefix string) ([]ConfigurationEntry, error) {
r := make([]ConfigurationEntry, 0, 10)
configurations, ok := root["configurations"].([]any)
if !ok {
return r, fmt.Errorf("'configurations' must be an array")
}
for i, configuration := range configurations {
confmap, ok := configuration.(map[string]any)
if !ok {
return r, fmt.Errorf("each 'configuration' must be a map")
}
name, ok := confmap["name"].(string)
if !ok {
return r, fmt.Errorf("'configuration.name' must be a string")
}
if strings.HasPrefix(name, prefix) {
r = append(r, ConfigurationEntry{
C: confmap,
Index: i,
})
}
}
return r, nil
}
// ProcessCCppPropertiesJson processes 'c_cpp_properties.json'
// so that it contains values required for VSCode intellisense.
// If the file doesn't exist, it will be created
// from the 'c_cpp_properties.persistent.json'.
func ProcessCCppPropertiesJson(r CCppPropertiesReplacements) error {
var err error
// Read c_cpp_properties.json if exists
currentFile := filepath.Join(".vscode", "c_cpp_properties.json")
persistentFile := filepath.Join(".vscode", "c_cpp_properties.persistent.json")
currentFileExists := utils.FileExists(currentFile)
var currentFileMap map[string]any
var persistentFileMap map[string]any
// If c_cpp_properties.json doesn't exist, create it as a copy
// of c_cpp_properties.persistent.json
if !currentFileExists {
err = utils.CopyFile(persistentFile, currentFile)
if err != nil {
return fmt.Errorf("failed to copy %q to %q: %w", persistentFile, currentFile, err)
}
}
currentFileMap, err = readJsonFileToMap(currentFile)
if err != nil {
return fmt.Errorf("failed to read %q: %w", currentFile, err)
}
// Read c_cpp_properties.persistent.json if exists
if !utils.FileExists(persistentFile) {
return fmt.Errorf("%q does not exist", persistentFile)
}
persistentFileMap, err = readJsonFileToMap(persistentFile)
if err != nil {
return fmt.Errorf("failed to read %q: %w", persistentFile, err)
}
prefix := "linux-gcc-arm"
destConfigs, err := findConfigurations(currentFileMap, prefix)
if err != nil {
return fmt.Errorf("failed to read %q: %w", currentFile, err)
}
srcConfigs, err := findConfigurations(persistentFileMap, prefix)
if err != nil {
return fmt.Errorf("failed to read %q: %w", persistentFile, err)
}
if len(srcConfigs) < 1 {
return fmt.Errorf(
"file %q doesn't contain any configuration that has name starting with %q", persistentFile, prefix)
}
if len(destConfigs) < 1 {
return fmt.Errorf(
"file %q doesn't contain any configuration that has name starting with %q", currentFile, prefix)
}
// Overwrite all top-level values in dest with the values from src[0]
for _, dest := range destConfigs {
for k, v := range srcConfigs[0].C {
dest.C[k] = v
}
// Overwrite auto-generated values with the values from the replacements
dest.C["includePath"] = r.IncludePath
dest.C["defines"] = r.Defines
dest.C["compilerPath"] = r.CompilerPath
// Save changes in the currentFileMap
configs := currentFileMap["configurations"].([]any)
configs[dest.Index] = dest.C
}
data, err := json.MarshalIndent(currentFileMap, "", " ")
if err != nil {
return fmt.Errorf("ProcessCCppPropertiesJson: failed to marshal json: %w", err)
}
// Save the updated file
return os.WriteFile(currentFile, data, fs.FileMode(config.DefaultFilePermissions))
}
// ProcessLaunchJson processes 'launch.json'
// so that it contains values required for VSCode intellisense.
// If the file doesn't exist, it will be created
// from 'launch.persistent.json'.
func ProcessLaunchJson(r LaunchReplacements) error {
var err error
// Read c_cpp_properties.json if exists
currentFile := filepath.Join(".vscode", "launch.json")
persistentFile := filepath.Join(".vscode", "launch.persistent.json")
currentFileExists := utils.FileExists(currentFile)
var currentFileMap map[string]any
var persistentFileMap map[string]any
// If launch.json doesn't exist, create it as a copy
// of launch.persistent.json
if !currentFileExists {
err = utils.CopyFile(persistentFile, currentFile)
if err != nil {
return fmt.Errorf("failed to copy %q to %q: %w", persistentFile, currentFile, err)
}
}
currentFileMap, err = readJsonFileToMap(currentFile)
if err != nil {
return fmt.Errorf("failed to read %q: %w", currentFile, err)
}
// Read launch.persistent.json if exists
if !utils.FileExists(persistentFile) {
return fmt.Errorf("%q does not exist", persistentFile)
}
persistentFileMap, err = readJsonFileToMap(persistentFile)
if err != nil {
return fmt.Errorf("failed to read %q: %w", persistentFile, err)
}
prefix := "STM32_Debug"
destConfigs, err := findConfigurations(currentFileMap, prefix)
if err != nil {
return fmt.Errorf("failed to read %q: %w", currentFile, err)
}
srcConfigs, err := findConfigurations(persistentFileMap, prefix)
if err != nil {
return fmt.Errorf("failed to read %q: %w", persistentFile, err)
}
if len(srcConfigs) < 1 {
return fmt.Errorf(
"file %q doesn't contain any configuration that has name starting with %q", persistentFile, prefix)
}
if len(destConfigs) < 1 {
return fmt.Errorf(
"file %q doesn't contain any configuration that has name starting with %q", currentFile, prefix)
}
// Overwrite all top-level values in dest with the values from src[0]
for _, dest := range destConfigs {
for k, v := range srcConfigs[0].C {
dest.C[k] = v
}
// Overwrite auto-generated values with the values from the replacements
dest.C["configFiles"] = r.ConfigFiles
dest.C["executable"] = r.Executable
dest.C["svdFile"] = r.SvdFile
// Save changes in the currentFileMap
configs := currentFileMap["configurations"].([]any)
configs[dest.Index] = dest.C
}
data, err := json.MarshalIndent(currentFileMap, "", " ")
if err != nil {
return fmt.Errorf("ProcessLaunchJson: failed to marshal json: %w", err)
}
// Save the updated file
return os.WriteFile(currentFile, data, fs.FileMode(config.DefaultFilePermissions))
}
// ProcessSettingsJson processes 'settings.json'
// so that it contains values required for VSCode intellisense.
// If the file doesn't exist, it will be created
// from 'settings.persistent.json'.
func ProcessSettingsJson(r SettingsReplacements) error {
var err error
// Read c_cpp_properties.json if exists
currentFile := filepath.Join(".vscode", "settings.json")
persistentFile := filepath.Join(".vscode", "settings.persistent.json")
currentFileExists := utils.FileExists(currentFile)
var currentFileMap map[string]any
var persistentFileMap map[string]any
// If settings.json doesn't exist, create it as a copy
// of settings.persistent.json
if !currentFileExists {
err = utils.CopyFile(persistentFile, currentFile)
if err != nil {
return fmt.Errorf("failed to copy %q to %q: %w", persistentFile, currentFile, err)
}
}
currentFileMap, err = readJsonFileToMap(currentFile)
if err != nil {
return fmt.Errorf("failed to read %q: %w", currentFile, err)
}
// Read settings.persistent.json if exists
if !utils.FileExists(persistentFile) {
return fmt.Errorf("%q does not exist", persistentFile)
}
persistentFileMap, err = readJsonFileToMap(persistentFile)
if err != nil {
return fmt.Errorf("failed to read %q: %w", persistentFile, err)
}
// Overwrite all top-level values in dest with the values from src
dest := currentFileMap
src := persistentFileMap
for k, v := range src {
dest[k] = v
}
// Overwrite auto-generated values with the values from the replacements
dest["C_Cpp_Runner.cCompilerPath"] = r.CCompilerPath
dest["C_Cpp_Runner.cppCompilerPath"] = r.CppCompilerPath
dest["C_Cpp_Runner.debuggerPath"] = r.DebuggerPath
dest["C_Cpp_Runner.includePaths"] = r.IncludePaths
dest["cortex-debug.armToolchainPath"] = r.CortexDebugArmToolchainPath
dest["cortex-debug.openocdPath"] = r.CortexDebugOpenocdPath
dest["cortex-debug.gdbPath"] = r.CortexDebugGdbPath
data, err := json.MarshalIndent(currentFileMap, "", " ")
if err != nil {
return fmt.Errorf("ProcessSettingsJson: failed to marshal json: %w", err)
}
// Save the updated file
return os.WriteFile(currentFile, data, fs.FileMode(config.DefaultFilePermissions))
}
// ProcessTasksJson processes 'tasks.json'
// so that it contains values required for VSCode intellisense.
// If the file doesn't exist, it will be created
// from 'tasks.persistent.json'.
func ProcessTasksJson() error {
var err error
// Read c_cpp_properties.json if exists
currentFile := filepath.Join(".vscode", "tasks.json")
persistentFile := filepath.Join(".vscode", "tasks.persistent.json")
currentFileExists := utils.FileExists(currentFile)
// If tasks.json doesn't exist, create it as a copy
// of tasks.persistent.json
if !currentFileExists {
err = utils.CopyFile(persistentFile, currentFile)
if err != nil {
return fmt.Errorf("failed to copy %q to %q: %w", persistentFile, currentFile, err)
}
}
// No additional processing required for tasks.json
return nil
}