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

Reload language config with :config-reload #5239

Merged
merged 5 commits into from
Dec 29, 2022

Conversation

willful759
Copy link
Contributor

Just had a go at implementing this

Not quite sure where to make test or what tests to make, but I tried it and it seems to be able to reload auto formatting, at least

quick_edit.mp4

helix-term/src/application.rs Outdated Show resolved Hide resolved
helix-term/src/application.rs Outdated Show resolved Hide resolved
@kirawi kirawi added A-helix-term Area: Helix term improvements S-waiting-on-review Status: Awaiting review from a maintainer. labels Dec 24, 2022
@dead10ck
Copy link
Member

If the language config changes the LSP, will this handle sending the new config? Or if the command changes, will it shut down the current instance and start up the new one?

@the-mikedavis
Copy link
Member

It looks like Document::detect_language calls Document::set_language under the hood which only changes syntax parts of the doc. :set-language/:lang has the same problem (#3359 (comment))

@dead10ck
Copy link
Member

Oh interesting. Should this be a separate issue/PR?

@sudormrfbin
Copy link
Member

If the language config changes the LSP, will this handle sending the new config?

We can send the didChangeConfiguration request to the LSP to signal a change, but I'm not sure how many server's support dynamic config registration. I suppose the most reliable way is to restart the server if the config or the lsp command changes.

@the-mikedavis
Copy link
Member

Ah I didn't even think about changing config. For a first iteration, stopping the old server and starting a new one would be a good improvement. That can be done in a separate PR though - I'll make an issue 👍

@@ -417,6 +433,7 @@ impl Application {
fn refresh_config(&mut self) {
Copy link
Member

Choose a reason for hiding this comment

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

I think the refresh_config, refresh_language_config and refresh_theme commands could use some refactoring to improve the error handling. With this change, if you have an error in your language config and a theme error, you only end up seeing the theme error since it overwrites the editor.set_error from refresh_language_config

refresh_language_config and refresh_theme can return Result<(), Error>s instead and then use the anyhow helpers like anyhow::anyhow! or anyhow::bail! to format the error messages and return early in cases of errors. Then refresh_config can set the editor error if there are any failures:

fn refresh_config(&mut self) {
    let mut refresh_config = || -> Result<(), Error> {
        let config = Config::load_default()
            .map_err(|err| anyhow::anyhow!("failed to load config: {}", err))?;
        self.refresh_language_config()?;
        self.refresh_theme(&config)?;

        // Store new config
        self.config.store(Arc::new(config));

        Ok(())
    };
    if let Err(err) = refresh_config() {
        self.editor.set_error(err.to_string());
    }
}

And the other functions can use ? to return errors early and remove the duplicated error handling code:

fn refresh_language_config(&mut self) -> Result<(), Error> {
    let syntax_config = helix_core::config::user_syntax_loader()?;
    self.syn_loader = // ...
    // ...
    Ok(())
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Liked the suggestion, code looks a lot cleaner now.

I'm new to rust so recommendations like this are always welcomed

Copy link
Member

@dead10ck dead10ck left a comment

Choose a reason for hiding this comment

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

Haven't tested locally, but the code looks good to me 👍

Comment on lines 421 to 423
return Err(anyhow::anyhow!(
"theme requires truecolor support, which is not available"
));
Copy link
Member

Choose a reason for hiding this comment

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

Very minor thing but this can be written with anyhow::bail!:

anyhow::bail!("theme requires truecolor support, which is not available")

@the-mikedavis the-mikedavis changed the title language config reloading added Reload language config with :config-reload Dec 29, 2022
@the-mikedavis the-mikedavis merged commit 9d15b85 into helix-editor:master Dec 29, 2022
@willful759 willful759 deleted the language-reload branch December 29, 2022 19:13
mejo13 pushed a commit to mejo13/helix that referenced this pull request Dec 29, 2022
semin-park pushed a commit to semin-park/helix that referenced this pull request Jan 4, 2023
hadronized pushed a commit to hadronized/helix that referenced this pull request Jan 4, 2023
loralb pushed a commit to loralb/helix that referenced this pull request Jan 6, 2023
After changes in helix-editor#5239, the loaded configuration wasn't stored,
resulting in a success message even if the instance kept the previous
configuration values.
the-mikedavis pushed a commit that referenced this pull request Jan 8, 2023
After changes in #5239, the loaded configuration wasn't stored,
resulting in a success message even if the instance kept the previous
configuration values.
kirawi pushed a commit to kirawi/helix that referenced this pull request Jan 25, 2023
After changes in helix-editor#5239, the loaded configuration wasn't stored,
resulting in a success message even if the instance kept the previous
configuration values.
freqmod pushed a commit to freqmod/helix that referenced this pull request Feb 8, 2023
freqmod pushed a commit to freqmod/helix that referenced this pull request Feb 8, 2023
After changes in helix-editor#5239, the loaded configuration wasn't stored,
resulting in a success message even if the instance kept the previous
configuration values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-helix-term Area: Helix term improvements S-waiting-on-review Status: Awaiting review from a maintainer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants