tjdevries / vim9jit

a vim9script -> lua transpiler (written in Rust)
MIT License
510 stars 21 forks source link

Minor question about crates/format::lua function #37

Closed johnhuichen closed 8 months ago

johnhuichen commented 8 months ago

Hello @tjdevries, I am digging around in this repo to see how I can help contribute to the Neovim project.

In foramt::lua, it calls stylua_lib::format_code twice to format the input string reference.

pub fn lua(code: &str) -> Result<String, (String, String)> {
    let conf = get_stylua_config();
    let code = match stylua_lib::format_code(code, conf, None, OutputVerification::None) {
        Ok(res) => res,
        Err(err) => return Err((code.to_string(), err.to_string())),
    };

    let code = match stylua_lib::format_code(&code, conf, None, OutputVerification::None) {
        Ok(res) => res,
        Err(err) => return Err((code.to_string(), err.to_string())),
    };

    Ok(code)
}

I don't really understand why format_code has to be called twice. When I removed the second call, the function still works fine.

Another minor question is the stylua_config. If it's always the same config, would it make more sense to use a constant instead of a function generator?

fn get_stylua_config() -> stylua_lib::Config {
    stylua_lib::Config::new()
        .with_column_width(100)
        .with_line_endings(stylua_lib::LineEndings::Unix)
        .with_indent_type(stylua_lib::IndentType::Spaces)
        .with_indent_width(2)
        .with_quote_style(stylua_lib::QuoteStyle::AutoPreferSingle)
        .with_call_parentheses(stylua_lib::CallParenType::Always)
        .with_collapse_simple_statement(stylua_lib::CollapseSimpleStatement::Never)
}
tjdevries commented 8 months ago

The first one was because sometimes i would get code that changed formatting if you formatted more than once (i.e. some bug in stylua that made it not 100% completely formatted). I found running it twice made that never happen haha.

The second would probably work, I'd have to go check the code again to be sure though

johnhuichen commented 8 months ago

I see, thanks TJ