Skip to content

Commit

Permalink
fix: use default policy for non-file uris
Browse files Browse the repository at this point in the history
resolves #36
  • Loading branch information
tekumara committed Mar 3, 2024
1 parent 02c7e68 commit 0265ff1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 35 deletions.
68 changes: 34 additions & 34 deletions crates/typos-lsp/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,47 +246,47 @@ impl<'s, 'p> Backend<'s, 'p> {

// mimics typos_cli::file::FileChecker::check_file
fn check_text(&self, buffer: &str, uri: &Url) -> Vec<Diagnostic> {
let path = uri.to_file_path().unwrap_or_else(|_| {
// TODO: return error if cannot convert uri to file path
tracing::warn!("check_text: Cannot convert uri {} to file path", uri);
PathBuf::default()
});

let uri_path = url_path_sanitised(uri);

let state = self.state.lock().unwrap();

// find relevant ignores, tokenizer, and dict for the workspace folder
let (ignores, tokenizer, dict) = match state.router.at(&uri_path) {
// TODO: return error if no route found
let (tokenizer, dict) = match uri.to_file_path() {
Err(_) => {
tracing::warn!(
"check_text: Using default policy because no route found for {}",
uri_path
);
(
None,
self.default_policy.tokenizer,
self.default_policy.dict,
)
}
Ok(Match { value, params: _ }) => {
tracing::debug!("check_text: path {}", &path.display());
let policy = value.engine.policy(&path);
(Some(&value.ignores), policy.tokenizer, policy.dict)
}
};

// skip file if matches extend-exclude
if let Some(ignores) = ignores {
if ignores.matched(path, false).is_ignore() {
// eg: uris like untitled:* or term://*
tracing::debug!(
"check_text: Ignoring {} because it matches extend-exclude.",
"check_text: Using default policy because cannot convert uri {} to file path",
uri
);
return Vec::default();
(self.default_policy.tokenizer, self.default_policy.dict)
}
}
Ok(path) => {
let uri_path = url_path_sanitised(uri);

// find relevant tokenizer, and dict for the workspace folder
let (tokenizer, dict) = match state.router.at(&uri_path) {
Err(_) => {
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)
}
};

(tokenizer, dict)
}
};

let mut accum = AccumulatePosition::new();

Expand Down
20 changes: 19 additions & 1 deletion crates/typos-lsp/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde_json::{json, Value};
use std::path::PathBuf;
use std::{path::PathBuf, str::FromStr};
use tower_lsp::lsp_types::Url;
mod common;
use common::TestServer;
Expand Down Expand Up @@ -245,6 +245,24 @@ async fn test_custom_config_no_workspace_folder() {
);
}

#[test_log::test(tokio::test)]
async fn test_non_file_uri() {
// a Neovim toggleterm uri
let term = Url::from_str("term://~/code/typos-lsp//59317:/bin/zsh;#toggleterm#1").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 0265ff1

Please sign in to comment.