Repository: helix-editor/helix
File: helix-term/src/commands.rs (Rust)

Code before:
            };

            let head = start
                .clone()
                .reversed()
                .take_while(is_valid_path_char)
                .collect::<String>()
                .chars()
                .rev()
                .collect::<String>();

            let tail = start.take_while(is_valid_path_char).collect::<String>();

            format!("{}{}", head, tail)
        };
        log::debug!("Goto file path: {}", path);

        match shellexpand::full(&path) {
            Ok(path) => paths.push(path.to_string()),
            Err(e) => cx.editor.set_error(format!("{}", e)),
        };
    }

    for sel in paths {
        let p = sel.trim();
        if p.is_empty() {
            continue;
        }

        if let Ok(url) = Url::parse(p) {
            return open_url(cx, url, action);
        }

        let path = &rel_path.join(p);
        if path.is_dir() {
            let picker = ui::file_picker(path.into(), &cx.editor.config());
            cx.push_layer(Box::new(overlaid(picker)));
        } else if let Err(e) = cx.editor.open(path, action) {
            cx.editor.set_error(format!("Open file failed: {:?}", e));
        }
    }
}

/// Opens the given url. If the URL points to a valid textual file it is open in helix.
//  Otherwise, the file is open using external program.
fn open_url(cx: &mut Context, url: Url, action: Action) {
    let doc = doc!(cx.editor);
    let rel_path = doc
        .relative_path()
        .map(|path| path.parent().unwrap().to_path_buf())
        .unwrap_or_default();

Review comment (suggestion):
Instead of collecting twice simply find the length (using count and then use `text.slice(cursor-prefix_len..cursor+postfix_len)`..

Then you can even convert to a Cow<str> (usually for free) without extra work.

That will be both faster and less messy

Code after:
                '@', '/', '\\', '.', '-', '_', '+', '#', '$', '%', '{', '}', '[', ']', ':', '!',
                '~', '=',
            ];
            #[cfg(not(target_os = "windows"))]
            let valid_chars = &['@', '/', '.', '-', '_', '+', '#', '$', '%', '~', '=', ':'];

            valid_chars.contains(c) || c.is_alphabetic() || c.is_numeric()
        };

        let cursor_pos = primary.cursor(text.slice(..));
        let pre_cursor_pos = cursor_pos.saturating_sub(1);
        let post_cursor_pos = cursor_pos + 1;
        let start_pos = if is_valid_path_char(&text.char(cursor_pos)) {
            cursor_pos
        } else if is_valid_path_char(&text.char(pre_cursor_pos)) {
            pre_cursor_pos
        } else {
            post_cursor_pos
        };

        let prefix_len = text
            .chars_at(start_pos)
            .reversed()
            .take_while(is_valid_path_char)
            .count();

        let postfix_len = text
            .chars_at(start_pos)
            .take_while(is_valid_path_char)
            .count();

        let path: Cow<str> = text
            .slice((start_pos - prefix_len)..(start_pos + postfix_len))
            .into();
        log::debug!("Goto file path: {}", path);

        match expand_tilde(PathBuf::from(path.as_ref())).to_str() {
            Some(path) => paths.push(path.to_string()),
            None => cx.editor.set_error("Couldn't get string out of path."),
        };
    }

    for sel in paths {
        let p = sel.trim();
        if p.is_empty() {
            continue;
        }

        if let Ok(url) = Url::parse(p) {
            return open_url(cx, url, action);
        }