mirror of
https://github.com/nushell/nushell.git
synced 2024-12-24 16:09:11 +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 reedline::Suggestion;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub struct CommandCompletion<'a> {
|
pub struct CommandCompletion {
|
||||||
engine_state: Arc<EngineState>,
|
engine_state: Arc<EngineState>,
|
||||||
flattened: &'a [(Span, FlatShape)],
|
flattened: Vec<(Span, FlatShape)>,
|
||||||
flat_idx: usize,
|
flat_idx: usize,
|
||||||
flat_shape: &'a FlatShape,
|
flat_shape: FlatShape,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CommandCompletion<'a> {
|
impl CommandCompletion {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
engine_state: Arc<EngineState>,
|
engine_state: Arc<EngineState>,
|
||||||
_: &StateWorkingSet,
|
_: &StateWorkingSet,
|
||||||
flattened: &'a [(Span, FlatShape)],
|
flattened: Vec<(Span, FlatShape)>,
|
||||||
flat_idx: usize,
|
flat_idx: usize,
|
||||||
flat_shape: &'a FlatShape,
|
flat_shape: FlatShape,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
engine_state,
|
engine_state,
|
||||||
@ -146,7 +146,7 @@ impl<'a> CommandCompletion<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Completer for CommandCompletion<'a> {
|
impl Completer for CommandCompletion {
|
||||||
fn fetch(
|
fn fetch(
|
||||||
&mut self,
|
&mut self,
|
||||||
working_set: &StateWorkingSet,
|
working_set: &StateWorkingSet,
|
||||||
|
@ -1,23 +1,20 @@
|
|||||||
use crate::completions::{
|
use crate::completions::{
|
||||||
CommandCompletion, Completer, CompletionOptions, CustomCompletion, FileCompletion,
|
CommandCompletion, Completer, CustomCompletion, FileCompletion, FlagCompletion,
|
||||||
FlagCompletion, VariableCompletion,
|
VariableCompletion,
|
||||||
};
|
};
|
||||||
use nu_parser::{flatten_expression, parse, FlatShape};
|
use nu_parser::{flatten_expression, parse, FlatShape};
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
engine::{EngineState, Stack, StateWorkingSet},
|
engine::{EngineState, Stack, StateWorkingSet},
|
||||||
Span, Value,
|
Span, Value,
|
||||||
};
|
};
|
||||||
use reedline::{Completer as ReedlineCompleter, Span as ReedlineSpan, Suggestion};
|
use reedline::{Completer as ReedlineCompleter, Suggestion};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct NuCompleter {
|
pub struct NuCompleter {
|
||||||
engine_state: Arc<EngineState>,
|
engine_state: Arc<EngineState>,
|
||||||
stack: Stack,
|
stack: Stack,
|
||||||
config: Option<Value>,
|
config: Option<Value>,
|
||||||
cached_results: Option<(Vec<Suggestion>, CompletionOptions)>,
|
|
||||||
last_fetch: Option<Instant>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NuCompleter {
|
impl NuCompleter {
|
||||||
@ -26,14 +23,12 @@ impl NuCompleter {
|
|||||||
engine_state,
|
engine_state,
|
||||||
stack,
|
stack,
|
||||||
config,
|
config,
|
||||||
cached_results: None,
|
|
||||||
last_fetch: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the completion for a given completer
|
// Process the completion for a given completer
|
||||||
fn process_completion<T: Completer>(
|
fn process_completion<T: Completer>(
|
||||||
&mut self,
|
&self,
|
||||||
completer: &mut T,
|
completer: &mut T,
|
||||||
working_set: &StateWorkingSet,
|
working_set: &StateWorkingSet,
|
||||||
prefix: Vec<u8>,
|
prefix: Vec<u8>,
|
||||||
@ -41,41 +36,9 @@ impl NuCompleter {
|
|||||||
offset: usize,
|
offset: usize,
|
||||||
pos: usize,
|
pos: usize,
|
||||||
) -> Vec<Suggestion> {
|
) -> 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
|
// Fetch
|
||||||
let (mut suggestions, options) = match self.cached_results.clone() {
|
let (mut suggestions, options) =
|
||||||
Some((suggestions, options)) => {
|
completer.fetch(working_set, prefix.clone(), new_span, offset, pos);
|
||||||
// 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
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Filter
|
// Filter
|
||||||
suggestions = completer.filter(prefix.clone(), suggestions, options.clone());
|
suggestions = completer.filter(prefix.clone(), suggestions, options.clone());
|
||||||
@ -83,15 +46,11 @@ impl NuCompleter {
|
|||||||
// Sort
|
// Sort
|
||||||
suggestions = completer.sort(suggestions, prefix, options);
|
suggestions = completer.sort(suggestions, prefix, options);
|
||||||
|
|
||||||
// Update last fetch
|
|
||||||
self.last_fetch = Some(Instant::now());
|
|
||||||
|
|
||||||
suggestions
|
suggestions
|
||||||
}
|
}
|
||||||
|
|
||||||
fn completion_helper(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
|
fn completion_helper(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
|
||||||
let engine_state = self.engine_state.clone();
|
let mut working_set = StateWorkingSet::new(&self.engine_state);
|
||||||
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
||||||
let offset = working_set.next_span_start();
|
let offset = working_set.next_span_start();
|
||||||
let mut line = line.to_string();
|
let mut line = line.to_string();
|
||||||
line.insert(pos, 'a');
|
line.insert(pos, 'a');
|
||||||
@ -184,9 +143,9 @@ impl NuCompleter {
|
|||||||
let mut completer = CommandCompletion::new(
|
let mut completer = CommandCompletion::new(
|
||||||
self.engine_state.clone(),
|
self.engine_state.clone(),
|
||||||
&working_set,
|
&working_set,
|
||||||
&flattened,
|
flattened.clone(),
|
||||||
flat_idx,
|
flat_idx,
|
||||||
flat_shape,
|
flat_shape.clone(),
|
||||||
);
|
);
|
||||||
|
|
||||||
return self.process_completion(
|
return self.process_completion(
|
||||||
|
Loading…
Reference in New Issue
Block a user