Automatically generate ShortcutAction code from ~JSON Schema~ X-Macros #4858

Closed
opened 2026-01-30 23:58:18 +00:00 by claunia · 2 comments
Owner

Originally created by @zadjii-msft on GitHub (Nov 7, 2019).

Originally assigned to: @zadjii-msft on GitHub.

While working on #3391, I felt like there was a ton of boilerplate that was needed for each and every keybindings. The thought crossed my mind that we could do better, but I wasn't sure how. In my mind, I imagined a single file that described each of the ShortcutActions and their possible args, and a small script that just automatically generated all the necessary code to parse them, their args, and hook them up to the TerminalApp.

Now that I've thought on it more, here are some of the steps that'll be needed:

  • Take code from my command palette branch for ShortcutActionDispatch. This is a class that just handles the dispatching of actions. In that branch, I needed it because both keybindings and commands needed to be able to dispatch ShortcutActions, but only AppKeyBindings needed to do a KeyChord lookup. This code being encapsulated will be necessary for a future step.
  • #2550 Add more templated json utils. We'll need this to be able to automagically generate the code to parse a given type from a Json::Value
  • Move the code around so that anything having to do with the entirety of the list of ShortcutActions is in single files by themselves:
    • AppKeybindingSerialization should only deal with parsing keychords. Lets put the ShortcutActions and their keys in another file
    • Add a IShortcutActionHandler interface, with _Handle{{SomeShortcutAction}}(auto&& sender, ActionArgs args) methods for each ShortcutAction. It should declare all the event handlers, but not the definitions.
    • Make TerminalPage extend IShortcutActionHandler. This will be necessary to make sure TerminalPage implements each of them.
    • That one method that hooks up the TerminalPage to the ShortcutActionDispatch. Pull that into it's own file as well.
  • Write a script that parses profiles.schema.json for each of the "*ShortcutAction" definitions, and turns them into code in all the necessary files
    • ShortcutActionKeys.h
    • ActionAndArgs.idl/.h/.cpp
    • ShortcutActionDispatch.idl/.h/.cpp
    • IShortcutActionHandler.h
    • For actions that are listed in ShortcutActions but don't have a proper Args type, then make sure that they're still dispatched correctly
    • When the number of args changes, write the above files, and display a message that the developer needs to update AppActionHandlers.cpp to add implementations for the new types.
    • Validate that all "*ShortcutAction"s listed in the schema actually show up in the onOf schema for Keybinding

Each top-level box could be it's own task here.

In the end, the developer should be able to modify the schema for add their new argument or ShortcutAction, and run a script, and the script should automatically write all the modifications necessary.

I'll probably need to do this in powershell. cmd would be impossible, and as much as I love python, I don't think that's really a part of our toolchain at all currently.

Originally created by @zadjii-msft on GitHub (Nov 7, 2019). Originally assigned to: @zadjii-msft on GitHub. While working on #3391, I felt like there was a ton of boilerplate that was needed for each and every keybindings. The thought crossed my mind that we could do better, but I wasn't sure how. In my mind, I imagined a single file that described each of the `ShortcutAction`s and their possible args, and a small script that just automatically generated all the necessary code to parse them, their args, and hook them up to the `TerminalApp`. Now that I've thought on it more, here are some of the steps that'll be needed: * [ ] Take code from my command palette branch for `ShortcutActionDispatch`. This is a class that just handles the dispatching of actions. In that branch, I needed it because both keybindings and commands needed to be able to dispatch `ShortcutAction`s, but only `AppKeyBindings` needed to do a `KeyChord` lookup. This code being encapsulated will be necessary for a future step. * [ ] #2550 Add more templated json utils. We'll need this to be able to automagically generate the code to parse a given type from a `Json::Value` * [ ] Move the code around so that anything having to do with the entirety of the list of `ShortcutAction`s is in single files by themselves: - [ ] AppKeybindingSerialization should only deal with parsing keychords. Lets put the `ShortcutAction`s and their keys in another file - [ ] Add a `IShortcutActionHandler` interface, with `_Handle{{SomeShortcutAction}}(auto&& sender, ActionArgs args)` methods for each `ShortcutAction`. It should declare all the event handlers, but not the definitions. - [ ] Make `TerminalPage` extend `IShortcutActionHandler`. This will be necessary to make sure TerminalPage implements each of them. - [ ] That one method that hooks up the `TerminalPage` to the `ShortcutActionDispatch`. Pull that into it's own file as well. * [ ] Write a script that parses `profiles.schema.json` for each of the "*ShortcutAction" definitions, and turns them into code in all the necessary files - [ ] `ShortcutActionKeys.h` - [ ] `ActionAndArgs.idl`/`.h`/`.cpp` - [ ] `ShortcutActionDispatch.idl`/`.h`/`.cpp` - [ ] `IShortcutActionHandler.h` - [ ] For actions that are listed in `ShortcutAction`s but don't have a proper `Args` type, then make sure that they're still dispatched correctly - [ ] When the number of args _changes_, write the above files, and display a message that the developer needs to update `AppActionHandlers.cpp` to add implementations for the new types. - [ ] Validate that all "*ShortcutAction"s listed in the schema actually show up in the `onOf` schema for `Keybinding` Each top-level box could be it's own task here. In the end, the developer should be able to modify the schema for add their new argument or `ShortcutAction`, and run a script, and the script should automatically write all the modifications necessary. I'll probably need to do this in powershell. `cmd` would be impossible, and as much as I love `python`, I don't think that's really a part of our toolchain at all currently.
Author
Owner

@zadjii-msft commented on GitHub (Nov 7, 2019):

(discussed with @DHowett-MSFT, so yanking triage)

@zadjii-msft commented on GitHub (Nov 7, 2019): (discussed with @DHowett-MSFT, so yanking triage)
Author
Owner

@carlos-zamora commented on GitHub (May 13, 2020):

There's a lot in here. @zadjii-msft since you created this issue, think it's worth throwing in the v2 milestone?

@carlos-zamora commented on GitHub (May 13, 2020): There's a lot in here. @zadjii-msft since you created this issue, think it's worth throwing in the v2 milestone?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#4858