Skip to content

Commit a3f452e

Browse files
committed
move NuMatcher to nu-protocol
1 parent 6fbf254 commit a3f452e

19 files changed

Lines changed: 231 additions & 178 deletions

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/nu-cli/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ is_executable = { workspace = true }
4747
log = { workspace = true }
4848
lscolors = { workspace = true, default-features = false, features = ["nu-ansi-term"] }
4949
miette = { workspace = true, features = ["fancy-no-backtrace"] }
50-
nucleo-matcher = { workspace = true }
5150
percent-encoding = { workspace = true }
5251
sysinfo = { workspace = true }
5352
strum = { workspace = true }

crates/nu-cli/src/completions/arg_value_completion.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ use crate::{
22
FileCompletion, NuCompleter,
33
completions::{
44
CommandCompletion, Completer, DirectoryCompletion, DotNuCompletion, ExportableCompletion,
5-
SemanticSuggestion, completer::Context, completion_options::NuMatcher,
5+
SemanticSuggestion,
6+
completer::Context,
7+
matcher_helper::{add_semantic_suggestion, suggestion_results},
68
},
79
};
810
use nu_parser::parse_module_file_or_dir;
911
use nu_protocol::{
10-
CompletionOptions, DynamicCompletionCallRef, Span,
12+
CompletionOptions, DynamicCompletionCallRef, NuMatcher, Span,
1113
ast::{Argument, Call, Expr, Expression, ListItem},
1214
engine::{ArgType, Stack, StateWorkingSet},
1315
};
@@ -62,9 +64,9 @@ impl<'a> Completer for ArgValueCompletion<'a> {
6264
},
6365
None,
6466
);
65-
matcher.add_semantic_suggestion(suggestion);
67+
add_semantic_suggestion(&mut matcher, suggestion);
6668
}
67-
return matcher.suggestion_results();
69+
return suggestion_results(matcher);
6870
}
6971
Err(e) => {
7072
log::error!(
Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
use super::{SemanticSuggestion, completion_options::NuMatcher};
2-
use crate::completions::Completer;
1+
use super::SemanticSuggestion;
2+
use crate::completions::{
3+
Completer,
4+
matcher_helper::{add_semantic_suggestion, suggestion_results},
5+
};
36
use nu_protocol::{
4-
CompletionOptions, Span, SuggestionKind,
7+
CompletionOptions, NuMatcher, Span, SuggestionKind,
58
engine::{Stack, StateWorkingSet},
69
};
710
use reedline::Suggestion;
@@ -26,22 +29,25 @@ impl Completer for AttributeCompletion {
2629

2730
for (decl_id, name, desc, ty) in attr_commands {
2831
let name = name.strip_prefix(b"attr ").unwrap_or(&name);
29-
matcher.add_semantic_suggestion(SemanticSuggestion {
30-
suggestion: Suggestion {
31-
value: String::from_utf8_lossy(name).into_owned(),
32-
description: desc,
33-
span: reedline::Span {
34-
start: span.start - offset,
35-
end: span.end - offset,
32+
add_semantic_suggestion(
33+
&mut matcher,
34+
SemanticSuggestion {
35+
suggestion: Suggestion {
36+
value: String::from_utf8_lossy(name).into_owned(),
37+
description: desc,
38+
span: reedline::Span {
39+
start: span.start - offset,
40+
end: span.end - offset,
41+
},
42+
append_whitespace: false,
43+
..Default::default()
3644
},
37-
append_whitespace: false,
38-
..Default::default()
45+
kind: Some(SuggestionKind::Command(ty, Some(decl_id))),
3946
},
40-
kind: Some(SuggestionKind::Command(ty, Some(decl_id))),
41-
});
47+
);
4248
}
4349

44-
matcher.suggestion_results()
50+
suggestion_results(matcher)
4551
}
4652
}
4753

@@ -62,25 +68,28 @@ impl Completer for AttributableCompletion {
6268
.find_decl(s.as_bytes())
6369
.expect("internal error, builtin declaration not found");
6470
let cmd = working_set.get_decl(decl_id);
65-
matcher.add_semantic_suggestion(SemanticSuggestion {
66-
suggestion: Suggestion {
67-
value: cmd.name().into(),
68-
description: Some(cmd.description().into()),
69-
span: reedline::Span {
70-
start: span.start - offset,
71-
end: span.end - offset,
71+
add_semantic_suggestion(
72+
&mut matcher,
73+
SemanticSuggestion {
74+
suggestion: Suggestion {
75+
value: cmd.name().into(),
76+
description: Some(cmd.description().into()),
77+
span: reedline::Span {
78+
start: span.start - offset,
79+
end: span.end - offset,
80+
},
81+
append_whitespace: true,
82+
..Default::default()
7283
},
73-
append_whitespace: true,
74-
..Default::default()
84+
kind: Some(SuggestionKind::Command(
85+
cmd.command_type(),
86+
// for snippet completion in LSP
87+
working_set.find_decl(s.as_bytes()),
88+
)),
7589
},
76-
kind: Some(SuggestionKind::Command(
77-
cmd.command_type(),
78-
// for snippet completion in LSP
79-
working_set.find_decl(s.as_bytes()),
80-
)),
81-
});
90+
);
8291
}
8392

84-
matcher.suggestion_results()
93+
suggestion_results(matcher)
8594
}
8695
}

crates/nu-cli/src/completions/cell_path_completions.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
use std::borrow::Cow;
22

3-
use crate::completions::{Completer, SemanticSuggestion};
3+
use crate::completions::{
4+
Completer, SemanticSuggestion,
5+
matcher_helper::{add_semantic_suggestion, suggestion_results},
6+
};
47
use nu_engine::{column::get_columns, eval_variable};
58
use nu_protocol::{
6-
CompletionOptions, ShellError, Span, SuggestionKind, Type, Value,
9+
CompletionOptions, NuMatcher, ShellError, Span, SuggestionKind, Type, Value,
710
ast::{Expr, Expression, FullCellPath, PathMember},
811
engine::{Stack, StateWorkingSet},
912
eval_const::eval_constant,
1013
};
1114
use reedline::Suggestion;
1215

13-
use super::completion_options::NuMatcher;
14-
1516
pub struct CellPathCompletion<'a> {
1617
pub full_cell_path: &'a FullCellPath,
1718
pub position: usize,
@@ -70,15 +71,15 @@ impl Completer for CellPathCompletion<'_> {
7071

7172
if let Ok(value) = value {
7273
for suggestion in get_suggestions_by_value(&value, current_span) {
73-
matcher.add_semantic_suggestion(suggestion);
74+
add_semantic_suggestion(&mut matcher, suggestion);
7475
}
7576
} else if let Some(ty) = self.full_cell_path.head.ty.follow_cell_path(path_members) {
7677
for suggestion in get_suggestions_by_type(&ty, current_span) {
77-
matcher.add_semantic_suggestion(suggestion);
78+
add_semantic_suggestion(&mut matcher, suggestion);
7879
}
7980
}
8081

81-
matcher.suggestion_results()
82+
suggestion_results(matcher)
8283
}
8384
}
8485

crates/nu-cli/src/completions/command_completions.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
use std::collections::HashSet;
22

3-
use crate::completions::Completer;
3+
use crate::completions::{
4+
Completer,
5+
matcher_helper::{add_semantic_suggestion, suggestion_results},
6+
};
47
use nu_protocol::{
5-
Category, CompletionOptions, Span, SuggestionKind,
8+
Category, CompletionOptions, NuMatcher, Span, SuggestionKind,
69
engine::{CommandType, Stack, StateWorkingSet},
710
};
811
use reedline::Suggestion;
912

10-
use super::{SemanticSuggestion, completion_options::NuMatcher};
13+
use super::SemanticSuggestion;
1114

1215
// TODO: Add a toggle for quoting multi word commands. Useful for: `which` and `attr complete`
1316
pub struct CommandCompletion {
@@ -88,7 +91,7 @@ impl CommandCompletion {
8891
}
8992
}
9093

91-
matcher.suggestion_results()
94+
suggestion_results(matcher)
9295
}
9396

9497
fn is_executable_command(path: impl AsRef<std::path::Path>) -> bool {
@@ -130,24 +133,27 @@ impl Completer for CommandCompletion {
130133
if command.signature().category == Category::Removed {
131134
return;
132135
}
133-
let matched = matcher.add_semantic_suggestion(SemanticSuggestion {
134-
suggestion: Suggestion {
135-
value: name.to_string(),
136-
description: Some(command.description().to_string()),
137-
span: sugg_span,
138-
append_whitespace: true,
139-
..Suggestion::default()
136+
let matched = add_semantic_suggestion(
137+
&mut matcher,
138+
SemanticSuggestion {
139+
suggestion: Suggestion {
140+
value: name.to_string(),
141+
description: Some(command.description().to_string()),
142+
span: sugg_span,
143+
append_whitespace: true,
144+
..Suggestion::default()
145+
},
146+
kind: Some(SuggestionKind::Command(
147+
command.command_type(),
148+
Some(decl_id),
149+
)),
140150
},
141-
kind: Some(SuggestionKind::Command(
142-
command.command_type(),
143-
Some(decl_id),
144-
)),
145-
});
151+
);
146152
if matched {
147153
internal_suggs.insert(name.to_string());
148154
}
149155
});
150-
res.extend(matcher.suggestion_results());
156+
res.extend(suggestion_results(matcher));
151157
}
152158

153159
if self.externals {

crates/nu-cli/src/completions/completion_common.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use super::completion_options::NuMatcher;
21
use nu_ansi_term::Style;
32
use nu_engine::env_to_string;
43
use nu_path::dots::expand_ndots;
54
use nu_path::{expand_to_real_path, home_dir};
65
use nu_protocol::{
7-
CompletionAlgorithm, CompletionOptions, Span,
6+
CompletionAlgorithm, CompletionOptions, NuMatcher, Span,
87
engine::{EngineState, Stack, StateWorkingSet},
98
};
109
use nu_utils::IgnoreCaseExt;

crates/nu-cli/src/completions/custom_completions.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::completions::{Completer, SemanticSuggestion};
1+
use crate::completions::{Completer, SemanticSuggestion, matcher_helper::suggestion_results};
22
use nu_color_config::{color_record_to_nustyle, lookup_ansi_color_style};
33
use nu_engine::{compile, eval_call};
44
use nu_parser::flatten_expression;
55
use nu_protocol::{
6-
BlockId, CompletionAlgorithm, CompletionOptions, DeclId, GetSpan, IntoSpanned, PipelineData,
7-
Record, ShellError, Span, Spanned, SuggestionKind, Type, Value, VarId,
6+
BlockId, CompletionAlgorithm, CompletionOptions, DeclId, GetSpan, IntoSpanned, NuMatcher,
7+
PipelineData, Record, ShellError, Span, Spanned, SuggestionKind, Type, Value, VarId,
88
ast::{Argument, Call, Expr, Expression},
99
debugger::WithoutDebug,
1010
engine::{Closure, EngineState, Stack, StateWorkingSet},
@@ -13,8 +13,6 @@ use nu_utils::{SharedCow, strip_ansi_string_unlikely};
1313
use reedline::Suggestion;
1414
use std::{collections::HashMap, str::FromStr, sync::Arc};
1515

16-
use super::completion_options::NuMatcher;
17-
1816
fn map_value_completions<'a>(
1917
list: impl Iterator<Item = &'a Value>,
2018
span: Span,
@@ -238,8 +236,8 @@ impl<T: Completer> Completer for CustomCompletion<T> {
238236
}
239237
if let Some(algorithm) = options
240238
.get("completion_algorithm")
241-
.and_then(|option| option.coerce_string().ok())
242-
.and_then(|option| CompletionAlgorithm::from_str(option.as_str()).ok())
239+
.and_then(|option| option.coerce_str().ok())
240+
.and_then(|option| CompletionAlgorithm::from_str(&option).ok())
243241
{
244242
completion_options.match_algorithm = algorithm;
245243
if let Some(false) = positional
@@ -294,7 +292,7 @@ impl<T: Completer> Completer for CustomCompletion<T> {
294292
sugg,
295293
);
296294
}
297-
matcher.suggestion_results()
295+
suggestion_results(matcher)
298296
}
299297
}
300298

0 commit comments

Comments
 (0)