Skip to content

Commit 1d40bf2

Browse files
committed
refactor(completion, lsp): include decl_id in suggetion_kind for later usage
1 parent 173162d commit 1d40bf2

File tree

7 files changed

+27
-27
lines changed

7 files changed

+27
-27
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Completer for AttributeCompletion {
2727
let attr_commands =
2828
working_set.find_commands_by_predicate(|s| s.starts_with(b"attr "), true);
2929

30-
for (name, desc, ty) in attr_commands {
30+
for (decl_id, name, desc, ty) in attr_commands {
3131
let name = name.strip_prefix(b"attr ").unwrap_or(&name);
3232
matcher.add_semantic_suggestion(SemanticSuggestion {
3333
suggestion: Suggestion {
@@ -41,7 +41,7 @@ impl Completer for AttributeCompletion {
4141
},
4242
append_whitespace: false,
4343
},
44-
kind: Some(SuggestionKind::Command(ty)),
44+
kind: Some(SuggestionKind::Command(ty, Some(decl_id))),
4545
});
4646
}
4747

@@ -78,7 +78,7 @@ impl Completer for AttributableCompletion {
7878
},
7979
append_whitespace: false,
8080
},
81-
kind: Some(SuggestionKind::Command(cmd.command_type())),
81+
kind: Some(SuggestionKind::Command(cmd.command_type(), None)),
8282
});
8383
}
8484

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::completions::CompletionOptions;
22
use nu_protocol::{
33
engine::{Stack, StateWorkingSet},
4-
Span,
4+
DeclId, Span,
55
};
66
use reedline::Suggestion;
77

@@ -28,7 +28,7 @@ pub struct SemanticSuggestion {
2828
// TODO: think about name: maybe suggestion context?
2929
#[derive(Clone, Debug, PartialEq)]
3030
pub enum SuggestionKind {
31-
Command(nu_protocol::engine::CommandType),
31+
Command(nu_protocol::engine::CommandType, Option<DeclId>),
3232
Value(nu_protocol::Type),
3333
CellPath,
3434
Directory,

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ impl CommandCompletion {
7575
append_whitespace: true,
7676
..Default::default()
7777
},
78-
kind: Some(SuggestionKind::Command(CommandType::External)),
78+
kind: Some(SuggestionKind::Command(
79+
CommandType::External,
80+
None,
81+
)),
7982
},
8083
);
8184
}
@@ -112,7 +115,7 @@ impl Completer for CommandCompletion {
112115
},
113116
true,
114117
);
115-
for (name, description, typ) in filtered_commands {
118+
for (decl_id, name, description, typ) in filtered_commands {
116119
let name = String::from_utf8_lossy(&name);
117120
internal_suggs.insert(
118121
name.to_string(),
@@ -124,7 +127,7 @@ impl Completer for CommandCompletion {
124127
append_whitespace: true,
125128
..Suggestion::default()
126129
},
127-
kind: Some(SuggestionKind::Command(typ)),
130+
kind: Some(SuggestionKind::Command(typ, Some(decl_id))),
128131
},
129132
);
130133
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Completer for ExportableCompletion<'_> {
6969
wrapped_name(name),
7070
Some(cmd.description().to_string()),
7171
None,
72-
SuggestionKind::Command(cmd.command_type()),
72+
SuggestionKind::Command(cmd.command_type(), Some(*decl_id)),
7373
);
7474
}
7575
}

crates/nu-lsp/src/completion.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,12 @@ impl LanguageServer {
6262
suggestion: SemanticSuggestion,
6363
range: Range,
6464
) -> CompletionItem {
65-
let decl_id = suggestion.kind.as_ref().and_then(|kind| {
66-
matches!(kind, SuggestionKind::Command(_))
67-
.then_some(engine_state.find_decl(suggestion.suggestion.value.as_bytes(), &[])?)
68-
});
69-
7065
let mut snippet_text = suggestion.suggestion.value.clone();
7166
let mut doc_string = suggestion.suggestion.extra.map(|ex| ex.join("\n"));
7267
let mut insert_text_format = None;
7368
let mut idx = 0;
7469
// use snippet as `insert_text_format` for command argument completion
75-
if let Some(decl_id) = decl_id {
70+
if let Some(SuggestionKind::Command(_, Some(decl_id))) = suggestion.kind {
7671
let cmd = engine_state.get_decl(decl_id);
7772
doc_string = Some(Self::get_decl_description(cmd, true));
7873
insert_text_format = Some(InsertTextFormat::SNIPPET);
@@ -138,7 +133,7 @@ impl LanguageServer {
138133
.as_ref()
139134
.map(|kind| match kind {
140135
SuggestionKind::Value(t) => t.to_string(),
141-
SuggestionKind::Command(cmd) => cmd.to_string(),
136+
SuggestionKind::Command(cmd, _) => cmd.to_string(),
142137
SuggestionKind::Module => "module".to_string(),
143138
SuggestionKind::Operator => "operator".to_string(),
144139
SuggestionKind::Variable => "variable".to_string(),
@@ -172,7 +167,7 @@ impl LanguageServer {
172167
_ => None,
173168
},
174169
SuggestionKind::CellPath => Some(CompletionItemKind::PROPERTY),
175-
SuggestionKind::Command(c) => match c {
170+
SuggestionKind::Command(c, _) => match c {
176171
CommandType::Keyword => Some(CompletionItemKind::KEYWORD),
177172
CommandType::Builtin => Some(CompletionItemKind::FUNCTION),
178173
CommandType::Alias => Some(CompletionItemKind::REFERENCE),

crates/nu-protocol/src/engine/engine_state.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -731,18 +731,19 @@ impl EngineState {
731731
&self,
732732
mut predicate: impl FnMut(&[u8]) -> bool,
733733
ignore_deprecated: bool,
734-
) -> Vec<(Vec<u8>, Option<String>, CommandType)> {
734+
) -> Vec<(DeclId, Vec<u8>, Option<String>, CommandType)> {
735735
let mut output = vec![];
736736

737737
for overlay_frame in self.active_overlays(&[]).rev() {
738-
for decl in &overlay_frame.decls {
739-
if overlay_frame.visibility.is_decl_id_visible(decl.1) && predicate(decl.0) {
740-
let command = self.get_decl(*decl.1);
738+
for (name, decl_id) in &overlay_frame.decls {
739+
if overlay_frame.visibility.is_decl_id_visible(decl_id) && predicate(name) {
740+
let command = self.get_decl(*decl_id);
741741
if ignore_deprecated && command.signature().category == Category::Removed {
742742
continue;
743743
}
744744
output.push((
745-
decl.0.clone(),
745+
*decl_id,
746+
name.clone(),
746747
Some(command.description().to_string()),
747748
command.command_type(),
748749
));

crates/nu-protocol/src/engine/state_working_set.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -780,21 +780,22 @@ impl<'a> StateWorkingSet<'a> {
780780
&self,
781781
mut predicate: impl FnMut(&[u8]) -> bool,
782782
ignore_deprecated: bool,
783-
) -> Vec<(Vec<u8>, Option<String>, CommandType)> {
783+
) -> Vec<(DeclId, Vec<u8>, Option<String>, CommandType)> {
784784
let mut output = vec![];
785785

786786
for scope_frame in self.delta.scope.iter().rev() {
787787
for overlay_id in scope_frame.active_overlays.iter().rev() {
788788
let overlay_frame = scope_frame.get_overlay(*overlay_id);
789789

790-
for decl in &overlay_frame.decls {
791-
if overlay_frame.visibility.is_decl_id_visible(decl.1) && predicate(decl.0) {
792-
let command = self.get_decl(*decl.1);
790+
for (name, decl_id) in &overlay_frame.decls {
791+
if overlay_frame.visibility.is_decl_id_visible(decl_id) && predicate(name) {
792+
let command = self.get_decl(*decl_id);
793793
if ignore_deprecated && command.signature().category == Category::Removed {
794794
continue;
795795
}
796796
output.push((
797-
decl.0.clone(),
797+
*decl_id,
798+
name.clone(),
798799
Some(command.description().to_string()),
799800
command.command_type(),
800801
));

0 commit comments

Comments
 (0)