fix(lsp): completion label descriptions for cell_path and external values (#15226)

# Description

The type shown in the completion description is 1 level higher than the
actual entry.
Also cleans some TODOs for `SuggetionKind`.

# User-Facing Changes

## Before

<img width="409" alt="image"
src="https://github.com/user-attachments/assets/c7d7df02-aed9-4ea9-892a-0bca707352eb"
/>

<img width="491" alt="image"
src="https://github.com/user-attachments/assets/9b9394d4-62ee-4924-9840-402f00d88a8a"
/>

## After

<img width="425" alt="image"
src="https://github.com/user-attachments/assets/d8f41059-2c68-4902-9c32-d789f91b6d77"
/>

<img width="425" alt="image"
src="https://github.com/user-attachments/assets/ce03afb9-6c1f-4a65-a1cc-cbba4655abb3"
/>

# Tests + Formatting

Adjusted accordingly

# After Submitting
This commit is contained in:
zc he
2025-03-03 06:17:12 +08:00
committed by GitHub
parent 95f89a093a
commit 8e1385417e
9 changed files with 88 additions and 54 deletions

View File

@ -29,9 +29,14 @@ pub struct SemanticSuggestion {
#[derive(Clone, Debug, PartialEq)]
pub enum SuggestionKind {
Command(nu_protocol::engine::CommandType),
Type(nu_protocol::Type),
Value(nu_protocol::Type),
CellPath,
Directory,
File,
Flag,
Module,
Operator,
Variable,
}
impl From<Suggestion> for SemanticSuggestion {

View File

@ -108,23 +108,29 @@ fn get_suggestions_by_value(
value: &Value,
current_span: reedline::Span,
) -> Vec<SemanticSuggestion> {
let kind = SuggestionKind::Type(value.get_type());
let str_to_suggestion = |s: String| SemanticSuggestion {
let to_suggestion = |s: String, v: Option<&Value>| SemanticSuggestion {
suggestion: Suggestion {
value: s,
span: current_span,
description: v.map(|v| v.get_type().to_string()),
..Suggestion::default()
},
kind: Some(kind.to_owned()),
kind: Some(SuggestionKind::CellPath),
};
match value {
Value::Record { val, .. } => val
.columns()
.map(|s| str_to_suggestion(s.to_string()))
.map(|s| to_suggestion(s.to_string(), val.get(s)))
.collect(),
Value::List { vals, .. } => get_columns(vals.as_slice())
.into_iter()
.map(str_to_suggestion)
.map(|s| {
let sub_val = vals
.first()
.and_then(|v| v.as_record().ok())
.and_then(|rv| rv.get(&s));
to_suggestion(s, sub_val)
})
.collect(),
_ => vec![],
}

View File

@ -10,7 +10,7 @@ use nu_protocol::{
ast::{Argument, Block, Expr, Expression, FindMapResult, Traverse},
debugger::WithoutDebug,
engine::{Closure, EngineState, Stack, StateWorkingSet},
PipelineData, Span, Value,
PipelineData, Span, Type, Value,
};
use reedline::{Completer as ReedlineCompleter, Suggestion};
use std::{str, sync::Arc};
@ -639,7 +639,7 @@ pub fn map_value_completions<'a>(
},
..Suggestion::default()
},
kind: Some(SuggestionKind::Type(x.get_type())),
kind: Some(SuggestionKind::Value(x.get_type())),
});
}
@ -653,41 +653,41 @@ pub fn map_value_completions<'a>(
},
..Suggestion::default()
};
let mut value_type = Type::String;
// Iterate the cols looking for `value` and `description`
record.iter().for_each(|it| {
// Match `value` column
if it.0 == "value" {
// Convert the value to string
if let Ok(val_str) = it.1.coerce_string() {
// Update the suggestion value
suggestion.value = val_str;
record.iter().for_each(|(key, value)| {
match key.as_str() {
"value" => {
value_type = value.get_type();
// Convert the value to string
if let Ok(val_str) = value.coerce_string() {
// Update the suggestion value
suggestion.value = val_str;
}
}
}
// Match `description` column
if it.0 == "description" {
// Convert the value to string
if let Ok(desc_str) = it.1.coerce_string() {
// Update the suggestion value
suggestion.description = Some(desc_str);
"description" => {
// Convert the value to string
if let Ok(desc_str) = value.coerce_string() {
// Update the suggestion value
suggestion.description = Some(desc_str);
}
}
}
// Match `style` column
if it.0 == "style" {
// Convert the value to string
suggestion.style = match it.1 {
Value::String { val, .. } => Some(lookup_ansi_color_style(val)),
Value::Record { .. } => Some(color_record_to_nustyle(it.1)),
_ => None,
};
"style" => {
// Convert the value to string
suggestion.style = match value {
Value::String { val, .. } => Some(lookup_ansi_color_style(val)),
Value::Record { .. } => Some(color_record_to_nustyle(value)),
_ => None,
};
}
_ => (),
}
});
return Some(SemanticSuggestion {
suggestion,
kind: Some(SuggestionKind::Type(x.get_type())),
kind: Some(SuggestionKind::Value(value_type)),
});
}

View File

@ -157,6 +157,7 @@ pub struct FileSuggestion {
pub span: nu_protocol::Span,
pub path: String,
pub style: Option<Style>,
pub is_dir: bool,
}
/// # Parameters
@ -260,6 +261,7 @@ pub fn complete_item(
if should_collapse_dots {
p = collapse_ndots(p);
}
let is_dir = p.isdir;
let path = original_cwd.apply(p, path_separator);
let style = ls_colors.as_ref().map(|lsc| {
lsc.style_for_path_with_metadata(
@ -275,6 +277,7 @@ pub fn complete_item(
span,
path: escape_path(path, want_directory),
style,
is_dir,
}
})
.collect()

View File

@ -9,7 +9,7 @@ use nu_protocol::{
use reedline::Suggestion;
use std::path::Path;
use super::{completion_common::FileSuggestion, SemanticSuggestion};
use super::{completion_common::FileSuggestion, SemanticSuggestion, SuggestionKind};
pub struct DirectoryCompletion;
@ -47,8 +47,7 @@ impl Completer for DirectoryCompletion {
},
..Suggestion::default()
},
// TODO????
kind: None,
kind: Some(SuggestionKind::Directory),
})
.collect();

View File

@ -9,7 +9,7 @@ use nu_protocol::{
use reedline::Suggestion;
use std::path::Path;
use super::{completion_common::FileSuggestion, SemanticSuggestion};
use super::{completion_common::FileSuggestion, SemanticSuggestion, SuggestionKind};
pub struct FileCompletion;
@ -50,8 +50,11 @@ impl Completer for FileCompletion {
},
..Suggestion::default()
},
// TODO????
kind: None,
kind: Some(if x.is_dir {
SuggestionKind::Directory
} else {
SuggestionKind::File
}),
})
.collect();

View File

@ -5,7 +5,7 @@ use nu_protocol::{
};
use reedline::Suggestion;
use super::SemanticSuggestion;
use super::{SemanticSuggestion, SuggestionKind};
#[derive(Clone)]
pub struct FlagCompletion {
@ -35,8 +35,7 @@ impl Completer for FlagCompletion {
append_whitespace: true,
..Suggestion::default()
},
// TODO????
kind: None,
kind: Some(SuggestionKind::Flag),
});
};

View File

@ -32,10 +32,10 @@ impl Completer for VariableCompletion {
suggestion: Suggestion {
value: builtin.to_string(),
span: current_span,
description: Some("reserved".into()),
..Suggestion::default()
},
// TODO is there a way to get the VarId to get the type???
kind: None,
kind: Some(SuggestionKind::Variable),
});
}
@ -44,11 +44,10 @@ impl Completer for VariableCompletion {
suggestion: Suggestion {
value: String::from_utf8_lossy(name).to_string(),
span: current_span,
description: Some(working_set.get_variable(*var_id).ty.to_string()),
..Suggestion::default()
},
kind: Some(SuggestionKind::Type(
working_set.get_variable(*var_id).ty.clone(),
)),
kind: Some(SuggestionKind::Variable),
})
};