mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-07-11 14:30:24 +02:00
hyperlink: rejigger how hyperlinks work
This essentially takes the work done in #2483 and does a bit of a facelift. A brief summary: * We reduce the hyperlink API we expose to just the format, a configuration and an environment. * We move buffer management into a hyperlink-specific interpolator. * We expand the documentation on --hyperlink-format. * We rewrite the hyperlink format parser to be a simple state machine with support for escaping '{{' and '}}'. * We remove the 'gethostname' dependency and instead insist on the caller to provide the hostname. (So grep-printer doesn't get it itself, but the application will.) Similarly for the WSL prefix. * Probably some other things. Overall, the general structure of #2483 was kept. The biggest change is probably requiring the caller to pass in things like a hostname instead of having the crate do it. I did this for a couple reasons: 1. I feel uncomfortable with code deep inside the printing logic reaching out into the environment to assume responsibility for retrieving the hostname. This feels more like an application-level responsibility. Arguably, path canonicalization falls into this same bucket, but it is more difficult to rip that out. (And we can do it in the future in a backwards compatible fashion I think.) 2. I wanted to permit end users to tell ripgrep about their system's hostname in their own way, e.g., by running a custom executable. I want this because I know at least for my own use cases, I sometimes log into systems using an SSH hostname that is distinct from the system's actual hostname (usually because the system is shared in some way or changing its hostname is not allowed/practical). I think that's about it. Closes #665, Closes #2483
This commit is contained in:
@ -1,23 +1,87 @@
|
||||
/// Aliases to well-known hyperlink schemes.
|
||||
///
|
||||
/// These need to be sorted by name.
|
||||
pub(crate) const HYPERLINK_PATTERN_ALIASES: &[(&str, &str)] = &[
|
||||
#[cfg(unix)]
|
||||
("file", "file://{host}/{file}"),
|
||||
const HYPERLINK_PATTERN_ALIASES: &[(&str, &str)] = &[
|
||||
#[cfg(not(windows))]
|
||||
("default", "file://{host}{path}"),
|
||||
#[cfg(windows)]
|
||||
("file", "file:///{file}"),
|
||||
("default", "file://{path}"),
|
||||
("file", "file://{host}{path}"),
|
||||
// https://github.com/misaki-web/grepp
|
||||
("grep+", "grep+:///{file}:{line}"),
|
||||
("kitty", "file://{host}/{file}#{line}"),
|
||||
("grep+", "grep+://{path}:{line}"),
|
||||
("kitty", "file://{host}{path}#{line}"),
|
||||
// https://macvim.org/docs/gui_mac.txt.html#mvim%3A%2F%2F
|
||||
("macvim", "mvim://open?url=file:///{file}&line={line}&column={column}"),
|
||||
("macvim", "mvim://open?url=file://{path}&line={line}&column={column}"),
|
||||
("none", ""),
|
||||
// https://github.com/inopinatus/sublime_url
|
||||
("subl", "subl://open?url=file:///{file}&line={line}&column={column}"),
|
||||
("subl", "subl://open?url=file://{path}&line={line}&column={column}"),
|
||||
// https://macromates.com/blog/2007/the-textmate-url-scheme/
|
||||
("textmate", "txmt://open?url=file:///{file}&line={line}&column={column}"),
|
||||
("textmate", "txmt://open?url=file://{path}&line={line}&column={column}"),
|
||||
// https://code.visualstudio.com/docs/editor/command-line#_opening-vs-code-with-urls
|
||||
("vscode", "vscode://file/{file}:{line}:{column}"),
|
||||
("vscode-insiders", "vscode-insiders://file/{file}:{line}:{column}"),
|
||||
("vscodium", "vscodium://file/{file}:{line}:{column}"),
|
||||
("vscode", "vscode://file{path}:{line}:{column}"),
|
||||
("vscode-insiders", "vscode-insiders://file{path}:{line}:{column}"),
|
||||
("vscodium", "vscodium://file{path}:{line}:{column}"),
|
||||
];
|
||||
|
||||
/// Look for the hyperlink format defined by the given alias name.
|
||||
///
|
||||
/// If one does not exist, `None` is returned.
|
||||
pub(crate) fn find(name: &str) -> Option<&str> {
|
||||
HYPERLINK_PATTERN_ALIASES
|
||||
.binary_search_by_key(&name, |&(name, _)| name)
|
||||
.map(|i| HYPERLINK_PATTERN_ALIASES[i].1)
|
||||
.ok()
|
||||
}
|
||||
|
||||
/// Return an iterator over all available alias names and their definitions.
|
||||
pub(crate) fn iter() -> impl Iterator<Item = (&'static str, &'static str)> {
|
||||
HYPERLINK_PATTERN_ALIASES.iter().copied()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::HyperlinkFormat;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn is_sorted() {
|
||||
let mut prev = HYPERLINK_PATTERN_ALIASES
|
||||
.get(0)
|
||||
.expect("aliases should be non-empty")
|
||||
.0;
|
||||
for &(name, _) in HYPERLINK_PATTERN_ALIASES.iter().skip(1) {
|
||||
assert!(
|
||||
name > prev,
|
||||
"'{prev}' should come before '{name}' in \
|
||||
HYPERLINK_PATTERN_ALIASES",
|
||||
);
|
||||
prev = name;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alias_names_are_reasonable() {
|
||||
for &(name, _) in HYPERLINK_PATTERN_ALIASES.iter() {
|
||||
// There's no hard rule here, but if we want to define an alias
|
||||
// with a name that doesn't pass this assert, then we should
|
||||
// probably flag it as worthy of consideration. For example, we
|
||||
// really do not want to define an alias that contains `{` or `}`,
|
||||
// which might confuse it for a variable.
|
||||
assert!(name.chars().all(|c| c.is_alphanumeric()
|
||||
|| c == '+'
|
||||
|| c == '-'
|
||||
|| c == '.'));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aliases_are_valid_formats() {
|
||||
for (name, definition) in HYPERLINK_PATTERN_ALIASES {
|
||||
assert!(
|
||||
definition.parse::<HyperlinkFormat>().is_ok(),
|
||||
"invalid hyperlink alias '{name}': {definition}",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user