Skip to content

Commit c2780c8

Browse files
author
UnboundVariable
committed
Code review feedback.
1 parent 1e7a510 commit c2780c8

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

crates/ty_server/src/server/api/requests/signature_help.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::borrow::Cow;
22

33
use crate::DocumentSnapshot;
4-
use crate::document::PositionExt;
4+
use crate::document::{PositionEncoding, PositionExt};
55
use crate::server::api::traits::{
66
BackgroundDocumentRequestHandler, RequestHandler, RetriableRequestHandler,
77
};
@@ -75,9 +75,31 @@ impl BackgroundDocumentRequestHandler for SignatureHelpRequestHandler {
7575
let label = if resolved_capabilities.signature_label_offset_support {
7676
// Find the parameter's offset in the signature label
7777
if let Some(start) = sig.label.find(&param.label) {
78-
let start_u32 = u32::try_from(start).unwrap_or(u32::MAX);
79-
let end_u32 =
80-
u32::try_from(start + param.label.len()).unwrap_or(u32::MAX);
78+
let encoding = snapshot.encoding();
79+
80+
// Convert byte offsets to character offsets based on negotiated encoding
81+
let start_char_offset = match encoding {
82+
PositionEncoding::UTF8 => start,
83+
PositionEncoding::UTF16 => {
84+
sig.label[..start].encode_utf16().count()
85+
}
86+
PositionEncoding::UTF32 => sig.label[..start].chars().count(),
87+
};
88+
89+
let end_char_offset = match encoding {
90+
PositionEncoding::UTF8 => start + param.label.len(),
91+
PositionEncoding::UTF16 => sig.label
92+
[..start + param.label.len()]
93+
.encode_utf16()
94+
.count(),
95+
PositionEncoding::UTF32 => {
96+
sig.label[..start + param.label.len()].chars().count()
97+
}
98+
};
99+
100+
let start_u32 =
101+
u32::try_from(start_char_offset).unwrap_or(u32::MAX);
102+
let end_u32 = u32::try_from(end_char_offset).unwrap_or(u32::MAX);
81103
ParameterLabel::LabelOffsets([start_u32, end_u32])
82104
} else {
83105
ParameterLabel::Simple(param.label)

0 commit comments

Comments
 (0)