MCPcopy
hub / github.com/SurgeDM/Surge / LoadKeyMap

Function LoadKeyMap

internal/config/keymaps.go:213–251  ·  view source on GitHub ↗

LoadKeyMap loads the keymap configuration from file.

()

Source from the content-addressed store, hash-verified

211
212// LoadKeyMap loads the keymap configuration from file.
213func LoadKeyMap() (*KeyMap, error) {
214
215 defaults := DefaultKeyMap()
216 path := GetKeyMapConfigPath()
217 utils.Debug("Loading keymap from %s", path)
218 data, err := os.ReadFile(path)
219 if err != nil {
220 utils.Debug("Failed to read keymap file: %v", err)
221 if os.IsNotExist(err) {
222 utils.Debug("Warning: Created New %s file \u2014 using defaults", path)
223 defaults.StartupWarnings = append(defaults.StartupWarnings, "Config: created new keymap file "+path)
224 _ = SaveKeyMap(defaults)
225 return defaults, nil
226 }
227 return nil, err
228 }
229
230 var cfg KeyMapConfig
231 if err := json.Unmarshal(data, &cfg); err != nil {
232 utils.Debug("Warning: corrupt keymap file %s: %v \u2014 using defaults", path, err)
233 defaults.StartupWarnings = append(defaults.StartupWarnings,
234 fmt.Sprintf("Config: keymap file is corrupt (%v) \u2014 all keybindings reset to defaults & rewrite the file", err))
235 err = SaveKeyMap(defaults)
236 return defaults, err
237 }
238
239 defaults.ApplyConfig(&cfg)
240 defaults.Validate()
241
242 // Self-healing: save the fully-merged and validated keymap back to disk
243 // ONLY if it differs from what was on disk to avoid constant disk thrashing.
244 mergedData, _ := json.MarshalIndent(defaults.ToConfig(), "", " ")
245 if string(data) != string(mergedData) {
246 defaults.StartupWarnings = append(defaults.StartupWarnings, "Config: keymap file was updated to include missing keys or fix formatting")
247 _ = SaveKeyMap(defaults)
248 }
249
250 return defaults, nil
251}
252
253// SaveKeyMap saves the keymap configuration to file.
254func SaveKeyMap(k *KeyMap) error {

Callers 3

InitialRootModelFunction · 0.92
UpdateMethod · 0.92

Calls 7

DebugFunction · 0.92
DefaultKeyMapFunction · 0.85
GetKeyMapConfigPathFunction · 0.85
SaveKeyMapFunction · 0.85
ApplyConfigMethod · 0.80
ToConfigMethod · 0.80
ValidateMethod · 0.45

Tested by 1