Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add refresh-config and open-config command #1803

Merged

Conversation

jharrilim
Copy link
Contributor

@jharrilim jharrilim commented Mar 13, 2022

config-refresh

This PR aims to enable config refreshes while in the editor, and provide an in-editor alternative to --edit-config.

It adds two new :commands:

  • refresh-config
  • open-config

Basically what happens is:

  1. When the editor receives the refresh-command, it fires off an event up to the application
  2. The application sees the event and resets the config to the latest version from the config.toml file
  3. The main config, editor config, and theme are refreshed

commentary:

I couldn't get dynamic dispatch working using DynAccess. This would presumably save a lot of work when updating the config and propagating changes. Figured it out, but I had to remove #[derive(Debug)] from Editor.

This is also my first time using tokio and arc_swap. I actually don't know what I'm doing, I'm being carried heavily by the compiler rn

related issues:

Copy link
Member

@archseer archseer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable, just some cleanup:

helix-term/src/config.rs Outdated Show resolved Hide resolved
helix-term/src/config.rs Outdated Show resolved Hide resolved
helix-term/src/config.rs Outdated Show resolved Hide resolved
helix-view/src/editor.rs Outdated Show resolved Hide resolved
helix-term/src/application.rs Show resolved Hide resolved
helix-term/src/application.rs Show resolved Hide resolved
helix-term/src/application.rs Outdated Show resolved Hide resolved
helix-term/src/config.rs Outdated Show resolved Hide resolved
@@ -881,6 +879,9 @@ fn setting(
_ => anyhow::bail!("Unknown key `{}`.", args[0]),
}

cx.editor
.config_events
.push(tokio_stream::once(ConfigEvent::Update(runtime_config)));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the change to put the editor config behind dynamic dispatch, we now have to send updates back up to the application, and let it make the changes there.

Copy link
Member

@archseer archseer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry it took me a few days for another look, was trying to think of a way to avoid that extra channel in the event loop but I think this is the simplest implementation for now.

Just some minor changes

helix-term/src/application.rs Outdated Show resolved Hide resolved
helix-term/src/application.rs Outdated Show resolved Hide resolved
helix-term/src/application.rs Outdated Show resolved Hide resolved
helix-term/src/commands.rs Outdated Show resolved Hide resolved
helix-term/src/commands.rs Outdated Show resolved Hide resolved
helix-view/src/editor.rs Outdated Show resolved Hide resolved
book/src/generated/typable-cmd.md Outdated Show resolved Hide resolved
@archseer
Copy link
Member

(By the way, feel free to join us on the Matrix channel!)

@jharrilim
Copy link
Contributor Author

I've made all requested changes:

  • commands renamed to :config-reload and :config-save
  • config() helper created
  • keymaps now have dynamic access, tested that :config-reload works with this now
  • config_events uses an unbounded channel instead of SelectAll
  • The other refactors

And also:

  • Removed --edit-config
  • Added keymaps() helper
  • Refactored handle_keymap_event (this was necessary due to mut issues occuring after changing keymaps)

Copy link
Member

@archseer archseer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting closer, some thoughts about the keymaps type

Comment on lines 1187 to 1188
.keymaps
.load()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keymaps()?

Comment on lines 1189 to 1190
.clone()
.get_mut(&mode)
Copy link
Member

@archseer archseer Mar 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not clear on why this was doing get_mut() before, you could probably avoid the .clone() by using .get()

Copy link
Contributor Author

@jharrilim jharrilim Mar 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a glance, it looks like it was done because of the unwrap() after it
edit: The .get after the unwrap() is actually &mut self

Comment on lines 109 to 116
Box::new(Map::new(Arc::clone(&config), |config: &Config| {
&config.editor
})),
);

let editor_view = Box::new(ui::EditorView::new(std::mem::take(&mut config.keys)));
let keymaps = Box::new(Map::new(Arc::clone(&config), |config: &Config| {
&config.keys
}));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid boxing by using Map<> directly? For that to work you will probably need to use a fn as the last parameter rather than a closure, so that you can name it on the type

helix-term/src/ui/editor.rs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@jharrilim jharrilim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@archseer made changes and actually got it working! I left some comments inline describing some of the changes.

"a" => goto_prev_parameter,
"o" => goto_prev_comment,
"space" => add_newline_above,
fn default_keymaps() -> HashMap<Mode, Keymap> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(1/2) Had to move the default keymaps into its own function, this was to allow...

let mut delta = std::mem::take(&mut config.keys);
for (mode, keys) in &mut config.keys.map {
keys.merge(delta.map.remove(mode).unwrap_or_default())
let mut delta = std::mem::replace(&mut config.keys, default_keymaps());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(2/2) ...this merge to continue working

@@ -12,7 +14,7 @@ pub struct Config {
#[serde(default)]
pub lsp: LspConfig,
#[serde(default)]
pub keys: Keymaps,
pub keys: HashMap<Mode, Keymap>,
Copy link
Contributor Author

@jharrilim jharrilim Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "Keymaps" struct is now divorced from the config. Since the Keymaps encapsulated view state alongside config, it wasn't quite fitting, and had to be changed for config projection to work accordingly.

edit: Oops just realized the #[serde(default)], that needs to be updated to return the proper default HashMap

edit edit: This has now been updated to use the proper default HashMap

pub sticky: Option<KeyTrieNode>,
}

impl Keymaps {
pub fn new(map: HashMap<Mode, Keymap>) -> Self {
pub fn new(map: Box<dyn DynAccess<HashMap<Mode, Keymap>>>) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept this as Box<dyn DynAccess<T>> because defining a projection function here would prevent using arc_swap::access::Constant which is useful for testing.

helix-term/src/keymap.rs Outdated Show resolved Hide resolved
helix-term/src/keymap.rs Outdated Show resolved Hide resolved
@jharrilim jharrilim requested a review from archseer March 24, 2022 05:03
Comment on lines +123 to +127
pub use alt;
pub use ctrl;
pub use key;
pub use keymap;
pub use shift;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary? macro_export should be enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caught me off guard as well, macro_export is a bit different from macro_use:
https://stackoverflow.com/a/31749071/11886888

Without the pub use, it can't be imported from other files

@jharrilim jharrilim requested a review from archseer March 25, 2022 04:35
Copy link
Member

@archseer archseer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and works great! I'm quite happy with the keymap refactor too, thanks for working on this! 🎉

@archseer archseer merged commit bee05dd into helix-editor:master Mar 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants