Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complete/fish: Take RIPGREP_CONFIG_PATH into account #2708

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion crates/core/flags/complete/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ use crate::flags::{defs::FLAGS, CompletionType};

const TEMPLATE: &'static str = "complete -c rg !SHORT! -l !LONG! -d '!DOC!'";
const TEMPLATE_NEGATED: &'static str =
"complete -c rg -l !NEGATED! -n '__fish_contains_opt !SHORT! !LONG!' -d '!DOC!'\n";
"complete -c rg -l !NEGATED! -n '__rg_contains_opt !LONG! !SHORT!' -d '!DOC!'\n";

/// Generate completions for Fish.
///
/// Reference: <https://fishshell.com/docs/current/completions.html>
pub(crate) fn generate() -> String {
let mut out = String::new();
out.push_str(include_str!("prelude.fish"));
out.push('\n');
for flag in FLAGS.iter() {
let short = match flag.name_short() {
None => "".to_string(),
Expand Down Expand Up @@ -55,6 +59,10 @@ pub(crate) fn generate() -> String {
out.push_str(&completion);

if let Some(negated) = flag.name_negated() {
let short = match flag.name_short() {
None => "".to_string(),
Some(byte) => char::from(byte).to_string(),
};
out.push_str(
&TEMPLATE_NEGATED
.replace("!NEGATED!", &negated)
Expand Down
31 changes: 31 additions & 0 deletions crates/core/flags/complete/prelude.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Usage: __rg_contains_opt LONG [SHORT]
function __rg_contains_opt --description 'Specialized __fish_contains_opt'
# Cache the config file because this function is called many times per
# completion attempt.
# The cache will persist for the entire shell session (even if the
# variable or the file contents change).
if not set -q __rg_config
set -g __rg_config
if set -qx RIPGREP_CONFIG_PATH
set __rg_config (
cat -- $RIPGREP_CONFIG_PATH 2>/dev/null \
| string trim \
| string match -rv '^$|^#'
)
end
end

set -l commandline (commandline -cpo) (commandline -ct) $__rg_config

if contains -- "--$argv[1]" $commandline
return 0
end

if set -q argv[2]
if string match -qr -- "^-[^-]*$argv[2]" $commandline
return 0
end
end

return 1
end