Skip to content

Commit 1ad0779

Browse files
committed
Make WideEncoding non-exhaustive
1 parent 59b4916 commit 1ad0779

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

crates/ide-db/src/tests/line_index.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,11 @@ fn test_every_chars() {
2828
let got_lin_col = line_index.line_col(got_offset);
2929
assert_eq!(got_lin_col, lin_col);
3030

31-
for enc in [WideEncoding::Utf16, WideEncoding::Utf32] {
31+
for (enc, col) in [(WideEncoding::Utf16, col_utf16), (WideEncoding::Utf32, col_utf32)] {
3232
let wide_lin_col = line_index.to_wide(enc, lin_col);
3333
let got_lin_col = line_index.to_utf8(enc, wide_lin_col);
3434
assert_eq!(got_lin_col, lin_col);
35-
36-
let want_col = match enc {
37-
WideEncoding::Utf16 => col_utf16,
38-
WideEncoding::Utf32 => col_utf32,
39-
};
40-
assert_eq!(wide_lin_col.col, want_col)
35+
assert_eq!(wide_lin_col.col, col)
4136
}
4237

4338
if c == '\n' {

crates/rust-analyzer/src/caps.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ use crate::semantic_tokens;
2323

2424
pub fn server_capabilities(config: &Config) -> ServerCapabilities {
2525
ServerCapabilities {
26-
position_encoding: Some(match negotiated_encoding(config.caps()) {
27-
PositionEncoding::Utf8 => PositionEncodingKind::UTF8,
26+
position_encoding: match negotiated_encoding(config.caps()) {
27+
PositionEncoding::Utf8 => Some(PositionEncodingKind::UTF8),
2828
PositionEncoding::Wide(wide) => match wide {
29-
WideEncoding::Utf16 => PositionEncodingKind::UTF16,
30-
WideEncoding::Utf32 => PositionEncodingKind::UTF32,
29+
WideEncoding::Utf16 => Some(PositionEncodingKind::UTF16),
30+
WideEncoding::Utf32 => Some(PositionEncodingKind::UTF32),
31+
_ => None,
3132
},
32-
}),
33+
},
3334
text_document_sync: Some(TextDocumentSyncCapability::Options(TextDocumentSyncOptions {
3435
open_close: Some(true),
3536
change: Some(TextDocumentSyncKind::INCREMENTAL),

crates/rust-analyzer/src/diagnostics/to_proto.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::collections::HashMap;
44

55
use flycheck::{Applicability, DiagnosticLevel, DiagnosticSpan};
6-
use ide_db::line_index::WideEncoding;
76
use itertools::Itertools;
87
use stdx::format_to;
98
use vfs::{AbsPath, AbsPathBuf};
@@ -94,17 +93,16 @@ fn position(
9493
};
9594
}
9695
let mut char_offset = 0;
97-
let len_func = match position_encoding {
98-
PositionEncoding::Utf8 => char::len_utf8,
99-
PositionEncoding::Wide(WideEncoding::Utf16) => char::len_utf16,
100-
PositionEncoding::Wide(WideEncoding::Utf32) => |_| 1,
101-
};
10296
for c in line.text.chars() {
10397
char_offset += 1;
10498
if char_offset > column_offset {
10599
break;
106100
}
107-
true_column_offset += len_func(c) - 1;
101+
let len = match position_encoding {
102+
PositionEncoding::Utf8 => c.len_utf8(),
103+
PositionEncoding::Wide(w) => w.measure(&c.to_string()),
104+
};
105+
true_column_offset += len - 1;
108106
}
109107
}
110108

lib/line-index/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,24 @@ pub struct LineCol {
2020

2121
/// A kind of wide character encoding.
2222
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
23+
#[non_exhaustive]
2324
pub enum WideEncoding {
2425
/// UTF-16.
2526
Utf16,
2627
/// UTF-32.
2728
Utf32,
2829
}
2930

31+
impl WideEncoding {
32+
/// Returns the number of units it takes to encode `text` in this encoding.
33+
pub fn measure(&self, text: &str) -> usize {
34+
match self {
35+
WideEncoding::Utf16 => text.encode_utf16().count(),
36+
WideEncoding::Utf32 => text.chars().count(),
37+
}
38+
}
39+
}
40+
3041
/// Line/Column information in legacy encodings.
3142
//
3243
// Deliberately not a generic type and different from `LineCol`.

0 commit comments

Comments
 (0)