Skip to content

Commit

Permalink
fix: use default policy for empty file uri
Browse files Browse the repository at this point in the history
resolves #39
  • Loading branch information
tekumara committed Mar 3, 2024
1 parent d77c53c commit 34998d4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
40 changes: 24 additions & 16 deletions crates/typos-lsp/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
};

Expand Down
20 changes: 20 additions & 0 deletions crates/typos-lsp/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 34998d4

Please sign in to comment.