|
1 | 1 | use std::borrow::Cow;
|
2 | 2 |
|
3 | 3 | use crate::DocumentSnapshot;
|
4 |
| -use crate::document::PositionExt; |
| 4 | +use crate::document::{PositionEncoding, PositionExt}; |
5 | 5 | use crate::server::api::traits::{
|
6 | 6 | BackgroundDocumentRequestHandler, RequestHandler, RetriableRequestHandler,
|
7 | 7 | };
|
@@ -75,9 +75,31 @@ impl BackgroundDocumentRequestHandler for SignatureHelpRequestHandler {
|
75 | 75 | let label = if resolved_capabilities.signature_label_offset_support {
|
76 | 76 | // Find the parameter's offset in the signature label
|
77 | 77 | if let Some(start) = sig.label.find(¶m.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); |
81 | 103 | ParameterLabel::LabelOffsets([start_u32, end_u32])
|
82 | 104 | } else {
|
83 | 105 | ParameterLabel::Simple(param.label)
|
|
0 commit comments