keymap_file.rs

  1use crate::{settings_store::parse_json_with_comments, SettingsAssets};
  2use anyhow::{anyhow, Context, Result};
  3use collections::{BTreeMap, HashMap};
  4use gpui::{Action, AppContext, KeyBinding, SharedString};
  5use schemars::{
  6    gen::{SchemaGenerator, SchemaSettings},
  7    schema::{ArrayValidation, InstanceType, Metadata, Schema, SchemaObject, SubschemaValidation},
  8    JsonSchema,
  9};
 10use serde::Deserialize;
 11use serde_json::Value;
 12use util::{asset_str, ResultExt};
 13
 14#[derive(Debug, Deserialize, Default, Clone, JsonSchema)]
 15#[serde(transparent)]
 16pub struct KeymapFile(Vec<KeymapBlock>);
 17
 18#[derive(Debug, Deserialize, Default, Clone, JsonSchema)]
 19pub struct KeymapBlock {
 20    #[serde(default)]
 21    context: Option<String>,
 22    #[serde(default)]
 23    use_key_equivalents: Option<bool>,
 24    bindings: BTreeMap<String, KeymapAction>,
 25}
 26
 27impl KeymapBlock {
 28    pub fn context(&self) -> Option<&str> {
 29        self.context.as_deref()
 30    }
 31
 32    pub fn bindings(&self) -> &BTreeMap<String, KeymapAction> {
 33        &self.bindings
 34    }
 35}
 36
 37#[derive(Debug, Deserialize, Default, Clone)]
 38#[serde(transparent)]
 39pub struct KeymapAction(Value);
 40
 41impl std::fmt::Display for KeymapAction {
 42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 43        match &self.0 {
 44            Value::String(s) => write!(f, "{}", s),
 45            Value::Array(arr) => {
 46                let strings: Vec<String> = arr.iter().map(|v| v.to_string()).collect();
 47                write!(f, "{}", strings.join(", "))
 48            }
 49            _ => write!(f, "{}", self.0),
 50        }
 51    }
 52}
 53
 54impl JsonSchema for KeymapAction {
 55    fn schema_name() -> String {
 56        "KeymapAction".into()
 57    }
 58
 59    fn json_schema(_: &mut SchemaGenerator) -> Schema {
 60        Schema::Bool(true)
 61    }
 62}
 63
 64impl KeymapFile {
 65    pub fn load_asset(asset_path: &str, cx: &mut AppContext) -> Result<()> {
 66        let content = asset_str::<SettingsAssets>(asset_path);
 67
 68        Self::parse(content.as_ref())?.add_to_cx(cx)
 69    }
 70
 71    pub fn parse(content: &str) -> Result<Self> {
 72        if content.is_empty() {
 73            return Ok(Self::default());
 74        }
 75        parse_json_with_comments::<Self>(content)
 76    }
 77
 78    pub fn add_to_cx(self, cx: &mut AppContext) -> Result<()> {
 79        let key_equivalents = crate::key_equivalents::get_key_equivalents(&cx.keyboard_layout());
 80
 81        for KeymapBlock {
 82            context,
 83            use_key_equivalents,
 84            bindings,
 85        } in self.0
 86        {
 87            let bindings = bindings
 88                .into_iter()
 89                .filter_map(|(keystroke, action)| {
 90                    let action = action.0;
 91
 92                    // This is a workaround for a limitation in serde: serde-rs/json#497
 93                    // We want to deserialize the action data as a `RawValue` so that we can
 94                    // deserialize the action itself dynamically directly from the JSON
 95                    // string. But `RawValue` currently does not work inside of an untagged enum.
 96                    match action {
 97                        Value::Array(items) => {
 98                            let Ok([name, data]): Result<[serde_json::Value; 2], _> =
 99                                items.try_into()
100                            else {
101                                return Some(Err(anyhow!("Expected array of length 2")));
102                            };
103                            let serde_json::Value::String(name) = name else {
104                                return Some(Err(anyhow!(
105                                    "Expected first item in array to be a string."
106                                )));
107                            };
108                            cx.build_action(&name, Some(data))
109                        }
110                        Value::String(name) => cx.build_action(&name, None),
111                        Value::Null => Ok(no_action()),
112                        _ => {
113                            return Some(Err(anyhow!("Expected two-element array, got {action:?}")))
114                        }
115                    }
116                    .with_context(|| {
117                        format!(
118                            "invalid binding value for keystroke {keystroke}, context {context:?}"
119                        )
120                    })
121                    .log_err()
122                    .map(|action| {
123                        KeyBinding::load(
124                            &keystroke,
125                            action,
126                            context.as_deref(),
127                            if use_key_equivalents.unwrap_or_default() {
128                                key_equivalents.as_ref()
129                            } else {
130                                None
131                            },
132                        )
133                    })
134                })
135                .collect::<Result<Vec<_>>>()?;
136
137            cx.bind_keys(bindings);
138        }
139        Ok(())
140    }
141
142    pub fn generate_json_schema_for_registered_actions(cx: &mut AppContext) -> Value {
143        let mut generator = SchemaSettings::draft07()
144            .with(|settings| settings.option_add_null_type = false)
145            .into_generator();
146
147        let action_schemas = cx.action_schemas(&mut generator);
148        let deprecations = cx.action_deprecations();
149        KeymapFile::generate_json_schema(generator, action_schemas, deprecations)
150    }
151
152    fn generate_json_schema(
153        generator: SchemaGenerator,
154        action_schemas: Vec<(SharedString, Option<Schema>)>,
155        deprecations: &HashMap<SharedString, SharedString>,
156    ) -> serde_json::Value {
157        fn set<I, O>(input: I) -> Option<O>
158        where
159            I: Into<O>,
160        {
161            Some(input.into())
162        }
163
164        fn add_deprecation_notice(schema_object: &mut SchemaObject, new_name: &SharedString) {
165            schema_object.extensions.insert(
166                // deprecationMessage is not part of the JSON Schema spec,
167                // but json-language-server recognizes it.
168                "deprecationMessage".to_owned(),
169                format!("Deprecated, use {new_name}").into(),
170            );
171        }
172
173        let empty_object: SchemaObject = SchemaObject {
174            instance_type: set(InstanceType::Object),
175            ..Default::default()
176        };
177
178        let mut keymap_action_alternatives = Vec::new();
179        for (name, action_schema) in action_schemas.iter() {
180            let schema = if let Some(Schema::Object(schema)) = action_schema {
181                Some(schema.clone())
182            } else {
183                None
184            };
185
186            // If the type has a description, also apply it to the value. Ideally it would be
187            // removed and applied to the overall array, but `json-language-server` does not show
188            // these descriptions.
189            let description = schema.as_ref().and_then(|schema| {
190                schema
191                    .metadata
192                    .as_ref()
193                    .and_then(|metadata| metadata.description.as_ref())
194            });
195            let mut matches_action_name = SchemaObject {
196                const_value: Some(Value::String(name.to_string())),
197                ..Default::default()
198            };
199            if let Some(description) = description {
200                matches_action_name.metadata = set(Metadata {
201                    description: Some(description.clone()),
202                    ..Default::default()
203                });
204            }
205
206            // Add an alternative for plain action names.
207            let deprecation = deprecations.get(name);
208            let mut plain_action = SchemaObject {
209                instance_type: set(InstanceType::String),
210                const_value: Some(Value::String(name.to_string())),
211                ..Default::default()
212            };
213            if let Some(new_name) = deprecation {
214                add_deprecation_notice(&mut plain_action, new_name);
215            }
216            keymap_action_alternatives.push(plain_action.into());
217
218            // When all fields are skipped or an empty struct is added with impl_actions! /
219            // impl_actions_as! an empty struct is produced. The action should be invoked without
220            // data in this case.
221            if let Some(schema) = schema {
222                if schema != empty_object {
223                    let mut action_with_data = SchemaObject {
224                        instance_type: set(InstanceType::Array),
225                        array: Some(
226                            ArrayValidation {
227                                items: set(vec![matches_action_name.into(), schema.into()]),
228                                min_items: Some(2),
229                                max_items: Some(2),
230                                ..Default::default()
231                            }
232                            .into(),
233                        ),
234                        ..Default::default()
235                    };
236                    if let Some(new_name) = deprecation {
237                        add_deprecation_notice(&mut action_with_data, new_name);
238                    }
239                    keymap_action_alternatives.push(action_with_data.into());
240                }
241            }
242        }
243
244        // Placing null first causes json-language-server to default assuming actions should be
245        // null, so place it last.
246        keymap_action_alternatives.push(
247            SchemaObject {
248                instance_type: set(InstanceType::Null),
249                ..Default::default()
250            }
251            .into(),
252        );
253
254        let action_schema = SchemaObject {
255            subschemas: set(SubschemaValidation {
256                one_of: Some(keymap_action_alternatives),
257                ..Default::default()
258            }),
259            ..Default::default()
260        }
261        .into();
262
263        let mut root_schema = generator.into_root_schema_for::<KeymapFile>();
264        root_schema
265            .definitions
266            .insert("KeymapAction".to_owned(), action_schema);
267
268        serde_json::to_value(root_schema).unwrap()
269    }
270
271    pub fn blocks(&self) -> &[KeymapBlock] {
272        &self.0
273    }
274}
275
276fn no_action() -> Box<dyn gpui::Action> {
277    gpui::NoAction.boxed_clone()
278}
279
280#[cfg(test)]
281mod tests {
282    use crate::KeymapFile;
283
284    #[test]
285    fn can_deserialize_keymap_with_trailing_comma() {
286        let json = indoc::indoc! {"[
287              // Standard macOS bindings
288              {
289                \"bindings\": {
290                  \"up\": \"menu::SelectPrev\",
291                },
292              },
293            ]
294                  "
295
296        };
297        KeymapFile::parse(json).unwrap();
298    }
299}