mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 22:54:58 +00:00
fix completion handler trying to seek outside of character boundaries.
With this patch, in these examples ```rust fn main() { "⊞$0"; } ``` ```rust struct S { д$0 u8 } ``` entering ':' character in `$0` places shouldn't cause panics.
This commit is contained in:
parent
00b19846c9
commit
0ff271d38f
1 changed files with 7 additions and 10 deletions
|
@ -27,7 +27,7 @@ use lsp_types::{
|
||||||
use project_model::TargetKind;
|
use project_model::TargetKind;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use stdx::{format_to, never};
|
use stdx::{format_to, never};
|
||||||
use syntax::{algo, ast, AstNode, TextRange, TextSize};
|
use syntax::{algo, ast, AstNode, TextRange, TextSize, T};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
cargo_target_spec::CargoTargetSpec,
|
cargo_target_spec::CargoTargetSpec,
|
||||||
|
@ -727,16 +727,13 @@ pub(crate) fn handle_completion(
|
||||||
let completion_triggered_after_single_colon = {
|
let completion_triggered_after_single_colon = {
|
||||||
let mut res = false;
|
let mut res = false;
|
||||||
if let Some(ctx) = params.context {
|
if let Some(ctx) = params.context {
|
||||||
if ctx.trigger_character.unwrap_or_default() == ":" {
|
if ctx.trigger_character.as_deref() == Some(":") {
|
||||||
let source_file = snap.analysis.parse(position.file_id)?;
|
let source_file = snap.analysis.parse(position.file_id)?;
|
||||||
let syntax = source_file.syntax();
|
let left_token =
|
||||||
let text = syntax.text();
|
source_file.syntax().token_at_offset(position.offset).left_biased();
|
||||||
if let Some(next_char) = text.char_at(position.offset) {
|
match left_token {
|
||||||
let diff = TextSize::of(next_char) + TextSize::of(':');
|
Some(left_token) => res = left_token.kind() == T![:],
|
||||||
let prev_char = position.offset - diff;
|
None => res = true,
|
||||||
if text.char_at(prev_char) != Some(':') {
|
|
||||||
res = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue