mirror of
https://github.com/nushell/nushell.git
synced 2024-12-23 23:49:44 +01:00
This reverts commit e86c1b118e
.
This commit is contained in:
parent
6ed033737d
commit
5afd45414e
@ -9,20 +9,20 @@ use nu_protocol::{
|
||||
use reedline::Suggestion;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct CommandCompletion<'a> {
|
||||
pub struct CommandCompletion {
|
||||
engine_state: Arc<EngineState>,
|
||||
flattened: &'a [(Span, FlatShape)],
|
||||
flattened: Vec<(Span, FlatShape)>,
|
||||
flat_idx: usize,
|
||||
flat_shape: &'a FlatShape,
|
||||
flat_shape: FlatShape,
|
||||
}
|
||||
|
||||
impl<'a> CommandCompletion<'a> {
|
||||
impl CommandCompletion {
|
||||
pub fn new(
|
||||
engine_state: Arc<EngineState>,
|
||||
_: &StateWorkingSet,
|
||||
flattened: &'a [(Span, FlatShape)],
|
||||
flattened: Vec<(Span, FlatShape)>,
|
||||
flat_idx: usize,
|
||||
flat_shape: &'a FlatShape,
|
||||
flat_shape: FlatShape,
|
||||
) -> Self {
|
||||
Self {
|
||||
engine_state,
|
||||
@ -146,7 +146,7 @@ impl<'a> CommandCompletion<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Completer for CommandCompletion<'a> {
|
||||
impl Completer for CommandCompletion {
|
||||
fn fetch(
|
||||
&mut self,
|
||||
working_set: &StateWorkingSet,
|
||||
|
@ -1,23 +1,20 @@
|
||||
use crate::completions::{
|
||||
CommandCompletion, Completer, CompletionOptions, CustomCompletion, FileCompletion,
|
||||
FlagCompletion, VariableCompletion,
|
||||
CommandCompletion, Completer, CustomCompletion, FileCompletion, FlagCompletion,
|
||||
VariableCompletion,
|
||||
};
|
||||
use nu_parser::{flatten_expression, parse, FlatShape};
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack, StateWorkingSet},
|
||||
Span, Value,
|
||||
};
|
||||
use reedline::{Completer as ReedlineCompleter, Span as ReedlineSpan, Suggestion};
|
||||
use reedline::{Completer as ReedlineCompleter, Suggestion};
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct NuCompleter {
|
||||
engine_state: Arc<EngineState>,
|
||||
stack: Stack,
|
||||
config: Option<Value>,
|
||||
cached_results: Option<(Vec<Suggestion>, CompletionOptions)>,
|
||||
last_fetch: Option<Instant>,
|
||||
}
|
||||
|
||||
impl NuCompleter {
|
||||
@ -26,14 +23,12 @@ impl NuCompleter {
|
||||
engine_state,
|
||||
stack,
|
||||
config,
|
||||
cached_results: None,
|
||||
last_fetch: None,
|
||||
}
|
||||
}
|
||||
|
||||
// Process the completion for a given completer
|
||||
fn process_completion<T: Completer>(
|
||||
&mut self,
|
||||
&self,
|
||||
completer: &mut T,
|
||||
working_set: &StateWorkingSet,
|
||||
prefix: Vec<u8>,
|
||||
@ -41,41 +36,9 @@ impl NuCompleter {
|
||||
offset: usize,
|
||||
pos: usize,
|
||||
) -> Vec<Suggestion> {
|
||||
// Cleanup the result cache if it's old
|
||||
if let Some(instant) = self.last_fetch {
|
||||
if instant.elapsed() > Duration::from_millis(1000) {
|
||||
self.cached_results = None;
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch
|
||||
let (mut suggestions, options) = match self.cached_results.clone() {
|
||||
Some((suggestions, options)) => {
|
||||
// Update cached spans
|
||||
let suggestions = suggestions
|
||||
.into_iter()
|
||||
.map(|suggestion| Suggestion {
|
||||
value: suggestion.value,
|
||||
description: suggestion.description,
|
||||
extra: suggestion.extra,
|
||||
span: ReedlineSpan {
|
||||
start: new_span.start - offset,
|
||||
end: new_span.end - offset,
|
||||
},
|
||||
})
|
||||
.collect();
|
||||
|
||||
(suggestions, options)
|
||||
}
|
||||
None => {
|
||||
let result = completer.fetch(working_set, prefix.clone(), new_span, offset, pos);
|
||||
|
||||
// Update cache results
|
||||
self.cached_results = Some(result.clone());
|
||||
|
||||
result
|
||||
}
|
||||
};
|
||||
let (mut suggestions, options) =
|
||||
completer.fetch(working_set, prefix.clone(), new_span, offset, pos);
|
||||
|
||||
// Filter
|
||||
suggestions = completer.filter(prefix.clone(), suggestions, options.clone());
|
||||
@ -83,15 +46,11 @@ impl NuCompleter {
|
||||
// Sort
|
||||
suggestions = completer.sort(suggestions, prefix, options);
|
||||
|
||||
// Update last fetch
|
||||
self.last_fetch = Some(Instant::now());
|
||||
|
||||
suggestions
|
||||
}
|
||||
|
||||
fn completion_helper(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
|
||||
let engine_state = self.engine_state.clone();
|
||||
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||
let mut working_set = StateWorkingSet::new(&self.engine_state);
|
||||
let offset = working_set.next_span_start();
|
||||
let mut line = line.to_string();
|
||||
line.insert(pos, 'a');
|
||||
@ -184,9 +143,9 @@ impl NuCompleter {
|
||||
let mut completer = CommandCompletion::new(
|
||||
self.engine_state.clone(),
|
||||
&working_set,
|
||||
&flattened,
|
||||
flattened.clone(),
|
||||
flat_idx,
|
||||
flat_shape,
|
||||
flat_shape.clone(),
|
||||
);
|
||||
|
||||
return self.process_completion(
|
||||
|
Loading…
Reference in New Issue
Block a user