|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::future::Future; |
| 3 | + |
| 4 | +use warp_completer::completer::{ |
| 5 | + self, CompleterOptions, CompletionContext, CompletionsFallbackStrategy, MatchStrategy, |
| 6 | + SuggestionResults, |
| 7 | +}; |
| 8 | +use warp_core::features::FeatureFlag; |
| 9 | +use warp_core::user_preferences::GetUserPreferences; |
| 10 | +use warpui::AppContext; |
| 11 | + |
| 12 | +use crate::terminal::model::completions::ShellCompletion; |
| 13 | +use crate::terminal::model::session::Session; |
| 14 | + |
| 15 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 16 | +pub struct CompletionSourcePolicy { |
| 17 | + use_native_shell_completions: bool, |
| 18 | + force_native_shell_completions: bool, |
| 19 | +} |
| 20 | + |
| 21 | +impl CompletionSourcePolicy { |
| 22 | + pub fn for_session(session: &Session, buffer_text: &str, ctx: &AppContext) -> Self { |
| 23 | + let force_native_shell_completions = ctx |
| 24 | + .private_user_preferences() |
| 25 | + .read_value("ForceNativeShellCompletions") |
| 26 | + .ok() |
| 27 | + .flatten() |
| 28 | + .and_then(|value| value.parse().ok()) |
| 29 | + .unwrap_or(false); |
| 30 | + Self::from_inputs( |
| 31 | + FeatureFlag::NativeShellCompletions.is_enabled(), |
| 32 | + force_native_shell_completions, |
| 33 | + session.shell().supports_native_shell_completions(), |
| 34 | + buffer_text.contains('\n'), |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + fn from_inputs( |
| 39 | + native_shell_completions_enabled: bool, |
| 40 | + force_native_shell_completions: bool, |
| 41 | + shell_supports_native_completions: bool, |
| 42 | + is_multiline: bool, |
| 43 | + ) -> Self { |
| 44 | + Self { |
| 45 | + use_native_shell_completions: (native_shell_completions_enabled |
| 46 | + || force_native_shell_completions) |
| 47 | + && shell_supports_native_completions |
| 48 | + && !is_multiline, |
| 49 | + force_native_shell_completions, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + pub fn should_request_native_shell_completions(self) -> bool { |
| 54 | + self.use_native_shell_completions |
| 55 | + } |
| 56 | + |
| 57 | + pub fn fallback_strategy( |
| 58 | + self, |
| 59 | + fallback_when_native_is_unavailable: CompletionsFallbackStrategy, |
| 60 | + ) -> CompletionsFallbackStrategy { |
| 61 | + if self.use_native_shell_completions { |
| 62 | + CompletionsFallbackStrategy::None |
| 63 | + } else { |
| 64 | + fallback_when_native_is_unavailable |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +pub async fn completion_suggestions_with_native_fallback<T, F>( |
| 70 | + buffer_text: &str, |
| 71 | + cursor_position: usize, |
| 72 | + session_env_vars: Option<&HashMap<String, String>>, |
| 73 | + mut options: CompleterOptions, |
| 74 | + policy: CompletionSourcePolicy, |
| 75 | + native_results: F, |
| 76 | + ctx: &T, |
| 77 | +) -> Option<SuggestionResults> |
| 78 | +where |
| 79 | + T: CompletionContext, |
| 80 | + F: Future<Output = Option<Vec<ShellCompletion>>>, |
| 81 | +{ |
| 82 | + let before_cursor_text = buffer_text.get(..cursor_position)?; |
| 83 | + options.fallback_strategy = policy.fallback_strategy(options.fallback_strategy); |
| 84 | + let warp_results = completer::suggestions( |
| 85 | + before_cursor_text, |
| 86 | + before_cursor_text.len(), |
| 87 | + session_env_vars, |
| 88 | + options, |
| 89 | + ctx, |
| 90 | + ) |
| 91 | + .await; |
| 92 | + |
| 93 | + resolve_completion_results( |
| 94 | + warp_results, |
| 95 | + native_results, |
| 96 | + before_cursor_text, |
| 97 | + policy.force_native_shell_completions, |
| 98 | + ) |
| 99 | + .await |
| 100 | +} |
| 101 | + |
| 102 | +async fn resolve_completion_results<F>( |
| 103 | + warp_results: Option<SuggestionResults>, |
| 104 | + native_results: F, |
| 105 | + before_cursor_text: &str, |
| 106 | + force_native_shell_completions: bool, |
| 107 | +) -> Option<SuggestionResults> |
| 108 | +where |
| 109 | + F: Future<Output = Option<Vec<ShellCompletion>>>, |
| 110 | +{ |
| 111 | + if let Some(warp_results) = warp_results |
| 112 | + && !warp_results.suggestions.is_empty() |
| 113 | + && !force_native_shell_completions |
| 114 | + { |
| 115 | + return Some(warp_results); |
| 116 | + } |
| 117 | + |
| 118 | + native_results.await.map(|results| { |
| 119 | + let token_end = before_cursor_text.len(); |
| 120 | + let token_start = before_cursor_text |
| 121 | + .rfind(char::is_whitespace) |
| 122 | + .map(|position| position + 1) |
| 123 | + .unwrap_or_default(); |
| 124 | + SuggestionResults { |
| 125 | + replacement_span: (token_start, token_end).into(), |
| 126 | + suggestions: results.into_iter().map(Into::into).collect(), |
| 127 | + match_strategy: MatchStrategy::Fuzzy, |
| 128 | + } |
| 129 | + }) |
| 130 | +} |
| 131 | + |
| 132 | +#[cfg(test)] |
| 133 | +#[path = "completion_source_tests.rs"] |
| 134 | +mod tests; |
0 commit comments