refactor(lsp): span fix made easy by bumping lsp-textdocument to 0.4.2 (#15287)

# Description

The upstream crate fixed a bug of position calc, which made some extra
checking in lsp unnecessary.
Also moved some follow-up fixing of #15238 from #15270 here, as it has
something to do with previous position calc bug.

# User-Facing Changes

# Tests + Formatting

Adjusted

# After Submitting
This commit is contained in:
zc he
2025-03-11 19:13:58 +08:00
committed by GitHub
parent 2dab65f852
commit 81e496673e
5 changed files with 34 additions and 43 deletions

View File

@ -1,13 +1,15 @@
use std::sync::Arc;
use crate::{uri_to_path, LanguageServer};
use crate::{span_to_range, uri_to_path, LanguageServer};
use lsp_types::{
CompletionItem, CompletionItemKind, CompletionItemLabelDetails, CompletionParams,
CompletionResponse, CompletionTextEdit, Documentation, MarkupContent, MarkupKind, Range,
TextEdit,
CompletionResponse, CompletionTextEdit, Documentation, MarkupContent, MarkupKind, TextEdit,
};
use nu_cli::{NuCompleter, SuggestionKind};
use nu_protocol::engine::{CommandType, Stack};
use nu_protocol::{
engine::{CommandType, Stack},
Span,
};
impl LanguageServer {
pub(crate) fn complete(&mut self, params: &CompletionParams) -> Option<CompletionResponse> {
@ -44,17 +46,12 @@ impl LanguageServer {
)
};
let docs = self.docs.lock().ok()?;
let file = docs.get_document(&path_uri)?;
(!results.is_empty()).then_some(CompletionResponse::Array(
results
.into_iter()
.map(|r| {
let mut start = params.text_document_position.position;
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| {
matches!(kind, SuggestionKind::Command(_))
.then_some(engine_state.find_decl(r.suggestion.value.as_bytes(), &[])?)
@ -64,8 +61,17 @@ impl LanguageServer {
if r.suggestion.append_whitespace {
label_value.push(' ');
}
let span = r.suggestion.span;
let range = span_to_range(&Span::new(span.start, span.end), file, 0);
let text_edit = Some(CompletionTextEdit::Edit(TextEdit {
range,
new_text: label_value.clone(),
}));
CompletionItem {
label: label_value.clone(),
label: label_value,
label_details: r
.kind
.clone()
@ -97,13 +103,7 @@ impl LanguageServer {
})
}),
kind: Self::lsp_completion_item_kind(r.kind),
text_edit: Some(CompletionTextEdit::Edit(TextEdit {
range: Range {
start,
end: params.text_document_position.position,
},
new_text: label_value,
})),
text_edit,
..Default::default()
}
})
@ -230,16 +230,14 @@ mod tests {
actual: result_from_message(resp),
expected: serde_json::json!([
// defined after the cursor
{
"label": "config ",
"detail": "Edit nushell configuration files.",
"textEdit": { "range": { "start": { "line": 0, "character": 0 }, "end": { "line": 0, "character": 6 }, },
"newText": "config "
},
},
{ "label": "config env ", "kind": 3 },
{ "label": "config flatten ", "kind": 3 },
{ "label": "config n foo bar ", "detail": detail_str, "kind": 2 },
{
"label": "config nu ",
"detail": "Edit nu configurations.",
"textEdit": { "range": { "start": { "line": 0, "character": 0 }, "end": { "line": 0, "character": 8 }, },
"newText": "config nu "
}
},
])
);

View File

@ -80,27 +80,21 @@ impl LanguageServer {
if sp < last_span {
continue;
}
// in case the start position is at the end of lastline
let real_start_char = if range.end.line != range.start.line {
0
} else {
range.start.character
};
let mut delta_start = real_start_char;
let mut delta_start = range.start.character;
if range.end.line == last_token_line {
delta_start -= last_token_char;
}
tokens.push(SemanticToken {
delta_start,
delta_line: range.end.line.saturating_sub(last_token_line),
length: range.end.character.saturating_sub(real_start_char),
length: range.end.character.saturating_sub(range.start.character),
// 0 means function in semantic_token_legend
token_type: 0,
token_modifiers_bitset: 0,
});
last_span = sp;
last_token_line = range.end.line;
last_token_char = real_start_char;
last_token_char = range.start.character;
}
tokens
}