Giter Club home page Giter Club logo

Comments (4)

RoloEdits avatar RoloEdits commented on July 2, 2024

What operating system are you on?

When trying to reproduce behavior on Windows I get asdf,\nasdf not asdf,nasdf.

I also grepped for ShellWords and nothing came up. What module would this be in? Ah, found it. Shellwords, not ShellWords.

from helix.

RoloEdits avatar RoloEdits commented on July 2, 2024

Made a pretty naive implimentation to escape given sequences. Seems to work fine? I added other a few other common patterns that might crop up for light testing. Not sure if something like this already exists as a helper function somewhere, so just hacked this one together.

fn yank_joined_impl(editor: &mut Editor, separator: &str, register: char) {
    let (view, doc) = current!(editor);
    let text = doc.text().slice(..);

    let selection = doc.selection(view.id);
    let selections = selection.len();
    let joined = selection
        .fragments(text)
        .fold(String::new(), |mut acc, fragment| {
            if !acc.is_empty() {
-                acc.push_str(separator);
+                acc.push_str(&escape(separator));
            }
            acc.push_str(&fragment);
            acc
        });

    match editor.registers.write(register, vec![joined]) {
        Ok(_) => editor.set_status(format!(
            "joined and yanked {selections} selection{} to register {register}",
            if selections == 1 { "" } else { "s" }
        )),
        Err(err) => editor.set_error(err.to_string()),
    }
}
fn escape(separator: &str) -> Cow<'_, str> {
    enum State {
        Normal,
        Escape,
    }

    let mut escaped = String::new();
    let mut state = State::Normal;
    let mut is_escaped = false;

    for (idx, ch) in separator.char_indices() {
        match state {
            State::Normal => match ch {
                '\\' => {
                    if !is_escaped {
                        // PERF: As not every separator will be escaped, we use `String::new` as that has no initial
                        // allocation. If an escape is found, then we reserve capacity thats the len of the separator
                        // as the new escaped string will be at least that long.
                        escaped.reserve(separator.len());
                        if idx > 0 {
                            // First time finding an escape, so all prior chars can be added to the new escaped version
                            // if its not the very first char found.
                            escaped.push_str(&separator[0..idx]);
                        }
                    }
                    state = State::Escape;
                    is_escaped = true;
                }
                _ => {
                    if is_escaped {
                        escaped.push(ch);
                    }
                }
            },
            State::Escape => match ch {
                'n' => {
                    escaped.push('\n');
                    state = State::Normal;
                }
                't' => {
                    escaped.push('\t');
                    state = State::Normal;
                }
                'r' => {
                    escaped.push('\r');
                    state = State::Normal;
                }
                '\\' => {
                    escaped.push('\\');
                    state = State::Normal;
                }
                _ => {
                    escaped.push('\\');
                    escaped.push(ch);
                    state = State::Normal;
                }
            },
        }
    }

    if is_escaped {
        escaped.into()
    } else {
        separator.into()
    }
}

from helix.

bitcrshr avatar bitcrshr commented on July 2, 2024

Ah, interesting. I work with a remote setup with helix running in tmux on Ubuntu, but SSHd from MacOS with Alacritty. Thanks for the code, I'll give it a shot and see what I can come up with!

from helix.

RoloEdits avatar RoloEdits commented on July 2, 2024

This kind of escaping can expand further than just newlines. Currently you cannot paste in unicode, like 🤩, in the yank-joined command buffer. But by offering a way to escape the literal \u{1f929} then you could join with this emoji even if you can't paste it in. Tabs would be another one. Can't tab in the command buffer. You could even add spaces with \u{20}, something that has no meaning currently as it gets ignored.

If you don't mind, I'll try opening up a pr that would provide a way to unescape these things in general, and then use it to unescape the separator.

from helix.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.