mirror of
https://github.com/nushell/nushell.git
synced 2025-06-19 08:26:57 +02:00
fix(lsp): completion on command with following text (#15238)
# Description Fixes a bug introduced by #15188 # User-Facing Changes Before: <img width="216" alt="image" src="https://github.com/user-attachments/assets/5846a844-d88e-4d9f-b9e2-e2478c7acb37" /> And will crash the lsp server. After: <img width="454" alt="image" src="https://github.com/user-attachments/assets/85e727d6-fef5-426b-818c-e554d3c49c7d" /> # Tests + Formatting adjusted # After Submitting
This commit is contained in:
parent
9eaa8908d2
commit
de7b000505
@ -307,6 +307,7 @@ impl NuCompleter {
|
|||||||
let need_externals = !prefix_str.contains(' ');
|
let need_externals = !prefix_str.contains(' ');
|
||||||
let need_internals = !prefix_str.starts_with('^');
|
let need_internals = !prefix_str.starts_with('^');
|
||||||
let mut span = element_expression.span;
|
let mut span = element_expression.span;
|
||||||
|
span.end = std::cmp::min(span.end, pos + 1);
|
||||||
if !need_internals {
|
if !need_internals {
|
||||||
span = Span::new(span.start + 1, span.end)
|
span = Span::new(span.start + 1, span.end)
|
||||||
};
|
};
|
||||||
|
@ -7,7 +7,7 @@ use lsp_types::{
|
|||||||
TextEdit,
|
TextEdit,
|
||||||
};
|
};
|
||||||
use nu_cli::{NuCompleter, SuggestionKind};
|
use nu_cli::{NuCompleter, SuggestionKind};
|
||||||
use nu_protocol::engine::Stack;
|
use nu_protocol::engine::{CommandType, Stack};
|
||||||
|
|
||||||
impl LanguageServer {
|
impl LanguageServer {
|
||||||
pub(crate) fn complete(&mut self, params: &CompletionParams) -> Option<CompletionResponse> {
|
pub(crate) fn complete(&mut self, params: &CompletionParams) -> Option<CompletionResponse> {
|
||||||
@ -49,7 +49,12 @@ impl LanguageServer {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|r| {
|
.map(|r| {
|
||||||
let mut start = params.text_document_position.position;
|
let mut start = params.text_document_position.position;
|
||||||
start.character -= (r.suggestion.span.end - r.suggestion.span.start) as u32;
|
start.character = start.character.saturating_sub(
|
||||||
|
r.suggestion
|
||||||
|
.span
|
||||||
|
.end
|
||||||
|
.saturating_sub(r.suggestion.span.start) as u32,
|
||||||
|
);
|
||||||
let decl_id = r.kind.clone().and_then(|kind| {
|
let decl_id = r.kind.clone().and_then(|kind| {
|
||||||
matches!(kind, SuggestionKind::Command(_))
|
matches!(kind, SuggestionKind::Command(_))
|
||||||
.then_some(engine_state.find_decl(r.suggestion.value.as_bytes(), &[])?)
|
.then_some(engine_state.find_decl(r.suggestion.value.as_bytes(), &[])?)
|
||||||
@ -112,10 +117,11 @@ impl LanguageServer {
|
|||||||
},
|
},
|
||||||
SuggestionKind::CellPath => Some(CompletionItemKind::PROPERTY),
|
SuggestionKind::CellPath => Some(CompletionItemKind::PROPERTY),
|
||||||
SuggestionKind::Command(c) => match c {
|
SuggestionKind::Command(c) => match c {
|
||||||
nu_protocol::engine::CommandType::Keyword => Some(CompletionItemKind::KEYWORD),
|
CommandType::Keyword => Some(CompletionItemKind::KEYWORD),
|
||||||
nu_protocol::engine::CommandType::Builtin => Some(CompletionItemKind::FUNCTION),
|
CommandType::Builtin => Some(CompletionItemKind::FUNCTION),
|
||||||
nu_protocol::engine::CommandType::External => Some(CompletionItemKind::INTERFACE),
|
CommandType::Custom => Some(CompletionItemKind::METHOD),
|
||||||
_ => None,
|
CommandType::Alias => Some(CompletionItemKind::REFERENCE),
|
||||||
|
CommandType::External | CommandType::Plugin => Some(CompletionItemKind::INTERFACE),
|
||||||
},
|
},
|
||||||
SuggestionKind::Directory => Some(CompletionItemKind::FOLDER),
|
SuggestionKind::Directory => Some(CompletionItemKind::FOLDER),
|
||||||
SuggestionKind::File => Some(CompletionItemKind::FILE),
|
SuggestionKind::File => Some(CompletionItemKind::FILE),
|
||||||
@ -210,7 +216,7 @@ mod tests {
|
|||||||
let script = path_to_uri(&script);
|
let script = path_to_uri(&script);
|
||||||
|
|
||||||
open_unchecked(&client_connection, script.clone());
|
open_unchecked(&client_connection, script.clone());
|
||||||
let resp = send_complete_request(&client_connection, script.clone(), 0, 8);
|
let resp = send_complete_request(&client_connection, script.clone(), 0, 6);
|
||||||
|
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
let detail_str = "detail";
|
let detail_str = "detail";
|
||||||
@ -221,20 +227,15 @@ mod tests {
|
|||||||
expected: serde_json::json!([
|
expected: serde_json::json!([
|
||||||
// defined after the cursor
|
// defined after the cursor
|
||||||
{
|
{
|
||||||
"label": "config n foo bar",
|
"label": "config",
|
||||||
"detail": detail_str,
|
"detail": "Edit nushell configuration files.",
|
||||||
"textEdit": { "range": { "start": { "line": 0, "character": 0 }, "end": { "line": 0, "character": 8 }, },
|
"textEdit": { "range": { "start": { "line": 0, "character": 0 }, "end": { "line": 0, "character": 6 }, },
|
||||||
"newText": "config n foo bar"
|
"newText": "config"
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{ "label": "config env", "kind": 3 },
|
||||||
"label": "config nu",
|
{ "label": "config flatten", "kind": 3 },
|
||||||
"detail": "Edit nu configurations.",
|
{ "label": "config n foo bar", "detail": detail_str, "kind": 2 },
|
||||||
"textEdit": { "range": { "start": { "line": 0, "character": 0 }, "end": { "line": 0, "character": 8 }, },
|
|
||||||
"newText": "config nu"
|
|
||||||
},
|
|
||||||
"kind": 3
|
|
||||||
},
|
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user