|
| 1 | +// |
| 2 | +// Copyright RIME Developers |
| 3 | +// Distributed under the BSD License |
| 4 | +// |
| 5 | +#include <boost/algorithm/string.hpp> |
| 6 | +#include <rime/config/config_compiler_impl.h> |
| 7 | +#include <rime/config/config_cow_ref.h> |
| 8 | +#include <rime/config/plugins.h> |
| 9 | + |
| 10 | +namespace rime { |
| 11 | + |
| 12 | +bool LegacyPresetConfigPlugin::ReviewCompileOutput( |
| 13 | + ConfigCompiler* compiler, an<ConfigResource> resource) { |
| 14 | + return true; |
| 15 | +} |
| 16 | + |
| 17 | +bool LegacyPresetConfigPlugin::ReviewLinkOutput( |
| 18 | + ConfigCompiler* compiler, an<ConfigResource> resource) { |
| 19 | + if (!boost::ends_with(resource->resource_id, ".schema")) |
| 20 | + return true; |
| 21 | + if (auto preset = resource->data->Traverse("key_binder/import_preset")) { |
| 22 | + if (!Is<ConfigValue>(preset)) |
| 23 | + return false; |
| 24 | + auto preset_config_id = As<ConfigValue>(preset)->str(); |
| 25 | + LOG(INFO) << "interpreting key_binder/import_preset: " << preset_config_id; |
| 26 | + auto target = Cow(resource, "key_binder"); |
| 27 | + auto map = As<ConfigMap>(**target); |
| 28 | + if (map && map->HasKey("bindings")) { |
| 29 | + // append to included list `key_binder/bindings/+` instead of overwriting |
| 30 | + auto appended = map->Get("bindings"); |
| 31 | + *Cow(target, "bindings/+") = appended; |
| 32 | + // `*target` is already referencing a copied map, safe to edit directly |
| 33 | + (*target)["bindings"] = nullptr; |
| 34 | + } |
| 35 | + Reference reference{preset_config_id, "key_binder", false}; |
| 36 | + if (!IncludeReference{reference} |
| 37 | + .TargetedAt(target).Resolve(compiler)) { |
| 38 | + LOG(ERROR) << "failed to include section " << reference; |
| 39 | + return false; |
| 40 | + } |
| 41 | + } |
| 42 | + // NOTE: in the following cases, Cow() is not strictly necessary because |
| 43 | + // we know for sure that no other ConfigResource is going to reference the |
| 44 | + // root map node that will be modified. But other than the root node of the |
| 45 | + // resource being linked, it's possbile a map or list has multiple references |
| 46 | + // in the node tree, therefore Cow() is recommended to make sure the |
| 47 | + // modifications only happen to one place. |
| 48 | + if (auto preset = resource->data->Traverse("punctuator/import_preset")) { |
| 49 | + if (!Is<ConfigValue>(preset)) |
| 50 | + return false; |
| 51 | + auto preset_config_id = As<ConfigValue>(preset)->str(); |
| 52 | + LOG(INFO) << "interpreting punctuator/import_preset: " << preset_config_id; |
| 53 | + Reference reference{preset_config_id, "punctuator", false}; |
| 54 | + if (!IncludeReference{reference} |
| 55 | + .TargetedAt(Cow(resource, "punctuator")).Resolve(compiler)) { |
| 56 | + LOG(ERROR) << "failed to include section " << reference; |
| 57 | + return false; |
| 58 | + } |
| 59 | + } |
| 60 | + if (auto preset = resource->data->Traverse("recognizer/import_preset")) { |
| 61 | + if (!Is<ConfigValue>(preset)) |
| 62 | + return false; |
| 63 | + auto preset_config_id = As<ConfigValue>(preset)->str(); |
| 64 | + LOG(INFO) << "interpreting recognizer/import_preset: " << preset_config_id; |
| 65 | + Reference reference{preset_config_id, "recognizer", false}; |
| 66 | + if (!IncludeReference{reference} |
| 67 | + .TargetedAt(Cow(resource, "recognizer")).Resolve(compiler)) { |
| 68 | + LOG(ERROR) << "failed to include section " << reference; |
| 69 | + return false; |
| 70 | + } |
| 71 | + } |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace rime |
0 commit comments