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

Code before:
                .fragment(text_slice)
                .trim_matches(surrounding_chars)
                .to_string(),
        );
    }

    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();

    if url.scheme() != "file" {
        return cx.jobs.callback(crate::open_external_url_callback(url));
    }

    let content_type = std::fs::File::open(url.path()).and_then(|file| {
        // Read up to 1kb to detect the content type
        let mut read_buffer = Vec::new();
        let n = file.take(1024).read_to_end(&mut read_buffer)?;
        Ok(content_inspector::inspect(&read_buffer[..n]))
    });

    // we attempt to open binary files - files that can't be open in helix - using external
    // program as well, e.g. pdf files or images
    match content_type {
        Ok(content_inspector::ContentType::BINARY) => {
            cx.jobs.callback(crate::open_external_url_callback(url))

Review comment (suggestion):
For readers' context: rope slicing is by character index rather than byte index (like `&str`) which is why this uses `chars_at(..).count()` rather than a len of a substring

Code after:
        };

        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);
        }

        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));