From 34998d48567d0b59c432296142934381f0258c4e Mon Sep 17 00:00:00 2001 From: Oliver Mannion <125105+tekumara@users.noreply.github.com> Date: Sun, 3 Mar 2024 22:40:59 +1100 Subject: [PATCH] fix: use default policy for empty file uri resolves #39 --- crates/typos-lsp/src/lsp.rs | 40 +++++++++++++--------- crates/typos-lsp/tests/integration_test.rs | 20 +++++++++++ 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/crates/typos-lsp/src/lsp.rs b/crates/typos-lsp/src/lsp.rs index ec6ff6e..47c9cb0 100644 --- a/crates/typos-lsp/src/lsp.rs +++ b/crates/typos-lsp/src/lsp.rs @@ -261,23 +261,31 @@ impl<'s, 'p> Backend<'s, 'p> { let uri_path = url_path_sanitised(uri); // find relevant tokenizer, and dict for the workspace folder - // safe to unwrap because the catch-all will match anything - let Match { - value: instance, - params: _, - } = state.router.at(&uri_path).unwrap(); + let (tokenizer, dict) = match state.router.at(&uri_path) { + Err(_) => { + // ie: file:/// + tracing::debug!( + "check_text: Using default policy because no route found for {}", + uri_path + ); + (self.default_policy.tokenizer, self.default_policy.dict) + } + Ok(Match { value, params: _ }) => { + tracing::debug!("check_text: path {}", &path.display()); + // skip file if matches extend-exclude + if value.ignores.matched(&path, false).is_ignore() { + tracing::debug!( + "check_text: Ignoring {} because it matches extend-exclude.", + uri + ); + return Vec::default(); + } + let policy = value.engine.policy(&path); + (policy.tokenizer, policy.dict) + } + }; - tracing::debug!("check_text: path {}", &path.display()); - // skip file if matches extend-exclude - if instance.ignores.matched(&path, false).is_ignore() { - tracing::debug!( - "check_text: Ignoring {} because it matches extend-exclude.", - uri - ); - return Vec::default(); - } - let policy = instance.engine.policy(&path); - (policy.tokenizer, policy.dict) + (tokenizer, dict) } }; diff --git a/crates/typos-lsp/tests/integration_test.rs b/crates/typos-lsp/tests/integration_test.rs index c51f090..82ae6de 100644 --- a/crates/typos-lsp/tests/integration_test.rs +++ b/crates/typos-lsp/tests/integration_test.rs @@ -263,6 +263,26 @@ async fn test_non_file_uri() { ) ); } + +#[test_log::test(tokio::test)] +async fn test_empty_file_uri() { + // eg: when using nvim telescope + let term = Url::from_str("file:///").unwrap(); + + let did_open_diag_txt = &did_open_with("apropriate", Some(&term)); + + let mut server = TestServer::new(); + let _ = server.request(&initialize_with(None, None)).await; + + similar_asserts::assert_eq!( + server.request(&did_open_diag_txt).await, + publish_diagnostics_with( + &[diag("`apropriate` should be `appropriate`", 0, 0, 10)], + Some(&term) + ) + ); +} + #[test_log::test(tokio::test)] async fn test_position_with_unicode_text() { let mut server = TestServer::new();