Skip to content

Commit

Permalink
fix: typo start position corrected for multiple code point unicode
Browse files Browse the repository at this point in the history
resolves #22
  • Loading branch information
tekumara committed Dec 10, 2023
1 parent e1a47a9 commit e3d2752
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/typos-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serde = { version = "1.0", features = ["derive"] }
ignore = "0.4.20"
matchit = "0.7.1"
shellexpand = "3.1.0"
unicode-segmentation = "1.10.1"

[dev-dependencies]
test-log = { version = "0.2.11", features = ["trace"] }
Expand Down
7 changes: 6 additions & 1 deletion crates/typos-lsp/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,13 @@ impl AccumulatePosition {
.map(|s| s + 1)
.unwrap_or(0);

let before_typo = String::from_utf8_lossy(&buffer[line_start..byte_offset]);
let line_pos =
unicode_segmentation::UnicodeSegmentation::graphemes(before_typo.as_ref(), true)
.count();

self.line_num = line_num;
self.line_pos = byte_offset - line_start;
self.line_pos = line_pos;
self.last_offset = byte_offset;

(self.line_num, self.line_pos)
Expand Down
56 changes: 54 additions & 2 deletions crates/typos-lsp/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,6 @@ async fn test_custom_config_file_e2e() {
workspace_folder_uri,
);

println!("{}", initialize);

let did_open_diag_txt = format!(
r#"{{
"jsonrpc": "2.0",
Expand Down Expand Up @@ -382,6 +380,60 @@ async fn test_custom_config_file_e2e() {
);
}

#[test_log::test(tokio::test)]
async fn test_unicode_diagnostics() {
let initialize = r#"{
"jsonrpc": "2.0",
"method": "initialize",
"params": {
"initializationOptions": {
"diagnosticSeverity": "Warning"
},
"capabilities": {
"textDocument": { "publishDiagnostics": { "dataSupport": true } }
}
},
"id": 1
}
"#;

let did_open = r#"{
"jsonrpc": "2.0",
"method": "textDocument/didOpen",
"params": {
"textDocument": {
"uri": "file:///diagnostics.txt",
"languageId": "plaintext",
"version": 1,
"text": "¿Qué hace él?"
}
}
}
"#;

let (mut req_client, mut resp_client) = start_server();
let mut buf = vec![0; 1024];

req_client
.write_all(req(initialize).as_bytes())
.await
.unwrap();
let _ = resp_client.read(&mut buf).await.unwrap();

tracing::debug!("{}", did_open);
req_client
.write_all(req(did_open).as_bytes())
.await
.unwrap();
let n = resp_client.read(&mut buf).await.unwrap();

// start position should count graphemes with multiple code points as one visible character
similar_asserts::assert_eq!(
body(&buf[..n]).unwrap(),
r#"{"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"diagnostics":[{"data":{"corrections":["have"]},"message":"`hace` should be `have`","range":{"end":{"character":9,"line":0},"start":{"character":5,"line":0}},"severity":2,"source":"typos"}],"uri":"file:///diagnostics.txt","version":1}}"#,
);
}

fn start_server() -> (tokio::io::DuplexStream, tokio::io::DuplexStream) {
let (req_client, req_server) = tokio::io::duplex(1024);
let (resp_server, resp_client) = tokio::io::duplex(1024);
Expand Down

0 comments on commit e3d2752

Please sign in to comment.